-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtest_cli.py
302 lines (266 loc) · 7.02 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from click.testing import CliRunner
from csv_diff import cli, load_csv
import csv
import pytest
from .test_csv_diff import ONE, ONE_TSV, TWO, TWO_TSV, THREE, FIVE
import io
import json
from textwrap import dedent
@pytest.fixture
def tsv_files(tmpdir):
one = tmpdir / "one.tsv"
one.write(ONE_TSV)
two = tmpdir / "two.tsv"
two.write(TWO_TSV)
return str(one), str(two)
@pytest.fixture
def json_files(tmpdir):
one = tmpdir / "one.json"
one.write(
json.dumps(
[
{"id": 1, "name": "Cleo", "nested": {"foo": 3}, "extra": 1},
{"id": 2, "name": "Pancakes", "nested": {"foo": 3}},
]
)
)
two = tmpdir / "two.json"
two.write(
json.dumps(
[
{"id": 1, "name": "Cleo", "nested": {"foo": 3, "bar": 5}, "extra": 1},
{"id": 2, "name": "Pancakes!", "nested": {"foo": 3}, "extra": 1},
]
)
)
return str(one), str(two)
def test_human_cli(tmpdir):
one = tmpdir / "one.csv"
one.write(ONE)
two = tmpdir / "two.csv"
two.write(TWO)
result = CliRunner().invoke(cli.cli, [str(one), str(two), "--key", "id"])
assert 0 == result.exit_code
assert (
dedent(
"""
1 row changed
id: 1
age: "4" => "5"
"""
).strip()
== result.output.strip()
)
def test_human_cli_alternative_names(tmpdir):
one = tmpdir / "one.csv"
one.write(ONE)
five = tmpdir / "five.csv"
five.write(FIVE)
result = CliRunner().invoke(
cli.cli,
[str(one), str(five), "--key", "id", "--singular", "tree", "--plural", "trees"],
)
assert 0 == result.exit_code, result.output
assert (
dedent(
"""
1 tree changed, 2 trees added
1 tree changed
id: 1
age: "4" => "5"
2 trees added
id: 3
name: Bailey
age: 1
id: 4
name: Carl
age: 7
"""
).strip()
== result.output.strip()
)
def test_human_cli_json(tmpdir):
one = tmpdir / "one.csv"
one.write(ONE)
two = tmpdir / "two.csv"
two.write(TWO)
result = CliRunner().invoke(cli.cli, [str(one), str(two), "--key", "id", "--json"])
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"age": ["4", "5"]}}],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_tsv_files(tsv_files):
one, two = tsv_files
result = CliRunner().invoke(
cli.cli, [one, two, "--key", "id", "--json", "--format", "tsv"]
)
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"age": ["4", "5"]}}],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_json_files(json_files):
one, two = json_files
result = CliRunner().invoke(
cli.cli,
[one, two, "--key", "id", "--json", "--format", "json"],
catch_exceptions=False,
)
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [
{"key": 1, "changes": {"nested": ['{"foo": 3}', '{"foo": 3, "bar": 5}']}},
{
"key": 2,
"changes": {"name": ["Pancakes", "Pancakes!"], "extra": [None, 1]},
},
],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_sniff_format(tsv_files):
one, two = tsv_files
result = CliRunner().invoke(cli.cli, [one, two, "--key", "id", "--json"])
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"age": ["4", "5"]}}],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_format_overrides_sniff(tsv_files):
one, two = tsv_files
result = CliRunner().invoke(
cli.cli, [one, two, "--key", "id", "--json", "--format", "csv"]
)
assert 1 == result.exit_code
def test_column_containing_dot(tmpdir):
# https://github.com/simonw/csv-diff/issues/7
one = tmpdir / "one.csv"
two = tmpdir / "two.csv"
one.write(
dedent(
"""
id,foo.bar,foo.baz
1,Dog,Cat
"""
).strip()
)
two.write(
dedent(
"""
id,foo.bar,foo.baz
1,Dog,Beaver
"""
).strip()
)
result = CliRunner().invoke(
cli.cli, [str(one), str(two), "--key", "id", "--json"], catch_exceptions=False
)
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"foo.baz": ["Cat", "Beaver"]}}],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_semicolon_delimited(tmpdir):
# https://github.com/simonw/csv-diff/issues/6
one = tmpdir / "one.csv"
two = tmpdir / "two.csv"
one.write(
dedent(
"""
id;name
1;Mark
"""
).strip()
)
two.write(
dedent(
"""
id;name
1;Brian
"""
).strip()
)
result = CliRunner().invoke(
cli.cli, [str(one), str(two), "--key", "id", "--json"], catch_exceptions=False
)
assert 0 == result.exit_code
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"name": ["Mark", "Brian"]}}],
"columns_added": [],
"columns_removed": [],
} == json.loads(result.output.strip())
def test_diff_with_extras(tmpdir):
one = tmpdir / "one.json"
two = tmpdir / "two.json"
one.write(
json.dumps(
[
{"id": 1, "name": "Cleo", "type": "dog"},
{"id": 2, "name": "Suna", "type": "chicken"},
]
)
)
two.write(
json.dumps(
[
{"id": 2, "name": "Suna", "type": "pretty chicken"},
{"id": 3, "name": "Artie", "type": "bunny"},
]
)
)
result = CliRunner().invoke(
cli.cli,
[
str(one),
str(two),
"--key",
"id",
"--format",
"json",
"--extra",
"search",
"https://www.google.com/search?q={name}",
],
catch_exceptions=False,
)
assert result.exit_code == 0
expected = dedent(
"""
1 row changed, 1 row added, 1 row removed
1 row changed
id: 2
type: "chicken" => "pretty chicken"
extras:
search: https://www.google.com/search?q=Suna
1 row added
id: 3
name: Artie
type: bunny
extras:
search: https://www.google.com/search?q=Artie
1 row removed
id: 1
name: Cleo
type: dog
extras:
search: https://www.google.com/search?q=Cleo
"""
).strip()
assert result.output.strip() == expected