test_promises.py 5.8 KB

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