work from weekend
This commit is contained in:
0
blackmesa/core/tests/__init__.py
Normal file
0
blackmesa/core/tests/__init__.py
Normal file
19
blackmesa/core/tests/factories.py
Normal file
19
blackmesa/core/tests/factories.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from .. import models
|
||||
import factory
|
||||
import factory.fuzzy
|
||||
|
||||
class CelestialFactory(factory.django.DjangoModelFactory):
|
||||
name = factory.Faker('city')
|
||||
weight = factory.fuzzy.FuzzyFloat(100.0, 100000.0)
|
||||
size = factory.fuzzy.FuzzyFloat(1.0, 8.0)
|
||||
|
||||
class Meta:
|
||||
model = models.Celestial
|
||||
|
||||
class OrbitFactory(factory.DjangoModelFactory):
|
||||
parent = factory.SubFactory(CelestialFactory)
|
||||
celestial = factory.SubFactory(CelestialFactory)
|
||||
distance = factory.fuzzy.FuzzyFloat(2.0, 140.0)
|
||||
|
||||
class Meta:
|
||||
model = models.Orbit
|
||||
49
blackmesa/core/tests/test_base.py
Normal file
49
blackmesa/core/tests/test_base.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from dataclasses import dataclass
|
||||
from django.test import TestCase
|
||||
from ..models import Celestial, Orbit
|
||||
from ..workbench import Lambda, PureDict
|
||||
from .factories import CelestialFactory, OrbitFactory
|
||||
from django.db.models.expressions import Expression, Combinable, Value
|
||||
from django.db.models import F
|
||||
from django.utils import functional
|
||||
# Create your tests here.
|
||||
|
||||
|
||||
class TestBase(TestCase):
|
||||
def setUp(self):
|
||||
TestCase.setUp(self)
|
||||
self.celestials = CelestialFactory.create_batch(10)
|
||||
self.orbits = []
|
||||
for celestial in self.celestials[1:]:
|
||||
self.orbits.append(OrbitFactory(parent=self.celestials[0], celestial=celestial))
|
||||
|
||||
def test_data(self):
|
||||
self.assertEqual(Celestial.objects.all().count(), len(self.celestials))
|
||||
self.assertEqual(Orbit.objects.all().count(), len(self.orbits))
|
||||
|
||||
def test_building_blocks(self):
|
||||
klass = dict
|
||||
queryset = Celestial.objects.purify(PureDict(), hi=Lambda(lambda x:'hi'))
|
||||
data = queryset[0]
|
||||
self.assertIsInstance(data, klass)
|
||||
self.assertEqual(data['hi'], 'hi')
|
||||
|
||||
def test_dataclass(self):
|
||||
|
||||
@dataclass
|
||||
class MyDataclass:
|
||||
id: int
|
||||
name: str
|
||||
hi: str
|
||||
size: float
|
||||
weight: float
|
||||
|
||||
klass = MyDataclass
|
||||
queryset = Celestial.objects.purify(klass, hi=Lambda(lambda x:'hi'))
|
||||
data = queryset[3]
|
||||
self.assertIsInstance(data, klass)
|
||||
self.assertEqual(data.hi, 'hi')
|
||||
first = Celestial.objects.purify(klass, hi=F('name')).first()
|
||||
self.assertIsInstance(first, klass)
|
||||
self.assertEqual(first.hi, first.name)
|
||||
|
||||
Reference in New Issue
Block a user