Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated one_to_one doctests. Thanks to George Sakkis for the…

… patch. (We have always been at war with doctests)

Backport of r14167 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6f7f1f651b45b3cac20ccfa17acb0ec71ef6a671 1 parent 6b1a452
Russell Keith-Magee authored October 12, 2010
147  tests/modeltests/one_to_one/models.py
@@ -45,150 +45,3 @@ class MultiModel(models.Model):
45 45
 
46 46
     def __unicode__(self):
47 47
         return u"Multimodel %s" % self.name
48  
-
49  
-__test__ = {'API_TESTS':"""
50  
-# Create a couple of Places.
51  
->>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
52  
->>> p1.save()
53  
->>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
54  
->>> p2.save()
55  
-
56  
-# Create a Restaurant. Pass the ID of the "parent" object as this object's ID.
57  
->>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
58  
->>> r.save()
59  
-
60  
-# A Restaurant can access its place.
61  
->>> r.place
62  
-<Place: Demon Dogs the place>
63  
-
64  
-# A Place can access its restaurant, if available.
65  
->>> p1.restaurant
66  
-<Restaurant: Demon Dogs the restaurant>
67  
-
68  
-# p2 doesn't have an associated restaurant.
69  
->>> p2.restaurant
70  
-Traceback (most recent call last):
71  
-    ...
72  
-DoesNotExist: Restaurant matching query does not exist.
73  
-
74  
-# Set the place using assignment notation. Because place is the primary key on
75  
-# Restaurant, the save will create a new restaurant
76  
->>> r.place = p2
77  
->>> r.save()
78  
->>> p2.restaurant
79  
-<Restaurant: Ace Hardware the restaurant>
80  
->>> r.place
81  
-<Place: Ace Hardware the place>
82  
->>> p2.id
83  
-2
84  
-
85  
-# Set the place back again, using assignment in the reverse direction.
86  
->>> p1.restaurant = r
87  
->>> p1.restaurant
88  
-<Restaurant: Demon Dogs the restaurant>
89  
-
90  
->>> r = Restaurant.objects.get(pk=1)
91  
->>> r.place
92  
-<Place: Demon Dogs the place>
93  
-
94  
-# Restaurant.objects.all() just returns the Restaurants, not the Places.
95  
-# Note that there are two restaurants - Ace Hardware the Restaurant was created
96  
-# in the call to r.place = p2.
97  
->>> Restaurant.objects.all()
98  
-[<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]
99  
-
100  
-# Place.objects.all() returns all Places, regardless of whether they have
101  
-# Restaurants.
102  
->>> Place.objects.order_by('name')
103  
-[<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]
104  
-
105  
->>> Restaurant.objects.get(place__id__exact=1)
106  
-<Restaurant: Demon Dogs the restaurant>
107  
->>> Restaurant.objects.get(pk=1)
108  
-<Restaurant: Demon Dogs the restaurant>
109  
->>> Restaurant.objects.get(place__exact=1)
110  
-<Restaurant: Demon Dogs the restaurant>
111  
->>> Restaurant.objects.get(place__exact=p1)
112  
-<Restaurant: Demon Dogs the restaurant>
113  
->>> Restaurant.objects.get(place=1)
114  
-<Restaurant: Demon Dogs the restaurant>
115  
->>> Restaurant.objects.get(place=p1)
116  
-<Restaurant: Demon Dogs the restaurant>
117  
->>> Restaurant.objects.get(place__pk=1)
118  
-<Restaurant: Demon Dogs the restaurant>
119  
->>> Restaurant.objects.get(place__name__startswith="Demon")
120  
-<Restaurant: Demon Dogs the restaurant>
121  
-
122  
->>> Place.objects.get(id__exact=1)
123  
-<Place: Demon Dogs the place>
124  
->>> Place.objects.get(pk=1)
125  
-<Place: Demon Dogs the place>
126  
->>> Place.objects.get(restaurant__place__exact=1)
127  
-<Place: Demon Dogs the place>
128  
->>> Place.objects.get(restaurant__place__exact=p1)
129  
-<Place: Demon Dogs the place>
130  
->>> Place.objects.get(restaurant__pk=1)
131  
-<Place: Demon Dogs the place>
132  
->>> Place.objects.get(restaurant=1)
133  
-<Place: Demon Dogs the place>
134  
->>> Place.objects.get(restaurant=r)
135  
-<Place: Demon Dogs the place>
136  
->>> Place.objects.get(restaurant__exact=1)
137  
-<Place: Demon Dogs the place>
138  
->>> Place.objects.get(restaurant__exact=r)
139  
-<Place: Demon Dogs the place>
140  
-
141  
-# Add a Waiter to the Restaurant.
142  
->>> w = r.waiter_set.create(name='Joe')
143  
->>> w.save()
144  
->>> w
145  
-<Waiter: Joe the waiter at Demon Dogs the restaurant>
146  
-
147  
-# Query the waiters
148  
->>> Waiter.objects.filter(restaurant__place__pk=1)
149  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
150  
->>> Waiter.objects.filter(restaurant__place__exact=1)
151  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
152  
->>> Waiter.objects.filter(restaurant__place__exact=p1)
153  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
154  
->>> Waiter.objects.filter(restaurant__pk=1)
155  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
156  
->>> Waiter.objects.filter(id__exact=1)
157  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
158  
->>> Waiter.objects.filter(pk=1)
159  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
160  
->>> Waiter.objects.filter(restaurant=1)
161  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
162  
->>> Waiter.objects.filter(restaurant=r)
163  
-[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
164  
-
165  
-# Delete the restaurant; the waiter should also be removed
166  
->>> r = Restaurant.objects.get(pk=1)
167  
->>> r.delete()
168  
-
169  
-# One-to-one fields still work if you create your own primary key
170  
->>> o1 = ManualPrimaryKey(primary_key="abc123", name="primary")
171  
->>> o1.save()
172  
->>> o2 = RelatedModel(link=o1, name="secondary")
173  
->>> o2.save()
174  
-
175  
-# You can have multiple one-to-one fields on a model, too.
176  
->>> x1 = MultiModel(link1=p1, link2=o1, name="x1")
177  
->>> x1.save()
178  
->>> o1.multimodel
179  
-<MultiModel: Multimodel x1>
180  
-
181  
-# This will fail because each one-to-one field must be unique (and link2=o1 was
182  
-# used for x1, above).
183  
->>> sid = transaction.savepoint()
184  
->>> try:
185  
-...     MultiModel(link1=p2, link2=o1, name="x1").save()
186  
-... except Exception, e:
187  
-...     if isinstance(e, IntegrityError):
188  
-...         print "Pass"
189  
-...     else:
190  
-...         print "Fail with %s" % type(e)
191  
-Pass
192  
->>> transaction.savepoint_rollback(sid)
193  
-
194  
-"""}
119  tests/modeltests/one_to_one/tests.py
... ...
@@ -0,0 +1,119 @@
  1
