-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathtest_csv_diff.py
More file actions
142 lines (117 loc) · 2.86 KB
/
test_csv_diff.py
File metadata and controls
142 lines (117 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from csv_diff import load_csv, compare
import io
ONE = """id,name,age
1,Cleo,4
2,Pancakes,2"""
ONE_TSV = """id\tname\tage
1\tCleo\t4
2\tPancakes\t2"""
TWO = """id,name,age
1,Cleo,5
2,Pancakes,2"""
TWO_TSV = """id\tname\tage
1\tCleo\t5
2\tPancakes\t2"""
THREE = """id,name,age
1,Cleo,5"""
FOUR = """id,name,age
1,Cleo,5
2,Pancakes,2,
3,Bailey,1"""
FIVE = """id,name,age
1,Cleo,5
2,Pancakes,2,
3,Bailey,1
4,Carl,7"""
SIX = """id,name,age
1,Cleo,5
3,Bailey,1"""
SEVEN = """id,name,weight
1,Cleo,48
3,Bailey,20"""
EIGHT = """id,name,age,length
3,Bailee,1,100
4,Bob,7,422"""
NINE = """id,name,age
1,Cleo,5
2,Pancakes,4"""
TEN = """id,name,age
1,Cleo,5
2,Pancakes,3"""
ELEVEN = """name, state, age
Ann, UT, 56
Ann, NY, 24
Lisa, CA, 35
Bill, FL, 33
Bill, WY, 23"""
TWELVE = """name, state, age
Ann, UT, 45
Ann, NY, 24
Lisa, CA, 35
Bill, WY, 23"""
def test_row_changed():
diff = compare(
load_csv(io.StringIO(ONE), key="id"), load_csv(io.StringIO(TWO), key="id")
)
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"age": ["4", "5"]}}],
"columns_added": [],
"columns_removed": [],
} == diff
def test_row_added():
diff = compare(
load_csv(io.StringIO(THREE), key="id"), load_csv(io.StringIO(TWO), key="id")
)
assert {
"changed": [],
"removed": [],
"added": [{"age": "2", "id": "2", "name": "Pancakes"}],
"columns_added": [],
"columns_removed": [],
} == diff
def test_row_removed():
diff = compare(
load_csv(io.StringIO(TWO), key="id"), load_csv(io.StringIO(THREE), key="id")
)
assert {
"changed": [],
"removed": [{"age": "2", "id": "2", "name": "Pancakes"}],
"added": [],
"columns_added": [],
"columns_removed": [],
} == diff
def test_columns_changed():
diff = compare(
load_csv(io.StringIO(SIX), key="id"), load_csv(io.StringIO(SEVEN), key="id")
)
assert {
"changed": [],
"removed": [],
"added": [],
"columns_added": ["weight"],
"columns_removed": ["age"],
} == diff
def test_tsv():
diff = compare(
load_csv(io.StringIO(ONE), key="id"), load_csv(io.StringIO(TWO_TSV), key="id")
)
assert {
"added": [],
"removed": [],
"changed": [{"key": "1", "changes": {"age": ["4", "5"]}}],
"columns_added": [],
"columns_removed": [],
} == diff
def test_multi_key():
diff = compare(
load_csv(io.StringIO(ELEVEN), key=["name", "state"], key_sep='~'), load_csv(io.StringIO(TWELVE), key=["name", "state"], key_sep='~'),
)
assert {
'added': [],
'removed': [{'name': 'Bill', 'state': 'FL', 'age': '33'}],
'changed': [{'key': 'Ann~UT', 'changes': {'age': ['56', '45']}}],
'columns_added': [],
'columns_removed': [],
} == diff