Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #28643 -- Added Replace database function.
- Loading branch information
1 parent
fcd431c
commit 6572855
Showing
5 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from django.db.models import F, Value | ||
from django.db.models.functions import Concat, Replace | ||
from django.test import TestCase | ||
|
||
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') | ||
|
||
def test_replace_with_empty_string(self): | ||
qs = Author.objects.annotate( | ||
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) | ||
|
||
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) | ||
|
||
def test_replace_expression(self): | ||
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) | ||
|
||
def test_update(self): | ||
Author.objects.update( | ||
name=Replace(F('name'), Value('R. R. '), Value('')), | ||
) | ||
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) |