Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Migrate get_latest doctests. Thanks to Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13783 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 14a5e62f86d26991476e8dfb3541797d68369e5f 1 parent f1f86e0
Russell Keith-Magee authored September 12, 2010
49  tests/modeltests/get_latest/models.py
@@ -28,52 +28,3 @@ class Person(models.Model):
28 28
 
29 29
     def __unicode__(self):
30 30
         return self.name
31  
-
32  
-__test__ = {'API_TESTS':"""
33  
-# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
34  
->>> Article.objects.latest()
35  
-Traceback (most recent call last):
36  
-    ...
37  
-DoesNotExist: Article matching query does not exist.
38  
-
39  
-# Create a couple of Articles.
40  
->>> from datetime import datetime
41  
->>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1))
42  
->>> a1.save()
43  
->>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28))
44  
->>> a2.save()
45  
->>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27))
46  
->>> a3.save()
47  
->>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30))
48  
->>> a4.save()
49  
-
50  
-# Get the latest Article.
51  
->>> Article.objects.latest()
52  
-<Article: Article 4>
53  
-
54  
-# Get the latest Article that matches certain filters.
55  
->>> Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest()
56  
-<Article: Article 1>
57  
-
58  
-# Pass a custom field name to latest() to change the field that's used to
59  
-# determine the latest object.
60  
->>> Article.objects.latest('expire_date')
61  
-<Article: Article 1>
62  
-
63  
->>> Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date')
64  
-<Article: Article 3>
65  
-
66  
-# You can still use latest() with a model that doesn't have "get_latest_by"
67  
-# set -- just pass in the field name manually.
68  
->>> p1 = Person(name='Ralph', birthday=datetime(1950, 1, 1))
69  
->>> p1.save()
70  
->>> p2 = Person(name='Stephanie', birthday=datetime(1960, 2, 3))
71  
->>> p2.save()
72  
->>> Person.objects.latest()
73  
-Traceback (most recent call last):
74  
-    ...
75  
-AssertionError: latest() requires either a field_name parameter or 'get_latest_by' in the model
76  
-
77  
->>> Person.objects.latest('birthday')
78  
-<Person: Stephanie>
79  
-"""}
53  tests/modeltests/get_latest/tests.py
... ...
@@ -0,0 +1,53 @@
  1
+from datetime import datetime
  2
+
  3
+from django.test import TestCase
  4
+
  5
+from models import Article, Person
  6
+
  7
+
  8
+class LatestTests(TestCase):
  9
+    def test_latest(self):
  10
+        # Because no Articles exist yet, latest() raises ArticleDoesNotExist.
  11
+        self.assertRaises(Article.DoesNotExist, Article.objects.latest)
  12
+
  13
+        a1 = Article.objects.create(
  14
+            headline="Article 1", pub_date=datetime(2005, 7, 26),
  15
+            expire_date=datetime(2005, 9, 1)
  16
+        )
  17
+        a2 = Article.objects.create(
  18
+            headline="Article 2", pub_date=datetime(2005, 7, 27),
  19
+            expire_date=datetime(2005, 7, 28)
  20
+        )
  21
+        a3 = Article.objects.create(
  22
+            headline="Article 3", pub_date=datetime(2005, 7, 27),
  23
+            expire_date=datetime(2005, 8, 27)
  24
+        )
  25
+        a4 = Article.objects.create(
  26
+            headline="Article 4", pub_date=datetime(2005, 7, 28),
  27
+            expire_date=datetime(2005, 7, 30)
  28
+        )
  29
+
  30
+        # Get the latest Article.
  31
+        self.assertEqual(Article.objects.latest(), a4)
  32
+        # Get the latest Article that matches certain filters.
  33
+        self.assertEqual(
  34
+            Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest(),
  35
+            a1
  36
+        )
  37
+
  38
+        # Pass a custom field name to latest() to change the field that's used
  39
+        # to determine the latest object.
  40
+        self.assertEqual(Article.objects.latest('expire_date'), a1)
  41
+        self.assertEqual(
  42
+            Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date'),
  43
+            a3,
  44
+        )
  45
+
  46
+    def test_latest_manual(self):
  47
+        # You can still use latest() with a model that doesn't have
  48
+        # "get_latest_by" set -- just pass in the field name manually.
  49
+        p1 = Person.objects.create(name="Ralph", birthday=datetime(1950, 1, 1))
  50
+        p2 = Person.objects.create(name="Stephanie", birthday=datetime(1960, 2, 3))
  51
+        self.assertRaises(AssertionError, Person.objects.latest)
  52
+
  53
+        self.assertEqual(Person.objects.latest("birthday"), p2)

0 notes on commit 14a5e62

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