| 1 |
from django.db import IntegrityError, transaction |
| 2 |
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature |
| 3 |
|
| 4 |
from .fields import MyWrapper |
| 5 |
from .models import Bar, Business, CustomAutoFieldModel, Employee, Foo |
| 6 |
|
| 7 |
|
| 8 |
class BasicCustomPKTests(TestCase): |
| 9 |
@classmethod |
| 10 |
def setUpTestData(cls): |
| 11 |
cls.dan = Employee.objects.create( |
| 12 |
employee_code=123, |
| 13 |
first_name="Dan", |
| 14 |
last_name="Jones", |
| 15 |
) |
| 16 |
cls.fran = Employee.objects.create( |
| 17 |
employee_code=456, |
| 18 |
first_name="Fran", |
| 19 |
last_name="Bones", |
| 20 |
) |
| 21 |
cls.business = Business.objects.create(name="Sears") |
| 22 |
cls.business.employees.add(cls.dan, cls.fran) |
| 23 |
|
| 24 |
def test_querysets(self): |
| 25 |
""" |
| 26 |
Both pk and custom attribute_name can be used in filter and friends |
| 27 |
""" |
| 28 |
self.assertSequenceEqual(Employee.objects.filter(pk=123), [self.dan]) |
| 29 |
self.assertSequenceEqual(Employee.objects.filter(employee_code=123), [self.dan]) |
| 30 |
self.assertSequenceEqual( |
| 31 |
Employee.objects.filter(pk__in=[123, 456]), |
| 32 |
[self.fran, self.dan], |
| 33 |
) |
| 34 |
self.assertSequenceEqual(Employee.objects.all(), [self.fran, self.dan]) |
| 35 |
|
| 36 |
self.assertQuerySetEqual( |
| 37 |
Business.objects.filter(name="Sears"), ["Sears"], lambda b: b.name |
| 38 |
) |
| 39 |
self.assertQuerySetEqual( |
| 40 |
Business.objects.filter(pk="Sears"), |
| 41 |
[ |
| 42 |
"Sears", |
| 43 |
], |
| 44 |
lambda b: b.name, |
| 45 |
) |
| 46 |
|
| 47 |
def test_querysets_related_name(self): |
| 48 |
""" |
| 49 |
Custom pk doesn't affect related_name based lookups |
| 50 |
""" |
| 51 |
self.assertSequenceEqual( |
| 52 |
self.business.employees.all(), |
| 53 |
[self.fran, self.dan], |
| 54 |
) |
| 55 |
self.assertQuerySetEqual( |
| 56 |
self.fran.business_set.all(), |
| 57 |
[ |
| 58 |
"Sears", |
| 59 |
], |
| 60 |
lambda b: b.name, |
| 61 |
) |
| 62 |
|
| 63 |
def test_querysets_relational(self): |
| 64 |
""" |
| 65 |
Queries across tables, involving primary key |
| 66 |
""" |
| 67 |
self.assertSequenceEqual( |
| 68 |
Employee.objects.filter(business__name="Sears"), |
| 69 |
[self.fran, self.dan], |
| 70 |
) |
| 71 |
self.assertSequenceEqual( |
| 72 |
Employee.objects.filter(business__pk="Sears"), |
| 73 |
[self.fran, self.dan], |
| 74 |
) |
| 75 |
|
| 76 |
self.assertQuerySetEqual( |
| 77 |
Business.objects.filter(employees__employee_code=123), |
| 78 |
[ |
| 79 |
"Sears", |
| 80 |
], |
| 81 |
lambda b: b.name, |
| 82 |
) |
| 83 |
self.assertQuerySetEqual( |
| 84 |
Business.objects.filter(employees__pk=123), |
| 85 |
[ |
| 86 |
"Sears", |
| 87 |
], |
| 88 |
lambda b: b.name, |
| 89 |
) |
| 90 |
|
| 91 |
self.assertQuerySetEqual( |
| 92 |
Business.objects.filter(employees__first_name__startswith="Fran"), |
| 93 |
[ |
| 94 |
"Sears", |
| 95 |
], |
| 96 |
lambda b: b.name, |
| 97 |
) |
| 98 |
|
| 99 |
def test_get(self): |
| 100 |
""" |
| 101 |
Get can accept pk or the real attribute name |
| 102 |
""" |
| 103 |
self.assertEqual(Employee.objects.get(pk=123), self.dan) |
| 104 |
self.assertEqual(Employee.objects.get(pk=456), self.fran) |
| 105 |
|
| 106 |
with self.assertRaises(Employee.DoesNotExist): |
| 107 |
Employee.objects.get(pk=42) |
| 108 |
|
| 109 |
# Use the name of the primary key, rather than pk. |
| 110 |
self.assertEqual(Employee.objects.get(employee_code=123), self.dan) |
| 111 |
|
| 112 |
def test_pk_attributes(self): |
| 113 |
""" |
| 114 |
pk and attribute name are available on the model |
| 115 |
No default id attribute is added |
| 116 |
""" |
| 117 |
# pk can be used as a substitute for the primary key. |
| 118 |
# The primary key can be accessed via the pk property on the model. |
| 119 |
e = Employee.objects.get(pk=123) |
| 120 |
self.assertEqual(e.pk, 123) |
| 121 |
# Or we can use the real attribute name for the primary key: |
| 122 |
self.assertEqual(e.employee_code, 123) |
| 123 |
|
| 124 |
with self.assertRaisesMessage( |
| 125 |
AttributeError, "'Employee' object has no attribute 'id'" |
| 126 |
): |
| 127 |
e.id |
| 128 |
|
| 129 |
def test_in_bulk(self): |
| 130 |
""" |
| 131 |
Custom pks work with in_bulk, both for integer and non-integer types |
| 132 |
""" |
| 133 |
emps = Employee.objects.in_bulk([123, 456]) |
| 134 |
self.assertEqual(emps[123], self.dan) |
| 135 |
|
| 136 |
self.assertEqual( |
| 137 |
Business.objects.in_bulk(["Sears"]), |
| 138 |
{ |
| 139 |
"Sears": self.business, |
| 140 |
}, |
| 141 |
) |
| 142 |
|
| 143 |
def test_save(self): |
| 144 |
""" |
| 145 |
custom pks do not affect save |
| 146 |
""" |
| 147 |
fran = Employee.objects.get(pk=456) |
| 148 |
fran.last_name = "Jones" |
| 149 |
fran.save() |
| 150 |
|
| 151 |
self.assertSequenceEqual( |
| 152 |
Employee.objects.filter(last_name="Jones"), |
| 153 |
[self.dan, fran], |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
class CustomPKTests(TestCase): |
| 158 |
def test_custom_pk_create(self): |
| 159 |
""" |
| 160 |
New objects can be created both with pk and the custom name |
| 161 |
""" |
| 162 |
Employee.objects.create(employee_code=1234, first_name="Foo", last_name="Bar") |
| 163 |
Employee.objects.create(pk=1235, first_name="Foo", last_name="Baz") |
| 164 |
Business.objects.create(name="Bears") |
| 165 |
Business.objects.create(pk="Tears") |
| 166 |
|
| 167 |
def test_unicode_pk(self): |
| 168 |
# Primary key may be Unicode string. |
| 169 |
Business.objects.create(name="jaźń") |
| 170 |
|
| 171 |
def test_unique_pk(self): |
| 172 |
# The primary key must also be unique, so trying to create a new object |
| 173 |
# with the same primary key will fail. |
| 174 |
Employee.objects.create( |
| 175 |
employee_code=123, first_name="Frank", last_name="Jones" |
| 176 |
) |
| 177 |
with self.assertRaises(IntegrityError): |
| 178 |
with transaction.atomic(): |
| 179 |
Employee.objects.create( |
| 180 |
employee_code=123, first_name="Fred", last_name="Jones" |
| 181 |
) |
| 182 |
|
| 183 |
def test_zero_non_autoincrement_pk(self): |
| 184 |
Employee.objects.create(employee_code=0, first_name="Frank", last_name="Jones") |
| 185 |
employee = Employee.objects.get(pk=0) |
| 186 |
self.assertEqual(employee.employee_code, 0) |
| 187 |
|
| 188 |
def test_custom_field_pk(self): |
| 189 |
# Regression for #10785 -- Custom fields can be used for primary keys. |
| 190 |
new_bar = Bar.objects.create() |
| 191 |
new_foo = Foo.objects.create(bar=new_bar) |
| 192 |
|
| 193 |
f = Foo.objects.get(bar=new_bar.pk) |
| 194 |
self.assertEqual(f, new_foo) |
| 195 |
self.assertEqual(f.bar, new_bar) |
| 196 |
|
| 197 |
f = Foo.objects.get(bar=new_bar) |
| 198 |
self.assertEqual(f, new_foo) |
| 199 |
self.assertEqual(f.bar, new_bar) |
| 200 |
|
| 201 |
# SQLite lets objects be saved with an empty primary key, even though an |
| 202 |
# integer is expected. So we can't check for an error being raised in that |
| 203 |
# case for SQLite. Remove it from the suite for this next bit. |
| 204 |
@skipIfDBFeature("supports_unspecified_pk") |
| 205 |
def test_required_pk(self): |
| 206 |
# The primary key must be specified, so an error is raised if you |
| 207 |
# try to create an object without it. |
| 208 |
with self.assertRaises(IntegrityError): |
| 209 |
with transaction.atomic(): |
| 210 |
Employee.objects.create(first_name="Tom", last_name="Smith") |
| 211 |
|
| 212 |
def test_auto_field_subclass_create(self): |
| 213 |
obj = CustomAutoFieldModel.objects.create() |
| 214 |
self.assertIsInstance(obj.id, MyWrapper) |
| 215 |
|
| 216 |
@skipUnlessDBFeature("can_return_rows_from_bulk_insert") |
| 217 |
def test_auto_field_subclass_bulk_create(self): |
| 218 |
obj = CustomAutoFieldModel() |
| 219 |
CustomAutoFieldModel.objects.bulk_create([obj]) |
| 220 |
self.assertIsInstance(obj.id, MyWrapper) |