Debian Codegraph @

tests/model_fields/test_promises.py

raw
1 import datetime
2 from decimal import Decimal
3
4 from django.db.models import (
5 AutoField,
6 BinaryField,
7 BooleanField,
8 CharField,
9 DateField,
10 DateTimeField,
11 DecimalField,
12 EmailField,
13 FileField,
14 FilePathField,
15 FloatField,
16 GenericIPAddressField,
17 ImageField,
18 IntegerField,
19 IPAddressField,
20 PositiveBigIntegerField,
21 PositiveIntegerField,
22 PositiveSmallIntegerField,
23 SlugField,
24 SmallIntegerField,
25 TextField,
26 TimeField,
27 URLField,
28 )
29 from django.test import SimpleTestCase
30 from django.utils.functional import lazy
31
32
33 class PromiseTest(SimpleTestCase):
34 def test_AutoField(self):
35 lazy_func = lazy(lambda: 1, int)
36 self.assertIsInstance(
37 AutoField(primary_key=True).get_prep_value(lazy_func()), int
38 )
39
40 def test_BinaryField(self):
41 lazy_func = lazy(lambda: b"", bytes)
42 self.assertIsInstance(BinaryField().get_prep_value(lazy_func()), bytes)
43
44 def test_BooleanField(self):
45 lazy_func = lazy(lambda: True, bool)
46 self.assertIsInstance(BooleanField().get_prep_value(lazy_func()), bool)
47
48 def test_CharField(self):
49 lazy_func = lazy(lambda: "", str)
50 self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
51 lazy_func = lazy(lambda: 0, int)
52 self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
53
54 def test_DateField(self):
55 lazy_func = lazy(lambda: datetime.date.today(), datetime.date)
56 self.assertIsInstance(DateField().get_prep_value(lazy_func()), datetime.date)
57
58 def test_DateTimeField(self):
59 lazy_func = lazy(lambda: datetime.datetime.now(), datetime.datetime)
60 self.assertIsInstance(
61 DateTimeField().get_prep_value(lazy_func()), datetime.datetime
62 )
63
64 def test_DecimalField(self):
65 lazy_func = lazy(lambda: Decimal("1.2"), Decimal)
66 self.assertIsInstance(DecimalField().get_prep_value(lazy_func()), Decimal)
67
68 def test_EmailField(self):
69 lazy_func = lazy(lambda: "[email protected]", str)
70 self.assertIsInstance(EmailField().get_prep_value(lazy_func()), str)
71
72 def test_FileField(self):
73 lazy_func = lazy(lambda: "filename.ext", str)
74 self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
75 lazy_func = lazy(lambda: 0, int)
76 self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
77
78 def test_FilePathField(self):
79 lazy_func = lazy(lambda: "tests.py", str)
80 self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
81 lazy_func = lazy(lambda: 0, int)
82 self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
83
84 def test_FloatField(self):
85 lazy_func = lazy(lambda: 1.2, float)
86 self.assertIsInstance(FloatField().get_prep_value(lazy_func()), float)
87
88 def test_ImageField(self):
89 lazy_func = lazy(lambda: "filename.ext", str)
90 self.assertIsInstance(ImageField().get_prep_value(lazy_func()), str)
91
92 def test_IntegerField(self):
93 lazy_func = lazy(lambda: 1, int)
94 self.assertIsInstance(IntegerField().get_prep_value(lazy_func()), int)
95
96 def test_IPAddressField(self):
97 lazy_func = lazy(lambda: "127.0.0.1", str)
98 self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
99 lazy_func = lazy(lambda: 0, int)
100 self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
101
102 def test_GenericIPAddressField(self):
103 lazy_func = lazy(lambda: "127.0.0.1", str)
104 self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
105 lazy_func = lazy(lambda: 0, int)
106 self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
107
108 def test_PositiveIntegerField(self):
109 lazy_func = lazy(lambda: 1, int)
110 self.assertIsInstance(PositiveIntegerField().get_prep_value(lazy_func()), int)
111
112 def test_PositiveSmallIntegerField(self):
113 lazy_func = lazy(lambda: 1, int)
114 self.assertIsInstance(
115 PositiveSmallIntegerField().get_prep_value(lazy_func()), int
116 )
117
118 def test_PositiveBigIntegerField(self):
119 lazy_func = lazy(lambda: 1, int)
120 self.assertIsInstance(
121 PositiveBigIntegerField().get_prep_value(lazy_func()), int
122 )
123
124 def test_SlugField(self):
125 lazy_func = lazy(lambda: "slug", str)
126 self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
127 lazy_func = lazy(lambda: 0, int)
128 self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
129
130 def test_SmallIntegerField(self):
131 lazy_func = lazy(lambda: 1, int)
132 self.assertIsInstance(SmallIntegerField().get_prep_value(lazy_func()), int)
133
134 def test_TextField(self):
135 lazy_func = lazy(lambda: "Abc", str)
136 self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
137 lazy_func = lazy(lambda: 0, int)
138 self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
139
140 def test_TimeField(self):
141 lazy_func = lazy(lambda: datetime.datetime.now().time(), datetime.time)
142 self.assertIsInstance(TimeField().get_prep_value(lazy_func()), datetime.time)
143
144 def test_URLField(self):
145 lazy_func = lazy(lambda: "http://domain.com", str)
146 self.assertIsInstance(URLField().get_prep_value(lazy_func()), str)