Skip to content

Commit aff1a32

Browse files
committed
Display human-readable added/removed rows not using JSON
1 parent 34c2d29 commit aff1a32

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

csv_diff/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def human_text(result):
7373
if show_headers:
7474
summary.append(fragment + "\n")
7575
for row in result["added"]:
76-
summary.append(" {}".format(json.dumps(row)))
76+
summary.append(human_row(row, prefix=" "))
77+
summary.append("")
7778
summary.append("")
7879
if result["removed"]:
7980
fragment = "{} row{} removed".format(
@@ -83,6 +84,14 @@ def human_text(result):
8384
if show_headers:
8485
summary.append(fragment + "\n")
8586
for row in result["removed"]:
86-
summary.append(" {}".format(json.dumps(row)))
87+
summary.append(human_row(row, prefix=" "))
88+
summary.append("")
8789
summary.append("")
8890
return (", ".join(title) + "\n\n" + ("\n".join(summary))).strip()
91+
92+
93+
def human_row(row, prefix=""):
94+
bits = []
95+
for key, value in row.items():
96+
bits.append("{}{}: {}".format(prefix, key, value))
97+
return "\n".join(bits)

tests/test_human_text.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from csv_diff import load_csv, compare, human_text
22
from .test_csv_diff import ONE, TWO, THREE, FOUR
33

4+
FIVE = """id,name,age
5+
1,Cleo,5
6+
2,Pancakes,2,
7+
3,Bailey,1
8+
4,Carl,7"""
9+
410
from textwrap import dedent
511
import io
612

@@ -24,7 +30,30 @@ def test_row_added():
2430
assert dedent("""
2531
1 row added
2632
27-
{"id": "2", "name": "Pancakes", "age": "2"}
33+
id: 2
34+
name: Pancakes
35+
age: 2
36+
""").strip() == human_text(diff)
37+
38+
39+
def test_rows_added():
40+
diff = compare(
41+
load_csv(io.StringIO(THREE), key="id"), load_csv(io.StringIO(FIVE), key="id")
42+
)
43+
assert dedent("""
44+
3 rows added
45+
46+
id: 2
47+
name: Pancakes
48+
age: 2
49+
50+
id: 3
51+
name: Bailey
52+
age: 1
53+
54+
id: 4
55+
name: Carl
56+
age: 7
2857
""").strip() == human_text(diff)
2958

3059

@@ -35,7 +64,9 @@ def test_row_removed():
3564
assert dedent("""
3665
1 row removed
3766
38-
{"id": "2", "name": "Pancakes", "age": "2"}
67+
id: 2
68+
name: Pancakes
69+
age: 2
3970
""").strip() == human_text(diff)
4071

4172

@@ -54,5 +85,7 @@ def test_row_changed_and_row_added():
5485
5586
1 row added
5687
57-
{"id": "3", "name": "Bailey", "age": "1"}
88+
id: 3
89+
name: Bailey
90+
age: 1
5891
""").strip() == human_text(diff)

0 commit comments

Comments
 (0)