Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 64cb7da1508086781c51c85488e8e6fff9bb72a0 1 parent 891c3c9
Russell Keith-Magee authored February 24, 2006
0  tests/modeltests/m2m_recursive/__init__.py
No changes.
92  tests/modeltests/m2m_recursive/models.py
... ...
@@ -0,0 +1,92 @@
  1
+"""
  2
+26. Many-to-many relationships between the same two tables
  3
+
  4
+In this example, A Person can have many friends, who are also people. Friendship is a 
  5
+symmetrical relationshiup - if I am your friend, you are my friend.
  6
+
  7
+This test validates that the m2m table will create a mangled name for the m2m table if
  8
+there will be a clash, and tests that symmetry is preserved where appropriate. 
  9
+"""
  10
+
  11
+from django.db import models
  12
+
  13
+class Person(models.Model):
  14
+    name = models.CharField(maxlength=20)
  15
+    friends = models.ManyToManyField('self')
  16
+
  17
+    def __repr__(self):
  18
+        return self.name
  19
+
  20
+API_TESTS = """
  21
+>>> a = Person(name='Anne')
  22
+>>> a.save()
  23
+>>> b = Person(name='Bill')
  24
+>>> b.save()
  25
+>>> c = Person(name='Chuck')
  26
+>>> c.save()
  27
+>>> d = Person(name='David')
  28
+>>> d.save()
  29
+
  30
+# Add some friends in the direction of field definition
  31
+# Anne is friends with Bill and Chuck
  32
+>>> a.friends.add(b,c)
  33
+
  34
+# David is friends with Anne and Chuck - add in reverse direction
  35
+>>> d.friends.add(a,c)
  36
+
  37
+# Who is friends with Anne?
  38
+>>> a.friends.all() 
  39
+[Bill, Chuck, David]
  40
+
  41
+# Who is friends with Bill?
  42
+>>> b.friends.all()
  43
+[Anne]
  44
+
  45
+# Who is friends with Chuck?
  46
+>>> c.friends.all()
  47
+[Anne, David]
  48
+
  49
+# Who is friends with David?
  50
+>>> d.friends.all() 
  51
+[Anne, Chuck]
  52
+
  53
+# Bill is already friends with Anne - add Anne again, but in the reverse direction
  54
+>>> b.friends.add(a)
  55
+
  56
+# Who is friends with Anne?
  57
+>>> a.friends.all() 
  58
+[Bill, Chuck, David]
  59
+
  60
+# Who is friends with Bill?
  61
+>>> b.friends.all()
  62
+[Anne]
  63
+
  64
+# Remove Anne from Bill's friends
  65
+>>> b.friends.remove(a)
  66
+
  67
+# Who is friends with Anne?
  68
+>>> a.friends.all() 
  69
+[Chuck, David]
  70
+
  71
+# Who is friends with Bill?
  72
+>>> b.friends.all()
  73
+[]
  74
+
  75
+# Clear Anne's group of friends
  76
+>>> a.friends.clear()
  77
+
  78
+# Who is friends with Anne?
  79
+>>> a.friends.all() 
  80
+[]
  81
+
  82
+# Reverse relationships should also be gone
  83
+# Who is friends with Chuck?
  84
+>>> c.friends.all()
  85
+[David]
  86
+
  87
+# Who is friends with David?
  88
+>>> d.friends.all() 
  89
+[Chuck]
  90
+
  91
+
  92
+"""

0 notes on commit 64cb7da

Please sign in to comment.
Something went wrong with that request. Please try again.