From 4cd4bab9a840cc93eed8a2d0d5e9e51f83da7a49 Mon Sep 17 00:00:00 2001 From: redekok Date: Fri, 31 Mar 2023 16:19:20 +0200 Subject: [PATCH] Add tests for mobility demand parser --- app/models/parsers/demand.py | 4 +- tests/models/parsers/test_demand.py | 104 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/models/parsers/test_demand.py diff --git a/app/models/parsers/demand.py b/app/models/parsers/demand.py index ad14a4d..000ed96 100644 --- a/app/models/parsers/demand.py +++ b/app/models/parsers/demand.py @@ -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']}" diff --git a/tests/models/parsers/test_demand.py b/tests/models/parsers/test_demand.py new file mode 100644 index 0000000..c1cee3b --- /dev/null +++ b/tests/models/parsers/test_demand.py @@ -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