mirror of
https://github.com/django/django.git
synced 2025-04-14 20:34:36 +00:00
magic-removal: Refs #1346 -- Added unit tests for expected behaviour of self referential m2m fields.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2380 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
891c3c993b
commit
64cb7da150
0
tests/modeltests/m2m_recursive/__init__.py
Normal file
0
tests/modeltests/m2m_recursive/__init__.py
Normal file
92
tests/modeltests/m2m_recursive/models.py
Normal file
92
tests/modeltests/m2m_recursive/models.py
Normal file
@ -0,0 +1,92 @@
|
||||
"""
|
||||
26. Many-to-many relationships between the same two tables
|
||||
|
||||
In this example, A Person can have many friends, who are also people. Friendship is a
|
||||
symmetrical relationshiup - if I am your friend, you are my friend.
|
||||
|
||||
This test validates that the m2m table will create a mangled name for the m2m table if
|
||||
there will be a clash, and tests that symmetry is preserved where appropriate.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
|
||||
class Person(models.Model):
|
||||
name = models.CharField(maxlength=20)
|
||||
friends = models.ManyToManyField('self')
|
||||
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
||||
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()
|
||||
[Bill, Chuck, David]
|
||||
|
||||
# Who is friends with Bill?
|
||||
>>> b.friends.all()
|
||||
[Anne]
|
||||
|
||||
# Who is friends with Chuck?
|
||||
>>> c.friends.all()
|
||||
[Anne, David]
|
||||
|
||||
# Who is friends with David?
|
||||
>>> d.friends.all()
|
||||
[Anne, 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()
|
||||
[Bill, Chuck, David]
|
||||
|
||||
# Who is friends with Bill?
|
||||
>>> b.friends.all()
|
||||
[Anne]
|
||||
|
||||
# Remove Anne from Bill's friends
|
||||
>>> b.friends.remove(a)
|
||||
|
||||
# Who is friends with Anne?
|
||||
>>> a.friends.all()
|
||||
[Chuck, 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()
|
||||
[David]
|
||||
|
||||
# Who is friends with David?
|
||||
>>> d.friends.all()
|
||||
[Chuck]
|
||||
|
||||
|
||||
"""
|
Loading…
x
Reference in New Issue
Block a user