Skip to content

Commit

Permalink
test for web
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed May 14, 2020
1 parent a15a309 commit ce587a3
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 8 deletions.
2 changes: 1 addition & 1 deletion arcovid19/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
from .cache import CACHE, from_cache # noqa
from .cases import load_cases # noqa
from .models import load_infection_curve # noqa
from .web import get_webapp # noqa
from . import web # noqa
4 changes: 2 additions & 2 deletions arcovid19/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import sys

from .cases import load_cases, CASES_URL
from .web import get_webapp
from . import web


# =============================================================================
Expand Down Expand Up @@ -98,7 +98,7 @@ def webserver(
directory to the directory containing the first file found.
"""
app = get_webapp()
app = web.create_app()
app.run(
host=host, port=port,
use_reloader=True,
Expand Down
2 changes: 1 addition & 1 deletion arcovid19/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ class InfectionCurve:
intervention_end: int = attr.ib(default=25)
intervention_decrease: float = attr.ib(default=70)
t_incubation: float = attr.ib(default=5.)
t_infectious = attr.ib(default=9.)
t_infectious: float = attr.ib(default=9.)
t_death: float = attr.ib(default=32.)
mild_recovery: float = attr.ib(default=11.)
bed_stay: float = attr.ib(default=28.)
Expand Down
2 changes: 1 addition & 1 deletion arcovid19/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# PUBLIC API
# =============================================================================

def get_webapp(**kwargs):
def create_app(**kwargs):
"""Retrieve a flask app for arcovid 19 using the internal blueprint.
"""
Expand Down
4 changes: 3 additions & 1 deletion arcovid19/web/bp.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def get_context_data(self):
context_data = {}

form = forms.InfectionCurveForm()
if form.validate_on_submit():

if flask.request.method == "POST" and form.validate_on_submit():
# get all the data as string
data = form.data.copy()

Expand Down Expand Up @@ -139,6 +140,7 @@ class DownloadView(InfectionCurveView):

def dispatch_request(self):
context_data = self.get_context_data()

result = context_data["result"]

now = dt.datetime.now().isoformat()
Expand Down
2 changes: 1 addition & 1 deletion arcovid19/web/templates/InfectionCurve.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</a>
</tr>
{% for error in field.errors %}
<tr class="text-danger">
<tr class="field-error text-danger">
<td></td>
<td>{{error}}</td>
</tr>
Expand Down
153 changes: 153 additions & 0 deletions tests/test_web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2020, Bruno Sanchez, Vanessa Daza,
# Juan B Cabral, Marcelo Lares,
# Nadia Luczywo, Dante Paz, Rodrigo Quiroga,
# Martín de los Ríos, Federico Stasyszyn
# Cristian Giuppone.
# License: BSD-3-Clause
# Full Text: https://raw.githubusercontent.com/ivco19/libs/master/LICENSE


# =============================================================================
# DOCS
# =============================================================================

"""Test suite
"""


# =============================================================================
# IMPORTS
# =============================================================================

import os
import pathlib
import tempfile
import io

import pytest

import numpy as np

import pandas as pd

import arcovid19


# =============================================================================
# CONSTANTS
# =============================================================================

PATH = pathlib.Path(os.path.abspath(os.path.dirname(__file__)))


# =============================================================================
# SETUP
# =============================================================================

def setup_function(func):
arcovid19.CACHE.clear()


# =============================================================================
# FIXTURES
# =============================================================================

@pytest.fixture
def webclient():
app = arcovid19.web.create_app()
db_fd, app.config['DATABASE'] = tempfile.mkstemp()
app.config['TESTING'] = True

with app.test_client() as client:
yield client

os.close(db_fd)
os.unlink(app.config['DATABASE'])


# =============================================================================
# TEST INDEX
# =============================================================================

def test_web_index(webclient):
index_response = webclient.get("/")
icurve_response = webclient.get("/icurve")

assert index_response.status_code == 200
assert icurve_response.status_code == 200
assert index_response.data == icurve_response.data


def test_web_post_icurve(webclient):
data = {
'population': 600000, 'N_init': 10, 'R': 1.2,
'intervention_start': 15, 'intervention_end': 25,
'intervention_decrease': 70.0, 't_incubation': 5, 't_infectious': 9,
't_death': 32.0, 'mild_recovery': 11.0, 'bed_stay': 28.0,
'bed_rate': 0.2, 'bed_wait': 5, 'beta': 1.236, 'sigma': 1.1,
'gamma': 1.1, 't_max': 200, 'dt': 1.0}

response = webclient.post('/icurve', data=data)
assert response.status_code == 200
assert "field-error" not in str(response.data)


def test_web_missing_data_post_icurve(webclient):
conf = {
'population': 600000, 'N_init': 10, 'R': 1.2,
'intervention_start': 15, 'intervention_end': 25,
'intervention_decrease': 70.0, 't_incubation': 5, 't_infectious': 9,
't_death': 32.0, 'mild_recovery': 11.0, 'bed_stay': 28.0,
'bed_rate': 0.2, 'bed_wait': 5, 'beta': 1.236, 'sigma': 1.1,
'gamma': 1.1, 't_max': 200, 'dt': 1.0}

for rk in conf.keys():
data = {k: v for k, v in conf.items() if k != rk}
post_response = webclient.post('/icurve', data=data)
assert post_response.status_code == 200
assert "field-error" in str(post_response.data)

get_response = webclient.get('/icurve', data=data)
assert get_response.status_code == 200
assert "field-error" not in str(get_response.data)


def test_web_download_data(webclient):
curve_conf = {
'population': 600000, 'N_init': 10, 'R': 1.2,
'intervention_start': 15, 'intervention_end': 25,
'intervention_decrease': 70.0, 't_incubation': 5, 't_infectious': 9,
't_death': 32.0, 'mild_recovery': 11.0, 'bed_stay': 28.0,
'bed_rate': 0.2, 'bed_wait': 5, 'beta': 1.236, 'sigma': 1.1,
'gamma': 1.1}
sir_conf = {'t_max': 200, 'dt': 1.0}

curve = arcovid19.load_infection_curve(**curve_conf)
expected = curve.do_SIR(**sir_conf)

data = {"model": "do_SIR"}
data.update(curve_conf)
data.update(sir_conf)
response = webclient.post('/download_model', data=data)

ectype = (
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

assert response.status_code == 200
assert response.headers["Content-Type"] == ectype

# validate result
result = pd.read_excel(io.BytesIO(response.data), sheet_name="Data")
result = result.set_index("ts")

# validate in the 8 decimal place
np.testing.assert_array_almost_equal(result, expected.df, decimal=8)

config = pd.read_excel(io.BytesIO(response.data), sheet_name="Config")
config = config.set_index("Attribute")

assert config.to_dict()["Value"] == data
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# content of: tox.ini , put in same dir as setup.py
[tox]
envlist = py37, py38, style, coverage, docs
envlist = style, py37, py38, docs, coverage

[pytest]
addopts = -n "auto" --mpl
Expand Down

0 comments on commit ce587a3

Please sign in to comment.