Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrate get_or_create doctests. Thanks to Alex Gaynor.

Backport of r13785 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13802 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0023357eb0979fa75aceb131fa6dc03c007b4dbf 1 parent f0835ce
Russell Keith-Magee authored September 12, 2010
62  tests/modeltests/get_or_create/models.py
@@ -19,65 +19,3 @@ def __unicode__(self):
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  
-"""}
52  tests/modeltests/get_or_create/tests.py
... ...
@@ -0,0 +1,52 @@
  1
+from datetime import date
  2
+
  3
+from django.db import IntegrityError
  4
+from django.test import TestCase
  5
+
  6
+from models import Person, ManualPrimaryKeyTest
  7
+
  8
+
  9
+class GetOrCreateTests(TestCase):
  10
+    def test_get_or_create(self):
  11
+        p = Person.objects.create(
  12
+            first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)
  13
+        )
  14
+
  15
+        p, created = Person.objects.get_or_create(
  16
+            first_name="John", last_name="Lennon", defaults={
  17
+                "birthday": date(1940, 10, 9)
  18
+            }
  19
+        )
  20
+        self.assertFalse(created)
  21
+        self.assertEqual(Person.objects.count(), 1)
  22
+
  23
+        p, created = Person.objects.get_or_create(
  24
+            first_name='George', last_name='Harrison', defaults={
  25
+                'birthday': date(1943, 2, 25)
  26
+            }
  27
+        )
  28
+        self.assertTrue(created)
  29
+        self.assertEqual(Person.objects.count(), 2)
  30
+
  31
+        # If we execute the exact same statement, it won't create a Person.
  32
+        p, created = Person.objects.get_or_create(
  33
+            first_name='George', last_name='Harrison', defaults={
  34
+                'birthday': date(1943, 2, 25)
  35
+            }
  36
+        )
  37
+        self.assertFalse(created)
  38
+        self.assertEqual(Person.objects.count(), 2)
  39
+
  40
+        # If you don't specify a value or default value for all required
  41
+        # fields, you will get an error.
  42
+        self.assertRaises(IntegrityError,
  43
+            Person.objects.get_or_create, first_name="Tom", last_name="Smith"
  44
+        )
  45
+
  46
+        # If you specify an existing primary key, but different other fields,
  47
+        # then you will get an error and data will not be updated.
  48
+        m = ManualPrimaryKeyTest.objects.create(id=1, data="Original")
  49
+        self.assertRaises(IntegrityError,
  50
+            ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
  51
+        )
  52
+        self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")

0 notes on commit 0023357

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