summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2018-03-19 17:35:16 +0100
committerGitHub <noreply@github.com>2018-03-19 17:35:16 +0100
commitcede5111bbeea1f02a7d35941a4264c7ff95df0a (patch)
tree959f0785f3102ba84db7d4fcd96c17ae1baab479 /tests
parent8d67c7cffdcd5fd0c5cb0b87cd699a05b461e58d (diff)
Refs #28643 -- Added LPad and RPad database functions.
Thanks Tim Graham for the review.
Diffstat (limited to 'tests')
-rw-r--r--tests/db_functions/test_pad.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/db_functions/test_pad.py b/tests/db_functions/test_pad.py
new file mode 100644
index 0000000000..d873345fc4
--- /dev/null
+++ b/tests/db_functions/test_pad.py
@@ -0,0 +1,34 @@
+from django.db.models import Value
+from django.db.models.functions import LPad, RPad
+from django.test import TestCase
+
+from .models import Author
+
+
+class PadTests(TestCase):
+ def test_pad(self):
+ Author.objects.create(name='John', alias='j')
+ tests = (
+ (LPad('name', 7, Value('xy')), 'xyxJohn'),
+ (RPad('name', 7, Value('xy')), 'Johnxyx'),
+ (LPad('name', 6, Value('x')), 'xxJohn'),
+ (RPad('name', 6, Value('x')), 'Johnxx'),
+ # The default pad string is a space.
+ (LPad('name', 6), ' John'),
+ (RPad('name', 6), 'John '),
+ # If string is longer than length it is truncated.
+ (LPad('name', 2), 'Jo'),
+ (RPad('name', 2), 'Jo'),
+ (LPad('name', 0), ''),
+ (RPad('name', 0), ''),
+ )
+ for function, padded_name in tests:
+ with self.subTest(function=function):
+ authors = Author.objects.annotate(padded_name=function)
+ self.assertQuerysetEqual(authors, [padded_name], lambda a: a.padded_name, ordered=False)
+
+ def test_pad_negative_length(self):
+ for function in (LPad, RPad):
+ with self.subTest(function=function):
+ with self.assertRaisesMessage(ValueError, "'length' must be greater or equal to 0."):
+ function('name', -1)