Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted save_delete_hooks tests from doctests to unittests. We have…

… always been at war with doctests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14145 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3879c5907454e14dd0128ef33c927580c0fe383c 1 parent e01bce1
Alex Gaynor authored October 11, 2010
37  tests/modeltests/save_delete_hooks/models.py
@@ -7,37 +7,26 @@
7 7
 
8 8
 from django.db import models
9 9
 
  10
+
10 11
 class Person(models.Model):
11 12
     first_name = models.CharField(max_length=20)
12 13
     last_name = models.CharField(max_length=20)
13 14
 
  15
+    def __init__(self, *args, **kwargs):
  16
+        super(Person, self).__init__(*args, **kwargs)
  17
+        self.data = []
  18
+
14 19
     def __unicode__(self):
15 20
         return u"%s %s" % (self.first_name, self.last_name)
16 21
 
17  
-    def save(self, force_insert=False, force_update=False):
18  
-        print "Before save"
  22
+    def save(self, *args, **kwargs):
  23
+        self.data.append("Before save")
19 24
          # Call the "real" save() method
20  
-        super(Person, self).save(force_insert, force_update)
21  
-        print "After save"
  25
+        super(Person, self).save(*args, **kwargs)
  26
+        self.data.append("After save")
22 27
 
23 28
     def delete(self):
24  
-        print "Before deletion"
25  
-        super(Person, self).delete() # Call the "real" delete() method
26  
-        print "After deletion"
27  
-
28  
-__test__ = {'API_TESTS':"""
29  
->>> p1 = Person(first_name='John', last_name='Smith')
30  
->>> p1.save()
31  
-Before save
32  
-After save
33  
-
34  
->>> Person.objects.all()
35  
-[<Person: John Smith>]
36  
-
37  
->>> p1.delete()
38  
-Before deletion
39  
-After deletion
40  
-
41  
->>> Person.objects.all()
42  
-[]
43  
-"""}
  29
+        self.data.append("Before deletion")
  30
+        # Call the "real" delete() method
  31
+        super(Person, self).delete()
  32
+        self.data.append("After deletion")
30  tests/modeltests/save_delete_hooks/tests.py
... ...
@@ -0,0 +1,30 @@
  1
+from django.test import TestCase
  2
+
  3
+from models import Person
  4
+
  5
+
  6
+class SaveDeleteHookTests(TestCase):
  7
+    def test_basic(self):
  8
+        p = Person(first_name="John", last_name="Smith")
  9
+        self.assertEqual(p.data, [])
  10
+        p.save()
  11
+        self.assertEqual(p.data, [
  12
+            "Before save",
  13
+            "After save",
  14
+        ])
  15
+
  16
+        self.assertQuerysetEqual(
  17
+            Person.objects.all(), [
  18
+                "John Smith",
  19
+            ],
  20
+            unicode
  21
+        )
  22
+
  23
+        p.delete()
  24
+        self.assertEqual(p.data, [
  25
+            "Before save",
  26
+            "After save",
  27
+            "Before deletion",
  28
+            "After deletion",
  29
+        ])
  30
+        self.assertQuerysetEqual(Person.objects.all(), [])

0 notes on commit 3879c59

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