+from django.test import TestCase
  2
+from django.db import transaction, IntegrityError
  3
+from models import Place, Restaurant, Waiter, ManualPrimaryKey, RelatedModel, MultiModel
  4
+
  5
+class OneToOneTests(TestCase):
  6
+
  7
+    def setUp(self):
  8
+        self.p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
  9
+        self.p1.save()
  10
+        self.p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
  11
+        self.p2.save()
  12
+        self.r = Restaurant(place=self.p1, serves_hot_dogs=True, serves_pizza=False)
  13
+        self.r.save()
  14
+
  15
+    def test_getter(self):
  16
+        # A Restaurant can access its place.
  17
+        self.assertEqual(repr(self.r.place), '<Place: Demon Dogs the place>')
  18
+        # A Place can access its restaurant, if available.
  19
+        self.assertEqual(repr(self.p1.restaurant), '<Restaurant: Demon Dogs the restaurant>')
  20
+        # p2 doesn't have an associated restaurant.
  21
+        self.assertRaises(Restaurant.DoesNotExist, getattr, self.p2, 'restaurant')
  22
+
  23
+    def test_setter(self):
  24
+        # Set the place using assignment notation. Because place is the primary
  25
+        # key on Restaurant, the save will create a new restaurant
  26
+        self.r.place = self.p2
  27
+        self.r.save()
  28
+        self.assertEqual(repr(self.p2.restaurant), '<Restaurant: Ace Hardware the restaurant>')
  29
+        self.assertEqual(repr(self.r.place), '<Place: Ace Hardware the place>')
  30
+        self.assertEqual(self.p2.pk, self.r.pk)
  31
+        # Set the place back again, using assignment in the reverse direction.
  32
+        self.p1.restaurant = self.r
  33
+        self.assertEqual(repr(self.p1.restaurant), '<Restaurant: Demon Dogs the restaurant>')
  34
+        r = Restaurant.objects.get(pk=self.p1.id)
  35
+        self.assertEqual(repr(r.place), '<Place: Demon Dogs the place>')
  36
+
  37
+    def test_manager_all(self):
  38
+        # Restaurant.objects.all() just returns the Restaurants, not the Places.
  39
+        self.assertQuerysetEqual(Restaurant.objects.all(), [
  40
+            '<Restaurant: Demon Dogs the restaurant>',
  41
+        ])
  42
+        # Place.objects.all() returns all Places, regardless of whether they
  43
