Skip to content

Commit

Permalink
Merge master into paramval branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassz committed Jun 21, 2018
2 parents e014831 + b0a3485 commit 35037c6
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 68 deletions.
9 changes: 7 additions & 2 deletions webapp/apps/btax/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from django.contrib.postgres.fields import JSONField
import datetime
from django.utils.timezone import make_aware

from ..taxbrain.models import (SeparatedValuesField,
CommaSeparatedField)
Expand Down Expand Up @@ -120,7 +121,9 @@ class BTaxSaveInputs(Hostnameable, models.Model):
# Result
tax_result = models.TextField(default=None, blank=True, null=True)
# Creation DateTime
creation_date = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
creation_date = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)


class Meta:
Expand All @@ -138,7 +141,9 @@ class BTaxOutputUrl(models.Model):
user = models.ForeignKey(User, null=True, default=None)
model_pk = models.IntegerField(default=None, null=True)
# Expected Completion DateTime
exp_comp_datetime = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
exp_comp_datetime = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)
uuid = models.UUIDField(default=uuid.uuid4, null=True, editable=False, max_length=32, blank=True, unique=True)
btax_vers = models.CharField(blank=True, default=None, null=True, max_length=50)
taxcalc_vers = models.CharField(blank=True, default=None, null=True, max_length=50)
Expand Down
14 changes: 8 additions & 6 deletions webapp/apps/btax/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import csv
import pdfkit
import json
import pytz
import traceback
#Mock some module for imports because we can't fit them on Heroku slugs
from mock import Mock
Expand All @@ -13,6 +12,7 @@
import btax
import taxcalc
import datetime
from django.utils import timezone
import logging
from os import path
from urllib.parse import urlparse, parse_qs
Expand Down Expand Up @@ -201,8 +201,11 @@ def btax_results(request):

unique_url.unique_inputs = model
unique_url.model_pk = model.pk
cur_dt = datetime.datetime.utcnow()
future_offset = datetime.timedelta(seconds=((2 + max_q_length) * JOB_PROC_TIME_IN_SECONDS))
cur_dt = timezone.now()
future_offset = datetime.timedelta(
seconds=((2 + max_q_length)
* JOB_PROC_TIME_IN_SECONDS)
)
expected_completion = cur_dt + future_offset
unique_url.exp_comp_datetime = expected_completion
unique_url.save()
Expand Down Expand Up @@ -439,7 +442,7 @@ def output_detail(request, pk):
if all([job == 'YES' for job in jobs_ready]):
tax_result = dropq_compute.btax_get_results(normalize(job_ids))
model.tax_result = json.dumps(tax_result)
model.creation_date = datetime.datetime.now()
model.creation_date = timezone.now()
print('ready')
model.save()
return redirect(url)
Expand All @@ -453,8 +456,7 @@ def output_detail(request, pk):
if request.method == 'POST':
# if not ready yet, insert number of minutes remaining
exp_comp_dt = url.exp_comp_datetime
utc_now = datetime.datetime.utcnow()
utc_now = utc_now.replace(tzinfo=pytz.utc)
utc_now = timezone.now()
dt = exp_comp_dt - utc_now
exp_num_minutes = dt.total_seconds() / 60.
exp_num_minutes = round(exp_num_minutes, 2)
Expand Down
22 changes: 17 additions & 5 deletions webapp/apps/dynamic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..taxbrain import param_formatters

import datetime
from django.utils.timezone import make_aware


class DynamicSaveInputs(DataSourceable, models.Model):
Expand All @@ -47,7 +48,10 @@ class DynamicSaveInputs(DataSourceable, models.Model):
# Result
tax_result = JSONField(default=None, blank=True, null=True)
# Creation DateTime
creation_date = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
creation_date = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)

# Email address for user who started this job
user_email = models.CharField(blank=True, default=None, null=True,
max_length=50)
Expand Down Expand Up @@ -91,7 +95,9 @@ class DynamicBehaviorSaveInputs(DataSourceable, Fieldable, Resultable,
# Result
tax_result = JSONField(default=None, blank=True, null=True)
# Creation DateTime
creation_date = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
creation_date = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)

micro_sim = models.ForeignKey(OutputUrl, blank=True, null=True,
on_delete=models.SET_NULL)
Expand Down Expand Up @@ -166,7 +172,9 @@ class DynamicElasticitySaveInputs(DataSourceable, Hostnameable, models.Model):
# Result
tax_result = JSONField(default=None, blank=True, null=True)
# Creation DateTime
creation_date = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
creation_date = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)

micro_sim = models.ForeignKey(OutputUrl, blank=True, null=True,
on_delete=models.SET_NULL)
Expand Down Expand Up @@ -217,7 +225,9 @@ class DynamicBehaviorOutputUrl(models.Model):
user = models.ForeignKey(User, null=True, default=None)
model_pk = models.IntegerField(default=None, null=True)
# Expected Completion DateTime
exp_comp_datetime = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
exp_comp_datetime = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)
uuid = models.UUIDField(default=uuid.uuid4, null=True, editable=False, max_length=32, blank=True, unique=True)
taxcalc_vers = models.CharField(blank=True, default=None, null=True,
max_length=50)
Expand All @@ -239,7 +249,9 @@ class DynamicElasticityOutputUrl(models.Model):
user = models.ForeignKey(User, null=True, default=None)
model_pk = models.IntegerField(default=None, null=True)
# Expected Completion DateTime
exp_comp_datetime = models.DateTimeField(default=datetime.datetime(2015, 1, 1))
exp_comp_datetime = models.DateTimeField(
default=make_aware(datetime.datetime(2015, 1, 1))
)
uuid = models.UUIDField(default=uuid.uuid4, null=True, editable=False, max_length=32, blank=True, unique=True)
taxcalc_vers = models.CharField(blank=True, default=None, null=True,
max_length=50)
Expand Down
59 changes: 30 additions & 29 deletions webapp/apps/dynamic/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

from django.test import TestCase
from django.test import Client
import pytest
import mock
Expand All @@ -16,7 +14,7 @@
START_YEAR = '2016'


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

def setUp(self):
Expand All @@ -28,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 behavioral_post_helper(self):
#Monkey patch to mock out running of compute jobs
Expand All @@ -53,21 +51,21 @@ def behavioral_post_helper(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)

return dynamic_behavior

Expand All @@ -77,14 +75,14 @@ def test_behavioral_post(self):
# 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/"))
assert response.url[:link_idx+1].endswith("behavior_results/")

def test_behavioral_post_invalid_param(self):
"""
Expand All @@ -96,7 +94,12 @@ def test_behavioral_post_invalid_param(self):
response = self.client.post(dynamic_behavior, pe_data)
self.assertEqual(response.status_code, 400)

def test_elastic_post(self):
# 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 @@ -119,32 +122,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 @@ -170,33 +172,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'))
Loading

0 comments on commit 35037c6

Please sign in to comment.