|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +from django.test import TestCase |
| 6 | + |
| 7 | +from .models import Article, Person |
| 8 | + |
| 9 | + |
| 10 | +class EarliestOrLatestTests(TestCase): |
| 11 | + """Tests for the earliest() and latest() objects methods""" |
| 12 | + |
| 13 | + def tearDown(self): |
| 14 | + """Makes sure Article has a get_latest_by""" |
| 15 | + if not Article._meta.get_latest_by: |
| 16 | + Article._meta.get_latest_by = 'pub_date' |
| 17 | + |
| 18 | + def test_earliest(self): |
| 19 | + # Because no Articles exist yet, earliest() raises ArticleDoesNotExist. |
| 20 | + self.assertRaises(Article.DoesNotExist, Article.objects.earliest) |
| 21 | + |
| 22 | + a1 = Article.objects.create( |
| 23 | + headline="Article 1", pub_date=datetime(2005, 7, 26), |
| 24 | + expire_date=datetime(2005, 9, 1) |
| 25 | + ) |
| 26 | + a2 = Article.objects.create( |
| 27 | + headline="Article 2", pub_date=datetime(2005, 7, 27), |
| 28 | + expire_date=datetime(2005, 7, 28) |
| 29 | + ) |
| 30 | + a3 = Article.objects.create( |
| 31 | + headline="Article 3", pub_date=datetime(2005, 7, 28), |
| 32 | + expire_date=datetime(2005, 8, 27) |
| 33 | + ) |
| 34 | + a4 = Article.objects.create( |
| 35 | + headline="Article 4", pub_date=datetime(2005, 7, 28), |
| 36 | + expire_date=datetime(2005, 7, 30) |
| 37 | + ) |
| 38 | + |
| 39 | + # Get the earliest Article. |
| 40 | + self.assertEqual(Article.objects.earliest(), a1) |
| 41 | + # Get the earliest Article that matches certain filters. |
| 42 | + self.assertEqual( |
| 43 | + Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).earliest(), |
| 44 | + a2 |
| 45 | + ) |
| 46 | + |
| 47 | + # Pass a custom field name to earliest() to change the field that's used |
| 48 | + # to determine the earliest object. |
| 49 | + self.assertEqual(Article.objects.earliest('expire_date'), a2) |
| 50 | + self.assertEqual(Article.objects.filter( |
| 51 | + pub_date__gt=datetime(2005, 7, 26)).earliest('expire_date'), a2) |
| 52 | + |
| 53 | + # Ensure that earliest() overrides any other ordering specified on the |
| 54 | + # query. Refs #11283. |
| 55 | + self.assertEqual(Article.objects.order_by('id').earliest(), a1) |
| 56 | + |
| 57 | + # Ensure that error is raised if the user forgot to add a get_latest_by |
| 58 | + # in the Model.Meta |
| 59 | + Article.objects.model._meta.get_latest_by = None |
| 60 | + self.assertRaisesMessage( |
| 61 | + AssertionError, |
| 62 | + "earliest() and latest() require either a field_name parameter or " |
| 63 | + "'get_latest_by' in the model", |
| 64 | + lambda: Article.objects.earliest(), |
| 65 | + ) |
| 66 | + |
| 67 | + def test_latest(self): |
| 68 | + # Because no Articles exist yet, latest() raises ArticleDoesNotExist. |
| 69 | + self.assertRaises(Article.DoesNotExist, Article.objects.latest) |
| 70 | + |
| 71 | + a1 = Article.objects.create( |
| 72 | + headline="Article 1", pub_date=datetime(2005, 7, 26), |
| 73 | + expire_date=datetime(2005, 9, 1) |
| 74 | + ) |
| 75 | + a2 = Article.objects.create( |
| 76 | + headline="Article 2", pub_date=datetime(2005, 7, 27), |
| 77 | + expire_date=datetime(2005, 7, 28) |
| 78 | + ) |
| 79 | + a3 = Article.objects.create( |
| 80 | + headline="Article 3", pub_date=datetime(2005, 7, 27), |
| 81 | + expire_date=datetime(2005, 8, 27) |
| 82 | + ) |
| 83 | + a4 = Article.objects.create( |
| 84 | + headline="Article 4", pub_date=datetime(2005, 7, 28), |
| 85 | + expire_date=datetime(2005, 7, 30) |
| 86 | + ) |
| 87 | + |
| 88 | + # Get the latest Article. |
| 89 | + self.assertEqual(Article.objects.latest(), a4) |
| 90 | + # Get the latest Article that matches certain filters. |
| 91 | + self.assertEqual( |
| 92 | + Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest(), |
| 93 | + a1 |
| 94 | + ) |
| 95 | + |
| 96 | + # Pass a custom field name to latest() to change the field that's used |
| 97 | + # to determine the latest object. |
| 98 | + self.assertEqual(Article.objects.latest('expire_date'), a1) |
| 99 | + self.assertEqual( |
| 100 | + Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest('expire_date'), |
| 101 | + a3, |
| 102 | + ) |
| 103 | + |
| 104 | + # Ensure that latest() overrides any other ordering specified on the query. Refs #11283. |
| 105 | + self.assertEqual(Article.objects.order_by('id').latest(), a4) |
| 106 | + |
| 107 | + # Ensure that error is raised if the user forgot to add a get_latest_by |
| 108 | + # in the Model.Meta |
| 109 | + Article.objects.model._meta.get_latest_by = None |
| 110 | + self.assertRaisesMessage( |
| 111 | + AssertionError, |
| 112 | + "earliest() and latest() require either a field_name parameter or " |
| 113 | + "'get_latest_by' in the model", |
| 114 | + lambda: Article.objects.latest(), |
| 115 | + ) |
| 116 | + |
| 117 | + def test_latest_manual(self): |
| 118 | + # You can still use latest() with a model that doesn't have |
| 119 | + # "get_latest_by" set -- just pass in the field name manually. |
| 120 | + p1 = Person.objects.create(name="Ralph", birthday=datetime(1950, 1, 1)) |
| 121 | + p2 = Person.objects.create(name="Stephanie", birthday=datetime(1960, 2, 3)) |
| 122 | + self.assertRaises(AssertionError, Person.objects.latest) |
| 123 | + self.assertEqual(Person.objects.latest("birthday"), p2) |
0 commit comments