def test_leaf_external_connection_deserialization(): recovered = area_from_string( '''{ "name": "house", "children":[ {"name": "pv1", "type": "PV", "panel_count": 4, "display_type": "PV", "allow_external_connection": true}, {"name": "load1", "type": "LoadHours", "avg_power_W": 200, "display_type": "Load", "allow_external_connection": true}, {"name": "storage1", "type": "Storage", "display_type": "Storage", "allow_external_connection": true} ] } ''', create_config({"external_connection_enabled": True}) ) pv1, load1, storage1 = recovered.children assert isinstance(pv1, PV) assert isinstance(pv1.strategy, PVExternalStrategy) assert pv1.strategy.panel_count == 4 assert pv1.display_type == "PV" assert isinstance(load1, LoadHours) assert isinstance(load1.strategy, LoadHoursExternalStrategy) assert load1.strategy.avg_power_W == 200 assert load1.display_type == "Load" assert isinstance(storage1, Storage) assert isinstance(storage1.strategy, StorageExternalStrategy) assert storage1.display_type == "Storage"
def test_area_with_children_roundtrip(): child1 = Area("child1") child2 = Area("child2") parent = Area("parent", [child1, child2]) string = area_to_string(parent) recovered = area_from_string(string) assert recovered.name == "parent" assert recovered.children[0].name == "child1" assert recovered.children[1].name == "child2"
def get_setup(config): try: setup_path = os.environ['D3A_SETUP_PATH'] with open(setup_path, 'r') as area_file: area_str = area_file.read().replace('\n', '') recovered_area = area_from_string(area_str) recovered_area.config = config return recovered_area except KeyError as d3a_key_error: raise RuntimeError( 'D3_SETUP_PATH environment variable not found.') from d3a_key_error except FileNotFoundError as d3a_file_error: raise RuntimeError('D3A setup file containing area not found on the D3_SETUP_PATH') \ from d3a_file_error
def test_leaf_deserialization(): recovered = area_from_string('''{ "name": "house", "children":[ {"name": "pv1", "type": "PV", "panel_count": 4, "risk": 50}, {"name": "pv2", "type": "PV", "panel_count": 1, "risk": 10} ] } ''') pv1, pv2 = recovered.children assert isinstance(pv1, PV) assert pv1.strategy.panel_count == 4 and pv1.strategy.risk == 50 assert isinstance(pv2, PV) assert pv2.strategy.panel_count == 1 and pv2.strategy.risk == 10
def test_leaf_deserialization(): recovered = area_from_string('''{ "name": "house", "children":[ {"name": "pv1", "type": "PV", "panel_count": 4, "display_type": "PV"}, {"name": "pv2", "type": "PV", "panel_count": 1, "display_type": "PV"} ] } ''') pv1, pv2 = recovered.children assert isinstance(pv1, PV) assert pv1.strategy.panel_count == 4 assert pv1.display_type == "PV" assert isinstance(pv2, PV) assert pv2.strategy.panel_count == 1 assert pv2.display_type == "PV"
def test_leaf_deserialization(): recovered = area_from_string( '''{ "name": "house", "children":[ {"name": "pv1", "type": "PV", "panel_count": 4, "display_type": "PV"}, {"name": "pv2", "type": "PV", "panel_count": 1, "display_type": "PV"}, {"name": "home meter", "type": "HomeMeter", "home_meter_profile": "some_path.csv"} ] } ''', config=create_config() ) pv1, pv2, home_meter = recovered.children assert isinstance(pv1, PV) assert pv1.strategy.panel_count == 4 assert pv1.display_type == "PV" assert isinstance(pv2, PV) assert pv2.strategy.panel_count == 1 assert pv2.display_type == "PV" assert isinstance(home_meter, HomeMeter)
def test_appliance_roundtrip(appliance_fixture): recovered = area_from_string(appliance_fixture) assert recovered.children[1].appliance.initially_on assert not recovered.children[0].appliance.is_on
def test_non_attr_param(): area1 = Area('area1', [], None, PVStrategy()) recovered1 = area_from_string(area_to_string(area1)) assert recovered1.strategy.max_panel_power_W is None assert recovered1.strategy.offer_update.final_rate[area1.config.start_date] == \ ConstSettings.PVSettings.SELLING_RATE_RANGE.final
def test_strategy_roundtrip_with_params(): area = Area('area', [], None, PVStrategy(panel_count=42)) area_str = area_to_string(area) recovered = area_from_string(area_str) assert recovered.strategy.panel_count == 42
def test_raises_unknown_class(): with pytest.raises(ValueError): area_from_string("{'name':'broken','strategy':'NonexistentStrategy'}")
def test_strategy_appliance_roundtrip(): area = Area("child", [], None, PVStrategy(), PVAppliance()) recovered = area_from_string(area_to_string(area)) assert type(recovered.strategy) is PVStrategy assert type(recovered.appliance) is PVAppliance
def test_budget_keeper_roundtrip(budget_keeper_fixture): recovered = area_from_string(budget_keeper_fixture) assert recovered.budget_keeper.budget == 100.0 assert recovered.budget_keeper.days_per_period == 30
def test_roundtrip_with_leaf(fixture_with_leaves): recovered = area_from_string(fixture_with_leaves) assert isinstance(recovered.children[0].strategy, PVStrategy) assert isinstance(recovered.children[1].strategy, PVStrategy)
def test_roundtrip_with_leaf(fixture_with_leaves): recovered = area_from_string(fixture_with_leaves, create_config()) assert isinstance(recovered.children[0].strategy, PVStrategy) assert isinstance(recovered.children[1].strategy, PVStrategy) assert isinstance(recovered.children[2].strategy, HomeMeterStrategy)
def test_non_attr_param(): area1 = Area('area1', [], PVStrategy()) recovered1 = area_from_string(area_to_string(area1)) assert recovered1.strategy.min_selling_rate == ConstSettings.PVSettings.MIN_SELLING_RATE
def test_strategy_roundtrip_with_params(): area = Area('area', [], PVStrategy(panel_count=42, risk=1)) area_str = area_to_string(area) assert json.loads(area_str)['strategy']['kwargs']['risk'] == 1 recovered = area_from_string(area_str) assert recovered.strategy.panel_count == 42