diff options
author | Will Earp <will.earp@icloud.com> | 2014-12-23 21:11:55 +0000 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-12-31 11:56:06 -0500 |
commit | 89527576980ff14309990c152e5904ddd5ca3df7 (patch) | |
tree | 3db0c2d7513e417e62e7f2fe20c817d757673a24 /tests/defer | |
parent | 8a9b0c15a6c0ef60dea3ba3042317520bc201206 (diff) | |
download | django-89527576980ff14309990c152e5904ddd5ca3df7.tar.gz |
Refs #18586 -- Split up tests.defer.DeferTests.test_defer
Diffstat (limited to 'tests/defer')
-rw-r--r-- | tests/defer/tests.py | 176 |
1 files changed, 112 insertions, 64 deletions
diff --git a/tests/defer/tests.py b/tests/defer/tests.py index 597f871cc8..acc87d88b6 100644 --- a/tests/defer/tests.py +++ b/tests/defer/tests.py @@ -6,155 +6,203 @@ from django.test import TestCase from .models import Secondary, Primary, Child, BigChild, ChildProxy, RefreshPrimaryProxy -class DeferTests(TestCase): +class AssertionMixin(object): def assert_delayed(self, obj, num): + """ + Instances with deferred fields look the same as normal instances when + we examine attribute values. Therefore, this method returns the number + of deferred fields on returned instances. + """ count = 0 for field in obj._meta.fields: - if isinstance(obj.__class__.__dict__.get(field.attname), - DeferredAttribute): + if isinstance(obj.__class__.__dict__.get(field.attname), DeferredAttribute): count += 1 self.assertEqual(count, num) - def test_defer(self): - # To all outward appearances, instances with deferred fields look the - # same as normal instances when we examine attribute values. Therefore - # we test for the number of deferred fields on returned instances (by - # poking at the internals), as a way to observe what is going on. - s1 = Secondary.objects.create(first="x1", second="y1") - p1 = Primary.objects.create(name="p1", value="xx", related=s1) +class DeferTests(AssertionMixin, TestCase): + @classmethod + def setUpTestData(cls): + cls.s1 = Secondary.objects.create(first="x1", second="y1") + cls.p1 = Primary.objects.create(name="p1", value="xx", related=cls.s1) + def test_defer(self): qs = Primary.objects.all() - self.assert_delayed(qs.defer("name")[0], 1) - self.assert_delayed(qs.only("name")[0], 2) + self.assert_delayed(qs.defer("name").get(pk=self.p1.pk), 1) self.assert_delayed(qs.defer("related__first")[0], 0) + self.assert_delayed(qs.defer("name").defer("value")[0], 2) + def test_only(self): + qs = Primary.objects.all() + self.assert_delayed(qs.only("name")[0], 2) + self.assert_delayed(qs.only("name").get(pk=self.p1.pk), 2) + self.assert_delayed(qs.only("name").only("value")[0], 2) + self.assert_delayed(qs.only("related__first")[0], 2) # Using 'pk' with only() should result in 3 deferred fields, namely all # of them except the model's primary key see #15494 self.assert_delayed(qs.only("pk")[0], 3) - - obj = qs.select_related().only("related__first")[0] - self.assert_delayed(obj, 2) - - self.assertEqual(obj.related_id, s1.pk) - # You can use 'pk' with reverse foreign key lookups. - self.assert_delayed(s1.primary_set.all().only('pk')[0], 3) + self.assert_delayed(self.s1.primary_set.all().only('pk')[0], 3) - self.assert_delayed(qs.defer("name").extra(select={"a": 1})[0], 1) - self.assert_delayed(qs.extra(select={"a": 1}).defer("name")[0], 1) - self.assert_delayed(qs.defer("name").defer("value")[0], 2) - self.assert_delayed(qs.only("name").only("value")[0], 2) - self.assert_delayed(qs.only("name").defer("value")[0], 2) - self.assert_delayed(qs.only("name", "value").defer("value")[0], 2) + def test_defer_only_chaining(self): + qs = Primary.objects.all() + self.assert_delayed(qs.only("name", "value").defer("name")[0], 2) + self.assert_delayed(qs.defer("name").only("value", "name")[0], 2) self.assert_delayed(qs.defer("name").only("value")[0], 2) + self.assert_delayed(qs.only("name").defer("value")[0], 2) + + def test_defer_on_an_already_deferred_field(self): + qs = Primary.objects.all() + self.assert_delayed(qs.defer("name")[0], 1) + self.assert_delayed(qs.defer("name").defer("name")[0], 1) - obj = qs.only()[0] + def test_defer_none_to_clear_deferred_set(self): + qs = Primary.objects.all() + self.assert_delayed(qs.defer("name", "value")[0], 2) self.assert_delayed(qs.defer(None)[0], 0) self.assert_delayed(qs.only("name").defer(None)[0], 0) + def test_only_none_raises_error(self): + msg = 'Cannot pass None as an argument to only().' + with self.assertRaisesMessage(TypeError, msg): + Primary.objects.only(None) + + def test_defer_extra(self): + qs = Primary.objects.all() + self.assert_delayed(qs.defer("name").extra(select={"a": 1})[0], 1) + self.assert_delayed(qs.extra(select={"a": 1}).defer("name")[0], 1) + + def test_defer_values_does_not_defer(self): # User values() won't defer anything (you get the full list of # dictionaries back), but it still works. - self.assertEqual(qs.defer("name").values()[0], { - "id": p1.id, + self.assertEqual(Primary.objects.defer("name").values()[0], { + "id": self.p1.id, "name": "p1", "value": "xx", - "related_id": s1.id, + "related_id": self.s1.id, }) - self.assertEqual(qs.only("name").values()[0], { - "id": p1.id, + + def test_only_values_does_not_defer(self): + self.assertEqual(Primary.objects.only("name").values()[0], { + "id": self.p1.id, "name": "p1", "value": "xx", - "related_id": s1.id, + "related_id": self.s1.id, }) + def test_get(self): # Using defer() and only() with get() is also valid. - self.assert_delayed(qs.defer("name").get(pk=p1.pk), 1) - self.assert_delayed(qs.only("name").get(pk=p1.pk), 2) + qs = Primary.objects.all() + self.assert_delayed(qs.defer("name").get(pk=self.p1.pk), 1) + self.assert_delayed(qs.only("name").get(pk=self.p1.pk), 2) + def test_defer_with_select_related(self): + obj = Primary.objects.select_related().defer("related__first", "related__second")[0] + self.assert_delayed(obj.related, 2) + self.assert_delayed(obj, 0) + + def test_only_with_select_related(self): + obj = Primary.objects.select_related().only("related__first")[0] + self.assert_delayed(obj, 2) + self.assert_delayed(obj.related, 1) + self.assertEqual(obj.related_id, self.s1.pk) + self.assertEqual(obj.name, "p1") + + def test_defer_selected_related_raises_invalid_query(self): # When we defer a field and also select_related it, the query is # invalid and raises an exception. with self.assertRaises(InvalidQuery): - qs.only("name").select_related("related")[0] + Primary.objects.defer("related").select_related("related")[0] + + def test_only_selected_related_raises_invalid_query(self): with self.assertRaises(InvalidQuery): - qs.defer("related").select_related("related")[0] + Primary.objects.only("name").select_related("related")[0] + def test_defer_foreign_keys_are_deferred_and_not_traversed(self): # With a depth-based select_related, all deferred ForeignKeys are # deferred instead of traversed. with self.assertNumQueries(3): - obj = qs.defer("related").select_related()[0] + obj = Primary.objects.defer("related").select_related()[0] self.assert_delayed(obj, 1) - self.assertEqual(obj.related.id, s1.pk) + self.assertEqual(obj.related.id, self.s1.pk) + def test_saving_object_with_deferred_field(self): # Saving models with deferred fields is possible (but inefficient, # since every field has to be retrieved first). - obj = Primary.objects.defer("value").get(name="p1") + Primary.objects.create(name="p2", value="xy", related=self.s1) + obj = Primary.objects.defer("value").get(name="p2") obj.name = "a new name" obj.save() self.assertQuerysetEqual( Primary.objects.all(), [ - "a new name", + "p1", "a new name", ], - lambda p: p.name + lambda p: p.name, + ordered=False, ) + def test_defer_baseclass_when_subclass_has_no_added_fields(self): # Regression for #10572 - A subclass with no extra fields can defer # fields from the base class - Child.objects.create(name="c1", value="foo", related=s1) + Child.objects.create(name="c1", value="foo", related=self.s1) # You can defer a field on a baseclass when the subclass has no fields obj = Child.objects.defer("value").get(name="c1") self.assert_delayed(obj, 1) self.assertEqual(obj.name, "c1") self.assertEqual(obj.value, "foo") - obj.name = "c2" - obj.save() + def test_only_baseclass_when_subclass_has_no_added_fields(self): # You can retrieve a single column on a base class with no fields - obj = Child.objects.only("name").get(name="c2") + Child.objects.create(name="c1", value="foo", related=self.s1) + obj = Child.objects.only("name").get(name="c1") + # on an inherited model, its PK is also fetched, hence '3' deferred fields. self.assert_delayed(obj, 3) - self.assertEqual(obj.name, "c2") + self.assertEqual(obj.name, "c1") self.assertEqual(obj.value, "foo") - obj.name = "cc" - obj.save() - BigChild.objects.create(name="b1", value="foo", related=s1, other="bar") + +class BigChildDeferTests(AssertionMixin, TestCase): + @classmethod + def setUpTestData(cls): + cls.s1 = Secondary.objects.create(first="x1", second="y1") + BigChild.objects.create(name="b1", value="foo", related=cls.s1, other="bar") + + def test_defer_baseclass_when_subclass_has_added_field(self): # You can defer a field on a baseclass obj = BigChild.objects.defer("value").get(name="b1") self.assert_delayed(obj, 1) self.assertEqual(obj.name, "b1") self.assertEqual(obj.value, "foo") self.assertEqual(obj.other, "bar") - obj.name = "b2" - obj.save() + def test_defer_subclass(self): # You can defer a field on a subclass - obj = BigChild.objects.defer("other").get(name="b2") + obj = BigChild.objects.defer("other").get(name="b1") self.assert_delayed(obj, 1) - self.assertEqual(obj.name, "b2") + self.assertEqual(obj.name, "b1") self.assertEqual(obj.value, "foo") self.assertEqual(obj.other, "bar") - obj.name = "b3" - obj.save() + def test_only_baseclass_when_subclass_has_added_field(self): # You can retrieve a single field on a baseclass - obj = BigChild.objects.only("name").get(name="b3") + obj = BigChild.objects.only("name").get(name="b1") + # when inherited model, its PK is also fetched, hence '4' deferred fields. self.assert_delayed(obj, 4) - self.assertEqual(obj.name, "b3") + self.assertEqual(obj.name, "b1") self.assertEqual(obj.value, "foo") self.assertEqual(obj.other, "bar") - obj.name = "b4" - obj.save() - # You can retrieve a single field on a baseclass - obj = BigChild.objects.only("other").get(name="b4") + def test_only_sublcass(self): + # You can retrieve a single field on a subclass + obj = BigChild.objects.only("other").get(name="b1") self.assert_delayed(obj, 4) - self.assertEqual(obj.name, "b4") + self.assertEqual(obj.name, "b1") self.assertEqual(obj.value, "foo") self.assertEqual(obj.other, "bar") - obj.name = "bb" - obj.save() + +class TestDefer2(AssertionMixin, TestCase): def test_defer_proxy(self): """ Ensure select_related together with only on a proxy model behaves |