Skip to content

Commit

Permalink
Add tests for mobility demand parser
Browse files Browse the repository at this point in the history
  • Loading branch information
redekok committed May 4, 2023
1 parent 73a2276 commit 4cd4bab
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/parsers/demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def query_scenario(self, scenario_id, prop):
query_result = QueryScenario.execute(scenario_id, prop['gquery'])

if query_result.successful:
return query_result.value[prop['gquery']]['future'] / prop ['factor']
print('query_result.value', query_result.value)
print('prop["gquery"]', prop['gquery'])
return query_result.value[prop['gquery']]['future'] / prop['factor']

raise ETMParseError(
f"We currently do not support the ETM gquery listed in the config: {prop['gquery']}"
Expand Down
104 changes: 104 additions & 0 deletions tests/models/parsers/test_demand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
Tests for the MobilityDemand parser
"""
# pylint: disable=import-error disable=redefined-outer-name disable=missing-function-docstring disable=protected-access

import pytest
from app.models.energy_system import EnergySystemHandler
from app.models.esdl_to_scenario_converter import EsdlToScenarioConverter
from app.models.parsers.demand import MobilityDemandParser
from unittest.mock import MagicMock, patch

@pytest.fixture
def esh_without_mobility_demand():
"""
Energy system based on the MMvIB macro case WITHOUT mobility demand assets
"""
with open('tests/fixtures/mmvib_macro_without_demand.esdl') as file:
esdl_string = file.read()
return EnergySystemHandler.from_string(esdl_string)

@pytest.fixture
def esh_with_mobility_demand():
"""
Energy system based on the MMvIB macro case WITH mobility demand assets
"""
with open('tests/fixtures/mmvib_macro.esdl') as file:
esdl_string = file.read()
return EnergySystemHandler.from_string(esdl_string)

def mocked_values():
return {
'capacity_of_transport_car_using_hydrogen': {
'present': 0.,
'future': 100.,
'unit': 'MW'
},
'flh_of_transport_car_using_hydrogen': {
'present': 0.,
'future': 8760.,
'unit': 'hours'
}
}

def mock_query_response(scenario_id, app, requests_mock, output):
"""
Mocks a query response from ETEngine
"""
requests_mock.put(
f'{app.config["ETENGINE_URL"]}/scenarios/{scenario_id}',
json={'gqueries': output, 'scenario': {'end_year': 2050, 'area_code': 'nl2019'}},
status_code=200
)

def test_parse_without_mobility_demand(esh_without_mobility_demand, app, requests_mock, helpers):
mobility_demand_asset_config = helpers.get_first_config_for_asset_type('MobilityDemand')

# If no mobility demand asset is present in the ESDL,
# the mobility_demands dict is empty
mobility_demands = esh_without_mobility_demand.get_assets_of_type('MobilityDemand')
assert len(mobility_demands) == 0

# The MobilityDemandParser doesn't parse any inputs regardless of
# whether MobilityDemand assets are present in the ESDL
parsed_inputs = EsdlToScenarioConverter(esh_without_mobility_demand).inputs
assert parsed_inputs == {}

def test_parse_with_mobility_demand(esh_with_mobility_demand, app, requests_mock, helpers):
mock_query_response(123456, app, requests_mock, mocked_values())

mobility_demand_asset_config = helpers.get_first_config_for_asset_type('MobilityDemand')

# Check if the mobility demand asset(s) can be found
mobility_demands = esh_with_mobility_demand.get_assets_of_type('MobilityDemand')
assert len(mobility_demands) > 0

first_mobility_demand = mobility_demands[0]
second_mobility_demand = mobility_demands[1]

# Check the power and FLH before updating the attributes
power_before_update = first_mobility_demand.power
flh_before_update = first_mobility_demand.fullLoadHours

# Call the update method
with app.app_context():
MobilityDemandParser(esh_with_mobility_demand, mobility_demand_asset_config).update(123456)

# The MobilityDemandParser doesn't parse any inputs regardless of
# whether MobilityDemand assets are present in the ESDL
parsed_inputs = EsdlToScenarioConverter(esh_with_mobility_demand).inputs
assert parsed_inputs == {}

# If more than one mobility demand asset (with a specific vehicle
# fuel combination) is present in the ESDL, ...
# TODO

# Check if the power has been updated for the first asset
power_after_update = first_mobility_demand.power
assert power_before_update != power_after_update

# Check if the number of FLH has been updated and if it has been
# set to 8760 for the first asset
flh_after_update = first_mobility_demand.fullLoadHours
assert flh_before_update != flh_after_update
assert flh_after_update == 8760

0 comments on commit 4cd4bab

Please sign in to comment.