Skip to content

Commit

Permalink
Revert dpath bump
Browse files Browse the repository at this point in the history
Merge pull request openfisca#948 from openfisca/hello-old-dpath
  • Loading branch information
sandcha authored Feb 10, 2020
2 parents 7fe1e45 + 1eebbbc commit 430de21
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 42 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Changelog

### 34.7.2 [#948](https://github.com/openfisca/openfisca-core/pull/948)

#### Technical changes

- Revert `dpath` dependency bump introduced by [#940](https://github.com/openfisca/openfisca-core/pull/940)
- Fix bug in period interpretation by Web API ([openfisca-france#1413](https://github.com/openfisca/openfisca-france/issues/1413))

### 34.7.1 [#940](https://github.com/openfisca/openfisca-core/pull/940)

_Note: this version has been unpublished due to an issue introduced by dpath upgrade. Please use 34.7.2 or a more recent version._

#### Technical changes

- Update dependencies: dpath, autopep8
Expand Down
4 changes: 2 additions & 2 deletions openfisca_core/simulation_builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from typing import Dict, List, Iterable

import dpath.util
import dpath
import numpy as np
from copy import deepcopy

Expand Down Expand Up @@ -428,7 +428,7 @@ def raise_period_mismatch(self, entity, json, e):
# It is only raised when we consume the buffer. We thus don't know which exact key caused the error.
# We do a basic research to find the culprit path
culprit_path = next(
dpath.util.search(json, "*/{}/{}".format(e.variable_name, str(e.period)), yielded = True),
dpath.search(json, "*/{}/{}".format(e.variable_name, str(e.period)), yielded = True),
None)
if culprit_path:
path = [entity.plural] + culprit_path[0].split('/')
Expand Down
4 changes: 2 additions & 2 deletions openfisca_web_api/handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

import dpath.util
import dpath

from openfisca_core.simulation_builder import SimulationBuilder
from openfisca_core.indexed_enums import Enum
Expand Down Expand Up @@ -31,7 +31,7 @@ def calculate(tax_benefit_system, input_data):

dpath.util.new(computation_results, path, entity_result)

dpath.util.merge(input_data, computation_results)
dpath.merge(input_data, computation_results)

return input_data

Expand Down
30 changes: 15 additions & 15 deletions openfisca_web_api/loader/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import yaml
from copy import deepcopy

import dpath.util
import dpath

from openfisca_core.indexed_enums import Enum
from openfisca_web_api import handlers
Expand All @@ -18,18 +18,18 @@ def build_openAPI_specification(api_data):
file = open(OPEN_API_CONFIG_FILE, 'r')
spec = yaml.safe_load(file)
country_package_name = api_data['country_package_metadata']['name'].title()
dpath.util.new(spec, 'info/title', spec['info']['title'].replace("{COUNTRY_PACKAGE_NAME}", country_package_name))
dpath.util.new(spec, 'info/description', spec['info']['description'].replace("{COUNTRY_PACKAGE_NAME}", country_package_name))
dpath.util.new(spec, 'info/version', api_data['country_package_metadata']['version'])
dpath.new(spec, 'info/title', spec['info']['title'].replace("{COUNTRY_PACKAGE_NAME}", country_package_name))
dpath.new(spec, 'info/description', spec['info']['description'].replace("{COUNTRY_PACKAGE_NAME}", country_package_name))
dpath.new(spec, 'info/version', api_data['country_package_metadata']['version'])

for entity in tax_benefit_system.entities:
name = entity.key.title()
spec['definitions'][name] = get_entity_json_schema(entity, tax_benefit_system)

situation_schema = get_situation_json_schema(tax_benefit_system)
dpath.util.new(spec, 'definitions/SituationInput', situation_schema)
dpath.util.new(spec, 'definitions/SituationOutput', situation_schema.copy())
dpath.util.new(spec, 'definitions/Trace/properties/entitiesDescription/properties', {
dpath.new(spec, 'definitions/SituationInput', situation_schema)
dpath.new(spec, 'definitions/SituationOutput', situation_schema.copy())
dpath.new(spec, 'definitions/Trace/properties/entitiesDescription/properties', {
entity.plural: {'type': 'array', 'items': {"type": "string"}}
for entity in tax_benefit_system.entities
})
Expand All @@ -42,24 +42,24 @@ def build_openAPI_specification(api_data):
parameter_example = api_data['parameters'][parameter_path]
else:
parameter_example = next(iter(api_data['parameters'].values()))
dpath.util.new(spec, 'definitions/Parameter/example', parameter_example)
dpath.new(spec, 'definitions/Parameter/example', parameter_example)

if tax_benefit_system.open_api_config.get('variable_example'):
variable_example = api_data['variables'][tax_benefit_system.open_api_config['variable_example']]
else:
variable_example = next(iter(api_data['variables'].values()))
dpath.util.new(spec, 'definitions/Variable/example', variable_example)
dpath.new(spec, 'definitions/Variable/example', variable_example)

if tax_benefit_system.open_api_config.get('simulation_example'):
simulation_example = tax_benefit_system.open_api_config['simulation_example']
dpath.util.new(spec, 'definitions/SituationInput/example', simulation_example)
dpath.util.new(spec, 'definitions/SituationOutput/example', handlers.calculate(tax_benefit_system, deepcopy(simulation_example))) # calculate has side-effects
dpath.util.new(spec, 'definitions/Trace/example', handlers.trace(tax_benefit_system, simulation_example))
dpath.new(spec, 'definitions/SituationInput/example', simulation_example)
dpath.new(spec, 'definitions/SituationOutput/example', handlers.calculate(tax_benefit_system, deepcopy(simulation_example))) # calculate has side-effects
dpath.new(spec, 'definitions/Trace/example', handlers.trace(tax_benefit_system, simulation_example))
else:
message = "No simulation example has been defined for this tax and benefit system. If you are the maintainer of {}, you can define an example by following this documentation: https://openfisca.org/doc/openfisca-web-api/config-openapi.html".format(country_package_name)
dpath.util.new(spec, 'definitions/SituationInput/example', message)
dpath.util.new(spec, 'definitions/SituationOutput/example', message)
dpath.util.new(spec, 'definitions/Trace/example', message)
dpath.new(spec, 'definitions/SituationInput/example', message)
dpath.new(spec, 'definitions/SituationOutput/example', message)
dpath.new(spec, 'definitions/Trace/example', message)
return spec


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# functional and integration breaks caused by external code updates.

general_requirements = [
'dpath == 2.0.1',
'dpath >= 1.5.0, < 2.0.0',
'pytest >= 4.4.1, < 6.0.0', # For openfisca test
'numpy >= 1.11, < 1.18',
'psutil >= 5.4.7, < 6.0.0',
Expand Down Expand Up @@ -37,7 +37,7 @@

setup(
name = 'OpenFisca-Core',
version = '34.7.1',
version = '34.7.2',
author = 'OpenFisca Team',
author_email = '[email protected]',
classifiers = [
Expand Down
52 changes: 41 additions & 11 deletions tests/web_api/test_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from copy import deepcopy

import pytest
import dpath.util
import dpath

from openfisca_country_template.situation_examples import couple

Expand Down Expand Up @@ -104,12 +104,12 @@ def test_basic_calculation():
response = post_json(simulation_json)
assert response.status_code == OK
response_json = json.loads(response.data.decode('utf-8'))
assert dpath.util.get(response_json, 'persons/bill/basic_income/2017-12') == 600 # Universal basic income
assert dpath.util.get(response_json, 'persons/bill/income_tax/2017-12') == 300 # 15% of the salary
assert dpath.util.get(response_json, 'persons/bill/age/2017-12') == 37 # 15% of the salary
assert dpath.util.get(response_json, 'persons/bob/basic_income/2017-12') == 600
assert dpath.util.get(response_json, 'persons/bob/social_security_contribution/2017-12') == 816 # From social_security_contribution.yaml test
assert dpath.util.get(response_json, 'households/first_household/housing_tax/2017') == 3000
assert dpath.get(response_json, 'persons/bill/basic_income/2017-12') == 600 # Universal basic income
assert dpath.get(response_json, 'persons/bill/income_tax/2017-12') == 300 # 15% of the salary
assert dpath.get(response_json, 'persons/bill/age/2017-12') == 37 # 15% of the salary
assert dpath.get(response_json, 'persons/bob/basic_income/2017-12') == 600
assert dpath.get(response_json, 'persons/bob/social_security_contribution/2017-12') == 816 # From social_security_contribution.yaml test
assert dpath.get(response_json, 'households/first_household/housing_tax/2017') == 3000


def test_enums_sending_identifier():
Expand All @@ -136,7 +136,7 @@ def test_enums_sending_identifier():
response = post_json(simulation_json)
assert response.status_code == OK
response_json = json.loads(response.data.decode('utf-8'))
assert dpath.util.get(response_json, 'households/_/housing_tax/2017') == 0
assert dpath.get(response_json, 'households/_/housing_tax/2017') == 0


def test_enum_output():
Expand All @@ -157,7 +157,7 @@ def test_enum_output():
response = post_json(simulation_json)
assert response.status_code == OK
response_json = json.loads(response.data.decode('utf-8'))
assert dpath.util.get(response_json, "households/_/housing_occupancy_status/2017-01") == "tenant"
assert dpath.get(response_json, "households/_/housing_occupancy_status/2017-01") == "tenant"


def test_enum_wrong_value():
Expand All @@ -179,7 +179,7 @@ def test_enum_wrong_value():
assert response.status_code == BAD_REQUEST
response_json = json.loads(response.data.decode('utf-8'))
message = "Possible values are ['owner', 'tenant', 'free_lodger', 'homeless']"
text = dpath.util.get(response_json, "households/_/housing_occupancy_status/2017-01")
text = dpath.get(response_json, "households/_/housing_occupancy_status/2017-01")
assert message in text


Expand All @@ -206,7 +206,7 @@ def test_encoding_variable_value():
assert response.status_code == BAD_REQUEST, response.data.decode('utf-8')
response_json = json.loads(response.data.decode('utf-8'))
message = "'Locataire ou sous-locataire d‘un logement loué vide non-HLM' is not a known value for 'housing_occupancy_status'. Possible values are "
text = dpath.util.get(response_json, 'households/_/housing_occupancy_status/2017-07')
text = dpath.get(response_json, 'households/_/housing_occupancy_status/2017-07')
assert message in text


Expand Down Expand Up @@ -287,3 +287,33 @@ def test_str_variable():
response = subject.post('/calculate', data = simulation_json, content_type = 'application/json')

assert response.status_code == OK


def test_periods():
simulation_json = json.dumps({
"persons": {
"bill": {}
},
"households": {
"_": {
"parents": ["bill"],
"housing_tax": {
"2017": None
},
"housing_occupancy_status": {
"2017-01": None
}
}
}
})

response = post_json(simulation_json)
assert response.status_code == OK

response_json = json.loads(response.data.decode('utf-8'))

yearly_variable = dpath.get(response_json, 'households/_/housing_tax') # web api year is an int
assert yearly_variable == {'2017': 200.0}

monthly_variable = dpath.get(response_json, 'households/_/housing_occupancy_status') # web api month is a string
assert monthly_variable == {'2017-01': 'tenant'}
20 changes: 10 additions & 10 deletions tests/web_api/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from http.client import OK

import dpath.util
import dpath
from . import subject


Expand Down Expand Up @@ -36,21 +36,21 @@ def test_paths():


def test_entity_definition():
assert 'parents' in dpath.util.get(body, 'definitions/Household/properties')
assert 'children' in dpath.util.get(body, 'definitions/Household/properties')
assert 'salary' in dpath.util.get(body, 'definitions/Person/properties')
assert 'rent' in dpath.util.get(body, 'definitions/Household/properties')
assert 'number' == dpath.util.get(body, 'definitions/Person/properties/salary/additionalProperties/type')
assert 'parents' in dpath.get(body, 'definitions/Household/properties')
assert 'children' in dpath.get(body, 'definitions/Household/properties')
assert 'salary' in dpath.get(body, 'definitions/Person/properties')
assert 'rent' in dpath.get(body, 'definitions/Household/properties')
assert 'number' == dpath.get(body, 'definitions/Person/properties/salary/additionalProperties/type')


def test_situation_definition():
situation_input = body['definitions']['SituationInput']
situation_output = body['definitions']['SituationOutput']
for situation in situation_input, situation_output:
assert 'households' in dpath.util.get(situation, '/properties')
assert 'persons' in dpath.util.get(situation, '/properties')
assert "#/definitions/Household" == dpath.util.get(situation, '/properties/households/additionalProperties/$ref')
assert "#/definitions/Person" == dpath.util.get(situation, '/properties/persons/additionalProperties/$ref')
assert 'households' in dpath.get(situation, '/properties')
assert 'persons' in dpath.get(situation, '/properties')
assert "#/definitions/Household" == dpath.get(situation, '/properties/households/additionalProperties/$ref')
assert "#/definitions/Person" == dpath.get(situation, '/properties/persons/additionalProperties/$ref')


def test_host():
Expand Down

0 comments on commit 430de21

Please sign in to comment.