+        # have Restaurants.
  44
+        self.assertQuerysetEqual(Place.objects.order_by('name'), [
  45
+            '<Place: Ace Hardware the place>',
  46
+            '<Place: Demon Dogs the place>',
  47
+        ])
  48
+
  49
+    def test_manager_get(self):
  50
+        def assert_get_restaurant(**params):
  51
+            self.assertEqual(repr(Restaurant.objects.get(**params)),
  52
+                             '<Restaurant: Demon Dogs the restaurant>')
  53
+        assert_get_restaurant(place__id__exact=self.p1.pk)
  54
+        assert_get_restaurant(place__id=self.p1.pk)
  55
+        assert_get_restaurant(place__exact=self.p1.pk)
  56
+        assert_get_restaurant(place__exact=self.p1)
  57
+        assert_get_restaurant(place=self.p1.pk)
  58
+        assert_get_restaurant(place=self.p1)
  59
+        assert_get_restaurant(pk=self.p1.pk)
  60
+        assert_get_restaurant(place__pk__exact=self.p1.pk)
  61
+        assert_get_restaurant(place__pk=self.p1.pk)
  62
+        assert_get_restaurant(place__name__startswith="Demon")
  63
+
  64
+        def assert_get_place(**params):
  65
+            self.assertEqual(repr(Place.objects.get(**params)),
  66
+                             '<Place: Demon Dogs the place>')
  67
+        assert_get_place(restaurant__place__exact=self.p1.pk)
  68
+        assert_get_place(restaurant__place__exact=self.p1)
  69
+        assert_get_place(restaurant__place__pk=self.p1.pk)
  70
+        assert_get_place(restaurant__exact=self.p1.pk)
  71
+        assert_get_place(restaurant__exact=self.r)
  72
+        assert_get_place(restaurant__pk=self.p1.pk)
  73
+        assert_get_place(restaurant=self.p1.pk)
  74
+        assert_get_place(restaurant=self.r)
  75
+        assert_get_place(id__exact=self.p1.pk)
  76
+        assert_get_place(pk=self.p1.pk)
  77
+
  78
+    def test_foreign_key(self):
  79
+        # Add a Waiter to the Restaurant.
  80
+        w = self.r.waiter_set.create(name='Joe')
  81
+        w.save()
  82
+        self.assertEqual(repr(w), '<Waiter: Joe the waiter at Demon Dogs the restaurant>')
  83
+        # Query the waiters
  84
+        def assert_filter_waiters(**params):
  85
+            self.assertQuerysetEqual(Waiter.objects.filter(**params), [
  86
+                '<Waiter: Joe the waiter at Demon Dogs the restaurant>'
  87
+            ])
  88
+        assert_filter_waiters(restaurant__place__exact=self.p1.pk)
  89
+        assert_filter_waiters(restaurant__place__exact=self.p1)
  90
+        assert_filter_waiters(restaurant__place__pk=self.p1.pk)
  91
+        assert_filter_waiters(restaurant__exact=self.p1.pk)
  92
+        assert_filter_waiters(restaurant__exact=self.p1)
  93
+        assert_filter_waiters(restaurant__pk=self.p1.pk)
  94
+        assert_filter_waiters(restaurant=self.p1.pk)
  95
+        assert_filter_waiters(restaurant=self.r)
  96
+        assert_filter_waiters(id__exact=self.p1.pk)
  97
+        assert_filter_waiters(pk=self.p1.pk)
  98
+        # Delete the restaurant; the waiter should also be removed
  99
+        r = Restaurant.objects.get(pk=self.p1.pk)
  100
+        r.delete()
  101
+        self.assertEqual(Waiter.objects.count(), 0)
  102
+
  103
+    def test_multiple_o2o(self):
  104
+        # One-to-one fields still work if you create your own primary key
  105
+        o1 = ManualPrimaryKey(primary_key="abc123", name="primary")
  106
+        o1.save()
  107
+        o2 = RelatedModel(link=o1, name="secondary")
  108
+        o2.save()
  109
+
  110
+        # You can have multiple one-to-one fields on a model, too.
  111
+        x1 = MultiModel(link1=self.p1, link2=o1, name="x1")
  112
+        x1.save()
  113
+        self.assertEqual(repr(o1.multimodel), '<MultiModel: Multimodel x1>')
  114
+        # This will fail because each one-to-one field must be unique (and
  115
+        # link2=o1 was used for x1, above).
  116
+        sid = transaction.savepoint()
  117
+        mm = MultiModel(link1=self.p2, link2=o1, name="x1")
  118
+        self.assertRaises(IntegrityError, mm.save)
  119
+        transaction.savepoint_rollback(sid)

0 notes on commit 6f7f1f6

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