-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_cli.py
204 lines (154 loc) · 5.62 KB
/
test_cli.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Unit tests for cli
"""
from __future__ import annotations
import argparse
import pytest
from freezegun import freeze_time
from pypistats import cli
@pytest.mark.parametrize(
"test_input, expected",
[
("2018-07", ("2018-07-01", "2018-07-31")),
("2018-12", ("2018-12-01", "2018-12-31")),
],
)
def test__month(test_input: str, expected: str) -> None:
# Act
first, last = cli._month(test_input)
# Assert
assert expected == (first, last)
@pytest.mark.parametrize(
"test_input, expected",
[
("2018-01-25", ("2017-12-01", "2017-12-31")),
("2018-09-25", ("2018-08-01", "2018-08-31")),
("2018-12-25", ("2018-11-01", "2018-11-30")),
],
)
def test__last_month(test_input: str, expected: str) -> None:
# Act
with freeze_time(test_input):
first, last = cli._last_month()
# Assert
assert expected == (first, last)
@pytest.mark.parametrize(
"test_input, expected",
[
("2019-03-10", "2019-03-01"),
("2019-05-08", "2019-05-01"),
("2019-12-25", "2019-12-01"),
],
)
def test__this_month(test_input: str, expected: str) -> None:
# Act
with freeze_time(test_input):
first = cli._this_month()
# Assert
assert expected == first
@freeze_time("2019-05-08")
@pytest.mark.parametrize(
"name, date_format, expected",
[
("jan", "%b", "2019-01"),
("Jan", "%b", "2019-01"),
("january", "%B", "2019-01"),
("January", "%B", "2019-01"),
("feb", "%b", "2019-02"),
("february", "%B", "2019-02"),
("may", "%b", "2019-05"),
("dec", "%b", "2018-12"),
("december", "%B", "2018-12"),
],
)
def test__month_name_to_yyyy_mm(name: str, date_format: str, expected: str) -> None:
# Act
output = cli._month_name_to_yyyy_mm(name, date_format)
# Assert
assert expected == output
@pytest.mark.parametrize("test_input", ["2018-01-12", "2018-07-12", "2018-12-12"])
def test__valid_yyyy_mm_dd_valid(test_input: str) -> None:
assert test_input == cli._valid_yyyy_mm_dd(test_input)
@pytest.mark.parametrize("test_input", ["asdfsdssd", "2018-99-99", "2018-xx"])
def test__valid_yyyy_mm_dd_invalid(test_input: str) -> None:
with pytest.raises(argparse.ArgumentTypeError):
cli._valid_yyyy_mm_dd(test_input)
@pytest.mark.parametrize("test_input", ["2018-01", "2018-07", "2018-12"])
def test__valid_yyyy_mm_valid(test_input: str) -> None:
assert test_input == cli._valid_yyyy_mm(test_input)
@freeze_time("2019-05-08")
@pytest.mark.parametrize(
"test_input, expected",
[
("jan", "2019-01"),
("Jan", "2019-01"),
("january", "2019-01"),
("January", "2019-01"),
("feb", "2019-02"),
("february", "2019-02"),
("may", "2019-05"),
("dec", "2018-12"),
("december", "2018-12"),
],
)
def test__valid_yyyy_mm_valid_name(test_input: str, expected: str) -> None:
assert expected == cli._valid_yyyy_mm(test_input)
@pytest.mark.parametrize("test_input", ["dfkgjskfjgk", "2018-99", "2018-xx"])
def test__valid_yyyy_mm_invalid(test_input: str) -> None:
with pytest.raises(argparse.ArgumentTypeError):
cli._valid_yyyy_mm(test_input)
@pytest.mark.parametrize("test_input", ["2019-01-21", "2019-01"])
def test__valid_yyyy_mm_optional_dd_valid(test_input: str) -> None:
assert test_input == cli._valid_yyyy_mm_optional_dd(test_input)
@freeze_time("2019-05-08")
@pytest.mark.parametrize(
"test_input, expected", [("jan", "2019-01"), ("february", "2019-02")]
)
def test__valid_yyyy_mm_optional_dd_valid_name(test_input: str, expected: str) -> None:
assert expected == cli._valid_yyyy_mm_optional_dd(test_input)
@pytest.mark.parametrize("test_input", ["dkvnf", "2018-99", "2018-xx"])
def test__valid_yyyy_mm_optional_dd_invalid(test_input: str) -> None:
with pytest.raises(argparse.ArgumentTypeError):
cli._valid_yyyy_mm_optional_dd(test_input)
class __Args(argparse.Namespace):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.json = False # type: bool
self.format = "markdown" # type: str
@pytest.mark.parametrize("test_input, expected", [(False, "markdown"), (True, "json")])
def test__define_format_json_flag(test_input: bool, expected: str) -> None:
# Arrange
args = __Args()
args.json = test_input
# Act
_format = cli._define_format(args)
# Assert
assert expected == _format
@pytest.mark.parametrize("test_input", ["json", "markdown"])
def test__define_format_format_flag(test_input: str) -> None:
# Arrange
args = __Args()
args.json = False
args.format = test_input
# Act
_format = cli._define_format(args)
# Assert
assert test_input == _format
@pytest.mark.parametrize("test_input", ["2", "3", "4"])
def test__python_major_version_valid(test_input: str) -> None:
# Act / Assert
assert cli._python_major_version(test_input) == test_input
@pytest.mark.parametrize("test_input", ["2.7", "3.9", "3.11", "-5", "pillow"])
def test__python_major_version_invalid(test_input) -> None:
# Act / Assert
with pytest.raises(argparse.ArgumentTypeError):
cli._python_major_version(test_input)
@pytest.mark.parametrize("test_input", ["2.7", "3.9", "3.11"])
def test__python_minor_version_valid(test_input: str) -> None:
# Act / Assert
assert cli._python_minor_version(test_input) == test_input
@pytest.mark.parametrize("test_input", ["2", "3", "4", "-5", "pillow"])
def test__python_minor_version_invalid(test_input: str) -> None:
# Act / Assert
with pytest.raises(argparse.ArgumentTypeError):
cli._python_minor_version(test_input)