| 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, Greatest |
| 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 GreatestTests(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( |
| 22 |
last_updated=Greatest("written", "published") |
| 23 |
) |
| 24 |
self.assertEqual(articles.first().last_updated, now) |
| 25 |
|
| 26 |
@skipUnlessDBFeature("greatest_least_ignores_nulls") |
| 27 |
def test_ignores_null(self): |
| 28 |
now = timezone.now() |
| 29 |
Article.objects.create(title="Testing with Django", written=now) |
| 30 |
articles = Article.objects.annotate( |
| 31 |
last_updated=Greatest("written", "published") |
| 32 |
) |
| 33 |
self.assertEqual(articles.first().last_updated, now) |
| 34 |
|
| 35 |
@skipIfDBFeature("greatest_least_ignores_nulls") |
| 36 |
def test_propagates_null(self): |
| 37 |
Article.objects.create(title="Testing with Django", written=timezone.now()) |
| 38 |
articles = Article.objects.annotate( |
| 39 |
last_updated=Greatest("written", "published") |
| 40 |
) |
| 41 |
self.assertIsNone(articles.first().last_updated) |
| 42 |
|
| 43 |
def test_coalesce_workaround(self): |
| 44 |
past = datetime(1900, 1, 1) |
| 45 |
now = timezone.now() |
| 46 |
Article.objects.create(title="Testing with Django", written=now) |
| 47 |
articles = Article.objects.annotate( |
| 48 |
last_updated=Greatest( |
| 49 |
Coalesce("written", past), |
| 50 |
Coalesce("published", past), |
| 51 |
), |
| 52 |
) |
| 53 |
self.assertEqual(articles.first().last_updated, now) |
| 54 |
|
| 55 |
@skipUnless(connection.vendor == "mysql", "MySQL-specific workaround") |
| 56 |
def test_coalesce_workaround_mysql(self): |
| 57 |
past = datetime(1900, 1, 1) |
| 58 |
now = timezone.now() |
| 59 |
Article.objects.create(title="Testing with Django", written=now) |
| 60 |
past_sql = RawSQL("cast(%s as datetime)", (past,)) |
| 61 |
articles = Article.objects.annotate( |
| 62 |
last_updated=Greatest( |
| 63 |
Coalesce("written", past_sql), |
| 64 |
Coalesce("published", past_sql), |
| 65 |
), |
| 66 |
) |
| 67 |
self.assertEqual(articles.first().last_updated, now) |
| 68 |
|
| 69 |
def test_all_null(self): |
| 70 |
Article.objects.create(title="Testing with Django", written=timezone.now()) |
| 71 |
articles = Article.objects.annotate( |
| 72 |
last_updated=Greatest("published", "updated") |
| 73 |
) |
| 74 |
self.assertIsNone(articles.first().last_updated) |
| 75 |
|
| 76 |
def test_one_expressions(self): |
| 77 |
with self.assertRaisesMessage( |
| 78 |
ValueError, "Greatest must take at least two expressions" |
| 79 |
): |
| 80 |
Greatest("written") |
| 81 |
|
| 82 |
def test_related_field(self): |
| 83 |
author = Author.objects.create(name="John Smith", age=45) |
| 84 |
Fan.objects.create(name="Margaret", age=50, author=author) |
| 85 |
authors = Author.objects.annotate(highest_age=Greatest("age", "fans__age")) |
| 86 |
self.assertEqual(authors.first().highest_age, 50) |
| 87 |
|
| 88 |
def test_update(self): |
| 89 |
author = Author.objects.create(name="James Smith", goes_by="Jim") |
| 90 |
Author.objects.update(alias=Greatest("name", "goes_by")) |
| 91 |
author.refresh_from_db() |
| 92 |
self.assertEqual(author.alias, "Jim") |
| 93 |
|
| 94 |
def test_decimal_filter(self): |
| 95 |
obj = DecimalModel.objects.create(n1=Decimal("1.1"), n2=Decimal("1.2")) |
| 96 |
self.assertCountEqual( |
| 97 |
DecimalModel.objects.annotate( |
| 98 |
greatest=Greatest("n1", "n2"), |
| 99 |
).filter(greatest=Decimal("1.2")), |
| 100 |
[obj], |
| 101 |
) |