File: test_utils.py

package info (click to toggle)
python-django-constance 4.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 800 kB
  • sloc: python: 2,089; makefile: 25; javascript: 23; sh: 6
file content (95 lines) | stat: -rw-r--r-- 3,197 bytes parent folder | download
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
import datetime
from decimal import Decimal

from django.core.exceptions import ValidationError
from django.test import TestCase

from constance.management.commands.constance import _set_constance_value
from constance.utils import get_values
from constance.utils import get_values_for_keys


class UtilsTestCase(TestCase):
    def test_set_value_validation(self):
        self.assertRaisesMessage(ValidationError, "Enter a whole number.", _set_constance_value, "INT_VALUE", "foo")
        self.assertRaisesMessage(
            ValidationError,
            "Enter a valid email address.",
            _set_constance_value,
            "EMAIL_VALUE",
            "not a valid email",
        )
        self.assertRaisesMessage(
            ValidationError,
            "Enter a valid date.",
            _set_constance_value,
            "DATETIME_VALUE",
            (
                "2000-00-00",
                "99:99:99",
            ),
        )
        self.assertRaisesMessage(
            ValidationError,
            "Enter a valid time.",
            _set_constance_value,
            "DATETIME_VALUE",
            (
                "2016-01-01",
                "99:99:99",
            ),
        )

    def test_get_values(self):
        self.assertEqual(
            get_values(),
            {
                "FLOAT_VALUE": 3.1415926536,
                "BOOL_VALUE": True,
                "EMAIL_VALUE": "test@example.com",
                "INT_VALUE": 1,
                "CHOICE_VALUE": "yes",
                "TIME_VALUE": datetime.time(23, 59, 59),
                "DATE_VALUE": datetime.date(2010, 12, 24),
                "TIMEDELTA_VALUE": datetime.timedelta(days=1, hours=2, minutes=3),
                "LINEBREAK_VALUE": "Spam spam",
                "DECIMAL_VALUE": Decimal("0.1"),
                "STRING_VALUE": "Hello world",
                "DATETIME_VALUE": datetime.datetime(2010, 8, 23, 11, 29, 24),
                "LIST_VALUE": [1, "1", datetime.date(2019, 1, 1)],
                "JSON_VALUE": {
                    "key": "value",
                    "key2": 2,
                    "key3": [1, 2, 3],
                    "key4": {"key": "value"},
                    "key5": datetime.date(2019, 1, 1),
                    "key6": None,
                },
            },
        )

    def test_get_values_for_keys(self):
        self.assertEqual(
            get_values_for_keys(["BOOL_VALUE", "CHOICE_VALUE", "LINEBREAK_VALUE"]),
            {
                "BOOL_VALUE": True,
                "CHOICE_VALUE": "yes",
                "LINEBREAK_VALUE": "Spam spam",
            },
        )

    def test_get_values_for_keys_empty_keys(self):
        result = get_values_for_keys([])
        self.assertEqual(result, {})

    def test_get_values_for_keys_throw_error_if_no_key(self):
        self.assertRaisesMessage(
            AttributeError,
            '"OLD_VALUE, BOLD_VALUE" keys not found in configuration.',
            get_values_for_keys,
            ["BOOL_VALUE", "OLD_VALUE", "BOLD_VALUE"],
        )

    def test_get_values_for_keys_invalid_input_type(self):
        with self.assertRaises(TypeError):
            get_values_for_keys("key1")