test_promises.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import datetime
  2. from decimal import Decimal
  3. from django.db.models.fields import (
  4. AutoField, BinaryField, BooleanField, CharField, DateField, DateTimeField,
  5. DecimalField, EmailField, FilePathField, FloatField, GenericIPAddressField,
  6. IntegerField, IPAddressField, NullBooleanField, PositiveBigIntegerField,
  7. PositiveIntegerField, PositiveSmallIntegerField, SlugField,
  8. SmallIntegerField, TextField, TimeField, URLField,
  9. )
  10. from django.db.models.fields.files import FileField, ImageField
  11. from django.test import SimpleTestCase
  12. from django.utils.functional import lazy
  13. class PromiseTest(SimpleTestCase):
  14. def test_AutoField(self):
  15. lazy_func = lazy(lambda: 1, int)
  16. self.assertIsInstance(AutoField(primary_key=True).get_prep_value(lazy_func()), int)
  17. def test_BinaryField(self):
  18. lazy_func = lazy(lambda: b'', bytes)
  19. self.assertIsInstance(BinaryField().get_prep_value(lazy_func()), bytes)
  20. def test_BooleanField(self):
  21. lazy_func = lazy(lambda: True, bool)
  22. self.assertIsInstance(BooleanField().get_prep_value(lazy_func()), bool)
  23. def test_CharField(self):
  24. lazy_func = lazy(lambda: '', str)
  25. self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
  26. lazy_func = lazy(lambda: 0, int)
  27. self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
  28. def test_DateField(self):
  29. lazy_func = lazy(lambda: datetime.date.today(), datetime.date)
  30. self.assertIsInstance(DateField().get_prep_value(lazy_func()), datetime.date)
  31. def test_DateTimeField(self):
  32. lazy_func = lazy(lambda: datetime.datetime.now(), datetime.datetime)
  33. self.assertIsInstance(DateTimeField().get_prep_value(lazy_func()), datetime.datetime)
  34. def test_DecimalField(self):
  35. lazy_func = lazy(lambda: Decimal('1.2'), Decimal)
  36. self.assertIsInstance(DecimalField().get_prep_value(lazy_func()), Decimal)
  37. def test_EmailField(self):
  38. lazy_func = lazy(lambda: 'mailbox@domain.com', str)
  39. self.assertIsInstance(EmailField().get_prep_value(lazy_func()), str)
  40. def test_FileField(self):
  41. lazy_func = lazy(lambda: 'filename.ext', str)
  42. self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
  43. lazy_func = lazy(lambda: 0, int)
  44. self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
  45. def test_FilePathField(self):
  46. lazy_func = lazy(lambda: 'tests.py', str)
  47. self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
  48. lazy_func = lazy(lambda: 0, int)
  49. self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
  50. def test_FloatField(self):
  51. lazy_func = lazy(lambda: 1.2, float)
  52. self.assertIsInstance(FloatField().get_prep_value(lazy_func()), float)
  53. def test_ImageField(self):
  54. lazy_func = lazy(lambda: 'filename.ext', str)
  55. self.assertIsInstance(ImageField().get_prep_value(lazy_func()), str)
  56. def test_IntegerField(self):
  57. lazy_func = lazy(lambda: 1, int)
  58. self.assertIsInstance(IntegerField().get_prep_value(lazy_func()), int)
  59. def test_IPAddressField(self):
  60. lazy_func = lazy(lambda: '127.0.0.1', str)
  61. self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
  62. lazy_func = lazy(lambda: 0, int)
  63. self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
  64. def test_GenericIPAddressField(self):
  65. lazy_func = lazy(lambda: '127.0.0.1', str)
  66. self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
  67. lazy_func = lazy(lambda: 0, int)
  68. self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
  69. def test_NullBooleanField(self):
  70. lazy_func = lazy(lambda: True, bool)
  71. self.assertIsInstance(NullBooleanField().get_prep_value(lazy_func()), bool)
  72. def test_PositiveIntegerField(self):
  73. lazy_func = lazy(lambda: 1, int)
  74. self.assertIsInstance(PositiveIntegerField().get_prep_value(lazy_func()), int)
  75. def test_PositiveSmallIntegerField(self):
  76. lazy_func = lazy(lambda: 1, int)
  77. self.assertIsInstance(PositiveSmallIntegerField().get_prep_value(lazy_func()), int)
  78. def test_PositiveBigIntegerField(self):
  79. lazy_func = lazy(lambda: 1, int)
  80. self.assertIsInstance(PositiveBigIntegerField().get_prep_value(lazy_func()), int)
  81. def test_SlugField(self):
  82. lazy_func = lazy(lambda: 'slug', str)
  83. self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
  84. lazy_func = lazy(lambda: 0, int)
  85. self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
  86. def test_SmallIntegerField(self):
  87. lazy_func = lazy(lambda: 1, int)
  88. self.assertIsInstance(SmallIntegerField().get_prep_value(lazy_func()), int)
  89. def test_TextField(self):
  90. lazy_func = lazy(lambda: 'Abc', str)
  91. self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
  92. lazy_func = lazy(lambda: 0, int)
  93. self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
  94. def test_TimeField(self):
  95. lazy_func = lazy(lambda: datetime.datetime.now().time(), datetime.time)
  96. self.assertIsInstance(TimeField().get_prep_value(lazy_func()), datetime.time)
  97. def test_URLField(self):
  98. lazy_func = lazy(lambda: 'http://domain.com', str)
  99. self.assertIsInstance(URLField().get_prep_value(lazy_func()), str)