Skip to content

Commit 1ad815c

Browse files
committed
edits
1 parent d7d40c3 commit 1ad815c

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

tests/model_fields_/test_embedded_model.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
)
2424

2525

26+
def truncate_ms(value):
27+
"""Truncate microsends to millisecond precision as supported by MongoDB."""
28+
return value.replace(microsecond=(value.microsecond // 1000) * 1000)
29+
30+
2631
class MethodTests(SimpleTestCase):
2732
def test_deconstruct(self):
2833
field = EmbeddedModelField("Data", null=True)
@@ -46,10 +51,6 @@ def test_validate(self):
4651

4752

4853
class ModelTests(TestCase):
49-
def truncate_ms(self, value):
50-
"""Truncate microseconds to milliseconds as supported by MongoDB."""
51-
return value.replace(microsecond=(value.microsecond // 1000) * 1000)
52-
5354
def test_save_load(self):
5455
Holder.objects.create(data=Data(integer="5"))
5556
obj = Holder.objects.get()
@@ -72,12 +73,12 @@ def test_save_load_null(self):
7273
def test_pre_save(self):
7374
"""Field.pre_save() is called on embedded model fields."""
7475
obj = Holder.objects.create(data=Data())
75-
auto_now = self.truncate_ms(obj.data.auto_now)
76-
auto_now_add = self.truncate_ms(obj.data.auto_now_add)
76+
auto_now = truncate_ms(obj.data.auto_now)
77+
auto_now_add = truncate_ms(obj.data.auto_now_add)
7778
self.assertEqual(auto_now, auto_now_add)
7879
# save() updates auto_now but not auto_now_add.
7980
obj.save()
80-
self.assertEqual(self.truncate_ms(obj.data.auto_now_add), auto_now_add)
81+
self.assertEqual(truncate_ms(obj.data.auto_now_add), auto_now_add)
8182
auto_now_two = obj.data.auto_now
8283
self.assertGreater(auto_now_two, obj.data.auto_now_add)
8384
# And again, save() updates auto_now but not auto_now_add.
@@ -107,52 +108,54 @@ def test_gt(self):
107108
def test_gte(self):
108109
self.assertCountEqual(Holder.objects.filter(data__integer__gte=3), self.objs[3:])
109110

110-
def test_nested(self):
111-
obj = Book.objects.create(
112-
author=Author(name="Shakespeare", age=55, address=Address(city="NYC", state="NY"))
113-
)
114-
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
115-
116-
def truncate_ms(self, value):
117-
"""Truncate microsends to millisecond precision as supported by MongoDB."""
118-
return value.replace(microsecond=(value.microsecond // 1000) * 1000)
119-
120111
def test_ordering_by_embedded_field(self):
121112
query = Holder.objects.filter(data__integer__gt=3).order_by("-data__integer").values("pk")
122113
expected = [{"pk": e.pk} for e in list(reversed(self.objs[4:]))]
123114
self.assertSequenceEqual(query, expected)
124115

125116
def test_ordering_grouping_by_embedded_field(self):
126-
expected = sorted(
117+
# Create and sort test data by `data__integer`
118+
expected_obj = sorted(
127119
(Holder.objects.create(data=Data(integer=x)) for x in range(6)),
128120
key=lambda x: x.data.integer,
129121
)
130-
query = (
122+
# Group by `data__integer + 5` and get the latest `data__auto_now` timestamp
123+
qs = (
131124
Holder.objects.annotate(
132125
group=ExpressionWrapper(F("data__integer") + 5, output_field=IntegerField())
133126
)
134127
.values("group")
135128
.annotate(max_auto_now=Max("data__auto_now"))
136129
.order_by("data__integer")
137130
)
138-
query_response = [{**e, "max_auto_now": self.truncate_ms(e["max_auto_now"])} for e in query]
131+
response = [{**e, "max_auto_now": e["max_auto_now"]} for e in qs]
132+
# Validate that each unique `data__integer` is correctly grouped and annotated
139133
self.assertSequenceEqual(
140-
query_response,
134+
response,
141135
[
142-
{"group": e.data.integer + 5, "max_auto_now": self.truncate_ms(e.data.auto_now)}
143-
for e in expected
136+
{"group": e.data.integer + 5, "max_auto_now": truncate_ms(e.data.auto_now)}
137+
for e in expected_obj
144138
],
145139
)
146140

147141
def test_ordering_grouping_by_sum(self):
142+
# Create test data with repeated `data__integer` values
148143
[Holder.objects.create(data=Data(integer=x)) for x in range(6)]
144+
# Group by `data__integer` and compute the sum of occurrences
149145
qs = (
150146
Holder.objects.values("data__integer")
151147
.annotate(sum=Sum("data__integer"))
152148
.order_by("sum")
153149
)
150+
# The sum should be twice the integer values (since each appears twice)
154151
self.assertQuerySetEqual(qs, [0, 2, 4, 6, 8, 10], operator.itemgetter("sum"))
155152

153+
def test_nested(self):
154+
obj = Book.objects.create(
155+
author=Author(name="Shakespeare", age=55, address=Address(city="NYC", state="NY"))
156+
)
157+
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
158+
156159

157160
@isolate_apps("model_fields_")
158161
class CheckTests(SimpleTestCase):

0 commit comments

Comments
 (0)