Skip to content

Commit

Permalink
Merge pull request #876 from hdoupe/notavail-none-start-year
Browse files Browse the repository at this point in the history
Merged #876
  • Loading branch information
hdoupe committed Mar 29, 2018
2 parents 1d98b54 + dd9fd8f commit 5809183
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 36 deletions.
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Release 1.5.0 on 2018-03-2?
- [#870](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/870) - Update RELEASES.md and increase compute time to 100 seconds - Hank Doupe
- [#874](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/874) - Add special HTML pages for when a model result is not found or is incompatible - Sean Wang
- [#875](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/875) - Back-end for not_avail.html page - Hank Doupe
- [#876](https://github.com/OpenSourcePolicyCenter/PolicyBrain/pull/876) - Use current default start year if start year was not saved - Hank Doupe

Release 1.4.4 on 2018-03-08
----------------------------
Expand Down
32 changes: 30 additions & 2 deletions webapp/apps/btax/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from taxcalc import Policy
from ...btax import views


START_YEAR = 2016
from ...constants import START_YEAR

OK_POST_DATA = {u'btax_betr_pass': 0.33,
u'btax_depr_5yr': u'btax_depr_5yr_gds_Switch',
Expand Down Expand Up @@ -190,3 +189,32 @@ def test_get_not_avail_page_renders(self):
for t in response.templates])
edit_exp = '/ccc/edit/{}/?start_year={}'.format(pk, start_year)
assert response.context['edit_href'] == edit_exp

def test_get_not_avail_page_renders_start_year_is_none(self):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""
start_year = START_YEAR
#Monkey patch to mock out running of compute jobs
import sys
from webapp.apps.btax import views as webapp_views
webapp_views.dropq_compute = MockComputeBtax()
fields = {'first_year': str(start_year),
'btax_depr_5yr': 'btax_depr_5yr_ads_Switch'}
form = BTaxExemptionForm(str(start_year), fields)
model = form.save()
model.tax_result = "unrenderable"
model.first_year = None
model.save()
unique_url = BTaxOutputUrl()
unique_url.unique_inputs = model
unique_url.save()

pk = unique_url.pk
url = '/ccc/{}/'.format(pk)
response = self.client.get(url)
assert any([t.name == 'btax/not_avail.html'
for t in response.templates])
edit_exp = '/ccc/edit/{}/?start_year={}'.format(pk, start_year)
assert response.context['edit_href'] == edit_exp
4 changes: 2 additions & 2 deletions webapp/apps/btax/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def edit_btax_results(request, pk):
except:
raise Http404

model = BTaxSaveInputs.objects.get(pk=url.model_pk)
model = url.unique_inputs
start_year = model.first_year
#Get the user-input from the model in a way we can render
ser_model = serializers.serialize('json', [model])
Expand Down Expand Up @@ -376,7 +376,7 @@ def output_detail(request, pk):
traceback.print_exc()
edit_href = '/ccc/edit/{}/?start_year={}'.format(
pk,
model.first_year
model.first_year or START_YEAR # sometimes first_year is None
)
print('edit_href', edit_href, pk, model.first_year)
not_avail_context = dict(edit_href=edit_href,
Expand Down
3 changes: 1 addition & 2 deletions webapp/apps/dynamic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ def get_tax_result(self):

NONPARAM_FIELDS = set(["job_ids", "jobs_not_ready", "first_year",
"tax_result", "raw_input_fields", "input_fields",
"creation_date", "id", "raw_input_fields",
"input_fields"])
"creation_date", "id", "data_source"])

def set_fields(self):
"""
Expand Down
44 changes: 24 additions & 20 deletions webapp/apps/dynamic/tests/test_behavioral.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,41 @@
from ..models import DynamicBehaviorOutputUrl
from ..forms import DynamicBehavioralInputsModelForm

CLIENT = Client()

@pytest.mark.usefixtures("r1")
class DynamicBehavioralViewsTests(TestCase):
@pytest.mark.django_db
class TestDynamicBehavioralViews(object):
''' Test the partial equilibrium dynamic views of this app. '''

def setUp(self):
# Every test needs a client.
self.client = Client()

def test_behavioral_edit(self):
# Do the microsim
start_year = u'2016'
data = get_post_data(start_year)
data[u'II_em'] = [u'4333']

micro1 = do_micro_sim(self.client, data)["response"]
micro1 = do_micro_sim(CLIENT, data)["response"]

# Do another microsim
data[u'II_em'] += [u'4334']
micro2 = do_micro_sim(self.client, data)["response"]
micro2 = do_micro_sim(CLIENT, data)["response"]

# Do a third microsim
data[u'II_em'] += [u'4335']
micro3 = do_micro_sim(self.client, data)["response"]
micro3 = do_micro_sim(CLIENT, data)["response"]

# Do the partial equilibrium simulation based on the third microsim
pe_reform = {u'BE_inc': [u'-0.4']}
pe_response = do_dynamic_sim(self.client, 'behavioral', micro3, pe_reform)
pe_response = do_dynamic_sim(CLIENT, 'behavioral', micro3, pe_reform)
orig_micro_model_num = micro3.url[-2:-1]

# Now edit this partial equilibrium sim
# Go to behavioral input page
behavior_num = pe_response.url[pe_response.url[:-1].rfind('/')+1:-1]
dynamic_behavior_edit = '/dynamic/behavioral/edit/{0}/?start_year={1}'.format(behavior_num, START_YEAR)
# load page
response = self.client.get(dynamic_behavior_edit)
self.assertEqual(response.status_code, 200)
response = CLIENT.get(dynamic_behavior_edit)
assert response.status_code == 200
page = response.content
# Read the page to find the linked microsim. It should be the third
# microsim above
Expand All @@ -73,11 +72,11 @@ def test_behavioral_reform_with_wildcard(self):
data = get_post_data(start_year)
data[u'SS_Earnings_c'] = [u'*,*,*,*,15000']

micro1 = do_micro_sim(self.client, data)["response"]
micro1 = do_micro_sim(CLIENT, data)["response"]

# Do the partial equilibrium simulation based on the microsim
pe_reform = {u'BE_sub': [u'0.25']}
pe_response = do_dynamic_sim(self.client, 'behavioral', micro1, pe_reform)
pe_response = do_dynamic_sim(CLIENT, 'behavioral', micro1, pe_reform)
orig_micro_model_num = micro1.url[-2:-1]
from webapp.apps.dynamic import views
post = views.dropq_compute.last_posted
Expand All @@ -94,11 +93,11 @@ def test_behavioral_reform_post_gui(self):
data[u'SS_Earnings_c'] = [u'*,*,*,*,15000']
data['data_source'] = 'CPS'

micro1 = do_micro_sim(self.client, data)["response"]
micro1 = do_micro_sim(CLIENT, data)["response"]

# Do the partial equilibrium simulation based on the microsim
pe_reform = {u'BE_sub': [u'0.25']}
pe_response = do_dynamic_sim(self.client, 'behavioral', micro1,
pe_response = do_dynamic_sim(CLIENT, 'behavioral', micro1,
pe_reform, start_year=start_year)
orig_micro_model_num = micro1.url[-2:-1]
from webapp.apps.dynamic import views
Expand All @@ -114,12 +113,12 @@ def test_behavioral_reform_post_gui(self):
def test_behavioral_reform_from_file(self):
# Do the microsim from file
data = get_file_post_data(START_YEAR, self.r1)
micro1 = do_micro_sim(self.client, data, post_url='/taxbrain/file/')
micro1 = do_micro_sim(CLIENT, data, post_url='/taxbrain/file/')
micro1 = micro1["response"]

# Do the partial equilibrium simulation based on the microsim
be_reform = {u'BE_sub': [u'0.25']}
be_response = do_dynamic_sim(self.client, 'behavioral', micro1, be_reform)
be_response = do_dynamic_sim(CLIENT, 'behavioral', micro1, be_reform)
orig_micro_model_num = micro1.url[-2:-1]
from webapp.apps.dynamic import views
post = views.dropq_compute.last_posted
Expand All @@ -129,12 +128,15 @@ def test_behavioral_reform_from_file(self):
assert post["first_budget_year"] == int(START_YEAR)
assert user_mods["behavior"][str(START_YEAR)]["_BE_sub"][0] == 0.25

def test_get_not_avail_page_renders(self):
@pytest.mark.parametrize(
'start_year,start_year_is_none',
[(2018, False), (2017, True)]
)
def test_get_not_avail_page_renders(self, start_year, start_year_is_none):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""
start_year = 2018
fields = get_post_data(start_year, _ID_BenefitSurtax_Switches=False)
fields['BE_sub'] = ['0.25']
fields["first_year"] = start_year
Expand All @@ -150,13 +152,15 @@ def test_get_not_avail_page_renders(self):
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
if start_year_is_none:
model.first_year = None
model.save()
unique_url.unique_inputs = model
unique_url.save()

pk = unique_url.pk
url = '/dynamic/behavior_results/{}/'.format(pk)
response = self.client.get(url)
response = CLIENT.get(url)
assert any([t.name == 'taxbrain/not_avail.html'
for t in response.templates])
edit_exp = '/dynamic/behavioral/edit/{}/?start_year={}'.format(
Expand Down
6 changes: 3 additions & 3 deletions webapp/apps/dynamic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def edit_dynamic_behavioral(request, pk):
except:
raise Http404

model = DynamicBehaviorSaveInputs.objects.get(pk=url.model_pk)
model = url.unique_inputs
start_year = model.first_year
model.set_fields()
#Get the user-input from the model in a way we can render
Expand Down Expand Up @@ -491,7 +491,7 @@ def edit_dynamic_elastic(request, pk):
except:
raise Http404

model = DynamicElasticitySaveInputs.objects.get(pk=url.model_pk)
model = url.unique_inputs
start_year = model.first_year
#Get the user-input from the model in a way we can render
ser_model = serializers.serialize('json', [model])
Expand Down Expand Up @@ -818,7 +818,7 @@ def behavior_results(request, pk):
traceback.print_exc()
edit_href = '/dynamic/behavioral/edit/{}/?start_year={}'.format(
pk,
model.start_year
model.first_year or START_YEAR # sometimes first_year is None
)
not_avail_context = dict(edit_href=edit_href,
**context_vers_disp)
Expand Down
2 changes: 1 addition & 1 deletion webapp/apps/taxbrain/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def get_tax_result(self):
NONPARAM_FIELDS = set(["job_ids", "jobs_not_ready", "first_year", "quick_calc",
"tax_result", "raw_input_fields", "input_fields",
"json_text", "error_text", "creation_date", "id",
"raw_input_fields", "input_fields"])
"data_source"])

def set_fields(self):
"""
Expand Down
10 changes: 7 additions & 3 deletions webapp/apps/taxbrain/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,15 @@ def test_taxbrain_old_data_gives_deprecation_errors(self):
"PT_exclusion_wage_limit"]:
assert msg.format(param) in str(response.context["form"].errors)

def test_get_not_avail_page_renders(self):
@pytest.mark.parametrize(
'start_year,start_year_is_none',
[(2018, False), (2017, True)]
)
def test_get_not_avail_page_renders(self, start_year, start_year_is_none):
"""
Make sure not_avail.html page is rendered if exception is thrown
while parsing results
"""

start_year = 2018
fields = get_post_data(start_year)
fields["first_year"] = start_year
unique_url = get_taxbrain_model(fields,
Expand All @@ -800,6 +802,8 @@ def test_get_not_avail_page_renders(self):
model.input_fields = None
model.deprecated_fields = None
model.tax_result = "unrenderable"
if start_year_is_none:
model.first_year = None
model.save()
unique_url.unique_inputs = model
unique_url.save()
Expand Down
6 changes: 3 additions & 3 deletions webapp/apps/taxbrain/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def submit_micro(request, pk):
except:
raise Http404

model = TaxSaveInputs.objects.get(pk=url.model_pk)
model = url.unique_inputs
start_year = model.start_year
# This will be a new model instance so unset the primary key
model.pk = None
Expand Down Expand Up @@ -602,7 +602,7 @@ def edit_personal_results(request, pk):
except:
raise Http404

model = TaxSaveInputs.objects.get(pk=url.model_pk)
model = url.unique_inputs
start_year = model.first_year
model.set_fields()

Expand Down Expand Up @@ -767,7 +767,7 @@ def output_detail(request, pk):
traceback.print_exc()
edit_href = '/taxbrain/edit/{}/?start_year={}'.format(
pk,
model.start_year
model.first_year or START_YEAR # sometimes first_year is None
)
not_avail_context = dict(edit_href=edit_href,
**context_vers_disp)
Expand Down

0 comments on commit 5809183

Please sign in to comment.