2018-01-14 21:00:16 +00:00
|
|
|
from unittest import skipUnless
|
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
from django.db.models import Value
|
|
|
|
from django.db.models.functions import NullIf
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from ..models import Author
|
|
|
|
|
|
|
|
|
|
|
|
class NullIfTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
Author.objects.create(name="Rhonda", alias="Rhonda")
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
authors = Author.objects.annotate(nullif=NullIf("alias", "name")).values_list(
|
|
|
|
"nullif"
|
|
|
|
)
|
2022-04-14 10:12:13 +00:00
|
|
|
self.assertCountEqual(
|
2018-01-14 21:00:16 +00:00
|
|
|
authors,
|
|
|
|
[
|
|
|
|
("smithj",),
|
|
|
|
(
|
2024-01-26 11:45:07 +00:00
|
|
|
(
|
2022-02-03 19:24:19 +00:00
|
|
|
""
|
2018-01-14 21:00:16 +00:00
|
|
|
if connection.features.interprets_empty_strings_as_nulls
|
|
|
|
else None
|
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2018-01-14 21:00:16 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_null_argument(self):
|
|
|
|
authors = Author.objects.annotate(
|
|
|
|
nullif=NullIf("name", Value(None))
|
|
|
|
).values_list("nullif")
|
2022-04-14 10:12:13 +00:00
|
|
|
self.assertCountEqual(authors, [("John Smith",), ("Rhonda",)])
|
2018-01-14 21:00:16 +00:00
|
|
|
|
|
|
|
def test_too_few_args(self):
|
|
|
|
msg = "'NullIf' takes exactly 2 arguments (1 given)"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
NullIf("name")
|
|
|
|
|
|
|
|
@skipUnless(connection.vendor == "oracle", "Oracle specific test for NULL-literal")
|
|
|
|
def test_null_literal(self):
|
|
|
|
msg = "Oracle does not allow Value(None) for expression1."
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
list(
|
|
|
|
Author.objects.annotate(nullif=NullIf(Value(None), "name")).values_list(
|
|
|
|
"nullif"
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|