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] Migrated the custom_columns doctests to unit tests. Thanks to…

… Alex Gaynor.

Backport of r13765 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14071 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a322ba66a175abe4c34bfc6d7cdecb8d4e801ed3 1 parent 824ca0d
Russell Keith-Magee authored October 09, 2010
65  tests/modeltests/custom_columns/models.py
@@ -38,68 +38,3 @@ def __unicode__(self):
38 38
     class Meta:
39 39
         ordering = ('headline',)
40 40
 
41  
-__test__ = {'API_TESTS':"""
42  
-# Create a Author.
43  
->>> a = Author(first_name='John', last_name='Smith')
44  
->>> a.save()
45  
-
46  
->>> a.id
47  
-1
48  
-
49  
-# Create another author
50  
->>> a2 = Author(first_name='Peter', last_name='Jones')
51  
->>> a2.save()
52  
-
53  
-# Create an article
54  
->>> art = Article(headline='Django lets you build web apps easily')
55  
->>> art.save()
56  
->>> art.authors = [a, a2]
57  
-
58  
-# Although the table and column names on Author have been set to custom values,
59  
-# nothing about using the Author model has changed...
60  
-
61  
-# Query the available authors
62  
->>> Author.objects.all()
63  
-[<Author: Peter Jones>, <Author: John Smith>]
64  
-
65  
->>> Author.objects.filter(first_name__exact='John')
66  
-[<Author: John Smith>]
67  
-
68  
->>> Author.objects.get(first_name__exact='John')
69  
-<Author: John Smith>
70  
-
71  
->>> Author.objects.filter(firstname__exact='John')
72  
-Traceback (most recent call last):
73  
-    ...
74  
-FieldError: Cannot resolve keyword 'firstname' into field. Choices are: article, first_name, id, last_name
75  
-
76  
->>> a = Author.objects.get(last_name__exact='Smith')
77  
->>> a.first_name
78  
-u'John'
79  
->>> a.last_name
80  
-u'Smith'
81  
->>> a.firstname
82  
-Traceback (most recent call last):
83  
-    ...
84  
-AttributeError: 'Author' object has no attribute 'firstname'
85  
->>> a.last
86  
-Traceback (most recent call last):
87  
-    ...
88  
-AttributeError: 'Author' object has no attribute 'last'
89  
-
90  
-# Although the Article table uses a custom m2m table,
91  
-# nothing about using the m2m relationship has changed...
92  
-
93  
-# Get all the authors for an article
94  
->>> art.authors.all()
95  
-[<Author: Peter Jones>, <Author: John Smith>]
96  
-
97  
-# Get the articles for an author
98  
->>> a.article_set.all()
99  
-[<Article: Django lets you build web apps easily>]
100  
-
101  
-# Query the authors across the m2m relation
102  
->>> art.authors.filter(last_name='Jones')
103  
-[<Author: Peter Jones>]
104  
-
105  
-"""}
71  tests/modeltests/custom_columns/tests.py
... ...
@@ -0,0 +1,71 @@
  1
+from django.core.exceptions import FieldError
  2
+from django.test import TestCase
  3
+
  4
+from models import Author, Article
  5
+
  6
+
  7
+class CustomColumnsTests(TestCase):
  8
+    def test_db_column(self):
  9
+        a1 = Author.objects.create(first_name="John", last_name="Smith")
  10
+        a2 = Author.objects.create(first_name="Peter", last_name="Jones")
  11
+
  12
+        art = Article.objects.create(headline="Django lets you build web apps easily")
  13
+        art.authors = [a1, a2]
  14
+
  15
+        # Although the table and column names on Author have been set to custom
  16
+        # values, nothing about using the Author model has changed...
  17
+
  18
+        # Query the available authors
  19
+        self.assertQuerysetEqual(
  20
+            Author.objects.all(), [
  21
+                "Peter Jones", "John Smith",
  22
+            ],
  23
+            unicode
  24
+        )
  25
+        self.assertQuerysetEqual(
  26
+            Author.objects.filter(first_name__exact="John"), [
  27
+                "John Smith",
  28
+            ],
  29
+            unicode
  30
+        )
  31
+        self.assertEqual(
  32
+            Author.objects.get(first_name__exact="John"),
  33
+            a1,
  34
+        )
  35
+
  36
+        self.assertRaises(FieldError,
  37
+            lambda: Author.objects.filter(firstname__exact="John")
  38
+        )
  39
+
  40
+        a = Author.objects.get(last_name__exact="Smith")
  41
+        a.first_name = "John"
  42
+        a.last_name = "Smith"
  43
+
  44
+        self.assertRaises(AttributeError, lambda: a.firstname)
  45
+        self.assertRaises(AttributeError, lambda: a.last)
  46
+
  47
+        # Although the Article table uses a custom m2m table,
  48
+        # nothing about using the m2m relationship has changed...
  49
+
  50
+        # Get all the authors for an article
  51
+        self.assertQuerysetEqual(
  52
+            art.authors.all(), [
  53
+                "Peter Jones",
  54
+                "John Smith",
  55
+            ],
  56
+            unicode
  57
+        )
  58
+        # Get the articles for an author
  59
+        self.assertQuerysetEqual(
  60
+            a.article_set.all(), [
  61
+                "Django lets you build web apps easily",
  62
+            ],
  63
+            lambda a: a.headline
  64
+        )
  65
+        # Query the authors across the m2m relation
  66
+        self.assertQuerysetEqual(
  67
+            art.authors.filter(last_name='Jones'), [
  68
+                "Peter Jones"
  69
+            ],
  70
+            unicode
  71
+        )

0 notes on commit a322ba6

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