Skip to content

Commit 09409e3

Browse files
MainHanzosimonw
authored andcommitted
Fix for error with no key provided, closes #3
1 parent 9574395 commit 09409e3

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

csv_diff/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def load_csv(fp, key=None, dialect=None):
2020
if key:
2121
keyfn = lambda r: r[key]
2222
else:
23-
keyfn = lambda r: hashlib.sha1(json.dumps(r, sort_keys=True).encode("utf8"))
23+
keyfn = lambda r: hashlib.sha1(
24+
json.dumps(r, sort_keys=True).encode("utf8")
25+
).hexdigest()
2426
return {keyfn(r): r for r in rows}
2527

2628

tests/test_csv_diff.py

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
3,Bailee,1,100
4444
4,Bob,7,422"""
4545

46+
NINE = """id,name,age
47+
1,Cleo,5
48+
2,Pancakes,4"""
49+
50+
TEN = """id,name,age
51+
1,Cleo,5
52+
2,Pancakes,3"""
53+
4654

4755
def test_row_changed():
4856
diff = compare(

tests/test_human_text.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from csv_diff import load_csv, compare, human_text
2-
from .test_csv_diff import ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT
2+
from .test_csv_diff import ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN
33
from textwrap import dedent
44
import io
55

@@ -198,3 +198,27 @@ def test_columns_and_rows_changed():
198198
).strip()
199199
== human_text(diff, "id")
200200
)
201+
202+
203+
def test_no_key():
204+
diff = compare(load_csv(io.StringIO(NINE)), load_csv(io.StringIO(TEN)))
205+
assert (
206+
dedent(
207+
"""
208+
1 row added, 1 row removed
209+
210+
1 row added
211+
212+
id: 2
213+
name: Pancakes
214+
age: 3
215+
216+
1 row removed
217+
218+
id: 2
219+
name: Pancakes
220+
age: 4
221+
"""
222+
).strip()
223+
== human_text(diff)
224+
)

0 commit comments

Comments
 (0)