diff options
| author | Tim Graham <timograham@gmail.com> | 2015-03-14 21:25:33 -0400 |
|---|---|---|
| committer | Loïc Bistuer <loic.bistuer@gmail.com> | 2015-07-28 09:28:25 +0700 |
| commit | adc0c4fbac98f9cb975e8fa8220323b2de638b46 (patch) | |
| tree | 6d00b444423b09a764fa3eb7d5b9f278c6531c0e /tests/generic_relations | |
| parent | c2e70f02653519db3a49cd48f5158ccad7434d25 (diff) | |
Fixed #18556 -- Allowed RelatedManager.add() to execute 1 query where possible.
Thanks Loic Bistuer for review.
Diffstat (limited to 'tests/generic_relations')
| -rw-r--r-- | tests/generic_relations/tests.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py index 47545bdef9..0162565969 100644 --- a/tests/generic_relations/tests.py +++ b/tests/generic_relations/tests.py @@ -247,6 +247,32 @@ class GenericRelationsTests(TestCase): self.comp_func ) + def test_add_bulk(self): + bacon = Vegetable.objects.create(name="Bacon", is_yucky=False) + t1 = TaggedItem.objects.create(content_object=self.quartz, tag="shiny") + t2 = TaggedItem.objects.create(content_object=self.quartz, tag="clearish") + # One update() query. + with self.assertNumQueries(1): + bacon.tags.add(t1, t2) + self.assertEqual(t1.content_object, bacon) + self.assertEqual(t2.content_object, bacon) + + def test_add_bulk_false(self): + bacon = Vegetable.objects.create(name="Bacon", is_yucky=False) + t1 = TaggedItem.objects.create(content_object=self.quartz, tag="shiny") + t2 = TaggedItem.objects.create(content_object=self.quartz, tag="clearish") + # One save() for each object. + with self.assertNumQueries(2): + bacon.tags.add(t1, t2, bulk=False) + self.assertEqual(t1.content_object, bacon) + self.assertEqual(t2.content_object, bacon) + + def test_add_rejects_unsaved_objects(self): + t1 = TaggedItem(content_object=self.quartz, tag="shiny") + msg = "<TaggedItem: shiny> instance isn't saved. Use bulk=False or save the object first." + with self.assertRaisesMessage(ValueError, msg): + self.bacon.tags.add(t1) + def test_set(self): bacon = Vegetable.objects.create(name="Bacon", is_yucky=False) fatty = bacon.tags.create(tag="fatty") @@ -266,13 +292,13 @@ class GenericRelationsTests(TestCase): bacon.tags.set([]) self.assertQuerysetEqual(bacon.tags.all(), []) - bacon.tags.set([fatty, salty], clear=True) + bacon.tags.set([fatty, salty], bulk=False, clear=True) self.assertQuerysetEqual(bacon.tags.all(), [ "<TaggedItem: fatty>", "<TaggedItem: salty>", ]) - bacon.tags.set([fatty], clear=True) + bacon.tags.set([fatty], bulk=False, clear=True) self.assertQuerysetEqual(bacon.tags.all(), [ "<TaggedItem: fatty>", ]) |
