~ubuntu-branches/ubuntu/oneiric/python-django/oneiric

« back to all changes in this revision

Viewing changes to tests/modeltests/get_or_create/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2011-02-17 13:34:07 UTC
  • mfrom: (1.1.13 upstream) (4.4.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110217133407-rwr88elhhq6j7ba0
Tags: 1.2.5-1ubuntu1
* Merge from Debian for security fixes (LP: #719031). Remaining changes:
  - debian/control: don't Build-Depends on locales-all, which doesn't exist
    in natty
* Drop the following patches, now included upstream:
  - debian/patches/07_security_admin_infoleak.diff
  - debian/patches/08_security_pasword_reset_dos.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
class ManualPrimaryKeyTest(models.Model):
20
20
    id = models.IntegerField(primary_key=True)
21
21
    data = models.CharField(max_length=100)
22
 
 
23
 
__test__ = {'API_TESTS':"""
24
 
# Acting as a divine being, create an Person.
25
 
>>> from datetime import date
26
 
>>> p = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
27
 
>>> p.save()
28
 
 
29
 
# Only one Person is in the database at this point.
30
 
>>> Person.objects.count()
31
 
1
32
 
 
33
 
# get_or_create() a person with similar first names.
34
 
>>> p, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)})
35
 
 
36
 
# get_or_create() didn't have to create an object.
37
 
>>> created
38
 
False
39
 
 
40
 
# There's still only one Person in the database.
41
 
>>> Person.objects.count()
42
 
1
43
 
 
44
 
# get_or_create() a Person with a different name.
45
 
>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
46
 
>>> created
47
 
True
48
 
>>> Person.objects.count()
49
 
2
50
 
 
51
 
# If we execute the exact same statement, it won't create a Person.
52
 
>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
53
 
>>> created
54
 
False
55
 
>>> Person.objects.count()
56
 
2
57
 
 
58
 
# If you don't specify a value or default value for all required fields, you
59
 
# will get an error.
60
 
>>> try:
61
 
...     p, created = Person.objects.get_or_create(first_name='Tom', last_name='Smith')
62
 
... except Exception, e:
63
 
...     if isinstance(e, IntegrityError):
64
 
...         print "Pass"
65
 
...     else:
66
 
...         print "Fail with %s" % type(e)
67
 
Pass
68
 
 
69
 
# If you specify an existing primary key, but different other fields, then you
70
 
# will get an error and data will not be updated.
71
 
>>> m = ManualPrimaryKeyTest(id=1, data='Original')
72
 
>>> m.save()
73
 
>>> try:
74
 
...    m, created = ManualPrimaryKeyTest.objects.get_or_create(id=1, data='Different')
75
 
... except Exception, e:
76
 
...    if isinstance(e, IntegrityError):
77
 
...        print "Pass"
78
 
...    else:
79
 
...        print "Fail with %s" % type(e)
80
 
Pass
81
 
>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
82
 
True
83
 
"""}