Debian Codegraph @

tests/db_functions/comparison/test_least.py

raw
1 from datetime import datetime, timedelta
2 from decimal import Decimal
3 from unittest import skipUnless
4
5 from django.db import connection
6 from django.db.models.expressions import RawSQL
7 from django.db.models.functions import Coalesce, Least
8 from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
9 from django.utils import timezone
10
11 from ..models import Article, Author, DecimalModel, Fan
12
13
14 class LeastTests(TestCase):
15 def test_basic(self):
16 now = timezone.now()
17 before = now - timedelta(hours=1)
18 Article.objects.create(
19 title="Testing with Django", written=before, published=now
20 )
21 articles = Article.objects.annotate(first_updated=Least("written", "published"))
22 self.assertEqual(articles.first().first_updated, before)
23
24 @skipUnlessDBFeature("greatest_least_ignores_nulls")
25 def test_ignores_null(self):
26 now = timezone.now()
27 Article.objects.create(title="Testing with Django", written=now)
28 articles = Article.objects.annotate(
29 first_updated=Least("written", "published"),
30 )
31 self.assertEqual(articles.first().first_updated, now)
32
33 @skipIfDBFeature("greatest_least_ignores_nulls")
34 def test_propagates_null(self):
35 Article.objects.create(title="Testing with Django", written=timezone.now())
36 articles = Article.objects.annotate(first_updated=Least("written", "published"))
37 self.assertIsNone(articles.first().first_updated)
38
39 def test_coalesce_workaround(self):
40 future = datetime(2100, 1, 1)
41 now = timezone.now()
42 Article.objects.create(title="Testing with Django", written=now)
43 articles = Article.objects.annotate(
44 last_updated=Least(
45 Coalesce("written", future),
46 Coalesce("published", future),
47 ),
48 )
49 self.assertEqual(articles.first().last_updated, now)
50
51 @skipUnless(connection.vendor == "mysql", "MySQL-specific workaround")
52 def test_coalesce_workaround_mysql(self):
53 future = datetime(2100, 1, 1)
54 now = timezone.now()
55 Article.objects.create(title="Testing with Django", written=now)
56 future_sql = RawSQL("cast(%s as datetime)", (future,))
57 articles = Article.objects.annotate(
58 last_updated=Least(
59 Coalesce("written", future_sql),
60 Coalesce("published", future_sql),
61 ),
62 )
63 self.assertEqual(articles.first().last_updated, now)
64
65 def test_all_null(self):
66 Article.objects.create(title="Testing with Django", written=timezone.now())
67 articles = Article.objects.annotate(first_updated=Least("published", "updated"))
68 self.assertIsNone(articles.first().first_updated)
69
70 def test_one_expressions(self):
71 with self.assertRaisesMessage(
72 ValueError, "Least must take at least two expressions"
73 ):
74 Least("written")
75
76 def test_related_field(self):
77 author = Author.objects.create(name="John Smith", age=45)
78 Fan.objects.create(name="Margaret", age=50, author=author)
79 authors = Author.objects.annotate(lowest_age=Least("age", "fans__age"))
80 self.assertEqual(authors.first().lowest_age, 45)
81
82 def test_update(self):
83 author = Author.objects.create(name="James Smith", goes_by="Jim")
84 Author.objects.update(alias=Least("name", "goes_by"))
85 author.refresh_from_db()
86 self.assertEqual(author.alias, "James Smith")
87
88 def test_decimal_filter(self):
89 obj = DecimalModel.objects.create(n1=Decimal("1.1"), n2=Decimal("1.2"))
90 self.assertCountEqual(
91 DecimalModel.objects.annotate(
92 least=Least("n1", "n2"),
93 ).filter(least=Decimal("1.1")),
94 [obj],
95 )