Skip to content

Commit

Permalink
Fix trace and calculate endpoints responses for str variables
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagnoux authored Oct 12, 2018
2 parents 5ee5f1a + c7ddfdf commit 09db461
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# Changelog

## 24.5.3 [#734](https://github.com/openfisca/openfisca-core/pull/734)
## 24.5.5 [#742](https://github.com/openfisca/openfisca-core/pull/742)

- Fix the internal server error that appeared for the `/trace` and (less frequently) `/calculate` route of the Web API
- This error appeared when a simulation output was a variable of type string


> Note: Versions `24.5.3` and `24.5.4` have been unpublished as they accidentally introduced a breaking change. Please use version `24.5.5` or more recent.

### 24.5.2 [#734](https://github.com/openfisca/openfisca-core/pull/734)

- Ignore W503 to enforce Knuth's style (W504)
- Fix failing entities test
- Household description was outdated

## 24.5.1 [#732](https://github.com/openfisca/openfisca-core/pull/732)
### 24.5.1 [#732](https://github.com/openfisca/openfisca-core/pull/732)

- Further adopt simplified simulation initialisation
- See [#729](https://github.com/openfisca/openfisca-core/pull/729)
Expand Down
4 changes: 4 additions & 0 deletions openfisca_web_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def calculate():
entity_result = result.decode()[entity_index].name
elif variable.value_type == float:
entity_result = float(str(result[entity_index])) # To turn the float32 into a regular float without adding confusing extra decimals. There must be a better way.
elif variable.value_type == str:
entity_result = to_unicode(result[entity_index]) # From bytes to unicode
else:
entity_result = result.tolist()[entity_index]

Expand Down Expand Up @@ -196,6 +198,8 @@ def trace():
value = vector_trace['value'].tolist()
if isinstance(value[0], Enum):
value = [item.name for item in value]
if isinstance(value[0], bytes):
value = [to_unicode(item) for item in value]
vector_trace['value'] = value

return jsonify({
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
'flake8 >= 3.5.0, < 3.6.0',
'autopep8 >= 1.4.0, < 1.5.0',
'pycodestyle < 2.4.0',
'openfisca-country-template >= 3.3.1rc1, < 4.0.0',
'openfisca-country-template >= 3.4.0, < 4.0.0',
'openfisca-extension-template >= 1.1.3, < 2.0.0',
] + api_requirements

setup(
name = 'OpenFisca-Core',
version = '24.5.2',
version = '24.5.5',
author = 'OpenFisca Team',
author_email = 'contact@openfisca.org',
classifiers = [
Expand Down
12 changes: 12 additions & 0 deletions tests/web_api/test_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import os
import json
from http.client import BAD_REQUEST, OK, NOT_FOUND
from copy import deepcopy

from nose.tools import assert_equal, assert_in
import dpath

from openfisca_core.commons import to_unicode
from openfisca_country_template.situation_examples import couple

from . import subject

Expand Down Expand Up @@ -283,3 +285,13 @@ def test_encoding_period_id():
"'à' is not a valid ASCII value.",
response_json['error']
)


def test_str_variable():
new_couple = deepcopy(couple)
new_couple['households']['_']['postal_code'] = {'2017-01': None}
simulation_json = json.dumps(new_couple)

response = subject.post('/calculate', data = simulation_json, content_type = 'application/json')

assert_equal(response.status_code, OK)
11 changes: 11 additions & 0 deletions tests/web_api/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import unicode_literals, print_function, division, absolute_import
import json
from copy import deepcopy

from nose.tools import assert_equal, assert_is_instance
from http.client import OK
Expand Down Expand Up @@ -50,3 +51,13 @@ def test_root_nodes():
dpath.util.get(response_json, 'requestedCalculations'),
['disposable_income<2017-01>', 'total_benefits<2017-01>', 'total_taxes<2017-01>']
)


def test_str_variable():
new_couple = deepcopy(couple)
new_couple['households']['_']['postal_code'] = {'2017-01': None}
simulation_json = json.dumps(new_couple)

response = subject.post('/trace', data = simulation_json, content_type = 'application/json')

assert_equal(response.status_code, OK)

0 comments on commit 09db461

Please sign in to comment.