File: test_promises.py

package info (click to toggle)
python-django 1%3A1.11.29-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,428 kB
  • sloc: python: 220,776; javascript: 13,523; makefile: 209; xml: 201; sh: 64
file content (138 lines) | stat: -rw-r--r-- 6,342 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from __future__ import unicode_literals

import datetime
import unittest
from decimal import Decimal

from django.db.models.fields import (
    AutoField, BigIntegerField, BinaryField, BooleanField, CharField,
    CommaSeparatedIntegerField, DateField, DateTimeField, DecimalField,
    EmailField, FilePathField, FloatField, GenericIPAddressField, IntegerField,
    IPAddressField, NullBooleanField, PositiveIntegerField,
    PositiveSmallIntegerField, SlugField, SmallIntegerField, TextField,
    TimeField, URLField,
)
from django.db.models.fields.files import FileField, ImageField
from django.test import SimpleTestCase
from django.utils import six
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)

    @unittest.skipIf(six.PY3, 'Python 3 has no `long` type.')
    def test_BigIntegerField(self):
        lazy_func = lazy(lambda: long(9999999999999999999), long)  # NOQA: long undefined on PY3
        self.assertIsInstance(BigIntegerField().get_prep_value(lazy_func()), long)  # NOQA

    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):
        lazy_func = lazy(lambda: '', six.text_type)
        self.assertIsInstance(CharField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(CharField().get_prep_value(lazy_func()), six.text_type)

    def test_CommaSeparatedIntegerField(self):
        lazy_func = lazy(lambda: '1,2', six.text_type)
        self.assertIsInstance(CommaSeparatedIntegerField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(CommaSeparatedIntegerField().get_prep_value(lazy_func()), six.text_type)

    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):
        lazy_func = lazy(lambda: 'mailbox@domain.com', six.text_type)
        self.assertIsInstance(EmailField().get_prep_value(lazy_func()), six.text_type)

    def test_FileField(self):
        lazy_func = lazy(lambda: 'filename.ext', six.text_type)
        self.assertIsInstance(FileField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(FileField().get_prep_value(lazy_func()), six.text_type)

    def test_FilePathField(self):
        lazy_func = lazy(lambda: 'tests.py', six.text_type)
        self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), six.text_type)

    def test_FloatField(self):
        lazy_func = lazy(lambda: 1.2, float)
        self.assertIsInstance(FloatField().get_prep_value(lazy_func()), float)

    def test_ImageField(self):
        lazy_func = lazy(lambda: 'filename.ext', six.text_type)
        self.assertIsInstance(ImageField().get_prep_value(lazy_func()), six.text_type)

    def test_IntegerField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(IntegerField().get_prep_value(lazy_func()), int)

    def test_IPAddressField(self):
        lazy_func = lazy(lambda: '127.0.0.1', six.text_type)
        self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), six.text_type)

    def test_GenericIPAddressField(self):
        lazy_func = lazy(lambda: '127.0.0.1', six.text_type)
        self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), six.text_type)

    def test_NullBooleanField(self):
        lazy_func = lazy(lambda: True, bool)
        self.assertIsInstance(NullBooleanField().get_prep_value(lazy_func()), bool)

    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)

    def test_SlugField(self):
        lazy_func = lazy(lambda: 'slug', six.text_type)
        self.assertIsInstance(SlugField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(SlugField().get_prep_value(lazy_func()), six.text_type)

    def test_SmallIntegerField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(SmallIntegerField().get_prep_value(lazy_func()), int)

    def test_TextField(self):
        lazy_func = lazy(lambda: 'Abc', six.text_type)
        self.assertIsInstance(TextField().get_prep_value(lazy_func()), six.text_type)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(TextField().get_prep_value(lazy_func()), six.text_type)

    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):
        lazy_func = lazy(lambda: 'http://domain.com', six.text_type)
        self.assertIsInstance(URLField().get_prep_value(lazy_func()), six.text_type)