diff options
| author | Alex Gaynor <alex.gaynor@gmail.com> | 2010-10-09 16:27:19 +0000 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@gmail.com> | 2010-10-09 16:27:19 +0000 |
| commit | 2e5f0c228fb7bf65c83d8d83f3237db6a5e5ccb4 (patch) | |
| tree | 80ff2dddab81012a598e88fbeb257780be265c73 /tests | |
| parent | 935f6873dd9bfa1582575c0309f0f8dcab0c0102 (diff) | |
[1.2.X]. Convert m2m_recursive tests to unittests. We have always been at war with doctests. Backport of [14082].
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14083 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/m2m_recursive/models.py | 170 | ||||
| -rw-r--r-- | tests/modeltests/m2m_recursive/tests.py | 253 |
2 files changed, 254 insertions, 169 deletions
diff --git a/tests/modeltests/m2m_recursive/models.py b/tests/modeltests/m2m_recursive/models.py index 23be6f31ba..83c943ae60 100644 --- a/tests/modeltests/m2m_recursive/models.py +++ b/tests/modeltests/m2m_recursive/models.py @@ -18,6 +18,7 @@ appropriate. from django.db import models + class Person(models.Model): name = models.CharField(max_length=20) friends = models.ManyToManyField('self') @@ -25,172 +26,3 @@ class Person(models.Model): def __unicode__(self): return self.name - -__test__ = {'API_TESTS':""" ->>> a = Person(name='Anne') ->>> a.save() ->>> b = Person(name='Bill') ->>> b.save() ->>> c = Person(name='Chuck') ->>> c.save() ->>> d = Person(name='David') ->>> d.save() - -# Add some friends in the direction of field definition -# Anne is friends with Bill and Chuck ->>> a.friends.add(b,c) - -# David is friends with Anne and Chuck - add in reverse direction ->>> d.friends.add(a,c) - -# Who is friends with Anne? ->>> a.friends.all() -[<Person: Bill>, <Person: Chuck>, <Person: David>] - -# Who is friends with Bill? ->>> b.friends.all() -[<Person: Anne>] - -# Who is friends with Chuck? ->>> c.friends.all() -[<Person: Anne>, <Person: David>] - -# Who is friends with David? ->>> d.friends.all() -[<Person: Anne>, <Person: Chuck>] - -# Bill is already friends with Anne - add Anne again, but in the reverse direction ->>> b.friends.add(a) - -# Who is friends with Anne? ->>> a.friends.all() -[<Person: Bill>, <Person: Chuck>, <Person: David>] - -# Who is friends with Bill? ->>> b.friends.all() -[<Person: Anne>] - -# Remove Anne from Bill's friends ->>> b.friends.remove(a) - -# Who is friends with Anne? ->>> a.friends.all() -[<Person: Chuck>, <Person: David>] - -# Who is friends with Bill? ->>> b.friends.all() -[] - -# Clear Anne's group of friends ->>> a.friends.clear() - -# Who is friends with Anne? ->>> a.friends.all() -[] - -# Reverse relationships should also be gone -# Who is friends with Chuck? ->>> c.friends.all() -[<Person: David>] - -# Who is friends with David? ->>> d.friends.all() -[<Person: Chuck>] - - -# Add some idols in the direction of field definition -# Anne idolizes Bill and Chuck ->>> a.idols.add(b,c) - -# Bill idolizes Anne right back ->>> b.idols.add(a) - -# David is idolized by Anne and Chuck - add in reverse direction ->>> d.stalkers.add(a,c) - -# Who are Anne's idols? ->>> a.idols.all() -[<Person: Bill>, <Person: Chuck>, <Person: David>] - -# Who is stalking Anne? ->>> a.stalkers.all() -[<Person: Bill>] - -# Who are Bill's idols? ->>> b.idols.all() -[<Person: Anne>] - -# Who is stalking Bill? ->>> b.stalkers.all() -[<Person: Anne>] - -# Who are Chuck's idols? ->>> c.idols.all() -[<Person: David>] - -# Who is stalking Chuck? ->>> c.stalkers.all() -[<Person: Anne>] - -# Who are David's idols? ->>> d.idols.all() -[] - -# Who is stalking David ->>> d.stalkers.all() -[<Person: Anne>, <Person: Chuck>] - -# Bill is already being stalked by Anne - add Anne again, but in the reverse direction ->>> b.stalkers.add(a) - -# Who are Anne's idols? ->>> a.idols.all() -[<Person: Bill>, <Person: Chuck>, <Person: David>] - -# Who is stalking Anne? -[<Person: Bill>] - -# Who are Bill's idols ->>> b.idols.all() -[<Person: Anne>] - -# Who is stalking Bill? ->>> b.stalkers.all() -[<Person: Anne>] - -# Remove Anne from Bill's list of stalkers ->>> b.stalkers.remove(a) - -# Who are Anne's idols? ->>> a.idols.all() -[<Person: Chuck>, <Person: David>] - -# Who is stalking Anne? ->>> a.stalkers.all() -[<Person: Bill>] - -# Who are Bill's idols? ->>> b.idols.all() -[<Person: Anne>] - -# Who is stalking Bill? ->>> b.stalkers.all() -[] - -# Clear Anne's group of idols ->>> a.idols.clear() - -# Who are Anne's idols ->>> a.idols.all() -[] - -# Reverse relationships should also be gone -# Who is stalking Chuck? ->>> c.stalkers.all() -[] - -# Who is friends with David? ->>> d.stalkers.all() -[<Person: Chuck>] - -"""} diff --git a/tests/modeltests/m2m_recursive/tests.py b/tests/modeltests/m2m_recursive/tests.py new file mode 100644 index 0000000000..4251028093 --- /dev/null +++ b/tests/modeltests/m2m_recursive/tests.py @@ -0,0 +1,253 @@ +from operator import attrgetter + +from django.test import TestCase + +from models import Person + + +class RecursiveM2MTests(TestCase): + def test_recursive_m2m(self): + a, b, c, d = [ + Person.objects.create(name=name) + for name in ["Anne", "Bill", "Chuck", "David"] + ] + + # Add some friends in the direction of field definition + # Anne is friends with Bill and Chuck + a.friends.add(b, c) + + # David is friends with Anne and Chuck - add in reverse direction + d.friends.add(a,c) + + # Who is friends with Anne? + self.assertQuerysetEqual( + a.friends.all(), [ + "Bill", + "Chuck", + "David" + ], + attrgetter("name") + ) + # Who is friends with Bill? + self.assertQuerysetEqual( + b.friends.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who is friends with Chuck? + self.assertQuerysetEqual( + c.friends.all(), [ + "Anne", + "David" + ], + attrgetter("name") + ) + # Who is friends with David? + self.assertQuerysetEqual( + d.friends.all(), [ + "Anne", + "Chuck", + ], + attrgetter("name") + ) + # Bill is already friends with Anne - add Anne again, but in the + # reverse direction + b.friends.add(a) + + # Who is friends with Anne? + self.assertQuerysetEqual( + a.friends.all(), [ + "Bill", + "Chuck", + "David", + ], + attrgetter("name") + ) + # Who is friends with Bill? + self.assertQuerysetEqual( + b.friends.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Remove Anne from Bill's friends + b.friends.remove(a) + # Who is friends with Anne? + self.assertQuerysetEqual( + a.friends.all(), [ + "Chuck", + "David", + ], + attrgetter("name") + ) + # Who is friends with Bill? + self.assertQuerysetEqual( + b.friends.all(), [] + ) + + # Clear Anne's group of friends + a.friends.clear() + # Who is friends with Anne? + self.assertQuerysetEqual( + a.friends.all(), [] + ) + # Reverse relationships should also be gone + # Who is friends with Chuck? + self.assertQuerysetEqual( + c.friends.all(), [ + "David", + ], + attrgetter("name") + ) + # Who is friends with David? + self.assertQuerysetEqual( + d.friends.all(), [ + "Chuck", + ], + attrgetter("name") + ) + + # Add some idols in the direction of field definition + # Anne idolizes Bill and Chuck + a.idols.add(b, c) + # Bill idolizes Anne right back + b.idols.add(a) + # David is idolized by Anne and Chuck - add in reverse direction + d.stalkers.add(a, c) + + # Who are Anne's idols? + self.assertQuerysetEqual( + a.idols.all(), [ + "Bill", + "Chuck", + "David", + ], + attrgetter("name") + ) + # Who is stalking Anne? + self.assertQuerysetEqual( + a.stalkers.all(), [ + "Bill", + ], + attrgetter("name") + ) + # Who are Bill's idols? + self.assertQuerysetEqual( + b.idols.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who is stalking Bill? + self.assertQuerysetEqual( + b.stalkers.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who are Chuck's idols? + self.assertQuerysetEqual( + c.idols.all(), [ + "David", + ], + attrgetter("name"), + ) + # Who is stalking Chuck? + self.assertQuerysetEqual( + c.stalkers.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who are David's idols? + self.assertQuerysetEqual( + d.idols.all(), [] + ) + # Who is stalking David + self.assertQuerysetEqual( + d.stalkers.all(), [ + "Anne", + "Chuck", + ], + attrgetter("name") + ) + # Bill is already being stalked by Anne - add Anne again, but in the + # reverse direction + b.stalkers.add(a) + # Who are Anne's idols? + self.assertQuerysetEqual( + a.idols.all(), [ + "Bill", + "Chuck", + "David", + ], + attrgetter("name") + ) + # Who is stalking Anne? + self.assertQuerysetEqual( + a.stalkers.all(), [ + "Bill", + ], + attrgetter("name") + ) + # Who are Bill's idols + self.assertQuerysetEqual( + b.idols.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who is stalking Bill? + self.assertQuerysetEqual( + b.stalkers.all(), [ + "Anne", + ], + attrgetter("name"), + ) + # Remove Anne from Bill's list of stalkers + b.stalkers.remove(a) + # Who are Anne's idols? + self.assertQuerysetEqual( + a.idols.all(), [ + "Chuck", + "David", + ], + attrgetter("name") + ) + # Who is stalking Anne? + self.assertQuerysetEqual( + a.stalkers.all(), [ + "Bill", + ], + attrgetter("name") + ) + # Who are Bill's idols? + self.assertQuerysetEqual( + b.idols.all(), [ + "Anne", + ], + attrgetter("name") + ) + # Who is stalking Bill? + self.assertQuerysetEqual( + b.stalkers.all(), [] + ) + # Clear Anne's group of idols + a.idols.clear() + # Who are Anne's idols + self.assertQuerysetEqual( + a.idols.all(), [] + ) + # Reverse relationships should also be gone + # Who is stalking Chuck? + self.assertQuerysetEqual( + c.stalkers.all(), [] + ) + # Who is friends with David? + self.assertQuerysetEqual( + d.stalkers.all(), [ + "Chuck", + ], + attrgetter("name") + ) |
