summaryrefslogtreecommitdiff
path: root/tests/db_functions/text
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /tests/db_functions/text
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/db_functions/text')
-rw-r--r--tests/db_functions/text/test_chr.py33
-rw-r--r--tests/db_functions/text/test_concat.py84
-rw-r--r--tests/db_functions/text/test_left.py26
-rw-r--r--tests/db_functions/text/test_length.py42
-rw-r--r--tests/db_functions/text/test_lower.py36
-rw-r--r--tests/db_functions/text/test_md5.py44
-rw-r--r--tests/db_functions/text/test_ord.py26
-rw-r--r--tests/db_functions/text/test_pad.py54
-rw-r--r--tests/db_functions/text/test_repeat.py28
-rw-r--r--tests/db_functions/text/test_replace.py86
-rw-r--r--tests/db_functions/text/test_reverse.py44
-rw-r--r--tests/db_functions/text/test_right.py26
-rw-r--r--tests/db_functions/text/test_sha1.py45
-rw-r--r--tests/db_functions/text/test_sha224.py53
-rw-r--r--tests/db_functions/text/test_sha256.py45
-rw-r--r--tests/db_functions/text/test_sha384.py45
-rw-r--r--tests/db_functions/text/test_sha512.py59
-rw-r--r--tests/db_functions/text/test_strindex.py74
-rw-r--r--tests/db_functions/text/test_substr.py39
-rw-r--r--tests/db_functions/text/test_trim.py35
-rw-r--r--tests/db_functions/text/test_upper.py40
21 files changed, 558 insertions, 406 deletions
diff --git a/tests/db_functions/text/test_chr.py b/tests/db_functions/text/test_chr.py
index 67a2a6a98d..7a55994096 100644
--- a/tests/db_functions/text/test_chr.py
+++ b/tests/db_functions/text/test_chr.py
@@ -9,22 +9,31 @@ from ..models import Author
class ChrTests(TestCase):
@classmethod
def setUpTestData(cls):
- cls.john = Author.objects.create(name='John Smith', alias='smithj')
- cls.elena = Author.objects.create(name='Élena Jordan', alias='elena')
- cls.rhonda = Author.objects.create(name='Rhonda')
+ cls.john = Author.objects.create(name="John Smith", alias="smithj")
+ cls.elena = Author.objects.create(name="Élena Jordan", alias="elena")
+ cls.rhonda = Author.objects.create(name="Rhonda")
def test_basic(self):
- authors = Author.objects.annotate(first_initial=Left('name', 1))
- self.assertCountEqual(authors.filter(first_initial=Chr(ord('J'))), [self.john])
- self.assertCountEqual(authors.exclude(first_initial=Chr(ord('J'))), [self.elena, self.rhonda])
+ authors = Author.objects.annotate(first_initial=Left("name", 1))
+ self.assertCountEqual(authors.filter(first_initial=Chr(ord("J"))), [self.john])
+ self.assertCountEqual(
+ authors.exclude(first_initial=Chr(ord("J"))), [self.elena, self.rhonda]
+ )
def test_non_ascii(self):
- authors = Author.objects.annotate(first_initial=Left('name', 1))
- self.assertCountEqual(authors.filter(first_initial=Chr(ord('É'))), [self.elena])
- self.assertCountEqual(authors.exclude(first_initial=Chr(ord('É'))), [self.john, self.rhonda])
+ authors = Author.objects.annotate(first_initial=Left("name", 1))
+ self.assertCountEqual(authors.filter(first_initial=Chr(ord("É"))), [self.elena])
+ self.assertCountEqual(
+ authors.exclude(first_initial=Chr(ord("É"))), [self.john, self.rhonda]
+ )
def test_transform(self):
with register_lookup(IntegerField, Chr):
- authors = Author.objects.annotate(name_code_point=Ord('name'))
- self.assertCountEqual(authors.filter(name_code_point__chr=Chr(ord('J'))), [self.john])
- self.assertCountEqual(authors.exclude(name_code_point__chr=Chr(ord('J'))), [self.elena, self.rhonda])
+ authors = Author.objects.annotate(name_code_point=Ord("name"))
+ self.assertCountEqual(
+ authors.filter(name_code_point__chr=Chr(ord("J"))), [self.john]
+ )
+ self.assertCountEqual(
+ authors.exclude(name_code_point__chr=Chr(ord("J"))),
+ [self.elena, self.rhonda],
+ )
diff --git a/tests/db_functions/text/test_concat.py b/tests/db_functions/text/test_concat.py
index 9850b2fd0d..5932a9bed0 100644
--- a/tests/db_functions/text/test_concat.py
+++ b/tests/db_functions/text/test_concat.py
@@ -1,7 +1,8 @@
from unittest import skipUnless
from django.db import connection
-from django.db.models import CharField, TextField, Value as V
+from django.db.models import CharField, TextField
+from django.db.models import Value as V
from django.db.models.functions import Concat, ConcatPair, Upper
from django.test import TestCase
from django.utils import timezone
@@ -14,68 +15,77 @@ lorem_ipsum = """
class ConcatTests(TestCase):
-
def test_basic(self):
- Author.objects.create(name='Jayden')
- Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
- Author.objects.create(name='Margaret', goes_by='Maggie')
- Author.objects.create(name='Rhonda', alias='adnohR')
- authors = Author.objects.annotate(joined=Concat('alias', 'goes_by'))
+ Author.objects.create(name="Jayden")
+ Author.objects.create(name="John Smith", alias="smithj", goes_by="John")
+ Author.objects.create(name="Margaret", goes_by="Maggie")
+ Author.objects.create(name="Rhonda", alias="adnohR")
+ authors = Author.objects.annotate(joined=Concat("alias", "goes_by"))
self.assertQuerysetEqual(
- authors.order_by('name'), [
- '',
- 'smithjJohn',
- 'Maggie',
- 'adnohR',
+ authors.order_by("name"),
+ [
+ "",
+ "smithjJohn",
+ "Maggie",
+ "adnohR",
],
- lambda a: a.joined
+ lambda a: a.joined,
)
def test_gt_two_expressions(self):
- with self.assertRaisesMessage(ValueError, 'Concat must take at least two expressions'):
- Author.objects.annotate(joined=Concat('alias'))
+ with self.assertRaisesMessage(
+ ValueError, "Concat must take at least two expressions"
+ ):
+ Author.objects.annotate(joined=Concat("alias"))
def test_many(self):
- Author.objects.create(name='Jayden')
- Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
- Author.objects.create(name='Margaret', goes_by='Maggie')
- Author.objects.create(name='Rhonda', alias='adnohR')
+ Author.objects.create(name="Jayden")
+ Author.objects.create(name="John Smith", alias="smithj", goes_by="John")
+ Author.objects.create(name="Margaret", goes_by="Maggie")
+ Author.objects.create(name="Rhonda", alias="adnohR")
authors = Author.objects.annotate(
- joined=Concat('name', V(' ('), 'goes_by', V(')'), output_field=CharField()),
+ joined=Concat("name", V(" ("), "goes_by", V(")"), output_field=CharField()),
)
self.assertQuerysetEqual(
- authors.order_by('name'), [
- 'Jayden ()',
- 'John Smith (John)',
- 'Margaret (Maggie)',
- 'Rhonda ()',
+ authors.order_by("name"),
+ [
+ "Jayden ()",
+ "John Smith (John)",
+ "Margaret (Maggie)",
+ "Rhonda ()",
],
- lambda a: a.joined
+ lambda a: a.joined,
)
def test_mixed_char_text(self):
- Article.objects.create(title='The Title', text=lorem_ipsum, written=timezone.now())
+ Article.objects.create(
+ title="The Title", text=lorem_ipsum, written=timezone.now()
+ )
article = Article.objects.annotate(
- title_text=Concat('title', V(' - '), 'text', output_field=TextField()),
- ).get(title='The Title')
- self.assertEqual(article.title + ' - ' + article.text, article.title_text)
+ title_text=Concat("title", V(" - "), "text", output_field=TextField()),
+ ).get(title="The Title")
+ self.assertEqual(article.title + " - " + article.text, article.title_text)
# Wrap the concat in something else to ensure that text is returned
# rather than bytes.
article = Article.objects.annotate(
- title_text=Upper(Concat('title', V(' - '), 'text', output_field=TextField())),
- ).get(title='The Title')
- expected = article.title + ' - ' + article.text
+ title_text=Upper(
+ Concat("title", V(" - "), "text", output_field=TextField())
+ ),
+ ).get(title="The Title")
+ expected = article.title + " - " + article.text
self.assertEqual(expected.upper(), article.title_text)
- @skipUnless(connection.vendor == 'sqlite', "sqlite specific implementation detail.")
+ @skipUnless(connection.vendor == "sqlite", "sqlite specific implementation detail.")
def test_coalesce_idempotent(self):
- pair = ConcatPair(V('a'), V('b'))
+ pair = ConcatPair(V("a"), V("b"))
# Check nodes counts
self.assertEqual(len(list(pair.flatten())), 3)
- self.assertEqual(len(list(pair.coalesce().flatten())), 7) # + 2 Coalesce + 2 Value()
+ self.assertEqual(
+ len(list(pair.coalesce().flatten())), 7
+ ) # + 2 Coalesce + 2 Value()
self.assertEqual(len(list(pair.flatten())), 3)
def test_sql_generation_idempotency(self):
- qs = Article.objects.annotate(description=Concat('title', V(': '), 'summary'))
+ qs = Article.objects.annotate(description=Concat("title", V(": "), "summary"))
# Multiple compilations should not alter the generated query.
self.assertEqual(str(qs.query), str(qs.all().query))
diff --git a/tests/db_functions/text/test_left.py b/tests/db_functions/text/test_left.py
index 873bbf859b..8871f9ac79 100644
--- a/tests/db_functions/text/test_left.py
+++ b/tests/db_functions/text/test_left.py
@@ -8,20 +8,28 @@ from ..models import Author
class LeftTests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
def test_basic(self):
- authors = Author.objects.annotate(name_part=Left('name', 5))
- self.assertQuerysetEqual(authors.order_by('name'), ['John ', 'Rhond'], lambda a: a.name_part)
+ authors = Author.objects.annotate(name_part=Left("name", 5))
+ self.assertQuerysetEqual(
+ authors.order_by("name"), ["John ", "Rhond"], 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(Left('name', 2)))
- self.assertQuerysetEqual(authors.order_by('name'), ['smithj', 'rh'], lambda a: a.alias)
+ Author.objects.filter(alias__isnull=True).update(alias=Lower(Left("name", 2)))
+ self.assertQuerysetEqual(
+ authors.order_by("name"), ["smithj", "rh"], lambda a: a.alias
+ )
def test_invalid_length(self):
with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"):
- Author.objects.annotate(raises=Left('name', 0))
+ Author.objects.annotate(raises=Left("name", 0))
def test_expressions(self):
- authors = Author.objects.annotate(name_part=Left('name', Value(3, output_field=IntegerField())))
- self.assertQuerysetEqual(authors.order_by('name'), ['Joh', 'Rho'], lambda a: a.name_part)
+ authors = Author.objects.annotate(
+ name_part=Left("name", Value(3, output_field=IntegerField()))
+ )
+ self.assertQuerysetEqual(
+ authors.order_by("name"), ["Joh", "Rho"], lambda a: a.name_part
+ )
diff --git a/tests/db_functions/text/test_length.py b/tests/db_functions/text/test_length.py
index 62d1d1c775..c6afead6aa 100644
--- a/tests/db_functions/text/test_length.py
+++ b/tests/db_functions/text/test_length.py
@@ -7,40 +7,40 @@ from ..models import Author
class LengthTests(TestCase):
-
def test_basic(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
authors = Author.objects.annotate(
- name_length=Length('name'),
- alias_length=Length('alias'),
+ name_length=Length("name"),
+ alias_length=Length("alias"),
)
self.assertQuerysetEqual(
- authors.order_by('name'), [(10, 6), (6, None)],
- lambda a: (a.name_length, a.alias_length)
+ authors.order_by("name"),
+ [(10, 6), (6, None)],
+ lambda a: (a.name_length, a.alias_length),
)
- self.assertEqual(authors.filter(alias_length__lte=Length('name')).count(), 1)
+ self.assertEqual(authors.filter(alias_length__lte=Length("name")).count(), 1)
def test_ordering(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='John Smith', alias='smithj1')
- Author.objects.create(name='Rhonda', alias='ronny')
- authors = Author.objects.order_by(Length('name'), Length('alias'))
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="John Smith", alias="smithj1")
+ Author.objects.create(name="Rhonda", alias="ronny")
+ authors = Author.objects.order_by(Length("name"), Length("alias"))
self.assertQuerysetEqual(
- authors, [
- ('Rhonda', 'ronny'),
- ('John Smith', 'smithj'),
- ('John Smith', 'smithj1'),
+ authors,
+ [
+ ("Rhonda", "ronny"),
+ ("John Smith", "smithj"),
+ ("John Smith", "smithj1"),
],
- lambda a: (a.name, a.alias)
+ lambda a: (a.name, a.alias),
)
def test_transform(self):
with register_lookup(CharField, Length):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
authors = Author.objects.filter(name__length__gt=7)
self.assertQuerysetEqual(
- authors.order_by('name'), ['John Smith'],
- lambda a: a.name
+ authors.order_by("name"), ["John Smith"], lambda a: a.name
)
diff --git a/tests/db_functions/text/test_lower.py b/tests/db_functions/text/test_lower.py
index 2f8dd6257d..f4bdc04573 100644
--- a/tests/db_functions/text/test_lower.py
+++ b/tests/db_functions/text/test_lower.py
@@ -7,34 +7,34 @@ from ..models import Author
class LowerTests(TestCase):
-
def test_basic(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- authors = Author.objects.annotate(lower_name=Lower('name'))
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ authors = Author.objects.annotate(lower_name=Lower("name"))
self.assertQuerysetEqual(
- authors.order_by('name'), ['john smith', 'rhonda'],
- lambda a: a.lower_name
+ authors.order_by("name"), ["john smith", "rhonda"], lambda a: a.lower_name
)
- Author.objects.update(name=Lower('name'))
+ Author.objects.update(name=Lower("name"))
self.assertQuerysetEqual(
- authors.order_by('name'), [
- ('john smith', 'john smith'),
- ('rhonda', 'rhonda'),
+ authors.order_by("name"),
+ [
+ ("john smith", "john smith"),
+ ("rhonda", "rhonda"),
],
- lambda a: (a.lower_name, a.name)
+ lambda a: (a.lower_name, a.name),
)
def test_num_args(self):
- with self.assertRaisesMessage(TypeError, "'Lower' takes exactly 1 argument (2 given)"):
- Author.objects.update(name=Lower('name', 'name'))
+ with self.assertRaisesMessage(
+ TypeError, "'Lower' takes exactly 1 argument (2 given)"
+ ):
+ Author.objects.update(name=Lower("name", "name"))
def test_transform(self):
with register_lookup(CharField, Lower):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- authors = Author.objects.filter(name__lower__exact='john smith')
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ authors = Author.objects.filter(name__lower__exact="john smith")
self.assertQuerysetEqual(
- authors.order_by('name'), ['John Smith'],
- lambda a: a.name
+ authors.order_by("name"), ["John Smith"], lambda a: a.name
)
diff --git a/tests/db_functions/text/test_md5.py b/tests/db_functions/text/test_md5.py
index 931f80ba2c..fd0aec58f2 100644
--- a/tests/db_functions/text/test_md5.py
+++ b/tests/db_functions/text/test_md5.py
@@ -10,32 +10,40 @@ from ..models import Author
class MD5Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- md5_alias=MD5('alias'),
- ).values_list('md5_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ md5_alias=MD5("alias"),
+ )
+ .values_list("md5_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- '6117323d2cabbc17d44c2b44587f682c',
- 'ca6d48f6772000141e66591aee49d56c',
- 'bf2c13bc1154e3d2e7df848cbc8be73d',
- 'd41d8cd98f00b204e9800998ecf8427e',
- 'd41d8cd98f00b204e9800998ecf8427e' if connection.features.interprets_empty_strings_as_nulls else None,
+ "6117323d2cabbc17d44c2b44587f682c",
+ "ca6d48f6772000141e66591aee49d56c",
+ "bf2c13bc1154e3d2e7df848cbc8be73d",
+ "d41d8cd98f00b204e9800998ecf8427e",
+ "d41d8cd98f00b204e9800998ecf8427e"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
def test_transform(self):
with register_lookup(CharField, MD5):
authors = Author.objects.filter(
- alias__md5='6117323d2cabbc17d44c2b44587f682c',
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ alias__md5="6117323d2cabbc17d44c2b44587f682c",
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
diff --git a/tests/db_functions/text/test_ord.py b/tests/db_functions/text/test_ord.py
index dd704d3c9a..15ad4173f7 100644
--- a/tests/db_functions/text/test_ord.py
+++ b/tests/db_functions/text/test_ord.py
@@ -9,17 +9,25 @@ from ..models import Author
class OrdTests(TestCase):
@classmethod
def setUpTestData(cls):
- cls.john = Author.objects.create(name='John Smith', alias='smithj')
- cls.elena = Author.objects.create(name='Élena Jordan', alias='elena')
- cls.rhonda = Author.objects.create(name='Rhonda')
+ cls.john = Author.objects.create(name="John Smith", alias="smithj")
+ cls.elena = Author.objects.create(name="Élena Jordan", alias="elena")
+ cls.rhonda = Author.objects.create(name="Rhonda")
def test_basic(self):
- authors = Author.objects.annotate(name_part=Ord('name'))
- self.assertCountEqual(authors.filter(name_part__gt=Ord(Value('John'))), [self.elena, self.rhonda])
- self.assertCountEqual(authors.exclude(name_part__gt=Ord(Value('John'))), [self.john])
+ authors = Author.objects.annotate(name_part=Ord("name"))
+ self.assertCountEqual(
+ authors.filter(name_part__gt=Ord(Value("John"))), [self.elena, self.rhonda]
+ )
+ self.assertCountEqual(
+ authors.exclude(name_part__gt=Ord(Value("John"))), [self.john]
+ )
def test_transform(self):
with register_lookup(CharField, Ord):
- authors = Author.objects.annotate(first_initial=Left('name', 1))
- self.assertCountEqual(authors.filter(first_initial__ord=ord('J')), [self.john])
- self.assertCountEqual(authors.exclude(first_initial__ord=ord('J')), [self.elena, self.rhonda])
+ authors = Author.objects.annotate(first_initial=Left("name", 1))
+ self.assertCountEqual(
+ authors.filter(first_initial__ord=ord("J")), [self.john]
+ )
+ self.assertCountEqual(
+ authors.exclude(first_initial__ord=ord("J")), [self.elena, self.rhonda]
+ )
diff --git a/tests/db_functions/text/test_pad.py b/tests/db_functions/text/test_pad.py
index fbe82c166e..006a27c0e8 100644
--- a/tests/db_functions/text/test_pad.py
+++ b/tests/db_functions/text/test_pad.py
@@ -8,45 +8,51 @@ from ..models import Author
class PadTests(TestCase):
def test_pad(self):
- Author.objects.create(name='John', alias='j')
- none_value = '' if connection.features.interprets_empty_strings_as_nulls else None
+ Author.objects.create(name="John", alias="j")
+ none_value = (
+ "" if connection.features.interprets_empty_strings_as_nulls else None
+ )
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'),
+ (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 '),
+ (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), ''),
- (LPad('name', None), none_value),
- (RPad('name', None), none_value),
+ (LPad("name", 2), "Jo"),
+ (RPad("name", 2), "Jo"),
+ (LPad("name", 0), ""),
+ (RPad("name", 0), ""),
+ (LPad("name", None), none_value),
+ (RPad("name", None), none_value),
(LPad(Value(None), 1), none_value),
(RPad(Value(None), 1), none_value),
- (LPad('goes_by', 1), none_value),
- (RPad('goes_by', 1), none_value),
+ (LPad("goes_by", 1), none_value),
+ (RPad("goes_by", 1), none_value),
)
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)
+ 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)
+ with self.assertRaisesMessage(
+ ValueError, "'length' must be greater or equal to 0."
+ ):
+ function("name", -1)
def test_combined_with_length(self):
- Author.objects.create(name='Rhonda', alias='john_smith')
- Author.objects.create(name='♥♣♠', alias='bytes')
- authors = Author.objects.annotate(filled=LPad('name', Length('alias')))
+ Author.objects.create(name="Rhonda", alias="john_smith")
+ Author.objects.create(name="♥♣♠", alias="bytes")
+ authors = Author.objects.annotate(filled=LPad("name", Length("alias")))
self.assertQuerysetEqual(
- authors.order_by('alias'),
- [' ♥♣♠', ' Rhonda'],
+ authors.order_by("alias"),
+ [" ♥♣♠", " Rhonda"],
lambda a: a.filled,
)
diff --git a/tests/db_functions/text/test_repeat.py b/tests/db_functions/text/test_repeat.py
index 2234c2e2da..c2d21eae50 100644
--- a/tests/db_functions/text/test_repeat.py
+++ b/tests/db_functions/text/test_repeat.py
@@ -8,22 +8,28 @@ from ..models import Author
class RepeatTests(TestCase):
def test_basic(self):
- Author.objects.create(name='John', alias='xyz')
- none_value = '' if connection.features.interprets_empty_strings_as_nulls else None
+ Author.objects.create(name="John", alias="xyz")
+ none_value = (
+ "" if connection.features.interprets_empty_strings_as_nulls else None
+ )
tests = (
- (Repeat('name', 0), ''),
- (Repeat('name', 2), 'JohnJohn'),
- (Repeat('name', Length('alias')), 'JohnJohnJohn'),
- (Repeat(Value('x'), 3), 'xxx'),
- (Repeat('name', None), none_value),
+ (Repeat("name", 0), ""),
+ (Repeat("name", 2), "JohnJohn"),
+ (Repeat("name", Length("alias")), "JohnJohnJohn"),
+ (Repeat(Value("x"), 3), "xxx"),
+ (Repeat("name", None), none_value),
(Repeat(Value(None), 4), none_value),
- (Repeat('goes_by', 1), none_value),
+ (Repeat("goes_by", 1), none_value),
)
for function, repeated_text in tests:
with self.subTest(function=function):
authors = Author.objects.annotate(repeated_text=function)
- self.assertQuerysetEqual(authors, [repeated_text], lambda a: a.repeated_text, ordered=False)
+ self.assertQuerysetEqual(
+ authors, [repeated_text], lambda a: a.repeated_text, ordered=False
+ )
def test_negative_number(self):
- with self.assertRaisesMessage(ValueError, "'number' must be greater or equal to 0."):
- Repeat('name', -1)
+ with self.assertRaisesMessage(
+ ValueError, "'number' must be greater or equal to 0."
+ ):
+ Repeat("name", -1)
diff --git a/tests/db_functions/text/test_replace.py b/tests/db_functions/text/test_replace.py
index ae87781b8c..28e39faf09 100644
--- a/tests/db_functions/text/test_replace.py
+++ b/tests/db_functions/text/test_replace.py
@@ -6,50 +6,78 @@ from ..models import Author
class ReplaceTests(TestCase):
-
@classmethod
def setUpTestData(cls):
- Author.objects.create(name='George R. R. Martin')
- Author.objects.create(name='J. R. R. Tolkien')
+ Author.objects.create(name="George R. R. Martin")
+ Author.objects.create(name="J. R. R. Tolkien")
def test_replace_with_empty_string(self):
qs = Author.objects.annotate(
- without_middlename=Replace(F('name'), Value('R. R. '), Value('')),
+ without_middlename=Replace(F("name"), Value("R. R. "), Value("")),
+ )
+ self.assertQuerysetEqual(
+ qs,
+ [
+ ("George R. R. Martin", "George Martin"),
+ ("J. R. R. Tolkien", "J. Tolkien"),
+ ],
+ transform=lambda x: (x.name, x.without_middlename),
+ ordered=False,
)
- self.assertQuerysetEqual(qs, [
- ('George R. R. Martin', 'George Martin'),
- ('J. R. R. Tolkien', 'J. Tolkien'),
- ], transform=lambda x: (x.name, x.without_middlename), ordered=False)
def test_case_sensitive(self):
- qs = Author.objects.annotate(same_name=Replace(F('name'), Value('r. r.'), Value('')))
- self.assertQuerysetEqual(qs, [
- ('George R. R. Martin', 'George R. R. Martin'),
- ('J. R. R. Tolkien', 'J. R. R. Tolkien'),
- ], transform=lambda x: (x.name, x.same_name), ordered=False)
+ qs = Author.objects.annotate(
+ same_name=Replace(F("name"), Value("r. r."), Value(""))
+ )
+ self.assertQuerysetEqual(
+ qs,
+ [
+ ("George R. R. Martin", "George R. R. Martin"),
+ ("J. R. R. Tolkien", "J. R. R. Tolkien"),
+ ],
+ transform=lambda x: (x.name, x.same_name),
+ ordered=False,
+ )
def test_replace_expression(self):
- qs = Author.objects.annotate(same_name=Replace(
- Concat(Value('Author: '), F('name')), Value('Author: '), Value('')),
+ qs = Author.objects.annotate(
+ same_name=Replace(
+ Concat(Value("Author: "), F("name")), Value("Author: "), Value("")
+ ),
+ )
+ self.assertQuerysetEqual(
+ qs,
+ [
+ ("George R. R. Martin", "George R. R. Martin"),
+ ("J. R. R. Tolkien", "J. R. R. Tolkien"),
+ ],
+ transform=lambda x: (x.name, x.same_name),
+ ordered=False,
)
- self.assertQuerysetEqual(qs, [
- ('George R. R. Martin', 'George R. R. Martin'),
- ('J. R. R. Tolkien', 'J. R. R. Tolkien'),
- ], transform=lambda x: (x.name, x.same_name), ordered=False)
def test_update(self):
Author.objects.update(
- name=Replace(F('name'), Value('R. R. '), Value('')),
+ name=Replace(F("name"), Value("R. R. "), Value("")),
+ )
+ self.assertQuerysetEqual(
+ Author.objects.all(),
+ [
+ ("George Martin"),
+ ("J. Tolkien"),
+ ],
+ transform=lambda x: x.name,
+ ordered=False,
)
- self.assertQuerysetEqual(Author.objects.all(), [
- ('George Martin'),
- ('J. Tolkien'),
- ], transform=lambda x: x.name, ordered=False)
def test_replace_with_default_arg(self):
# The default replacement is an empty string.
- qs = Author.objects.annotate(same_name=Replace(F('name'), Value('R. R. ')))
- self.assertQuerysetEqual(qs, [
- ('George R. R. Martin', 'George Martin'),
- ('J. R. R. Tolkien', 'J. Tolkien'),
- ], transform=lambda x: (x.name, x.same_name), ordered=False)
+ qs = Author.objects.annotate(same_name=Replace(F("name"), Value("R. R. ")))
+ self.assertQuerysetEqual(
+ qs,
+ [
+ ("George R. R. Martin", "George Martin"),
+ ("J. R. R. Tolkien", "J. Tolkien"),
+ ],
+ transform=lambda x: (x.name, x.same_name),
+ ordered=False,
+ )
diff --git a/tests/db_functions/text/test_reverse.py b/tests/db_functions/text/test_reverse.py
index 1cc1045b04..4d1a81ccf2 100644
--- a/tests/db_functions/text/test_reverse.py
+++ b/tests/db_functions/text/test_reverse.py
@@ -10,22 +10,27 @@ from ..models import Author
class ReverseTests(TestCase):
@classmethod
def setUpTestData(cls):
- cls.john = Author.objects.create(name='John Smith', alias='smithj')
- cls.elena = Author.objects.create(name='Élena Jordan', alias='elena')
- cls.python = Author.objects.create(name='パイソン')
+ cls.john = Author.objects.create(name="John Smith", alias="smithj")
+ cls.elena = Author.objects.create(name="Élena Jordan", alias="elena")
+ cls.python = Author.objects.create(name="パイソン")
def test_null(self):
- author = Author.objects.annotate(backward=Reverse('alias')).get(pk=self.python.pk)
- self.assertEqual(author.backward, '' if connection.features.interprets_empty_strings_as_nulls else None)
+ author = Author.objects.annotate(backward=Reverse("alias")).get(
+ pk=self.python.pk
+ )
+ self.assertEqual(
+ author.backward,
+ "" if connection.features.interprets_empty_strings_as_nulls else None,
+ )
def test_basic(self):
- authors = Author.objects.annotate(backward=Reverse('name'))
+ authors = Author.objects.annotate(backward=Reverse("name"))
self.assertQuerysetEqual(
authors,
[
- ('John Smith', 'htimS nhoJ'),
- ('Élena Jordan', 'nadroJ anelÉ'),
- ('パイソン', 'ンソイパ'),
+ ("John Smith", "htimS nhoJ"),
+ ("Élena Jordan", "nadroJ anelÉ"),
+ ("パイソン", "ンソイパ"),
],
lambda a: (a.name, a.backward),
ordered=False,
@@ -34,13 +39,24 @@ class ReverseTests(TestCase):
def test_transform(self):
with register_lookup(CharField, Reverse):
authors = Author.objects.all()
- self.assertCountEqual(authors.filter(name__reverse=self.john.name[::-1]), [self.john])
- self.assertCountEqual(authors.exclude(name__reverse=self.john.name[::-1]), [self.elena, self.python])
+ self.assertCountEqual(
+ authors.filter(name__reverse=self.john.name[::-1]), [self.john]
+ )
+ self.assertCountEqual(
+ authors.exclude(name__reverse=self.john.name[::-1]),
+ [self.elena, self.python],
+ )
def test_expressions(self):
- author = Author.objects.annotate(backward=Reverse(Trim('name'))).get(pk=self.john.pk)
+ author = Author.objects.annotate(backward=Reverse(Trim("name"))).get(
+ pk=self.john.pk
+ )
self.assertEqual(author.backward, self.john.name[::-1])
with register_lookup(CharField, Reverse), register_lookup(CharField, Length):
authors = Author.objects.all()
- self.assertCountEqual(authors.filter(name__reverse__length__gt=7), [self.john, self.elena])
- self.assertCountEqual(authors.exclude(name__reverse__length__gt=7), [self.python])
+ self.assertCountEqual(
+ authors.filter(name__reverse__length__gt=7), [self.john, self.elena]
+ )
+ self.assertCountEqual(
+ authors.exclude(name__reverse__length__gt=7), [self.python]
+ )
diff --git a/tests/db_functions/text/test_right.py b/tests/db_functions/text/test_right.py
index ab29cb9456..8c271fcf7d 100644
--- a/tests/db_functions/text/test_right.py
+++ b/tests/db_functions/text/test_right.py
@@ -8,20 +8,28 @@ from ..models import Author
class RightTests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
+ 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)
+ 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)
+ 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))
+ Author.objects.annotate(raises=Right("name", 0))
def test_expressions(self):
- authors = Author.objects.annotate(name_part=Right('name', Value(3, output_field=IntegerField())))
- self.assertQuerysetEqual(authors.order_by('name'), ['ith', 'nda'], lambda a: a.name_part)
+ authors = Author.objects.annotate(
+ name_part=Right("name", Value(3, output_field=IntegerField()))
+ )
+ self.assertQuerysetEqual(
+ authors.order_by("name"), ["ith", "nda"], lambda a: a.name_part
+ )
diff --git a/tests/db_functions/text/test_sha1.py b/tests/db_functions/text/test_sha1.py
index e34057bd6c..175c5727ff 100644
--- a/tests/db_functions/text/test_sha1.py
+++ b/tests/db_functions/text/test_sha1.py
@@ -10,33 +10,40 @@ from ..models import Author
class SHA1Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- sha1_alias=SHA1('alias'),
- ).values_list('sha1_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ sha1_alias=SHA1("alias"),
+ )
+ .values_list("sha1_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- 'e61a3587b3f7a142b8c7b9263c82f8119398ecb7',
- '0781e0745a2503e6ded05ed5bc554c421d781b0c',
- '198d15ea139de04060caf95bc3e0ec5883cba881',
- 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
- 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
- if connection.features.interprets_empty_strings_as_nulls else None,
+ "e61a3587b3f7a142b8c7b9263c82f8119398ecb7",
+ "0781e0745a2503e6ded05ed5bc554c421d781b0c",
+ "198d15ea139de04060caf95bc3e0ec5883cba881",
+ "da39a3ee5e6b4b0d3255bfef95601890afd80709",
+ "da39a3ee5e6b4b0d3255bfef95601890afd80709"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
def test_transform(self):
with register_lookup(CharField, SHA1):
authors = Author.objects.filter(
- alias__sha1='e61a3587b3f7a142b8c7b9263c82f8119398ecb7',
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ alias__sha1="e61a3587b3f7a142b8c7b9263c82f8119398ecb7",
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
diff --git a/tests/db_functions/text/test_sha224.py b/tests/db_functions/text/test_sha224.py
index d2f1d538dc..83c30a3091 100644
--- a/tests/db_functions/text/test_sha224.py
+++ b/tests/db_functions/text/test_sha224.py
@@ -12,39 +12,48 @@ from ..models import Author
class SHA224Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- sha224_alias=SHA224('alias'),
- ).values_list('sha224_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ sha224_alias=SHA224("alias"),
+ )
+ .values_list("sha224_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- 'a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70',
- '2297904883e78183cb118fc3dc21a610d60daada7b6ebdbc85139f4d',
- 'eba942746e5855121d9d8f79e27dfdebed81adc85b6bf41591203080',
- 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f',
- 'd14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f'
- if connection.features.interprets_empty_strings_as_nulls else None,
+ "a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70",
+ "2297904883e78183cb118fc3dc21a610d60daada7b6ebdbc85139f4d",
+ "eba942746e5855121d9d8f79e27dfdebed81adc85b6bf41591203080",
+ "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
+ "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
def test_transform(self):
with register_lookup(CharField, SHA224):
authors = Author.objects.filter(
- alias__sha224='a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70',
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ alias__sha224="a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70",
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
- @unittest.skipUnless(connection.vendor == 'oracle', "Oracle doesn't support SHA224.")
+ @unittest.skipUnless(
+ connection.vendor == "oracle", "Oracle doesn't support SHA224."
+ )
def test_unsupported(self):
- msg = 'SHA224 is not supported on Oracle.'
+ msg = "SHA224 is not supported on Oracle."
with self.assertRaisesMessage(NotSupportedError, msg):
- Author.objects.annotate(sha224_alias=SHA224('alias')).first()
+ Author.objects.annotate(sha224_alias=SHA224("alias")).first()
diff --git a/tests/db_functions/text/test_sha256.py b/tests/db_functions/text/test_sha256.py
index 54ad841422..d6f431fdf6 100644
--- a/tests/db_functions/text/test_sha256.py
+++ b/tests/db_functions/text/test_sha256.py
@@ -10,33 +10,40 @@ from ..models import Author
class SHA256Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- sha256_alias=SHA256('alias'),
- ).values_list('sha256_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ sha256_alias=SHA256("alias"),
+ )
+ .values_list("sha256_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- 'ef61a579c907bbed674c0dbcbcf7f7af8f851538eef7b8e58c5bee0b8cfdac4a',
- '6e4cce20cd83fc7c202f21a8b2452a68509cf24d1c272a045b5e0cfc43f0d94e',
- '3ad2039e3ec0c88973ae1c0fce5a3dbafdd5a1627da0a92312c54ebfcf43988e',
- 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
- 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
- if connection.features.interprets_empty_strings_as_nulls else None,
+ "ef61a579c907bbed674c0dbcbcf7f7af8f851538eef7b8e58c5bee0b8cfdac4a",
+ "6e4cce20cd83fc7c202f21a8b2452a68509cf24d1c272a045b5e0cfc43f0d94e",
+ "3ad2039e3ec0c88973ae1c0fce5a3dbafdd5a1627da0a92312c54ebfcf43988e",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
def test_transform(self):
with register_lookup(CharField, SHA256):
authors = Author.objects.filter(
- alias__sha256='ef61a579c907bbed674c0dbcbcf7f7af8f851538eef7b8e58c5bee0b8cfdac4a',
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ alias__sha256="ef61a579c907bbed674c0dbcbcf7f7af8f851538eef7b8e58c5bee0b8cfdac4a",
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
diff --git a/tests/db_functions/text/test_sha384.py b/tests/db_functions/text/test_sha384.py
index 95aa54cdbb..cac72d54ce 100644
--- a/tests/db_functions/text/test_sha384.py
+++ b/tests/db_functions/text/test_sha384.py
@@ -10,27 +10,34 @@ from ..models import Author
class SHA384Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- sha384_alias=SHA384('alias'),
- ).values_list('sha384_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ sha384_alias=SHA384("alias"),
+ )
+ .values_list("sha384_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- '9df976bfbcf96c66fbe5cba866cd4deaa8248806f15b69c4010a404112906e4ca7b57e53b9967b80d77d4f5c2982cbc8',
- '72202c8005492016cc670219cce82d47d6d2d4273464c742ab5811d691b1e82a7489549e3a73ffa119694f90678ba2e3',
- 'eda87fae41e59692c36c49e43279c8111a00d79122a282a944e8ba9a403218f049a48326676a43c7ba378621175853b0',
- '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b',
- '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
- if connection.features.interprets_empty_strings_as_nulls else None,
+ "9df976bfbcf96c66fbe5cba866cd4deaa8248806f15b69c4010a404112906e4ca7b57e53b9967b80d77d4f5c2982cbc8",
+ "72202c8005492016cc670219cce82d47d6d2d4273464c742ab5811d691b1e82a7489549e3a73ffa119694f90678ba2e3",
+ "eda87fae41e59692c36c49e43279c8111a00d79122a282a944e8ba9a403218f049a48326676a43c7ba378621175853b0",
+ "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
+ "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
@@ -38,7 +45,7 @@ class SHA384Tests(TestCase):
with register_lookup(CharField, SHA384):
authors = Author.objects.filter(
alias__sha384=(
- '9df976bfbcf96c66fbe5cba866cd4deaa8248806f15b69c4010a404112906e4ca7b57e53b9967b80d77d4f5c2982cbc8'
+ "9df976bfbcf96c66fbe5cba866cd4deaa8248806f15b69c4010a404112906e4ca7b57e53b9967b80d77d4f5c2982cbc8"
),
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
diff --git a/tests/db_functions/text/test_sha512.py b/tests/db_functions/text/test_sha512.py
index aa0d1e6fdd..f5a7ad4ae5 100644
--- a/tests/db_functions/text/test_sha512.py
+++ b/tests/db_functions/text/test_sha512.py
@@ -10,32 +10,39 @@ from ..models import Author
class SHA512Tests(TestCase):
@classmethod
def setUpTestData(cls):
- Author.objects.bulk_create([
- Author(alias='John Smith'),
- Author(alias='Jordan Élena'),
- Author(alias='皇帝'),
- Author(alias=''),
- Author(alias=None),
- ])
+ Author.objects.bulk_create(
+ [
+ Author(alias="John Smith"),
+ Author(alias="Jordan Élena"),
+ Author(alias="皇帝"),
+ Author(alias=""),
+ Author(alias=None),
+ ]
+ )
def test_basic(self):
- authors = Author.objects.annotate(
- sha512_alias=SHA512('alias'),
- ).values_list('sha512_alias', flat=True).order_by('pk')
+ authors = (
+ Author.objects.annotate(
+ sha512_alias=SHA512("alias"),
+ )
+ .values_list("sha512_alias", flat=True)
+ .order_by("pk")
+ )
self.assertSequenceEqual(
authors,
[
- 'ed014a19bb67a85f9c8b1d81e04a0e7101725be8627d79d02ca4f3bd803f33cf'
- '3b8fed53e80d2a12c0d0e426824d99d110f0919298a5055efff040a3fc091518',
- 'b09c449f3ba49a32ab44754982d4749ac938af293e4af2de28858858080a1611'
- '2b719514b5e48cb6ce54687e843a4b3e69a04cdb2a9dc99c3b99bdee419fa7d0',
- 'b554d182e25fb487a3f2b4285bb8672f98956b5369138e681b467d1f079af116'
- '172d88798345a3a7666faf5f35a144c60812d3234dcd35f444624f2faee16857',
- 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce'
- '47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
- 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce'
- '47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
- if connection.features.interprets_empty_strings_as_nulls else None,
+ "ed014a19bb67a85f9c8b1d81e04a0e7101725be8627d79d02ca4f3bd803f33cf"
+ "3b8fed53e80d2a12c0d0e426824d99d110f0919298a5055efff040a3fc091518",
+ "b09c449f3ba49a32ab44754982d4749ac938af293e4af2de28858858080a1611"
+ "2b719514b5e48cb6ce54687e843a4b3e69a04cdb2a9dc99c3b99bdee419fa7d0",
+ "b554d182e25fb487a3f2b4285bb8672f98956b5369138e681b467d1f079af116"
+ "172d88798345a3a7666faf5f35a144c60812d3234dcd35f444624f2faee16857",
+ "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
+ "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
+ "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
+ "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
+ if connection.features.interprets_empty_strings_as_nulls
+ else None,
],
)
@@ -43,9 +50,9 @@ class SHA512Tests(TestCase):
with register_lookup(CharField, SHA512):
authors = Author.objects.filter(
alias__sha512=(
- 'ed014a19bb67a85f9c8b1d81e04a0e7101725be8627d79d02ca4f3bd8'
- '03f33cf3b8fed53e80d2a12c0d0e426824d99d110f0919298a5055eff'
- 'f040a3fc091518'
+ "ed014a19bb67a85f9c8b1d81e04a0e7101725be8627d79d02ca4f3bd8"
+ "03f33cf3b8fed53e80d2a12c0d0e426824d99d110f0919298a5055eff"
+ "f040a3fc091518"
),
- ).values_list('alias', flat=True)
- self.assertSequenceEqual(authors, ['John Smith'])
+ ).values_list("alias", flat=True)
+ self.assertSequenceEqual(authors, ["John Smith"])
diff --git a/tests/db_functions/text/test_strindex.py b/tests/db_functions/text/test_strindex.py
index 1670df00fd..84305ea1b7 100644
--- a/tests/db_functions/text/test_strindex.py
+++ b/tests/db_functions/text/test_strindex.py
@@ -8,59 +8,67 @@ from ..models import Article, Author
class StrIndexTests(TestCase):
def test_annotate_charfield(self):
- Author.objects.create(name='George. R. R. Martin')
- Author.objects.create(name='J. R. R. Tolkien')
- Author.objects.create(name='Terry Pratchett')
- authors = Author.objects.annotate(fullstop=StrIndex('name', Value('R.')))
- self.assertQuerysetEqual(authors.order_by('name'), [9, 4, 0], lambda a: a.fullstop)
+ Author.objects.create(name="George. R. R. Martin")
+ Author.objects.create(name="J. R. R. Tolkien")
+ Author.objects.create(name="Terry Pratchett")
+ authors = Author.objects.annotate(fullstop=StrIndex("name", Value("R.")))
+ self.assertQuerysetEqual(
+ authors.order_by("name"), [9, 4, 0], lambda a: a.fullstop
+ )
def test_annotate_textfield(self):
Article.objects.create(
- title='How to Django',
- text='This is about How to Django.',
+ title="How to Django",
+ text="This is about How to Django.",
written=timezone.now(),
)
Article.objects.create(
- title='How to Tango',
+ title="How to Tango",
text="Won't find anything here.",
written=timezone.now(),
)
- articles = Article.objects.annotate(title_pos=StrIndex('text', 'title'))
- self.assertQuerysetEqual(articles.order_by('title'), [15, 0], lambda a: a.title_pos)
+ articles = Article.objects.annotate(title_pos=StrIndex("text", "title"))
+ self.assertQuerysetEqual(
+ articles.order_by("title"), [15, 0], lambda a: a.title_pos
+ )
def test_order_by(self):
- Author.objects.create(name='Terry Pratchett')
- Author.objects.create(name='J. R. R. Tolkien')
- Author.objects.create(name='George. R. R. Martin')
+ Author.objects.create(name="Terry Pratchett")
+ Author.objects.create(name="J. R. R. Tolkien")
+ Author.objects.create(name="George. R. R. Martin")
self.assertQuerysetEqual(
- Author.objects.order_by(StrIndex('name', Value('R.')).asc()), [
- 'Terry Pratchett',
- 'J. R. R. Tolkien',
- 'George. R. R. Martin',
+ Author.objects.order_by(StrIndex("name", Value("R.")).asc()),
+ [
+ "Terry Pratchett",
+ "J. R. R. Tolkien",
+ "George. R. R. Martin",
],
- lambda a: a.name
+ lambda a: a.name,
)
self.assertQuerysetEqual(
- Author.objects.order_by(StrIndex('name', Value('R.')).desc()), [
- 'George. R. R. Martin',
- 'J. R. R. Tolkien',
- 'Terry Pratchett',
+ Author.objects.order_by(StrIndex("name", Value("R.")).desc()),
+ [
+ "George. R. R. Martin",
+ "J. R. R. Tolkien",
+ "Terry Pratchett",
],
- lambda a: a.name
+ lambda a: a.name,
)
def test_unicode_values(self):
- Author.objects.create(name='ツリー')
- Author.objects.create(name='皇帝')
- Author.objects.create(name='皇帝 ツリー')
- authors = Author.objects.annotate(sb=StrIndex('name', Value('リ')))
- self.assertQuerysetEqual(authors.order_by('name'), [2, 0, 5], lambda a: a.sb)
+ Author.objects.create(name="ツリー")
+ Author.objects.create(name="皇帝")
+ Author.objects.create(name="皇帝 ツリー")
+ authors = Author.objects.annotate(sb=StrIndex("name", Value("リ")))
+ self.assertQuerysetEqual(authors.order_by("name"), [2, 0, 5], lambda a: a.sb)
def test_filtering(self):
- Author.objects.create(name='George. R. R. Martin')
- Author.objects.create(name='Terry Pratchett')
+ Author.objects.create(name="George. R. R. Martin")
+ Author.objects.create(name="Terry Pratchett")
self.assertQuerysetEqual(
- Author.objects.annotate(middle_name=StrIndex('name', Value('R.'))).filter(middle_name__gt=0),
- ['George. R. R. Martin'],
- lambda a: a.name
+ Author.objects.annotate(middle_name=StrIndex("name", Value("R."))).filter(
+ middle_name__gt=0
+ ),
+ ["George. R. R. Martin"],
+ lambda a: a.name,
)
diff --git a/tests/db_functions/text/test_substr.py b/tests/db_functions/text/test_substr.py
index 35af5656ef..751995355c 100644
--- a/tests/db_functions/text/test_substr.py
+++ b/tests/db_functions/text/test_substr.py
@@ -6,48 +6,43 @@ from ..models import Author
class SubstrTests(TestCase):
-
def test_basic(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- authors = Author.objects.annotate(name_part=Substr('name', 5, 3))
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ authors = Author.objects.annotate(name_part=Substr("name", 5, 3))
self.assertQuerysetEqual(
- authors.order_by('name'), [' Sm', 'da'],
- lambda a: a.name_part
+ authors.order_by("name"), [" Sm", "da"], lambda a: a.name_part
)
- authors = Author.objects.annotate(name_part=Substr('name', 2))
+ authors = Author.objects.annotate(name_part=Substr("name", 2))
self.assertQuerysetEqual(
- authors.order_by('name'), ['ohn Smith', 'honda'],
- lambda a: a.name_part
+ authors.order_by("name"), ["ohn Smith", "honda"], lambda a: a.name_part
)
# If alias is null, set to first 5 lower characters of the name.
Author.objects.filter(alias__isnull=True).update(
- alias=Lower(Substr('name', 1, 5)),
+ alias=Lower(Substr("name", 1, 5)),
)
self.assertQuerysetEqual(
- authors.order_by('name'), ['smithj', 'rhond'],
- lambda a: a.alias
+ authors.order_by("name"), ["smithj", "rhond"], lambda a: a.alias
)
def test_start(self):
- Author.objects.create(name='John Smith', alias='smithj')
+ Author.objects.create(name="John Smith", alias="smithj")
a = Author.objects.annotate(
- name_part_1=Substr('name', 1),
- name_part_2=Substr('name', 2),
- ).get(alias='smithj')
+ name_part_1=Substr("name", 1),
+ name_part_2=Substr("name", 2),
+ ).get(alias="smithj")
self.assertEqual(a.name_part_1[1:], a.name_part_2)
def test_pos_gt_zero(self):
with self.assertRaisesMessage(ValueError, "'pos' must be greater than 0"):
- Author.objects.annotate(raises=Substr('name', 0))
+ Author.objects.annotate(raises=Substr("name", 0))
def test_expressions(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- substr = Substr(Upper('name'), StrIndex('name', V('h')), 5)
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ substr = Substr(Upper("name"), StrIndex("name", V("h")), 5)
authors = Author.objects.annotate(name_part=substr)
self.assertQuerysetEqual(
- authors.order_by('name'), ['HN SM', 'HONDA'],
- lambda a: a.name_part
+ authors.order_by("name"), ["HN SM", "HONDA"], lambda a: a.name_part
)
diff --git a/tests/db_functions/text/test_trim.py b/tests/db_functions/text/test_trim.py
index 9834cef1c6..57a342851e 100644
--- a/tests/db_functions/text/test_trim.py
+++ b/tests/db_functions/text/test_trim.py
@@ -8,31 +8,34 @@ from ..models import Author
class TrimTests(TestCase):
def test_trim(self):
- Author.objects.create(name=' John ', alias='j')
- Author.objects.create(name='Rhonda', alias='r')
+ Author.objects.create(name=" John ", alias="j")
+ Author.objects.create(name="Rhonda", alias="r")
authors = Author.objects.annotate(
- ltrim=LTrim('name'),
- rtrim=RTrim('name'),
- trim=Trim('name'),
+ ltrim=LTrim("name"),
+ rtrim=RTrim("name"),
+ trim=Trim("name"),
)
self.assertQuerysetEqual(
- authors.order_by('alias'), [
- ('John ', ' John', 'John'),
- ('Rhonda', 'Rhonda', 'Rhonda'),
+ authors.order_by("alias"),
+ [
+ ("John ", " John", "John"),
+ ("Rhonda", "Rhonda", "Rhonda"),
],
- lambda a: (a.ltrim, a.rtrim, a.trim)
+ lambda a: (a.ltrim, a.rtrim, a.trim),
)
def test_trim_transform(self):
- Author.objects.create(name=' John ')
- Author.objects.create(name='Rhonda')
+ Author.objects.create(name=" John ")
+ Author.objects.create(name="Rhonda")
tests = (
- (LTrim, 'John '),
- (RTrim, ' John'),
- (Trim, 'John'),
+ (LTrim, "John "),
+ (RTrim, " John"),
+ (Trim, "John"),
)
for transform, trimmed_name in tests:
with self.subTest(transform=transform):
with register_lookup(CharField, transform):
- authors = Author.objects.filter(**{'name__%s' % transform.lookup_name: trimmed_name})
- self.assertQuerysetEqual(authors, [' John '], lambda a: a.name)
+ authors = Author.objects.filter(
+ **{"name__%s" % transform.lookup_name: trimmed_name}
+ )
+ self.assertQuerysetEqual(authors, [" John "], lambda a: a.name)
diff --git a/tests/db_functions/text/test_upper.py b/tests/db_functions/text/test_upper.py
index 5b5df0af33..728ea82265 100644
--- a/tests/db_functions/text/test_upper.py
+++ b/tests/db_functions/text/test_upper.py
@@ -7,35 +7,37 @@ from ..models import Author
class UpperTests(TestCase):
-
def test_basic(self):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- authors = Author.objects.annotate(upper_name=Upper('name'))
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ authors = Author.objects.annotate(upper_name=Upper("name"))
self.assertQuerysetEqual(
- authors.order_by('name'), [
- 'JOHN SMITH',
- 'RHONDA',
+ authors.order_by("name"),
+ [
+ "JOHN SMITH",
+ "RHONDA",
],
- lambda a: a.upper_name
+ lambda a: a.upper_name,
)
- Author.objects.update(name=Upper('name'))
+ Author.objects.update(name=Upper("name"))
self.assertQuerysetEqual(
- authors.order_by('name'), [
- ('JOHN SMITH', 'JOHN SMITH'),
- ('RHONDA', 'RHONDA'),
+ authors.order_by("name"),
+ [
+ ("JOHN SMITH", "JOHN SMITH"),
+ ("RHONDA", "RHONDA"),
],
- lambda a: (a.upper_name, a.name)
+ lambda a: (a.upper_name, a.name),
)
def test_transform(self):
with register_lookup(CharField, Upper):
- Author.objects.create(name='John Smith', alias='smithj')
- Author.objects.create(name='Rhonda')
- authors = Author.objects.filter(name__upper__exact='JOHN SMITH')
+ Author.objects.create(name="John Smith", alias="smithj")
+ Author.objects.create(name="Rhonda")
+ authors = Author.objects.filter(name__upper__exact="JOHN SMITH")
self.assertQuerysetEqual(
- authors.order_by('name'), [
- 'John Smith',
+ authors.order_by("name"),
+ [
+ "John Smith",
],
- lambda a: a.name
+ lambda a: a.name,
)