def test_data_not_present(self, sos_model_dict, sector_model): """Raise a NotImplementedError if an input is defined but no dependency links it to a data source """ sos_model_dict['scenario_dependencies'] = [] sos_model_dict['model_dependencies'] = [] with raises(NotImplementedError): SosModel.from_dict(sos_model_dict, [sector_model])
def test_scenario_model_not_provided(self, sos_model_dict, sector_model, economic_model): """Error on scenario not provided """ with raises(SmifDataMismatchError) as ex: SosModel.from_dict(sos_model_dict, [sector_model, economic_model]) assert "SectorModel or ScenarioModel source `climate` required by " + \ "dependency `climate (precipitation) - water_supply (precipitation)` " + \ "was not provided by the builder" in str(ex.value)
def test_scenario_variant_not_present(self, sos_model_dict, scenario_model, economic_model): """Error on sector model not provided """ with raises(SmifDataMismatchError) as ex: SosModel.from_dict(sos_model_dict, [scenario_model, economic_model]) assert "SectorModel or ScenarioModel sink `water_supply` required " + \ "by dependency `economic_model (gva) - water_supply (rGVA)` " + \ "was not provided by the builder" in str(ex.value)
def test_undefined_unit_conversion(self, sos_model_dict, sector_model, scenario_model, economic_model): """Error on invalid dependency """ sector_model.inputs['precipitation'] = Spec(name='precipitation', dims=['LSOA'], coords={'LSOA': [1, 2, 3]}, dtype='float', unit='incompatible') with raises(ValueError) as ex: SosModel.from_dict(sos_model_dict, [sector_model, scenario_model, economic_model]) assert "ml!=incompatible" in str(ex.value)
def test_construct(self, sos_model_dict, scenario_model, sector_model, economic_model): """Constructing from config of the form:: { 'name': 'sos_model_name', 'description': 'friendly description of the sos model', 'dependencies': [ { 'source': str (Model.name), 'source_output': str (Metadata.name), 'sink': str (Model.name), 'sink_output': str (Metadata.name) } ] } With list of child SectorModel/ScenarioModel instances passed in alongside. """ sos_model = SosModel.from_dict( sos_model_dict, [scenario_model, sector_model, economic_model]) assert isinstance(sos_model, SosModel) assert list(sos_model.scenario_models) == [scenario_model] assert isinstance(sos_model.get_model('climate'), ScenarioModel) assert sos_model.sector_models == [sector_model, economic_model] or \ sos_model.sector_models == [economic_model, sector_model] assert isinstance(sos_model.get_model('economic_model'), SectorModel) assert isinstance(sos_model.get_model('water_supply'), SectorModel)
def test_dependencies_fields(self, sos_model_dict, scenario_model, sector_model, economic_model): """Compose dependencies from scenario- and model- fields """ sos_model_dict['scenario_dependencies'] = [{ 'source': 'climate', 'source_output': 'precipitation', 'sink_input': 'precipitation', 'sink': 'water_supply', 'timestep': 'CURRENT' }, { 'source': 'climate', 'source_output': 'reservoir_level', 'sink_input': 'reservoir_level', 'sink': 'water_supply', 'timestep': 'CURRENT' }] sos_model_dict['model_dependencies'] = [{ 'source': 'economic_model', 'source_output': 'gva', 'sink_input': 'rGVA', 'sink': 'water_supply', 'timestep': 'CURRENT' }, { 'source': 'water_supply', 'source_output': 'reservoir_level', 'sink_input': 'reservoir_level', 'sink': 'water_supply', 'timestep': 'PREVIOUS' }] sos_model = SosModel.from_dict( sos_model_dict, [scenario_model, sector_model, economic_model]) actual = sos_model.as_dict() TestCase().assertCountEqual(actual['scenario_dependencies'], sos_model_dict['scenario_dependencies']) TestCase().assertCountEqual(actual['model_dependencies'], sos_model_dict['model_dependencies'])
def test_optional_description(self, sos_model_dict): """Default to empty description """ del sos_model_dict['description'] sos_model = SosModel.from_dict(sos_model_dict) assert sos_model.description == ''