diff options
author | Nick Pope <nick.pope@flightdataservices.com> | 2018-08-16 00:45:11 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2018-08-16 15:44:31 -0400 |
commit | 328898582267d963d79eb871300a7f4d5f5e5959 (patch) | |
tree | 86cbe0844f59a5f7887217f28c1a37bcedc86fef /tests/db_functions/text/test_right.py | |
parent | cd790ed1a6dbdf910c41da44c306ddae6ea6fd4d (diff) | |
download | django-328898582267d963d79eb871300a7f4d5f5e5959.tar.gz |
Reorganized text db function tests.
Diffstat (limited to 'tests/db_functions/text/test_right.py')
-rw-r--r-- | tests/db_functions/text/test_right.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tests/db_functions/text/test_right.py b/tests/db_functions/text/test_right.py new file mode 100644 index 0000000000..6dcbcc18f5 --- /dev/null +++ b/tests/db_functions/text/test_right.py @@ -0,0 +1,27 @@ +from django.db.models import CharField, Value +from django.db.models.functions import Lower, Right +from django.test import TestCase + +from ..models import Author + + +class RightTests(TestCase): + @classmethod + def setUpTestData(cls): + Author.objects.create(name='John Smith', alias='smithj') + Author.objects.create(name='Rhonda') + + def test_basic(self): + authors = Author.objects.annotate(name_part=Right('name', 5)) + self.assertQuerysetEqual(authors.order_by('name'), ['Smith', 'honda'], lambda a: a.name_part) + # If alias is null, set it to the first 2 lower characters of the name. + Author.objects.filter(alias__isnull=True).update(alias=Lower(Right('name', 2))) + self.assertQuerysetEqual(authors.order_by('name'), ['smithj', 'da'], lambda a: a.alias) + + def test_invalid_length(self): + with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"): + Author.objects.annotate(raises=Right('name', 0)) + + def test_expressions(self): + authors = Author.objects.annotate(name_part=Right('name', Value(3), output_field=CharField())) + self.assertQuerysetEqual(authors.order_by('name'), ['ith', 'nda'], lambda a: a.name_part) |