|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | +from django.conf import settings |
| 5 | +from django.db import transaction, IntegrityError, DEFAULT_DB_ALIAS |
| 6 | + |
| 7 | +from models import Employee, Business, Bar, Foo |
| 8 | + |
| 9 | +class CustomPkTestCase(TestCase): |
| 10 | + #no fixture here because MyWrapper doesn't serialize nicely |
| 11 | + |
| 12 | + def test_custom_pk(self): |
| 13 | + dan = Employee(employee_code=123, first_name='Dan', last_name='Jones') |
| 14 | + dan.save() |
| 15 | + self.assertQuerysetEqual(Employee.objects.all(), |
| 16 | + ['<Employee: Dan Jones>']) |
| 17 | + |
| 18 | + fran = Employee(employee_code=456, first_name='Fran', last_name='Bones') |
| 19 | + fran.save() |
| 20 | + self.assertQuerysetEqual(Employee.objects.all(), |
| 21 | + ['<Employee: Fran Bones>', '<Employee: Dan Jones>']) |
| 22 | + |
| 23 | + self.assertEqual(repr(Employee.objects.get(pk=123)), |
| 24 | + '<Employee: Dan Jones>') |
| 25 | + self.assertEqual(repr(Employee.objects.get(pk=456)), |
| 26 | + '<Employee: Fran Bones>') |
| 27 | + |
| 28 | + self.assertRaises(Employee.DoesNotExist, |
| 29 | + Employee.objects.get, pk=42) |
| 30 | + |
| 31 | + # Use the name of the primary key, rather than pk. |
| 32 | + self.assertEqual(repr(Employee.objects.get(employee_code__exact=123)), |
| 33 | + '<Employee: Dan Jones>') |
| 34 | + |
| 35 | + # pk can be used as a substitute for the primary key. |
| 36 | + self.assertQuerysetEqual(Employee.objects.filter(pk__in=[123, 456]), |
| 37 | + ['<Employee: Fran Bones>', '<Employee: Dan Jones>']) |
| 38 | + |
| 39 | + # The primary key can be accessed via the pk property on the model. |
| 40 | + e = Employee.objects.get(pk=123) |
| 41 | + self.assertEqual(e.pk, 123) |
| 42 | + |
| 43 | + # Or we can use the real attribute name for the primary key: |
| 44 | + self.assertEqual(e.employee_code, 123) |
| 45 | + |
| 46 | + # Fran got married and changed her last name. |
| 47 | + fran = Employee.objects.get(pk=456) |
| 48 | + fran.last_name = 'Jones' |
| 49 | + fran.save() |
| 50 | + |
| 51 | + self.assertQuerysetEqual(Employee.objects.filter(last_name__exact='Jones') , |
| 52 | + ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) |
| 53 | + |
| 54 | + emps = Employee.objects.in_bulk([123, 456]) |
| 55 | + self.assertEqual(repr(emps[123]), |
| 56 | + '<Employee: Dan Jones>') |
| 57 | + |
| 58 | + |
| 59 | + b = Business(name='Sears') |
| 60 | + b.save() |
| 61 | + b.employees.add(dan, fran) |
| 62 | + self.assertQuerysetEqual(b.employees.all(), |
| 63 | + ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) |
| 64 | + self.assertQuerysetEqual(fran.business_set.all(), |
| 65 | + ['<Business: Sears>']) |
| 66 | + self.assertEqual(repr(Business.objects.in_bulk(['Sears'])), |
| 67 | + "{u'Sears': <Business: Sears>}") |
| 68 | + |
| 69 | + self.assertQuerysetEqual(Business.objects.filter(name__exact='Sears'), |
| 70 | + ['<Business: Sears>']) |
| 71 | + self.assertQuerysetEqual(Business.objects.filter(pk='Sears'), |
| 72 | + ['<Business: Sears>']) |
| 73 | + |
| 74 | + # Queries across tables, involving primary key |
| 75 | + self.assertQuerysetEqual(Employee.objects.filter(business__name__exact='Sears'), |
| 76 | + ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) |
| 77 | + self.assertQuerysetEqual(Employee.objects.filter(business__pk='Sears'), |
| 78 | + ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) |
| 79 | + |
| 80 | + self.assertQuerysetEqual(Business.objects.filter(employees__employee_code__exact=123), |
| 81 | + ['<Business: Sears>']) |
| 82 | + self.assertQuerysetEqual(Business.objects.filter(employees__pk=123), |
| 83 | + ['<Business: Sears>']) |
| 84 | + self.assertQuerysetEqual(Business.objects.filter(employees__first_name__startswith='Fran'), |
| 85 | + ['<Business: Sears>']) |
| 86 | + |
| 87 | + def test_unicode_pk(self): |
| 88 | + # Primary key may be unicode string |
| 89 | + bus = Business(name=u'jaźń') |
| 90 | + bus.save() |
| 91 | + |
| 92 | + def test_unique_primary_key(self): |
| 93 | + # The primary key must also obviously be unique, so trying to create a new |
| 94 | + # object with the same primary key will fail. |
| 95 | + e = Employee.objects.create(employee_code=123, first_name='Alex', last_name='Gaynor') |
| 96 | + e.save() |
| 97 | + self.assertRaises(IntegrityError, |
| 98 | + Employee.objects.create, |
| 99 | + employee_code=123, first_name='Russell', last_name='KM') |
| 100 | + |
| 101 | + |
| 102 | + def test_custom_fields_can_be_primary_keys(self): |
| 103 | + # Regression for #10785 -- Custom fields can be used for primary keys. |
| 104 | + new_bar = Bar.objects.create() |
| 105 | + new_foo = Foo.objects.create(bar=new_bar) |
| 106 | + |
| 107 | + #works because of changes in get_db_prep_lookup |
| 108 | + f = Foo.objects.get(bar=new_bar.pk) |
| 109 | + self.assertEqual(f, new_foo) |
| 110 | + self.assertEqual(f.bar, new_bar) |
| 111 | + |
| 112 | + f = Foo.objects.get(bar=new_bar) |
| 113 | + self.assertEqual(f, new_foo) |
| 114 | + self.assertEqual(f.bar, new_bar) |
| 115 | + |
| 116 | + # SQLite lets objects be saved with an empty primary key, even though an |
| 117 | + # integer is expected. So we can't check for an error being raised in that case |
| 118 | + # for SQLite. Remove it from the suite for this next bit. |
| 119 | + def test_empty_pk_error(self): |
| 120 | + #fixme, improve this skiping with unittest2 |
| 121 | + if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3': |
| 122 | + self.assertRaises(IntegrityError, |
| 123 | + Employee.objects.create, |
| 124 | + first_name='Tom', last_name='Smith') |
| 125 | + |
0 commit comments