Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[soc2010/test-refactor] Moved custom_pk modeltest to unittest

  • Loading branch information...
commit 42570206db67fa4206883ef5fd935f6eda8a47f7 1 parent 078b13e
Paul McMillan PaulMcMillan authored
136 tests/modeltests/custom_pk/models.py
@@ -39,139 +39,3 @@ def __unicode__(self):
39 39
40 40 class Foo(models.Model):
41 41 bar = models.ForeignKey(Bar)
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   -"""
125 tests/modeltests/custom_pk/tests.py
... ... @@ -0,0 +1,125 @@
  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 comments on commit 4257020

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