diff options
Diffstat (limited to 'tests/generic_relations')
| -rw-r--r-- | tests/generic_relations/tests.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py index c6230b057e..bc2cfaa579 100644 --- a/tests/generic_relations/tests.py +++ b/tests/generic_relations/tests.py @@ -246,6 +246,58 @@ class GenericRelationsTests(TestCase): self.comp_func ) + def test_set(self): + bacon = Vegetable.objects.create(name="Bacon", is_yucky=False) + fatty = bacon.tags.create(tag="fatty") + salty = bacon.tags.create(tag="salty") + + bacon.tags.set([fatty, salty]) + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + "<TaggedItem: salty>", + ]) + + bacon.tags.set([fatty]) + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + ]) + + bacon.tags.set([]) + self.assertQuerysetEqual(bacon.tags.all(), []) + + bacon.tags.set([fatty, salty], clear=True) + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + "<TaggedItem: salty>", + ]) + + bacon.tags.set([fatty], clear=True) + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + ]) + + bacon.tags.set([], clear=True) + self.assertQuerysetEqual(bacon.tags.all(), []) + + def test_assign(self): + bacon = Vegetable.objects.create(name="Bacon", is_yucky=False) + fatty = bacon.tags.create(tag="fatty") + salty = bacon.tags.create(tag="salty") + + bacon.tags = [fatty, salty] + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + "<TaggedItem: salty>", + ]) + + bacon.tags = [fatty] + self.assertQuerysetEqual(bacon.tags.all(), [ + "<TaggedItem: fatty>", + ]) + + bacon.tags = [] + self.assertQuerysetEqual(bacon.tags.all(), []) + def test_assign_with_queryset(self): # Ensure that querysets used in reverse GFK assignments are pre-evaluated # so their value isn't affected by the clearing operation in |
