Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default parameter handling for macro elasticity simulation #908

Merged
merged 3 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions webapp/apps/dynamic/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

from django.test import TestCase
from django.test import Client
import pytest
import mock
import os
# os.environ["NUM_BUDGET_YEARS"] = '2'
Expand All @@ -15,7 +14,7 @@
START_YEAR = '2016'


class DynamicViewsTests(TestCase):
class DynamicViewsTests(object):
''' Test the views of this app. '''

def setUp(self):
Expand All @@ -27,7 +26,7 @@ def test_taxbrain_get(self):
response = self.client.get('/taxbrain/')

# Check that the response is 200 OK.
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

def test_behavioral_post(self):
#Monkey patch to mock out running of compute jobs
Expand All @@ -52,36 +51,40 @@ def test_behavioral_post(self):

response = self.client.post('/taxbrain/', data)
# Check that redirect happens
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
# Go to results page
link_idx = response.url[:-1].rfind('/')
self.assertTrue(response.url[:link_idx+1].endswith("taxbrain/"))
assert response.url[:link_idx+1].endswith("taxbrain/")

# Link to dynamic simulation
model_num = response.url[link_idx+1:-1]
dynamic_landing = '/dynamic/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_landing)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Go to behavioral input page
dynamic_behavior = '/dynamic/behavioral/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_behavior)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Do the partial equilibrium job submission
pe_data = {'BE_inc': ['-0.4']}
response = self.client.post(dynamic_behavior, pe_data)
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
print(response)

#Check should get success this time
response_success = self.client.get(response.url)
self.assertEqual(response_success.status_code, 200)
assert (response_success.status_code == 200)
link_idx = response.url[:-1].rfind('/')
self.assertTrue(response.url[:link_idx+1].endswith("behavior_results/"))


def test_elastic_post(self):
assert response.url[:link_idx+1].endswith("behavior_results/")

# Test whether default elasticity is used if no param is given
@pytest.mark.parametrize(
'el_data',
[{'elastic_gdp': ['0.55']}, {}]
)
def test_elastic_post(self, el_data):
#Monkey patch to mock out running of compute jobs
import sys
from webapp.apps.taxbrain import views
Expand All @@ -104,32 +107,31 @@ def test_elastic_post(self):

response = self.client.post('/taxbrain/', data)
# Check that redirect happens
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
# Go to results page
link_idx = response.url[:-1].rfind('/')
self.assertTrue(response.url[:link_idx+1].endswith("taxbrain/"))
assert response.url[:link_idx+1].endswith("taxbrain/")

# Link to dynamic simulation
model_num = response.url[link_idx+1:-1]
dynamic_landing = '/dynamic/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_landing)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Go to macro input page
dynamic_egdp = '/dynamic/macro/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_egdp)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Do the elasticity job submission
el_data = {'elastic_gdp': ['0.55']}
response = self.client.post(dynamic_egdp, el_data)
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
print(response)

#Check that we get success this time
response_success = self.client.get(response.url)
self.assertEqual(response_success.status_code, 200)
self.assertTrue(response.url[:-2].endswith("macro_results/"))
assert (response_success.status_code == 200)
assert response.url[:-2].endswith("macro_results/")

def test_elastic_failed_job(self):
#Monkey patch to mock out running of compute jobs
Expand All @@ -155,33 +157,32 @@ def test_elastic_failed_job(self):

response = self.client.post('/taxbrain/', data)
# Check that redirect happens
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
# Go to results page
link_idx = response.url[:-1].rfind('/')
self.assertTrue(response.url[:link_idx+1].endswith("taxbrain/"))
assert response.url[:link_idx+1].endswith("taxbrain/")

# Link to dynamic simulation
model_num = response.url[link_idx+1:-1]
dynamic_landing = '/dynamic/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_landing)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Go to macro input page
dynamic_egdp = '/dynamic/macro/{0}/?start_year={1}'.format(model_num, START_YEAR)
response = self.client.get(dynamic_egdp)
self.assertEqual(response.status_code, 200)
assert (response.status_code == 200)

# Do the elasticity job submission
el_data = {'elastic_gdp': ['0.55']}
response = self.client.post(dynamic_egdp, el_data)
self.assertEqual(response.status_code, 302)
assert (response.status_code == 302)
print(response)

#Check that we get success this time
response_success = self.client.get(response.url)
self.assertEqual(response_success.status_code, 200)
self.assertTrue(response.url[:-2].endswith("macro_results/"))
assert (response_success.status_code == 200)
assert response.url[:-2].endswith("macro_results/")
response = self.client.get(response.url)
# Make sure the failure message is in the response
self.assertTrue("Your calculation failed"
in response.content.decode('utf-8'))
assert ("Your calculation failed" in response.content.decode('utf-8'))
16 changes: 13 additions & 3 deletions webapp/apps/dynamic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,19 @@ def dynamic_elasticities(request, pk):
start_year = START_YEAR
if request.method=='POST':
# Client is attempting to send inputs, validate as form data
fields = dict(request.GET)
fields.update(dict(request.POST))
fields = {k: v[0] if isinstance(v, list) else v for k, v in list(fields.items())}
init_fields = dict(request.GET)
init_fields.update(dict(request.POST))

fields = {}
for k, v in list(init_fields.items()):
if isinstance(v, list):
v = v[0]
if not v:
v = (default_elasticity_parameters(int(start_year))[k]
.col_fields[0]
.default_value)
fields[k] = v

start_year = fields.get('start_year', START_YEAR)
print(fields)
# TODO: migrate first_year to start_year to get rid of weird stuff like
Expand Down