summaryrefslogtreecommitdiff
path: root/tests/db_functions
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2018-01-14 22:00:16 +0100
committerTim Graham <timograham@gmail.com>2019-01-10 12:10:12 -0500
commit4b9d72210f0201efeafb9703e166aa3b38c04873 (patch)
tree05f193fcbd373c0c6bc22efbbb12b0aa894f8990 /tests/db_functions
parent217f4456d823cdc07b4d8e2721165739e666e1e7 (diff)
Refs #28643 -- Added NullIf database function.
Thanks Nick Pope, Mariusz Felisiak, and Tim Graham for reviews.
Diffstat (limited to 'tests/db_functions')
-rw-r--r--tests/db_functions/comparison/test_nullif.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/db_functions/comparison/test_nullif.py b/tests/db_functions/comparison/test_nullif.py
new file mode 100644
index 0000000000..36f881ed57
--- /dev/null
+++ b/tests/db_functions/comparison/test_nullif.py
@@ -0,0 +1,40 @@
+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')
+ self.assertSequenceEqual(
+ authors, [
+ ('smithj',),
+ ('' if connection.features.interprets_empty_strings_as_nulls else None,)
+ ]
+ )
+
+ def test_null_argument(self):
+ authors = Author.objects.annotate(nullif=NullIf('name', Value(None))).values_list('nullif')
+ self.assertSequenceEqual(authors, [('John Smith',), ('Rhonda',)])
+
+ 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'))