Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion csv_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,21 @@ def compare(previous, current, show_unchanged=False):
result["removed"] = [previous[id] for id in removed]
if changed:
for id in changed:
diffs = list(diff(previous[id], current[id], ignore=ignore_columns))
previous_row = previous[id]
current_row = current[id]
if ignore_columns:
# Filter literal column names before dictdiffer interprets dots as paths.
previous_row = {
field: value
for field, value in previous_row.items()
if field not in ignore_columns
}
current_row = {
field: value
for field, value in current_row.items()
if field not in ignore_columns
}
diffs = list(diff(previous_row, current_row))
if diffs:
changes = {
"key": id,
Expand Down
36 changes: 36 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,42 @@ def test_column_containing_dot(tmpdir):
} == json.loads(result.output.strip())


@pytest.mark.parametrize(
"format_,previous,current",
[
(
"csv",
"id,name.date_range,name.title\n1,1979,Before\n",
"id,name..date_range,name.title\n1,1979,After\n",
),
(
"json",
'[{"id": "1", "name.date_range": null, "name.title": "Before"}]',
'[{"id": "1", "name..date_range": null, "name.title": "After"}]',
),
],
ids=["csv", "json"],
)
def test_dotted_columns_added_and_removed(tmp_path, format_, previous, current):
one = tmp_path / ("one." + format_)
two = tmp_path / ("two." + format_)
one.write_text(previous, encoding="utf-8")
two.write_text(current, encoding="utf-8")
result = CliRunner().invoke(
cli.cli,
[str(one), str(two), "--key", "id", "--json", "--format", format_],
catch_exceptions=False,
)
assert result.exit_code == 0, result.output
assert json.loads(result.output) == {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"name.title": ["Before", "After"]}}],
"columns_added": ["name..date_range"],
"columns_removed": ["name.date_range"],
}


def test_semicolon_delimited(tmpdir):
# https://github.com/simonw/csv-diff/issues/6
one = tmpdir / "one.csv"
Expand Down