test_sha224.py 2.1 KB

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