-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_string_datetime.py
103 lines (91 loc) · 2.61 KB
/
test_string_datetime.py
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
import datetime
import pytest
from json_to_models.dynamic_typing import (BooleanString, FloatString, IntString, IsoDateString, IsoDatetimeString,
IsoTimeString, register_datetime_classes)
from json_to_models.generator import MetadataGenerator
register_datetime_classes()
test_detect_type_data = [
# Check that string datetime doesn't break default string types
pytest.param(
"1",
IntString,
id="default_check_int"
),
pytest.param(
"1.5",
FloatString,
id="default_check_float"
),
pytest.param(
"true",
BooleanString,
id="bool"
),
pytest.param(
"2018-12-31",
IsoDateString,
id="date"
),
pytest.param(
"12:58",
IsoTimeString,
id="time"
),
pytest.param(
"2018-12-31T12:58:12Z",
IsoDatetimeString,
id="datetime"
)
]
@pytest.mark.parametrize("value,expected", test_detect_type_data)
def test_detect_type(models_generator: MetadataGenerator, value, expected):
result = models_generator._detect_type(value)
assert result == expected
test_parse_data = [
pytest.param(
"true",
BooleanString(True),
id="bool"
),
pytest.param(
"2018-12-31",
IsoDateString(2018, 12, 31),
id="date"
),
pytest.param(
"12:13",
IsoTimeString(12, 13),
id="time"
),
pytest.param(
"04:15:34",
IsoTimeString(4, 15, 34),
id="time_seconds"
),
pytest.param(
"04:15:34.034",
IsoTimeString(4, 15, 34, 34000),
id="time_ms"
),
pytest.param(
"2018-12-04T04:15:34.034000+00:00",
IsoDatetimeString(2018, 12, 4, 4, 15, 34, 34000, tzinfo=datetime.timezone.utc),
id="datetime_full"
),
pytest.param(
"2018-12-04T04:15",
IsoDatetimeString(2018, 12, 4, 4, 15),
id="datetime_partial"
)
]
@pytest.mark.parametrize("value,expected", test_parse_data)
def test_parse(models_generator: MetadataGenerator, value, expected):
cls = models_generator._detect_type(value)
result = cls.to_internal_value(value)
assert result == expected
assert value in result.to_representation()
def test_replace():
assert IsoTimeString(14, 12, 57).replace(minute=58, second=32) == IsoTimeString(14, 58, 32)
assert IsoDateString(2014, 12, 5).replace(day=4, month=5) == IsoDateString(2014, 5, 4)
assert IsoDatetimeString(2014, 12, 5, 14, 12, 57).replace(minute=58, second=32, day=4, month=5) \
== IsoDatetimeString(2014, 5, 4, 14, 58, 32)