Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Migrated custom_pk doctests. Thanks to Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13776 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1d50f6af99679c5665a4e9996febdf527b78c46f 1 parent b26f1d4
Russell Keith-Magee authored September 12, 2010
1  tests/modeltests/custom_pk/fields.py
@@ -3,6 +3,7 @@
3 3
 
4 4
 from django.db import models
5 5
 
  6
+
6 7
 class MyWrapper(object):
7 8
     def __init__(self, value):
8 9
         self.value = value
135  tests/modeltests/custom_pk/models.py
@@ -40,138 +40,3 @@ def __unicode__(self):
40 40
 class Foo(models.Model):
41 41
     bar = models.ForeignKey(Bar)
42 42
 
43  
-__test__ = {'API_TESTS':"""
44  
->>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones')
45  
->>> dan.save()
46  
->>> Employee.objects.all()
47  
-[<Employee: Dan Jones>]
48  
-
49  
->>> fran = Employee(employee_code=456, first_name='Fran', last_name='Bones')
50  
->>> fran.save()
51  
->>> Employee.objects.all()
52  
-[<Employee: Fran Bones>, <Employee: Dan Jones>]
53  
-
54  
->>> Employee.objects.get(pk=123)
55  
-<Employee: Dan Jones>
56  
->>> Employee.objects.get(pk=456)
57  
-<Employee: Fran Bones>
58  
->>> Employee.objects.get(pk=42)
59  
-Traceback (most recent call last):
60  
-    ...
61  
-DoesNotExist: Employee matching query does not exist.
62  
-
63  
-# Use the name of the primary key, rather than pk.
64  
->>> Employee.objects.get(employee_code__exact=123)
65  
-<Employee: Dan Jones>
66  
-
67  
-# pk can be used as a substitute for the primary key.
68  
->>> Employee.objects.filter(pk__in=[123, 456])
69  
-[<Employee: Fran Bones>, <Employee: Dan Jones>]
70  
-
71  
-# The primary key can be accessed via the pk property on the model.
72  
->>> e = Employee.objects.get(pk=123)
73  
->>> e.pk
74  
-123
75  
-
76  
-# Or we can use the real attribute name for the primary key:
77  
->>> e.employee_code
78  
-123
79  
-
80  
-# Fran got married and changed her last name.
81  
->>> fran = Employee.objects.get(pk=456)
82  
->>> fran.last_name = 'Jones'
83  
->>> fran.save()
84  
->>> Employee.objects.filter(last_name__exact='Jones')
85  
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
86  
->>> emps = Employee.objects.in_bulk([123, 456])
87  
->>> emps[123]
88  
-<Employee: Dan Jones>
89  
-
90  
->>> b = Business(name='Sears')
91  
->>> b.save()
92  
->>> b.employees.add(dan, fran)
93  
->>> b.employees.all()
94  
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
95  
->>> fran.business_set.all()
96  
-[<Business: Sears>]
97  
->>> Business.objects.in_bulk(['Sears'])
98  
-{u'Sears': <Business: Sears>}
99  
-
100  
->>> Business.objects.filter(name__exact='Sears')
101  
-[<Business: Sears>]
102  
->>> Business.objects.filter(pk='Sears')
103  
-[<Business: Sears>]
104  
-
105  
-# Queries across tables, involving primary key
106  
->>> Employee.objects.filter(business__name__exact='Sears')
107  
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
108  
->>> Employee.objects.filter(business__pk='Sears')
109  
-[<Employee: Dan Jones>, <Employee: Fran Jones>]
110  
-
111  
->>> Business.objects.filter(employees__employee_code__exact=123)
112  
-[<Business: Sears>]
113  
->>> Business.objects.filter(employees__pk=123)
114  
-[<Business: Sears>]
115  
->>> Business.objects.filter(employees__first_name__startswith='Fran')
116  
-[<Business: Sears>]
117  
-
118  
-# Primary key may be unicode string
119  
->>> bus = Business(name=u'jaźń')
120  
->>> bus.save()
121  
-
122  
-# The primary key must also obviously be unique, so trying to create a new
123  
-# object with the same primary key will fail.
124  
->>> try:
125  
-...    sid = transaction.savepoint()
126  
-...    Employee.objects.create(employee_code=123, first_name='Fred', last_name='Jones')
127  
-...    transaction.savepoint_commit(sid)
128  
-... except Exception, e:
129  
-...    if isinstance(e, IntegrityError):
130  
-...        transaction.savepoint_rollback(sid)
131  
-...        print "Pass"
132  
-...    else:
133  
-...        print "Fail with %s" % type(e)
134  
-Pass
135  
-
136  
-# Regression for #10785 -- Custom fields can be used for primary keys.
137  
->>> new_bar = Bar.objects.create()
138  
->>> new_foo = Foo.objects.create(bar=new_bar)
139  
-
140  
-# FIXME: This still doesn't work, but will require some changes in
141  
-# get_db_prep_lookup to fix it.
142  
-# >>> f = Foo.objects.get(bar=new_bar.pk)
143  
-# >>> f == new_foo
144  
-# True
145  
-# >>> f.bar == new_bar
146  
-# True
147  
-
148  
->>> f = Foo.objects.get(bar=new_bar)
149  
->>> f == new_foo
150  
-True
151  
->>> f.bar == new_bar
152  
-True
153  
-
154  
-"""}
155  
-
156  
-# SQLite lets objects be saved with an empty primary key, even though an
157  
-# integer is expected. So we can't check for an error being raised in that case
158  
-# for SQLite. Remove it from the suite for this next bit.
159  
-if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
160  
-    __test__["API_TESTS"] += """
161  
-# The primary key must be specified, so an error is raised if you try to create
162  
-# an object without it.
163  
->>> try:
164  
-...     sid = transaction.savepoint()
165  
-...     Employee.objects.create(first_name='Tom', last_name='Smith')
166  
-...     print 'hello'
167  
-...     transaction.savepoint_commit(sid)
168  
-...     print 'hello2'
169  
-... except Exception, e:
170  
-...     if isinstance(e, IntegrityError):
171  
-...         transaction.savepoint_rollback(sid)
172  
-...         print "Pass"
173  
-...     else:
174  
-...         print "Fail with %s" % type(e)
175  
-Pass
176  
-
177  
-"""
183  tests/modeltests/custom_pk/tests.py
... ...
@@ -0,0 +1,183 @@
  1
+# -*- coding: utf-8 -*-
  2
+from django.conf import settings
  3
+from django.db import DEFAULT_DB_ALIAS, transaction, IntegrityError
  4
+from django.test import TestCase
  5
+
  6
+from models import Employee, Business, Bar, Foo
  7
+
  8
+
  9
+class 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
+        # FIXME: This still doesn't work, but will require some changes in
  162
+        # get_db_prep_lookup to fix it.
  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
+
  172
+    # SQLite lets objects be saved with an empty primary key, even though an
  173
+    # integer is expected. So we can't check for an error being raised in that
  174
+    # case for SQLite. Remove it from the suite for this next bit.
  175
+    if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
  176
+        def test_required_pk(self):
  177
+            # The primary key must be specified, so an error is raised if you
  178
+            # try to create an object without it.
  179
+            sid = transaction.savepoint()
  180
+            self.assertRaises(IntegrityError,
  181
+                Employee.objects.create, first_name="Tom", last_name="Smith"
  182
+            )
  183
+            transaction.savepoint_rollback(sid)

0 notes on commit 1d50f6a

Please sign in to comment.
Something went wrong with that request. Please try again.