2018-01-18 09:46:15 +08:00
|
|
|
from django.db.models import F, Value
|
|
|
|
from django.db.models.functions import Concat, Replace
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2018-08-16 07:45:11 +08:00
|
|
|
from ..models import Author
|
2018-01-18 09:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ReplaceTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
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("")),
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-04 03:24:19 +08:00
|
|
|
qs,
|
|
|
|
[
|
2018-01-18 09:46:15 +08:00
|
|
|
("George R. R. Martin", "George Martin"),
|
|
|
|
("J. R. R. Tolkien", "J. Tolkien"),
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
2018-01-18 09:46:15 +08:00
|
|
|
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(""))
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-01-18 09:46:15 +08:00
|
|
|
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("")
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-04 03:24:19 +08:00
|
|
|
qs,
|
|
|
|
[
|
2018-01-18 09:46:15 +08:00
|
|
|
("George R. R. Martin", "George R. R. Martin"),
|
|
|
|
("J. R. R. Tolkien", "J. R. R. Tolkien"),
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
2018-01-18 09:46:15 +08:00
|
|
|
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("")),
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-01-18 09:46:15 +08:00
|
|
|
Author.objects.all(),
|
2022-02-04 03:24:19 +08:00
|
|
|
[
|
2018-01-18 09:46:15 +08:00
|
|
|
("George Martin"),
|
|
|
|
("J. Tolkien"),
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
2018-01-18 09:46:15 +08:00
|
|
|
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. ")))
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-01-18 09:46:15 +08:00
|
|
|
qs,
|
|
|
|
[
|
|
|
|
("George R. R. Martin", "George Martin"),
|
|
|
|
("J. R. R. Tolkien", "J. Tolkien"),
|
|
|
|
],
|
|
|
|
transform=lambda x: (x.name, x.same_name),
|
|
|
|
ordered=False,
|
|
|
|
)
|