Skip to content

Commit 9574395

Browse files
committed
Fix for bug with columns containing a ., closes #7
1 parent e260edc commit 9574395

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

csv_diff/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ def compare(previous, current, show_unchanged=False):
5757
result["removed"] = [previous[id] for id in removed]
5858
if changed:
5959
for id in changed:
60-
d = list(diff(previous[id], current[id], ignore=ignore_columns))
61-
if d:
60+
diffs = list(diff(previous[id], current[id], ignore=ignore_columns))
61+
if diffs:
6262
changes = {
6363
"key": id,
6464
"changes": {
65-
field: [prev_value, current_value]
66-
for _, field, (prev_value, current_value) in d
65+
# field can be a list if id contained '.' - #7
66+
field[0]
67+
if isinstance(field, list)
68+
else field: [prev_value, current_value]
69+
for _, field, (prev_value, current_value) in diffs
6770
},
6871
}
6972
if show_unchanged:

tests/test_cli.py

+33
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,36 @@ def test_format_overrides_sniff(tsv_files):
120120
cli.cli, [one, two, "--key", "id", "--json", "--format", "csv"]
121121
)
122122
assert 1 == result.exit_code
123+
124+
125+
def test_column_containing_dot(tmpdir):
126+
# https://github.com/simonw/csv-diff/issues/7
127+
one = tmpdir / "one.csv"
128+
two = tmpdir / "two.csv"
129+
one.write(
130+
dedent(
131+
"""
132+
id,foo.bar,foo.baz
133+
1,Dog,Cat
134+
"""
135+
).strip()
136+
)
137+
two.write(
138+
dedent(
139+
"""
140+
id,foo.bar,foo.baz
141+
1,Dog,Beaver
142+
"""
143+
).strip()
144+
)
145+
result = CliRunner().invoke(
146+
cli.cli, [str(one), str(two), "--key", "id", "--json"], catch_exceptions=False
147+
)
148+
assert 0 == result.exit_code
149+
assert {
150+
"added": [],
151+
"removed": [],
152+
"changed": [{"key": "1", "changes": {"foo.baz": ["Cat", "Beaver"]}}],
153+
"columns_added": [],
154+
"columns_removed": [],
155+
} == json.loads(result.output.strip())

0 commit comments

Comments
 (0)