PageRenderTime 30ms CodeModel.GetById 10ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/modeltests/custom_pk/tests.py

https://code.google.com/p/mango-py/
Python | 180 lines | 142 code | 22 blank | 16 comment | 0 complexity | 9dcc3317b6d2f551034f52a34aebc35b MD5 | raw file
  1# -*- coding: utf-8 -*-
  2from django.conf import settings
  3from django.db import DEFAULT_DB_ALIAS, transaction, IntegrityError
  4from django.test import TestCase, skipIfDBFeature
  5
  6from models import Employee, Business, Bar, Foo
  7
  8
  9class CustomPKTests(TestCase):
 10    def test_custom_pk(self):
 11        dan = Employee.objects.create(
 12            employee_code=123, first_name="Dan", last_name="Jones"
 13        )
 14        self.assertQuerysetEqual(
 15            Employee.objects.all(), [
 16                "Dan Jones",
 17            ],
 18            unicode
 19        )
 20
 21        fran = Employee.objects.create(
 22            employee_code=456, first_name="Fran", last_name="Bones"
 23        )
 24        self.assertQuerysetEqual(
 25            Employee.objects.all(), [
 26                "Fran Bones",
 27                "Dan Jones",
 28            ],
 29            unicode
 30        )
 31
 32        self.assertEqual(Employee.objects.get(pk=123), dan)
 33        self.assertEqual(Employee.objects.get(pk=456), fran)
 34
 35        self.assertRaises(Employee.DoesNotExist,
 36            lambda: Employee.objects.get(pk=42)
 37        )
 38
 39        # Use the name of the primary key, rather than pk.
 40        self.assertEqual(Employee.objects.get(employee_code=123), dan)
 41        # pk can be used as a substitute for the primary key.
 42        self.assertQuerysetEqual(
 43            Employee.objects.filter(pk__in=[123, 456]), [
 44                "Fran Bones",
 45                "Dan Jones",
 46            ],
 47            unicode
 48        )
 49        # The primary key can be accessed via the pk property on the model.
 50        e = Employee.objects.get(pk=123)
 51        self.assertEqual(e.pk, 123)
 52        # Or we can use the real attribute name for the primary key:
 53        self.assertEqual(e.employee_code, 123)
 54
 55        # Fran got married and changed her last name.
 56        fran = Employee.objects.get(pk=456)
 57        fran.last_name = "Jones"
 58        fran.save()
 59
 60        self.assertQuerysetEqual(
 61            Employee.objects.filter(last_name="Jones"), [
 62                "Dan Jones",
 63                "Fran Jones",
 64            ],
 65            unicode
 66        )
 67
 68        emps = Employee.objects.in_bulk([123, 456])
 69        self.assertEqual(emps[123], dan)
 70
 71        b = Business.objects.create(name="Sears")
 72        b.employees.add(dan, fran)
 73        self.assertQuerysetEqual(
 74            b.employees.all(), [
 75                "Dan Jones",
 76                "Fran Jones",
 77            ],
 78            unicode
 79        )
 80        self.assertQuerysetEqual(
 81            fran.business_set.all(), [
 82                "Sears",
 83            ],
 84            lambda b: b.name
 85        )
 86
 87        self.assertEqual(Business.objects.in_bulk(["Sears"]), {
 88            "Sears": b,
 89        })
 90
 91        self.assertQuerysetEqual(
 92            Business.objects.filter(name="Sears"), [
 93                "Sears"
 94            ],
 95            lambda b: b.name
 96        )
 97        self.assertQuerysetEqual(
 98            Business.objects.filter(pk="Sears"), [
 99                "Sears",
100            ],
101            lambda b: b.name
102        )
103
104        # Queries across tables, involving primary key
105        self.assertQuerysetEqual(
106            Employee.objects.filter(business__name="Sears"), [
107                "Dan Jones",
108                "Fran Jones",
109            ],
110            unicode,
111        )
112        self.assertQuerysetEqual(
113            Employee.objects.filter(business__pk="Sears"), [
114                "Dan Jones",
115                "Fran Jones",
116            ],
117            unicode,
118        )
119
120        self.assertQuerysetEqual(
121            Business.objects.filter(employees__employee_code=123), [
122                "Sears",
123            ],
124            lambda b: b.name
125        )
126        self.assertQuerysetEqual(
127            Business.objects.filter(employees__pk=123), [
128                "Sears",
129            ],
130            lambda b: b.name,
131        )
132
133        self.assertQuerysetEqual(
134            Business.objects.filter(employees__first_name__startswith="Fran"), [
135                "Sears",
136            ],
137            lambda b: b.name
138        )
139
140    def test_unicode_pk(self):
141        # Primary key may be unicode string
142        bus = Business.objects.create(name=u'ja???„')
143
144    def test_unique_pk(self):
145        # The primary key must also obviously be unique, so trying to create a
146        # new object with the same primary key will fail.
147        e = Employee.objects.create(
148            employee_code=123, first_name="Frank", last_name="Jones"
149        )
150        sid = transaction.savepoint()
151        self.assertRaises(IntegrityError,
152            Employee.objects.create, employee_code=123, first_name="Fred", last_name="Jones"
153        )
154        transaction.savepoint_rollback(sid)
155
156    def test_custom_field_pk(self):
157        # Regression for #10785 -- Custom fields can be used for primary keys.
158        new_bar = Bar.objects.create()
159        new_foo = Foo.objects.create(bar=new_bar)
160
161        f = Foo.objects.get(bar=new_bar.pk)
162        self.assertEqual(f, new_foo)
163        self.assertEqual(f.bar, new_bar)
164
165        f = Foo.objects.get(bar=new_bar)
166        self.assertEqual(f, new_foo),
167        self.assertEqual(f.bar, new_bar)
168
169    # SQLite lets objects be saved with an empty primary key, even though an
170    # integer is expected. So we can't check for an error being raised in that
171    # case for SQLite. Remove it from the suite for this next bit.
172    @skipIfDBFeature('supports_unspecified_pk')
173    def test_required_pk(self):
174        # The primary key must be specified, so an error is raised if you
175        # try to create an object without it.
176        sid = transaction.savepoint()
177        self.assertRaises(IntegrityError,
178            Employee.objects.create, first_name="Tom", last_name="Smith"
179        )
180        transaction.savepoint_rollback(sid)