2017-04-03 12:39:57 +00:00
|
|
|
from django.db.models import Value
|
2017-02-21 06:43:38 +00:00
|
|
|
from django.db.models.functions import StrIndex
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2018-08-15 23:45:11 +00:00
|
|
|
from ..models import Article, Author
|
2017-02-21 06:43:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2017-04-03 12:39:57 +00:00
|
|
|
authors = Author.objects.annotate(fullstop=StrIndex("name", Value("R.")))
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2017-02-21 06:43:38 +00:00
|
|
|
authors.order_by("name"), [9, 4, 0], lambda a: a.fullstop
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_annotate_textfield(self):
|
|
|
|
Article.objects.create(
|
|
|
|
title="How to Django",
|
2017-04-03 12:39:57 +00:00
|
|
|
text="This is about How to Django.",
|
2017-02-21 06:43:38 +00:00
|
|
|
written=timezone.now(),
|
|
|
|
)
|
|
|
|
Article.objects.create(
|
|
|
|
title="How to Tango",
|
|
|
|
text="Won't find anything here.",
|
|
|
|
written=timezone.now(),
|
|
|
|
)
|
2017-04-03 12:39:57 +00:00
|
|
|
articles = Article.objects.annotate(title_pos=StrIndex("text", "title"))
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2017-04-03 12:39:57 +00:00
|
|
|
articles.order_by("title"), [15, 0], lambda a: a.title_pos
|
|
|
|
)
|
2017-02-21 06:43:38 +00:00
|
|
|
|
|
|
|
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")
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2017-04-03 12:39:57 +00:00
|
|
|
Author.objects.order_by(StrIndex("name", Value("R.")).asc()),
|
|
|
|
[
|
2017-02-21 06:43:38 +00:00
|
|
|
"Terry Pratchett",
|
|
|
|
"J. R. R. Tolkien",
|
|
|
|
"George. R. R. Martin",
|
|
|
|
],
|
|
|
|
lambda a: a.name,
|
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2017-04-03 12:39:57 +00:00
|
|
|
Author.objects.order_by(StrIndex("name", Value("R.")).desc()),
|
|
|
|
[
|
2017-02-21 06:43:38 +00:00
|
|
|
"George. R. R. Martin",
|
|
|
|
"J. R. R. Tolkien",
|
|
|
|
"Terry Pratchett",
|
|
|
|
],
|
|
|
|
lambda a: a.name,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unicode_values(self):
|
|
|
|
Author.objects.create(name="ツリー")
|
|
|
|
Author.objects.create(name="皇帝")
|
|
|
|
Author.objects.create(name="皇帝 ツリー")
|
2017-04-03 12:39:57 +00:00
|
|
|
authors = Author.objects.annotate(sb=StrIndex("name", Value("リ")))
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(authors.order_by("name"), [2, 0, 5], lambda a: a.sb)
|
2017-02-21 06:43:38 +00:00
|
|
|
|
|
|
|
def test_filtering(self):
|
|
|
|
Author.objects.create(name="George. R. R. Martin")
|
|
|
|
Author.objects.create(name="Terry Pratchett")
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2017-04-03 12:39:57 +00:00
|
|
|
Author.objects.annotate(middle_name=StrIndex("name", Value("R."))).filter(
|
|
|
|
middle_name__gt=0
|
|
|
|
),
|
2017-02-21 06:43:38 +00:00
|
|
|
["George. R. R. Martin"],
|
|
|
|
lambda a: a.name,
|
|
|
|
)
|