Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Migrated custom_columns_regress doctests. Thanks to Sebastian Hillig.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13885 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d3b47183a089cf6620da90a6cb7cd1c3025a7e0 1 parent cd9aed5
Russell Keith-Magee authored September 27, 2010
1  tests/regressiontests/backends/models.py
@@ -54,5 +54,4 @@ class Post(models.Model):
54 54
     class Meta:
55 55
         db_table = 'CaseSensitive_Post'
56 56
 
57  
-qn = connection.ops.quote_name
58 57
 
64  tests/regressiontests/custom_columns_regress/models.py
@@ -33,68 +33,4 @@ class Meta:
33 33
         ordering = ('last_name','first_name')
34 34
 
35 35
 
36  
-__test__ = {'API_TESTS':"""
37  
-# Create a Author.
38  
->>> a = Author(first_name='John', last_name='Smith')
39  
->>> a.save()
40 36
 
41  
->>> a.Author_ID
42  
-1
43  
-
44  
-# Create another author
45  
->>> a2 = Author(first_name='Peter', last_name='Jones')
46  
->>> a2.save()
47  
-
48  
-# Create an article
49  
->>> art = Article(headline='Django lets you build web apps easily', primary_author=a)
50  
->>> art.save()
51  
->>> art.authors = [a, a2]
52  
-
53  
-# Although the table and column names on Author have been set to custom values,
54  
-# nothing about using the Author model has changed...
55  
-
56  
-# Query the available authors
57  
->>> Author.objects.all()
58  
-[<Author: Peter Jones>, <Author: John Smith>]
59  
-
60  
->>> Author.objects.filter(first_name__exact='John')
61  
-[<Author: John Smith>]
62  
-
63  
->>> Author.objects.get(first_name__exact='John')
64  
-<Author: John Smith>
65  
-
66  
->>> Author.objects.filter(firstname__exact='John')
67  
-Traceback (most recent call last):
68  
-    ...
69  
-FieldError: Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set
70  
-
71  
->>> a = Author.objects.get(last_name__exact='Smith')
72  
->>> a.first_name
73  
-u'John'
74  
->>> a.last_name
75  
-u'Smith'
76  
->>> a.firstname
77  
-Traceback (most recent call last):
78  
-    ...
79  
-AttributeError: 'Author' object has no attribute 'firstname'
80  
->>> a.last
81  
-Traceback (most recent call last):
82  
-    ...
83  
-AttributeError: 'Author' object has no attribute 'last'
84  
-
85  
-# Although the Article table uses a custom m2m table,
86  
-# nothing about using the m2m relationship has changed...
87  
-
88  
-# Get all the authors for an article
89  
->>> art.authors.all()
90  
-[<Author: Peter Jones>, <Author: John Smith>]
91  
-
92  
-# Get the articles for an author
93  
->>> a.article_set.all()
94  
-[<Article: Django lets you build web apps easily>]
95  
-
96  
-# Query the authors across the m2m relation
97  
->>> art.authors.filter(last_name='Jones')
98  
-[<Author: Peter Jones>]
99  
-
100  
-"""}
86  tests/regressiontests/custom_columns_regress/tests.py
... ...
@@ -0,0 +1,86 @@
  1
+from django.test import TestCase
  2
+from django.core.exceptions import FieldError
  3
+
  4
+from models import Author, Article
  5
+
  6
+def pks(objects):
  7
+    """ Return pks to be able to compare lists"""
  8
+    return [o.pk for o in objects]
  9
+
  10
+class CustomColumnRegression(TestCase):
  11
+
  12
+    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
  13
+        try:
  14
+            func(*args, **kwargs)
  15
+        except Exception, e:
  16
+            self.assertEqual(msg, str(e))
  17
+            self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
  18
+
  19
+    def setUp(self):
  20
+        self.a1 = Author.objects.create(first_name='John', last_name='Smith')
  21
+        self.a2 = Author.objects.create(first_name='Peter', last_name='Jones')
  22
+        self.authors = [self.a1, self.a2]
  23
+
  24
+    def test_basic_creation(self):
  25
+        self.assertEqual(self.a1.Author_ID, 1)
  26
+
  27
+        art = Article(headline='Django lets you build web apps easily', primary_author=self.a1)
  28
+        art.save()
  29
+        art.authors = [self.a1, self.a2]
  30
+
  31
+    def test_author_querying(self):
  32
+        self.assertQuerysetEqual(
  33
+            Author.objects.all().order_by('last_name'),
  34
+            ['<Author: Peter Jones>', '<Author: John Smith>']
  35
+        )
  36
+
  37
+    def test_author_filtering(self):
  38
+        self.assertQuerysetEqual(
  39
+            Author.objects.filter(first_name__exact='John'),
  40
+            ['<Author: John Smith>']
  41
+        )
  42
+
  43
+    def test_author_get(self):
  44
+        self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))
  45
+
  46
+    def test_filter_on_nonexistant_field(self):
  47
+        self.assertRaisesMessage(
  48
+            FieldError,
  49
+            "Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
  50
+            Author.objects.filter,
  51
+            firstname__exact='John'
  52
+        )
  53
+
  54
+    def test_author_get_attributes(self):
  55
+        a = Author.objects.get(last_name__exact='Smith')
  56
+        self.assertEqual('John', a.first_name)
  57
+        self.assertEqual('Smith', a.last_name)
  58
+        self.assertRaisesMessage(
  59
+            AttributeError,
  60
+            "'Author' object has no attribute 'firstname'",
  61
+            getattr,
  62
+            a, 'firstname'
  63
+        )
  64
+
  65
+        self.assertRaisesMessage(
  66
+            AttributeError,
  67
+            "'Author' object has no attribute 'last'",
  68
+            getattr,
  69
+            a, 'last'
  70
+        )
  71
+
  72
+    def test_m2m_table(self):
  73
+        art = Article.objects.create(headline='Django lets you build web apps easily', primary_author=self.a1)
  74
+        art.authors = self.authors
  75
+        self.assertQuerysetEqual(
  76
+            art.authors.all().order_by('last_name'),
  77
+            ['<Author: Peter Jones>', '<Author: John Smith>']
  78
+        )
  79
+        self.assertQuerysetEqual(
  80
+            self.a1.article_set.all(),
  81
+            ['<Article: Django lets you build web apps easily>']
  82
+        )
  83
+        self.assertQuerysetEqual(
  84
+            art.authors.filter(last_name='Jones'),
  85
+            ['<Author: Peter Jones>']
  86
+        )

0 notes on commit 2d3b471

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