49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
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)
|
|
|