2016-03-22 09:06:54 +08:00
|
|
|
import datetime
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db.models import (
|
2016-12-01 18:38:01 +08:00
|
|
|
AutoField,
|
|
|
|
BinaryField,
|
|
|
|
BooleanField,
|
|
|
|
CharField,
|
|
|
|
DateField,
|
|
|
|
DateTimeField,
|
2019-08-20 15:54:41 +08:00
|
|
|
DecimalField,
|
|
|
|
EmailField,
|
|
|
|
FileField,
|
|
|
|
FilePathField,
|
|
|
|
FloatField,
|
|
|
|
GenericIPAddressField,
|
|
|
|
ImageField,
|
|
|
|
IntegerField,
|
|
|
|
IPAddressField,
|
2021-01-14 04:28:09 +08:00
|
|
|
PositiveBigIntegerField,
|
|
|
|
PositiveIntegerField,
|
|
|
|
PositiveSmallIntegerField,
|
|
|
|
SlugField,
|
|
|
|
SmallIntegerField,
|
|
|
|
TextField,
|
|
|
|
TimeField,
|
|
|
|
URLField,
|
2016-03-22 09:06:54 +08:00
|
|
|
)
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.utils.functional import lazy
|
|
|
|
|
|
|
|
|
|
|
|
class PromiseTest(SimpleTestCase):
|
|
|
|
def test_AutoField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(
|
|
|
|
AutoField(primary_key=True).get_prep_value(lazy_func()), int
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_BinaryField(self):
|
|
|
|
lazy_func = lazy(lambda: b"", bytes)
|
|
|
|
self.assertIsInstance(BinaryField().get_prep_value(lazy_func()), bytes)
|
|
|
|
|
|
|
|
def test_BooleanField(self):
|
|
|
|
lazy_func = lazy(lambda: True, bool)
|
|
|
|
self.assertIsInstance(BooleanField().get_prep_value(lazy_func()), bool)
|
|
|
|
|
|
|
|
def test_CharField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "", str)
|
|
|
|
self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_DateField(self):
|
|
|
|
lazy_func = lazy(lambda: datetime.date.today(), datetime.date)
|
|
|
|
self.assertIsInstance(DateField().get_prep_value(lazy_func()), datetime.date)
|
|
|
|
|
|
|
|
def test_DateTimeField(self):
|
|
|
|
lazy_func = lazy(lambda: datetime.datetime.now(), datetime.datetime)
|
|
|
|
self.assertIsInstance(
|
|
|
|
DateTimeField().get_prep_value(lazy_func()), datetime.datetime
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_DecimalField(self):
|
|
|
|
lazy_func = lazy(lambda: Decimal("1.2"), Decimal)
|
|
|
|
self.assertIsInstance(DecimalField().get_prep_value(lazy_func()), Decimal)
|
|
|
|
|
|
|
|
def test_EmailField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "mailbox@domain.com", str)
|
|
|
|
self.assertIsInstance(EmailField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_FileField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "filename.ext", str)
|
|
|
|
self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_FilePathField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "tests.py", str)
|
|
|
|
self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_FloatField(self):
|
|
|
|
lazy_func = lazy(lambda: 1.2, float)
|
|
|
|
self.assertIsInstance(FloatField().get_prep_value(lazy_func()), float)
|
|
|
|
|
|
|
|
def test_ImageField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "filename.ext", str)
|
|
|
|
self.assertIsInstance(ImageField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_IntegerField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(IntegerField().get_prep_value(lazy_func()), int)
|
|
|
|
|
|
|
|
def test_IPAddressField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "127.0.0.1", str)
|
|
|
|
self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_GenericIPAddressField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "127.0.0.1", str)
|
|
|
|
self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_PositiveIntegerField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(PositiveIntegerField().get_prep_value(lazy_func()), int)
|
|
|
|
|
|
|
|
def test_PositiveSmallIntegerField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(
|
|
|
|
PositiveSmallIntegerField().get_prep_value(lazy_func()), int
|
|
|
|
)
|
|
|
|
|
2019-10-16 20:32:12 +08:00
|
|
|
def test_PositiveBigIntegerField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(
|
|
|
|
PositiveBigIntegerField().get_prep_value(lazy_func()), int
|
|
|
|
)
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
def test_SlugField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "slug", str)
|
|
|
|
self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_SmallIntegerField(self):
|
|
|
|
lazy_func = lazy(lambda: 1, int)
|
|
|
|
self.assertIsInstance(SmallIntegerField().get_prep_value(lazy_func()), int)
|
|
|
|
|
|
|
|
def test_TextField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "Abc", str)
|
|
|
|
self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
lazy_func = lazy(lambda: 0, int)
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
def test_TimeField(self):
|
|
|
|
lazy_func = lazy(lambda: datetime.datetime.now().time(), datetime.time)
|
|
|
|
self.assertIsInstance(TimeField().get_prep_value(lazy_func()), datetime.time)
|
|
|
|
|
|
|
|
def test_URLField(self):
|
2016-12-29 23:27:49 +08:00
|
|
|
lazy_func = lazy(lambda: "http://domain.com", str)
|
|
|
|
self.assertIsInstance(URLField().get_prep_value(lazy_func()), str)
|