Debian Codegraph @

tests/db_functions/text/test_md5.py

raw
1 from django.db import connection
2 from django.db.models import CharField
3 from django.db.models.functions import MD5
4 from django.test import TestCase
5 from django.test.utils import register_lookup
6
7 from ..models import Author
8
9
10 class MD5Tests(TestCase):
11 @classmethod
12 def setUpTestData(cls):
13 Author.objects.bulk_create(
14 [
15 Author(alias="John Smith"),
16 Author(alias="Jordan Élena"),
17 Author(alias="皇帝"),
18 Author(alias=""),
19 Author(alias=None),
20 ]
21 )
22
23 def test_basic(self):
24 authors = (
25 Author.objects.annotate(
26 md5_alias=MD5("alias"),
27 )
28 .values_list("md5_alias", flat=True)
29 .order_by("pk")
30 )
31 self.assertSequenceEqual(
32 authors,
33 [
34 "6117323d2cabbc17d44c2b44587f682c",
35 "ca6d48f6772000141e66591aee49d56c",
36 "bf2c13bc1154e3d2e7df848cbc8be73d",
37 "d41d8cd98f00b204e9800998ecf8427e",
38 (
39 "d41d8cd98f00b204e9800998ecf8427e"
40 if connection.features.interprets_empty_strings_as_nulls
41 else None
42 ),
43 ],
44 )
45
46 def test_transform(self):
47 with register_lookup(CharField, MD5):
48 authors = Author.objects.filter(
49 alias__md5="6117323d2cabbc17d44c2b44587f682c",
50 ).values_list("alias", flat=True)
51 self.assertSequenceEqual(authors, ["John Smith"])