-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_cli_run.py
More file actions
193 lines (158 loc) · 5.23 KB
/
test_cli_run.py
File metadata and controls
193 lines (158 loc) · 5.23 KB
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
import pathlib
import os
import pytest
from opencage.command_line import main
# NOTE: Testing keys https://opencagedata.com/api#testingkeys
TEST_APIKEY_200 = '6d0e711d72d74daeb2b0bfd2a5cdfdba' # always returns same address
TEST_APIKEY_401 = '11111111111111111111111111111111' # invalid key
@pytest.fixture(autouse=True)
def around():
yield
try:
pathlib.Path("test/fixtures/cli/output.csv").unlink()
except FileNotFoundError:
pass
def assert_output(path, length, lines):
assert pathlib.Path(path).exists()
with open(path, "r", encoding="utf-8") as f:
actual = f.readlines()
# print(actual, file=sys.stderr)
assert len(actual) == length
for i, expected in enumerate(lines):
assert actual[i].strip() == expected
def test_forward():
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward.csv",
"--output", "test/fixtures/cli/output.csv",
"--input-columns", "2,3,4",
"--add-columns", "country_code,country,postcode,city"
])
assert_output(
path="test/fixtures/cli/output.csv",
length=3,
lines=['Rathausmarkt 1,Hamburg,20095,Germany,de,Germany,48153,Münster']
)
def test_reverse():
main([
"reverse",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/reverse.csv",
"--output", "test/fixtures/cli/output.csv",
"--add-columns", "country_code,country,postcode"
])
assert_output(
path="test/fixtures/cli/output.csv",
length=1,
lines=['51.9526622,7.6324709,de,Germany,48153']
)
def test_headers():
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward_with_headers.csv",
"--output", "test/fixtures/cli/output.csv",
"--input-columns", "1,2,3,4",
"--headers",
"--add-columns", "lat,lng,postcode"
])
assert_output(
path="test/fixtures/cli/output.csv",
length=4,
lines=[
'street and number,town,postcode,country,lat,lng,postcode',
'Rathausmarkt 1,Hamburg,20095,Germany,51.9526622,7.6324709,48153'
]
)
def test_input_errors(capfd):
main([
"reverse",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/reverse_with_errors.csv",
"--output", "test/fixtures/cli/output.csv",
"--add-columns", "country_code,postcode",
"--no-progress"
])
_, err = capfd.readouterr()
# assert err == ''
assert err.count("\n") == 7
assert "Line 1 - Missing input column 2 in ['50.101010']" in err
assert "Line 1 - Expected two comma-separated values for reverse geocoding, got ['50.101010']" in err
assert "Line 3 - Empty line" in err
assert "Line 3 - Missing input column 2 in ['']" in err
assert "Line 3 - Expected two comma-separated values for reverse geocoding, got ['']" in err
assert "Line 4 - Does not look like latitude and longitude: 'a' and 'b'" in err
assert_output(
path="test/fixtures/cli/output.csv",
length=4,
lines=[
'50.101010,,',
'-100,60.1,,',
',,',
'a,b,,'
]
)
def test_empty_result():
# 'NOWHERE-INTERESTING' is guaranteed to return no result
# https://opencagedata.com/api#testingkeys
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward_noresult.csv",
"--output", "test/fixtures/cli/output.csv",
"--input-columns", "2",
"--headers",
"--verbose",
"--add-columns", "lat,lng,postcode"
])
assert_output(
path="test/fixtures/cli/output.csv",
length=2,
lines=[
'id,full_address,lat,lng,postcode',
'123,NOWHERE-INTERESTING,,,'
]
)
def test_invalid_api_key(capfd):
main([
"forward",
"--api-key", TEST_APIKEY_401,
"--input", "test/fixtures/cli/forward_with_headers.csv",
"--output", "test/fixtures/cli/output.csv"
])
_, err = capfd.readouterr()
assert 'Your API key is not authorized' in err
def test_dryrun(capfd):
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward_with_headers.csv",
"--output", "test/fixtures/cli/output.csv",
"--dry-run"
])
assert not os.path.isfile("test/fixtures/cli/output.csv")
out, _ = capfd.readouterr()
assert out.count("\n") == 1
assert "All good." in out
def test_invalid_domain(capfd):
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward.csv",
"--output", "test/fixtures/cli/output.csv",
"--api-domain", "invalid73585348.opencagedata.com"
])
_, err = capfd.readouterr()
assert 'Cannot connect to host' in err
# with dry-run no request will be made
main([
"forward",
"--api-key", TEST_APIKEY_200,
"--input", "test/fixtures/cli/forward.csv",
"--output", "test/fixtures/cli/output.csv",
"--api-domain", "invalid73585348.opencagedata.com",
"--dry-run"
])
_, err = capfd.readouterr()
assert err == ''