~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/modeltests/custom_pk/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

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