-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_readme_examples.py
135 lines (114 loc) · 5.58 KB
/
test_readme_examples.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
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
import datetime
from typing import Callable
import pytest
def test_readme_example_skynet() -> None:
from utcnow import utcnow
tz_EDT = datetime.timezone(offset=datetime.timedelta(hours=-4))
dt = datetime.datetime(1997, 8, 4, 2, 14, tzinfo=tz_EDT)
result = utcnow.as_string(dt)
expected_str = "1997-08-04T06:14:00.000000Z"
expected_dt_str = dt.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
assert result == expected_str
assert result == expected_dt_str
assert result == utcnow(result)
assert result == utcnow(dt.strftime("%Y-%m-%dT%H:%M:%S.%f%z"))
assert result == utcnow(dt.strftime("%Y-%m-%d %H:%M:%S.%f%z"))
assert result == utcnow(dt.isoformat())
def test_readme_example_birthday() -> None:
from utcnow import utcnow
result = utcnow.as_datetime("1984-08-01T13:38:00.123450Z")
expected_dt = datetime.datetime(1984, 8, 1, 13, 38, 0, 123450, tzinfo=datetime.timezone.utc)
assert result == expected_dt
def test_readme_simple_transform() -> None:
from utcnow import utcnow
dt = datetime.datetime(1984, 8, 1, 13, 38, 0, 4711)
result = utcnow.as_string(dt)
expected_str = "1984-08-01T13:38:00.004711Z"
assert result == expected_str
assert result == utcnow(dt)
assert result == dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
assert result == dt.isoformat() + "Z"
assert result == utcnow(result)
assert result == utcnow(dt.isoformat())
assert result == utcnow(dt.isoformat() + "+00:00")
assert result == utcnow(dt.isoformat() + "-00:00")
assert result == utcnow(dt.isoformat() + " UTC")
assert result == utcnow(dt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z"))
assert result == utcnow(dt.replace(tzinfo=datetime.timezone.utc).isoformat())
@pytest.mark.parametrize(
"dt, expected_str_func",
[
(datetime.datetime.utcnow(), lambda dt: dt.isoformat() + "Z"),
(datetime.datetime.utcnow(), lambda dt: dt.isoformat(timespec="microseconds") + "Z"),
(datetime.datetime.now(datetime.timezone.utc), lambda dt: dt.isoformat().replace("+00:00", "Z")),
(
datetime.datetime.now(datetime.timezone.utc),
lambda dt: dt.isoformat(timespec="microseconds").replace("+00:00", "Z"),
),
(
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
lambda dt: dt.isoformat() + "Z",
),
(
datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
lambda dt: dt.isoformat(timespec="microseconds") + "Z",
),
(
datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=2))),
lambda dt: dt.astimezone(datetime.timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z"),
),
],
)
def test_readme_datetime_complement(
dt: datetime.datetime, expected_str_func: Callable[[datetime.datetime], str]
) -> None:
import utcnow
expected_str = expected_str_func(dt)
assert len(expected_str) == 27
assert expected_str[0] == "2"
assert expected_str[-1] == "Z"
assert expected_str.count("-") == 2
assert expected_str.count(":") == 2
assert expected_str.count("T") == 1
assert expected_str.count("Z") == 1
assert expected_str == datetime.datetime.fromtimestamp(
dt.replace(tzinfo=dt.tzinfo or datetime.timezone.utc).astimezone(datetime.timezone.utc).timestamp(),
tz=datetime.timezone.utc,
).isoformat(timespec="microseconds").replace("+00:00", "Z")
assert (
expected_str
== datetime.datetime.fromtimestamp(
dt.replace(tzinfo=dt.tzinfo or datetime.timezone.utc).astimezone(datetime.timezone.utc).timestamp(),
tz=datetime.timezone.utc,
)
.replace(tzinfo=None)
.isoformat(timespec="microseconds")
+ "Z"
)
assert expected_str == utcnow.utcnow(dt)
assert expected_str == utcnow.utcnow(dt)
assert expected_str == utcnow(dt) # type: ignore
assert expected_str == utcnow(dt) # type: ignore
assert expected_str == utcnow.as_string(dt)
assert expected_str == utcnow.utcnow.as_string(dt)
assert expected_str == utcnow.as_datetime(dt).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
assert expected_str == utcnow.as_datetime(dt).replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
assert expected_str == utcnow.as_datetime(dt).replace(tzinfo=None).isoformat() + "Z"
assert expected_str != utcnow.as_datetime(dt).isoformat() + "Z"
assert expected_str != dt.replace(tzinfo=datetime.timezone.utc).isoformat() + "Z"
assert expected_str != dt.astimezone(datetime.timezone.utc).isoformat() + "Z"
assert expected_str != dt.astimezone(datetime.timezone.utc).isoformat()
assert expected_str != dt.replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%S.%f%z")
assert expected_str != dt.replace(tzinfo=datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z")
assert expected_str != dt.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z")
# '2021-02-18T08:24:48.382262Z'
# same thing can be accomplished using datetime and all of these calls returns the same str value:
# 1. utcnow.utcnow()
# 2. str(utcnow)
# 3. str(utcnow.utcnow)
# 4. utcnow.as_string()
# 5. utcnow.utcnow.as_string()
# 6. datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
# 7. datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
# 8. datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
# 9. datetime.datetime.utcnow().isoformat(timespec="microseconds") + "Z"