diff --git a/csv_diff/__init__.py b/csv_diff/__init__.py index 59a2eaf..c488ab3 100644 --- a/csv_diff/__init__.py +++ b/csv_diff/__init__.py @@ -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, diff --git a/tests/test_cli.py b/tests/test_cli.py index 12f4e12..e53b095 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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"