PageRenderTime 29ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/modeltests/one_to_one/tests.py

https://code.google.com/p/mango-py/
Python | 119 lines | 93 code | 10 blank | 16 comment | 0 complexity | 5b4933c79930d861885fc643bd1677d9 MD5 | raw file
  1from django.test import TestCase
  2from django.db import transaction, IntegrityError
  3from models import Place, Restaurant, Waiter, ManualPrimaryKey, RelatedModel, MultiModel
  4
  5class 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)