-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_views.py
786 lines (666 loc) · 29.8 KB
/
test_views.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
from django.test import Client
from django.test.client import RequestFactory
import json
import pytest
import os
import msgpack
from ..models import TaxBrainRun, TaxSaveInputs
from ..mock_compute import NodeDownCompute, MockFailedCompute
import taxcalc
from ...test_assets.utils import (check_posted_params, do_micro_sim,
get_post_data, get_file_post_data,
get_dropq_compute_from_module,
get_taxbrain_model)
CLIENT = Client()
NUM_BUDGET_YEARS = int(os.environ.get("NUM_BUDGET_YEARS", "10"))
START_YEAR = 2016
@pytest.mark.django_db
class TestTaxBrainViews(object):
''' Test the views of this app. '''
def test_taxbrain_get(self):
# Issue a GET request.
response = CLIENT.get('/taxbrain/')
# Check that the response is 200 OK.
assert response.status_code == 200
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_post(self, data_source):
"""
submit simple reform
"""
data = get_post_data(START_YEAR)
data['II_em'] = ['4333']
data['BE_inc'] = ['-0.3']
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
result = do_micro_sim(CLIENT, data, post_url=url)
truth_mods = {START_YEAR: {'_II_em': [4333.0]}}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
behv_truth_mods = {START_YEAR: {'_BE_inc': [-0.3]}}
check_posted_params(result['tb_dropq_compute'], behv_truth_mods,
str(START_YEAR), data_source=data_source,
param_type='behavior')
def test_taxbrain_post_invalid_param(self):
"""
Check that we get a 400 error if we submit an invalid field
"""
data = get_post_data(START_YEAR)
data.pop('start_year')
data.pop('data_source')
data['foo'] = ['0.0']
url = '/taxbrain/?start_year={0}&data_source=PUF'.format(START_YEAR)
response = CLIENT.post(url, data)
assert response.status_code == 400
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_quick_calc_post(self, data_source):
"Test quick calculation post and full post from quick_calc page"
# switches 0, 4, 6 are False
data = get_post_data(START_YEAR, quick_calc=True)
data['ID_BenefitSurtax_Switch_0'] = ['False']
data['ID_BenefitSurtax_Switch_4'] = ['0']
data['ID_BenefitSurtax_Switch_6'] = ['0.0']
data['II_em'] = ['4333']
data['ID_AmountCap_Switch_0'] = ['0']
data['data_source'] = data_source
data['BE_inc'] = ['-0.3']
result = do_micro_sim(CLIENT, data, compute_count=1)
# Check that data was saved properly
truth_mods = {START_YEAR: {"_ID_BenefitSurtax_Switch":
[[0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0]],
"_ID_AmountCap_Switch":
[[0, 1, 1, 1, 1, 1, True]],
"_II_em": [4333.0]}
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
behv_truth_mods = {START_YEAR: {'_BE_inc': [-0.3]}}
check_posted_params(result['tb_dropq_compute'], behv_truth_mods,
str(START_YEAR), data_source=data_source,
param_type='behavior')
# reset worker node count without clearing MockCompute session
result['tb_dropq_compute'].reset_count()
post_url = '/taxbrain/submit/{0}/'.format(result['pk'])
submit_data = {'csrfmiddlewaretoken': 'abc123'}
result = do_micro_sim(
CLIENT,
submit_data,
compute_count=NUM_BUDGET_YEARS,
post_url=post_url
)
# Check that data was saved properly
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
behv_truth_mods = {START_YEAR: {'_BE_inc': [-0.3]}}
check_posted_params(result['tb_dropq_compute'], behv_truth_mods,
str(START_YEAR), data_source=data_source,
param_type='behavior')
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_file_post_quick_calc(self, data_source, r1):
"""
Using file-upload interface, test quick calculation post and full
post from quick_calc page
"""
data = get_file_post_data(START_YEAR, r1, quick_calc=False)
data.pop('data_source')
data.pop('start_year')
post_url = '/taxbrain/file/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
result = do_micro_sim(
CLIENT,
data,
compute_count=1,
post_url=post_url
)
# Check that data was saved properly
truth_mods = taxcalc.Calculator.read_json_param_objects(
r1,
None,
)
truth_mods = truth_mods["policy"]
check_posted_params(result["tb_dropq_compute"], truth_mods,
str(START_YEAR), data_source=data_source)
# reset worker node count without clearing MockCompute session
result['tb_dropq_compute'].reset_count()
post_url = '/taxbrain/submit/{0}/'.format(result['pk'])
submit_data = {'csrfmiddlewaretoken': 'abc123'}
result = do_micro_sim(
CLIENT,
submit_data,
compute_count=NUM_BUDGET_YEARS,
post_url=post_url
)
# Check that data was saved properly
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_back_to_back_quickcalc(self, data_source):
"Test back to back quick calc posts"
# switches 0, 4, 6 are False
data = get_post_data(START_YEAR, quick_calc=True)
data['ID_BenefitSurtax_Switch_0'] = ['False']
data['ID_BenefitSurtax_Switch_4'] = ['0']
data['ID_BenefitSurtax_Switch_6'] = ['0.0']
data['II_em'] = ['4333']
data['data_source'] = data_source
result = do_micro_sim(CLIENT, data)
# Check that data was saved properly
truth_mods = {START_YEAR: {"_ID_BenefitSurtax_Switch":
[[0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0]],
"_II_em": [4333.0]}
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
edit_micro = '/taxbrain/edit/{0}/?start_year={1}'.format(result["pk"],
START_YEAR)
edit_page = CLIENT.get(edit_micro)
assert edit_page.status_code == 200
next_csrf = str(edit_page.context['csrf_token'])
data['csrfmiddlewaretoken'] = next_csrf
result2 = do_micro_sim(CLIENT, data)
check_posted_params(result2['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
def test_taxbrain_nodes_down(self):
# Monkey patch to mock out running of compute jobs
dropq_compute = get_dropq_compute_from_module(
'webapp.apps.taxbrain.views',
MockComputeObj=NodeDownCompute
)
data = get_post_data(START_YEAR)
data['II_em'] = ['4333']
result = do_micro_sim(
CLIENT,
data,
tb_dropq_compute=dropq_compute
)
# Check that data was saved properly
truth_mods = {START_YEAR: {"_ID_BenefitSurtax_Switch":
[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]],
"_II_em": [4333.0]}
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR))
def test_taxbrain_failed_job(self):
# Monkey patch to mock out running of compute jobs
dropq_compute = get_dropq_compute_from_module(
'webapp.apps.taxbrain.views',
MockComputeObj=MockFailedCompute
)
data = get_post_data(START_YEAR)
data['II_em'] = ['4333']
response = CLIENT.post('/taxbrain/', data)
# Check that redirect happens
assert response.status_code == 302
link_idx = response.url[:-1].rfind('/')
assert response.url[:link_idx + 1].endswith("taxbrain/")
response = CLIENT.get(response.url)
# Make sure the failure message is in the response
assert "Your calculation failed" in response.content.decode('utf-8')
@pytest.mark.xfail
def test_taxbrain_has_growth_params(self):
reform = {'factor_adjustment': ['0.03'],
'FICA_ss_trt': ['0.11'],
'start_year': str(START_YEAR),
'has_errors': ['False'],
'growth_choice': 'factor_adjustment'
}
do_micro_sim(CLIENT, reform)
def test_taxbrain_edit_cpi_flags_show_correctly(self):
data = get_post_data(START_YEAR)
data['II_em'] = ['4333']
data['AMT_CG_brk2_cpi'] = 'False'
data['AMEDT_ec_cpi'] = 'True'
result = do_micro_sim(CLIENT, data)
edit_micro = '/taxbrain/edit/{0}/?start_year={1}'.format(result["pk"],
START_YEAR)
edit_page = CLIENT.get(edit_micro)
assert edit_page.status_code == 200
cpi_flag = (edit_page.context['form']['AMT_CG_brk2_cpi']
.field.widget.attrs['placeholder'])
assert cpi_flag is False
cpi_flag = (edit_page.context['form']['AMEDT_ec_cpi']
.field.widget.attrs['placeholder'])
assert cpi_flag
def test_taxbrain_edit_benefitsurtax_switch_show_correctly(self):
# This post has no BenefitSurtax flags, so the model
# sets them to False
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
data['II_em'] = ['4333']
data['ID_BenefitSurtax_Switch_3'] = ['True']
result = do_micro_sim(CLIENT, data)
out = TaxBrainRun.objects.get(pk=result["pk"])
tsi = out.inputs
_ids = ['ID_BenefitSurtax_Switch_' + str(i) for i in range(7)]
# only posted param is stored
assert ([_id in tsi.raw_gui_field_inputs for _id in _ids] ==
[False, False, False, True, False, False, False])
assert tsi.raw_gui_field_inputs['ID_BenefitSurtax_Switch_3'] == 'True'
# Now edit this page
edit_micro = '/taxbrain/edit/{0}/?start_year={1}'.format(result["pk"],
START_YEAR)
edit_page = CLIENT.get(edit_micro)
assert edit_page.status_code == 200
# post some more data from the edit parameters page. Posting the
# same data (switch_0) again looks a little funny, but this
# is how it looks to the backend
next_csrf = str(edit_page.context['csrf_token'])
data2 = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'II_em': ['4333'],
'ID_BenefitSurtax_Switch_0': ['False'],
'ID_BenefitSurtax_Switch_1': ['False,*,True'],
'ID_BenefitSurtax_Switch_3': ['True'],
'csrfmiddlewaretoken': next_csrf}
data2.update(mod)
result2 = do_micro_sim(CLIENT, data2)
out2 = TaxBrainRun.objects.get(pk=result2["pk"])
tsi2 = out2.inputs
assert (
tsi2.raw_gui_field_inputs['ID_BenefitSurtax_Switch_0'] == 'False')
assert (tsi2.raw_gui_field_inputs['ID_BenefitSurtax_Switch_1'] ==
'False,*,True')
assert tsi2.raw_gui_field_inputs['ID_BenefitSurtax_Switch_3'] == 'True'
def test_taxbrain_wildcard_params_with_validation_is_OK(self):
"""
Set upper threshold for income tax bracket 1 to *, *, 38000
income tax bracket 2 will inflate above 38000 so should give
no error
"""
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'II_brk1_0': ['*, *, 15000'],
'II_brk2_cpi': 'False'}
data.update(mod)
result = do_micro_sim(CLIENT, data)
# Check that data was saved properly
truth_mods = {
START_YEAR: {'_II_brk2_cpi': False},
START_YEAR + 2: {
'_II_brk1': [[15000.0, 19050.0, 9525.0, 13600.0, 19050.0]]
}
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR))
def test_taxbrain_wildcard_params_with_validation_gives_error(self):
"""
Set upper threshold for income tax bracket 1 to *, *, 38000
Set CPI flag for income tax bracket 2 to False
In 2018, income tax bracket 2 will still be 37625 if CPI flag
is false so should give an error
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'II_brk1_0': ['*, *, 38000'],
'II_brk2_cpi': 'False'}
data.update(mod)
response = CLIENT.post('/taxbrain/', data)
# Check that redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
def test_taxbrain_spec_operators_in_validation_params_OK(self):
"""
Set upper threshold for income tax bracket 1 to *, 38000
Set upper threshold for income tax bracket 2 to *, *, 39500
should be OK
"""
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'II_brk1_0': ['*, *, 38000'],
'II_brk2_0': ['*, *, 39500'],
'cpi_offset': ['<,-0.0025'],
'FICA_ss_trt': ['< ,0.1,*,0.15,0.2']}
data.update(mod)
result = do_micro_sim(CLIENT, data)
truth_mods = {
START_YEAR - 1: {
'_cpi_offset': [-0.0025],
'_FICA_ss_trt': [0.1]
},
START_YEAR + 1: {
'_FICA_ss_trt': [0.15]
},
START_YEAR + 2: {
'_FICA_ss_trt': [0.2]
}
}
check_posted_params(result['tb_dropq_compute'], truth_mods, START_YEAR)
def test_taxbrain_warning_on_widow_param(self):
"""
Test case where error is added on undisplayed parameter
"""
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/?start_year={0}&data_source={1}'.format(
START_YEAR, 'PUF')
data['STD_3'] = ['1000']
response = CLIENT.post(url, data)
assert response.status_code == 200
assert response.context['start_year'] == str(START_YEAR)
assert response.context['data_source'] == 'PUF'
assert response.context['form'] is not None
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_wildcard_in_validation_params_gives_error(
self, data_source):
"""
Set upper threshold for income tax bracket 1 to *, 38000
Set upper threshold for income tax bracket 2 to *, *, 39500
Set CPI flag for upper threshold for income tax brack to false
so should give an error
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
mod = {'II_brk1_0': ['*, 38000'],
'II_brk2_0': ['*, *, 39500'],
'II_brk2_cpi': 'False'}
data.update(mod)
response = CLIENT.post(url, data)
# Check that redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['start_year'] == str(START_YEAR)
assert response.context['data_source'] == data_source
def test_taxbrain_improper_reverse_gives_error1(self):
"""
Check reverse operator post without other numbers throws error
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'cpi_offset': ['<,']}
data.update(mod)
response = CLIENT.post('/taxbrain/', data)
# Check that redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['form'] is not None
def test_taxbrain_improper_reverse_gives_error2(self):
"""
Check reverse operator not in first position throws error
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {'cpi_offset': ['-0.002,<,-0.001']}
data.update(mod)
response = CLIENT.post('/taxbrain/', data)
# Check that redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['form'] is not None
def test_taxbrain_bool_separated_values(self):
"""
Test _DependentCredit_before_CTC can be posted as comma separated
string
"""
data = get_post_data(2018, _ID_BenefitSurtax_Switches=False)
data['DependentCredit_before_CTC'] = ['True,*, FALSE,tRUe,*,0']
result = do_micro_sim(CLIENT, data)
# Check that data was submitted properly
truth_mods = {
2018: {'_DependentCredit_before_CTC': [True]},
2020: {'_DependentCredit_before_CTC': [False]},
2021: {'_DependentCredit_before_CTC': [True]},
2023: {'_DependentCredit_before_CTC': [False]}
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(2018))
@pytest.mark.parametrize(
'data_source,use_assumptions',
[('PUF', True), ('CPS', False)]
)
def test_taxbrain_post_file(self, data_source, use_assumptions,
assumptions_text, r1):
if use_assumptions:
assumptions_text = assumptions_text
else:
assumptions_text = None
data = get_file_post_data(START_YEAR,
r1,
assumptions_text)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/file/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
result = do_micro_sim(CLIENT, data, post_url=url)
truth_mods = {}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(START_YEAR), data_source=data_source)
@pytest.mark.xfail
def test_taxbrain_view_old_data_model(self, test_coverage_fields):
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
unique_url = get_taxbrain_model(test_coverage_fields,
taxcalc_vers="0.10.0",
webapp_vers="1.1.0")
tsi = unique_url.inputs
old_result = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"example_old_result.json")
with open(old_result) as f:
tsi.tax_result = json.loads(f.read())
tsi.first_year = 2016
tsi.save()
factory = RequestFactory()
req = factory.get('/taxbrain/')
url = '/taxbrain/42'
# Assert we can make result tables from old data
ans = get_result_context(tsi, req, url)
assert ans
@pytest.mark.parametrize('param_name,bad_input',
[('II_brk1_0', 'XTOT*4500'),
('II_brk1_0', 'abc'),
('II_brk1_0', 'abc,123'),
('II_brk1_0', '01'),
('II_brk1_0', 'a' * 200),
('II_brk3_0', '0'),
('BE_inc', '0.3')])
def test_taxbrain_bad_input(self, param_name, bad_input):
"""
POST a bad expression for a TaxBrain parameter and verify that
it gives an error
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_post_data(START_YEAR, _ID_BenefitSurtax_Switches=False)
mod = {param_name: [bad_input],
'II_brk2_0': ['*, *, 39500']}
data.update(mod)
response = CLIENT.post('/taxbrain/', data)
assert response.status_code == 200
assert response.context['has_errors'] is True
err_string = str(response.context['form'].errors)
assert bad_input in err_string and param_name in err_string
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_error_reform_file(self, data_source, bad_reform):
"""
POST a reform file that causes errors. See PB issue #630
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_file_post_data(START_YEAR, bad_reform)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/file/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
response = CLIENT.post(url, data)
# Check that no redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['data_source'] == data_source
assert response.context['start_year'] == str(START_YEAR)
assert any(['_II_brk1_4' in msg and '2024' in msg
for msg in response.context['errors']])
# get most recent object
objects = TaxSaveInputs.objects.order_by('id')
obj = objects[len(objects) - 1]
next_token = str(response.context['csrf_token'])
form_id = obj.id
data2 = {
'csrfmiddlewaretoken': next_token,
'form_id': form_id,
'has_errors': ['True'],
}
response = CLIENT.post(url, data2)
assert response.status_code == 200
assert response.context['data_source'] == data_source
assert response.context['start_year'] == str(START_YEAR)
@pytest.mark.parametrize('data_source', ['PUF', 'CPS'])
def test_taxbrain_warning_reform_file(self, data_source, warning_reform):
"""
POST a reform file that causes warnings and check that re-submission
is allowed. See PB issue #630 and #761
"""
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
data = get_file_post_data(START_YEAR, warning_reform)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/file/?start_year={0}&data_source={1}'.format(
START_YEAR, data_source)
response = CLIENT.post(url, data)
# Check that no redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['start_year'] == str(START_YEAR)
assert response.context['data_source'] == data_source
assert any(['_STD_0' in msg and '2023' in msg
for msg in response.context['errors']])
# get most recent object
objects = TaxSaveInputs.objects.order_by('id')
obj = objects[len(objects) - 1]
next_token = str(response.context['csrf_token'])
form_id = obj.id
data2 = {
'csrfmiddlewaretoken': next_token,
'form_id': form_id,
'has_errors': ['True'],
}
result = do_micro_sim(CLIENT, data2, post_url=url)
truth_mods = {
2020: {
"_STD": [[1000, 24981.84, 12490.92, 18736.38, 24981.84]]
}
}
check_posted_params(result['tb_dropq_compute'], truth_mods, START_YEAR,
data_source=data_source)
@pytest.mark.parametrize(
'data_source,use_assumptions',
[('PUF', True), ('CPS', False)]
)
def test_taxbrain_reform_file_file_swap(
self, data_source, use_assumptions, assumptions_text,
warning_reform, r1):
"""
POST a reform file that causes warnings, swap files, and make sure
swapped files are used. See PB issue #630 and #761
"""
start_year = 2017
# Monkey patch to mock out running of compute jobs
get_dropq_compute_from_module('webapp.apps.taxbrain.views')
if use_assumptions:
assumptions_text = assumptions_text
else:
assumptions_text = None
data = get_file_post_data(start_year, warning_reform,
assumptions_text)
data.pop('start_year')
data.pop('data_source')
url = '/taxbrain/file/?start_year={0}&data_source={1}'.format(
start_year, data_source)
response = CLIENT.post(url, data)
# Check that no redirect happens
assert response.status_code == 200
assert response.context['has_errors'] is True
assert response.context['data_source'] == data_source
assert response.context['start_year'] == str(start_year)
assert any(['_STD_0' in msg and '2023' in msg
for msg in response.context['errors']])
# get most recent object
objects = TaxSaveInputs.objects.order_by('id')
obj = objects[len(objects) - 1]
next_token = str(response.context['csrf_token'])
form_id = obj.id
data2 = {
'csrfmiddlewaretoken': next_token,
'form_id': form_id,
'has_errors': ['True'],
}
data_file = get_file_post_data(START_YEAR,
r1,
assumptions_text)
data2['docfile'] = data_file['docfile']
result = do_micro_sim(CLIENT, data2, post_url=url)
dropq_compute = result['tb_dropq_compute']
# Pick the first of jobs submitted
inputs = msgpack.loads(dropq_compute.last_posted, encoding='utf8',
use_list=True)[0]
user_mods = inputs['user_mods']
if use_assumptions:
assert user_mods["behavior"][2018]["_BE_sub"][0] == 1.0
truth_mods = {2018: {'_II_em': [8000.0]}}
check_posted_params(dropq_compute, truth_mods, start_year,
data_source=data_source)
def test_taxbrain_up_to_2018(self):
start_year = 2018
data = get_post_data(start_year, _ID_BenefitSurtax_Switches=False)
mod = {'II_brk1_0': ['*, *, 15000'],
'II_brk2_cpi': 'False'}
data.update(mod)
result = do_micro_sim(CLIENT, data)
# Check that data was saved properly
truth_mods = {
start_year: {'_II_brk2_cpi': False},
}
check_posted_params(result['tb_dropq_compute'], truth_mods,
str(start_year))
def test_taxbrain_file_up_to_2018(self, r1):
start_year = 2018
data = get_file_post_data(start_year, r1)
post_url = '/taxbrain/file/'
result = do_micro_sim(
CLIENT,
data,
post_url=post_url
)
# Check that data was saved properly
truth_mods = taxcalc.Calculator.read_json_param_objects(
r1,
None,
)
truth_mods = truth_mods["policy"]
check_posted_params(result["tb_dropq_compute"], truth_mods,
str(start_year))
def test_taxbrain_old_data_gives_deprecation_errors(self):
"""
Simulate the creation of a previous PolicyBrain run and check for
deprecation error messages
"""
start_year = 2018
fields = get_post_data(start_year)
fields["first_year"] = start_year
unique_url = get_taxbrain_model(fields,
taxcalc_vers="0.14.2",
webapp_vers="1.3.0")
model = unique_url.inputs
model.raw_gui_field_inputs = {
'ALD_Alimony_hc': '*,1,*,*,*,*,*,*,0',
'PT_exclusion_rt': '0.2,*,*,*,*,*,*,*,0.0',
'PT_exclusion_wage_limit': '0.5,*,*,*,*,*,*,*,9e99'
}
model.save()
unique_url.inputs = model
unique_url.save()
pk = unique_url.pk
edit_micro = "/taxbrain/edit/{}/?start_year={}".format(pk, start_year)
response = CLIENT.get(edit_micro)
assert response.status_code == 200
assert response.context['has_errors'] is False
msg = ('Field {} has been deprecated. Refer to the Tax-Calculator '
'documentation for a sensible replacement.')
for param in ["ALD_Alimony_hc", "PT_exclusion_rt",
"PT_exclusion_wage_limit"]:
assert msg.format(param) in str(response.context["form"].errors)