Skip to content

Commit

Permalink
rename test models
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 14, 2025
1 parent 1b04e9c commit e522e32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
8 changes: 4 additions & 4 deletions tests/model_fields_/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ class ArrayEnumModel(models.Model):


# EmbeddedModelField
class EmbeddedModelFieldModel(models.Model):
simple = EmbeddedModelField("EmbeddedModel", null=True, blank=True)
class Holder(models.Model):
data = EmbeddedModelField("Data", null=True, blank=True)


class EmbeddedModel(models.Model):
someint = models.IntegerField(db_column="custom_column")
class Data(models.Model):
integer = models.IntegerField(db_column="custom_column")
auto_now = models.DateTimeField(auto_now=True)
auto_now_add = models.DateTimeField(auto_now_add=True)

Expand Down
86 changes: 35 additions & 51 deletions tests/model_fields_/test_embedded_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@
Address,
Author,
Book,
EmbeddedModel,
EmbeddedModelFieldModel,
Data,
Holder,
)


class MethodTests(SimpleTestCase):
def test_deconstruct(self):
field = EmbeddedModelField("EmbeddedModel", null=True)
field = EmbeddedModelField("Data", null=True)
name, path, args, kwargs = field.deconstruct()
self.assertEqual(path, "django_mongodb_backend.fields.EmbeddedModelField")
self.assertEqual(args, [])
self.assertEqual(kwargs, {"embedded_model": "EmbeddedModel", "null": True})
self.assertEqual(kwargs, {"embedded_model": "Data", "null": True})

def test_get_db_prep_save_invalid(self):
msg = (
"Expected instance of type <class 'model_fields_.models.EmbeddedModel'>, "
"not <class 'int'>."
)
msg = "Expected instance of type <class 'model_fields_.models.Data'>, " "not <class 'int'>."
with self.assertRaisesMessage(TypeError, msg):
EmbeddedModelFieldModel(simple=42).save()
Holder(data=42).save()

def test_validate(self):
obj = EmbeddedModelFieldModel(simple=EmbeddedModel(someint=None))
# This isn't quite right because "someint" is the field that's non-null.
msg = "{'simple': ['This field cannot be null.']}"
obj = Holder(data=Data(integer=None))
# This isn't quite right because "integer" is the field that's non-null.
msg = "{'data': ['This field cannot be null.']}"
with self.assertRaisesMessage(ValidationError, msg):
obj.full_clean()

Expand All @@ -44,74 +41,61 @@ def truncate_ms(self, value):
return value.replace(microsecond=(value.microsecond // 1000) * 1000)

def test_save_load(self):
EmbeddedModelFieldModel.objects.create(simple=EmbeddedModel(someint="5"))
obj = EmbeddedModelFieldModel.objects.get()
self.assertIsInstance(obj.simple, EmbeddedModel)
Holder.objects.create(data=Data(integer="5"))
obj = Holder.objects.get()
self.assertIsInstance(obj.data, Data)
# Make sure get_prep_value is called.
self.assertEqual(obj.simple.someint, 5)
self.assertEqual(obj.data.integer, 5)
# Primary keys should not be populated...
self.assertEqual(obj.simple.id, None)
self.assertEqual(obj.data.id, None)
# ... unless set explicitly.
obj.simple.id = obj.id
obj.data.id = obj.id
obj.save()
obj = EmbeddedModelFieldModel.objects.get()
self.assertEqual(obj.simple.id, obj.id)
obj = Holder.objects.get()
self.assertEqual(obj.data.id, obj.id)

def test_save_load_null(self):
EmbeddedModelFieldModel.objects.create(simple=None)
obj = EmbeddedModelFieldModel.objects.get()
self.assertIsNone(obj.simple)
Holder.objects.create(data=None)
obj = Holder.objects.get()
self.assertIsNone(obj.data)

def test_pre_save(self):
"""Field.pre_save() is called on embedded model fields."""
obj = EmbeddedModelFieldModel.objects.create(simple=EmbeddedModel())
auto_now = self.truncate_ms(obj.simple.auto_now)
auto_now_add = self.truncate_ms(obj.simple.auto_now_add)
obj = Holder.objects.create(data=Data())
auto_now = self.truncate_ms(obj.data.auto_now)
auto_now_add = self.truncate_ms(obj.data.auto_now_add)
self.assertEqual(auto_now, auto_now_add)
# save() updates auto_now but not auto_now_add.
obj.save()
self.assertEqual(self.truncate_ms(obj.simple.auto_now_add), auto_now_add)
auto_now_two = obj.simple.auto_now
self.assertGreater(auto_now_two, obj.simple.auto_now_add)
self.assertEqual(self.truncate_ms(obj.data.auto_now_add), auto_now_add)
auto_now_two = obj.data.auto_now
self.assertGreater(auto_now_two, obj.data.auto_now_add)
# And again, save() updates auto_now but not auto_now_add.
obj = EmbeddedModelFieldModel.objects.get()
obj = Holder.objects.get()
obj.save()
self.assertEqual(obj.simple.auto_now_add, auto_now_add)
self.assertGreater(obj.simple.auto_now, auto_now_two)
self.assertEqual(obj.data.auto_now_add, auto_now_add)
self.assertGreater(obj.data.auto_now, auto_now_two)


class QueryingTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.objs = [
EmbeddedModelFieldModel.objects.create(simple=EmbeddedModel(someint=x))
for x in range(6)
]
cls.objs = [Holder.objects.create(data=Data(integer=x)) for x in range(6)]

def test_exact(self):
self.assertCountEqual(
EmbeddedModelFieldModel.objects.filter(simple__someint=3), [self.objs[3]]
)
self.assertCountEqual(Holder.objects.filter(data__integer=3), [self.objs[3]])

def test_lt(self):
self.assertCountEqual(
EmbeddedModelFieldModel.objects.filter(simple__someint__lt=3), self.objs[:3]
)
self.assertCountEqual(Holder.objects.filter(data__integer__lt=3), self.objs[:3])

def test_lte(self):
self.assertCountEqual(
EmbeddedModelFieldModel.objects.filter(simple__someint__lte=3), self.objs[:4]
)
self.assertCountEqual(Holder.objects.filter(data__integer__lte=3), self.objs[:4])

def test_gt(self):
self.assertCountEqual(
EmbeddedModelFieldModel.objects.filter(simple__someint__gt=3), self.objs[4:]
)
self.assertCountEqual(Holder.objects.filter(data__integer__gt=3), self.objs[4:])

def test_gte(self):
self.assertCountEqual(
EmbeddedModelFieldModel.objects.filter(simple__someint__gte=3), self.objs[3:]
)
self.assertCountEqual(Holder.objects.filter(data__integer__gte=3), self.objs[3:])

def test_nested(self):
obj = Book.objects.create(
Expand Down

0 comments on commit e522e32

Please sign in to comment.