Debian Codegraph @

tests/db_functions/text/test_sha224.py

raw
1 import unittest
2
3 from django.db import NotSupportedError, connection
4 from django.db.models import CharField
5 from django.db.models.functions import SHA224
6 from django.test import TestCase
7 from django.test.utils import register_lookup
8
9 from ..models import Author
10
11
12 class SHA224Tests(TestCase):
13 @classmethod
14 def setUpTestData(cls):
15 Author.objects.bulk_create(
16 [
17 Author(alias="John Smith"),
18 Author(alias="Jordan Élena"),
19 Author(alias="皇帝"),
20 Author(alias=""),
21 Author(alias=None),
22 ]
23 )
24
25 def test_basic(self):
26 authors = (
27 Author.objects.annotate(
28 sha224_alias=SHA224("alias"),
29 )
30 .values_list("sha224_alias", flat=True)
31 .order_by("pk")
32 )
33 self.assertSequenceEqual(
34 authors,
35 [
36 "a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70",
37 "2297904883e78183cb118fc3dc21a610d60daada7b6ebdbc85139f4d",
38 "eba942746e5855121d9d8f79e27dfdebed81adc85b6bf41591203080",
39 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
40 (
41 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"
42 if connection.features.interprets_empty_strings_as_nulls
43 else None
44 ),
45 ],
46 )
47
48 def test_transform(self):
49 with register_lookup(CharField, SHA224):
50 authors = Author.objects.filter(
51 alias__sha224=(
52 "a61303c220731168452cb6acf3759438b1523e768f464e3704e12f70"
53 ),
54 ).values_list("alias", flat=True)
55 self.assertSequenceEqual(authors, ["John Smith"])
56
57 @unittest.skipUnless(
58 connection.vendor == "oracle", "Oracle doesn't support SHA224."
59 )
60 def test_unsupported(self):
61 msg = "SHA224 is not supported on Oracle."
62 with self.assertRaisesMessage(NotSupportedError, msg):
63 Author.objects.annotate(sha224_alias=SHA224("alias")).first()