forked from PolicyEngine/policyengine-taxsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
340 lines (309 loc) · 8.15 KB
/
tests.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
Testing framework for taxsim package
"""
import taxsim
import pickle
# Test 1 input
situation_1 = {
"people": {
"you": {
"age": {
"2024": 40
},
"employment_income": {
"2024": 100000
}
},
"your first dependent": {
"age": {
"2024": 10
},
"employment_income": {
"2024": 0
}
},
"your second dependent": {
"age": {
"2024": 10
},
"employment_income": {
"2024": 0
}
}
},
"families": {
"your family": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"marital_units": {
"your marital unit": {
"members": [
"you"
]
},
"your first dependent's marital unit": {
"members": [
"your first dependent"
],
"marital_unit_id": {
"2024": 1
}
},
"your second dependent's marital unit": {
"members": [
"your second dependent"
],
"marital_unit_id": {
"2024": 2
}
}
},
"tax_units": {
"your tax unit": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"spm_units": {
"your household": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"households": {
"your household": {
"members": [
"you",
"your first dependent",
"your second dependent"
],
"state_name": {
"2024": "CA"
}
}
}
}
# Test 2 input
situation_2 = {
"people": {
"you": {
"age": {
"2024": 40
},
"employment_income": {
"2024": 100000
}
},
"your first dependent": {
"age": {
"2024": 10
},
"employment_income": {
"2024": 0
}
},
"your second dependent": {
"age": {
"2024": 10
},
"employment_income": {
"2024": 0
}
}
},
"families": {
"your family": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"marital_units": {
"your marital unit": {
"members": [
"you"
]
},
"your first dependent's marital unit": {
"members": [
"your first dependent"
],
"marital_unit_id": {
"2024": 1
}
},
"your second dependent's marital unit": {
"members": [
"your second dependent"
],
"marital_unit_id": {
"2024": 2
}
}
},
"tax_units": {
"your tax unit": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"spm_units": {
"your household": {
"members": [
"you",
"your first dependent",
"your second dependent"
]
}
},
"households": {
"your household": {
"members": [
"you",
"your first dependent",
"your second dependent"
],
"state_name": {
"2024": "PA"
}
}
}
}
tests = { 'test1' :
{ 'taxsim_vars' : dict()
, 'situation' : situation_1
}
, 'test2' :
{ 'taxsim_vars' : dict()
, 'situation' : situation_2
}
}
tests_filename = 'saved_tests.pickle'
def reset_tests( testname : str = None ) -> None :
"""
Runs converter and saves to pickle file names saved_tests.pickle
Args:
testname : identifier of test to reset, or None for all
Returns:
None, but overwrites test saving file
"""
# TBD:
if( testname != None ) :
raise( NotImplementedError('Not working for individual tests yet.') )
else :
print("[INFO] Resetting all tests.")
# Run the tests and save results
for name, test_items in tests.items() :
# TBD: Do import step and save that output
# i.e. situation = taxsim.import_single_household( taxsim_vars )
# For now using 'situation' vars
situation = test_items['situation']
test_items['taxsim_results'] = taxsim.export_single_household( situation )
print( f"[INFO] Finished test: {name}")
with open(tests_filename, "wb") as f:
pickle.dump(tests, f)
print( f"[INFO] Saved all tests to {tests_filename}" )
def check_tests( testname : str = None ) -> bool :
"""
Runs converter and checks results against saved_tests.pickle
Args:
testname : identifier of test to reset, or None for all
Returns:
True if tests match
"""
# TBD:
if( testname != None ) :
raise( NotImplementedError('Not working for individual tests yet.'))
else :
print( "[INFO] Checking all tests.")
# Load the old tests
with open(tests_filename, "rb") as f:
old_tests = pickle.load(f)
# Run the tests and save results
is_different = False
for name, test_results in tests.items() :
# TBD: Do import step and save that output
# i.e. situation = taxsim.import_single_household( taxsim_vars )
# For now using 'situation' vars
situation = test_results['situation']
test_results['taxsim_results'] = taxsim.export_single_household( situation )
print( f"[INFO] Finished running test: {name}")
# Compare top-level dictionaries
differences = _compare_nested_dicts(test_results, old_tests[name])
# Print differences if any
if differences:
print(f"Differences found between dictionaries:")
_compare_dicts(test_results, old_tests[name], print_details=True) # Recursive call for detailed output
is_different = True
print( f"[INFO] Finished checking test: {name}")
print( f"[INFO] Completed checks." )
return is_different
def _compare_nested_dicts(dict1_val, dict2_val):
"""
Compare two dictionaries by drilling down into each
Args:
dict1_val : one dictionary
dict2_val : other dictionary
Returns:
set of differences or None if none
"""
# Base case: If both values are not dictionaries, compare directly
if not isinstance(dict1_val, dict) or not isinstance(dict2_val, dict):
return dict1_val != dict2_val
# Recursive case: Compare nested dictionaries
missing_keys = set(dict2_val.keys()) - set(dict1_val.keys())
extra_keys = set(dict1_val.keys()) - set(dict2_val.keys())
value_diffs = { key: dict2_val.get(key, None)
for key in dict1_val.keys() if _compare_nested_dicts(dict1_val[key], dict2_val.get(key, None))
}
return any(missing_keys or extra_keys or value_diffs)
def _compare_dicts(newdict, olddict, print_details : bool = False ):
"""
Show differences between the new and old dictionaries
Args:
newdict : new values
olddict : old values
print_details : whether to print the differences True/False
"""
# Identify missing and extra keys
missing_keys = set(olddict.keys()) - set(newdict.keys())
extra_keys = set(newdict.keys()) - set(olddict.keys())
# Print top-level missing/extra keys
if print_details and (missing_keys or extra_keys):
if missing_keys:
print(f"\tMissing keys: {missing_keys}")
if extra_keys:
print(f"\tExtra keys: {extra_keys}")
# Compare values for existing keys
value_diffs = {key: olddict.get(key, None)
for key in newdict.keys() if newdict[key] != olddict.get(key, None)
}
if print_details and value_diffs:
print(f"\tKeys with different values:")
for key, value in value_diffs.items():
print(f"\t\t- {key}: New value = {newdict[key]}" )
print(f"\t\t \t : Old value = {value}")
# Recursively compare nested dictionaries
for key, val in newdict.items():
if isinstance(val, dict) and key in olddict:
_compare_dicts(val, olddict[key], print_details=True) # Recursive call with details
# Run to check
if( __name__ == "__main__") :
reset_tests()
check_tests()