class TestAreaClass(unittest.TestCase): def setUp(self): ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True DeviceRegistry.REGISTRY = { "H1 General Load": (33, 35), "H2 General Load": (33, 35), "H1 Storage1": (23, 25), "H1 Storage2": (23, 25), } self.appliance = MagicMock(spec=SimpleAppliance) self.strategy = MagicMock(spec=StorageStrategy) self.config = MagicMock(spec=SimulationConfig) self.config.slot_length = duration(minutes=15) self.config.tick_length = duration(seconds=15) self.config.start_date = today(tz=TIME_ZONE) self.config.sim_duration = duration(days=1) self.area = Area("test_area", None, None, self.strategy, self.appliance, self.config, None, transfer_fee_pct=1) self.area.parent = self.area self.area.children = [self.area] self.area.transfer_fee_pct = 1 self.dispatcher = AreaDispatcher(self.area) self.stats = AreaStats(self.area._markets) def tearDown(self): GlobalConfig.market_count = 1 ConstSettings.GeneralSettings.KEEP_PAST_MARKETS = False def test_respective_area_grid_fee_is_applied(self): self.area = Area(name="Street", children=[Area(name="House")], config=GlobalConfig, transfer_fee_pct=5) self.area.parent = Area(name="GRID") self.area.config.market_count = 1 self.area.activate() assert self.area.next_market.transfer_fee_ratio == 0.05 self.area.next_market.offer(1, 1, "test") assert list(self.area.next_market.offers.values())[0].price == 1.05 def test_markets_are_cycled_according_to_market_count(self): self.area._bc = False for i in range(2, 97): self.config.market_count = i self.area._cycle_markets(False, False) assert len(self.area.all_markets) == i def test_delete_past_markets_instead_of_last(self): self.area = Area(name="Street", children=[Area(name="House")], config=GlobalConfig, transfer_fee_pct=5) self.area.config.market_count = 1 self.area.activate() self.area._bc = False self.area._cycle_markets(False, False, False) assert len(self.area.past_markets) == 0 current_time = today(tz=TIME_ZONE).add(hours=1) self.area._markets.rotate_markets(current_time, self.stats, self.dispatcher) assert len(self.area.past_markets) == 1 self.area._markets.create_future_markets(current_time, True, self.area) current_time = today(tz=TIME_ZONE).add(hours=2) self.area._markets.rotate_markets(current_time, self.stats, self.dispatcher) assert len(self.area.past_markets) == 1 assert list(self.area.past_markets)[-1].time_slot == today( tz=TIME_ZONE).add(hours=1) def test_keep_past_markets(self): ConstSettings.GeneralSettings.KEEP_PAST_MARKETS = True self.area = Area(name="Street", children=[Area(name="House")], config=GlobalConfig, transfer_fee_pct=5) self.area.config.market_count = 1 self.area.activate() self.area._bc = False self.area._cycle_markets(False, False, False) assert len(self.area.past_markets) == 0 current_time = today(tz=TIME_ZONE).add(hours=1) self.area._markets.rotate_markets(current_time, self.stats, self.dispatcher) assert len(self.area.past_markets) == 1 self.area._markets.create_future_markets(current_time, True, self.area) current_time = today(tz=TIME_ZONE).add(hours=2) self.area._markets.rotate_markets(current_time, self.stats, self.dispatcher) assert len(self.area.past_markets) == 2 def test_market_with_most_expensive_offer(self): m1 = MagicMock(spec=Market) o1 = MagicMock(spec=Offer) o1.price = 12 o1.energy = 1 m2 = MagicMock(spec=Market) o2 = MagicMock(spec=Offer) o2.price = 12 o2.energy = 1 m3 = MagicMock(spec=Market) o3 = MagicMock(spec=Offer) o3.price = 12 o3.energy = 1 markets = OrderedDict() td = today(tz=TIME_ZONE) td1 = td + self.config.slot_length m1.time_slot = td1 markets[m1.time_slot] = m1 td2 = td1 + self.config.slot_length m2.time_slot = td2 markets[m2.time_slot] = m2 td3 = td2 + self.config.slot_length m3.time_slot = td3 markets[m3.time_slot] = m3 self.area._markets = MagicMock(spec=AreaMarkets) self.area._markets.markets = markets m1.sorted_offers = [o1, o1] m2.sorted_offers = [o2, o2] m3.sorted_offers = [o3, o3] assert self.area.market_with_most_expensive_offer is m1 o1.price = 19 o2.price = 20 o3.price = 18 assert self.area.market_with_most_expensive_offer is m2 o1.price = 18 o2.price = 19 o3.price = 20 assert self.area.market_with_most_expensive_offer is m3 def test_cycle_markets(self): self.area = Area(name="Street", children=[Area(name="House")], config=GlobalConfig, transfer_fee_pct=1) self.area.parent = Area(name="GRID") self.area.config.market_count = 5 self.area.activate() assert len(self.area.all_markets) == 5 assert len(self.area.balancing_markets) == 5 self.area.current_tick = 900 self.area.tick(is_root_area=True) assert len(self.area.past_markets) == 1 assert len(self.area.past_balancing_markets) == 1 assert len(self.area.all_markets) == 5 assert len(self.area.balancing_markets) == 5
class TestAreaClass(unittest.TestCase): def setUp(self): ConstSettings.BalancingSettings.ENABLE_BALANCING_MARKET = True DeviceRegistry.REGISTRY = { "H1 General Load": (33, 35), "H2 General Load": (33, 35), "H1 Storage1": (23, 25), "H1 Storage2": (23, 25), } self.appliance = MagicMock(spec=SimpleAppliance) self.strategy = MagicMock(spec=StorageStrategy) self.config = MagicMock(spec=SimulationConfig) self.config.slot_length = duration(minutes=15) self.config.tick_length = duration(seconds=15) self.area = Area("test_area", None, self.strategy, self.appliance, self.config, None) self.area.parent = self.area self.area.children = [self.area] def tearDown(self): pass def test_markets_are_cycled_according_to_market_count(self): self.area._bc = False for i in range(2, 100): self.config.market_count = i self.area._cycle_markets(False, False) assert len(self.area.all_markets) == i def test_market_with_most_expensive_offer(self): m1 = MagicMock(spec=Market) o1 = MagicMock(spec=Offer) o1.price = 12 o1.energy = 1 m2 = MagicMock(spec=Market) o2 = MagicMock(spec=Offer) o2.price = 12 o2.energy = 1 m3 = MagicMock(spec=Market) o3 = MagicMock(spec=Offer) o3.price = 12 o3.energy = 1 markets = OrderedDict() markets[DateTime(2018, 1, 1, 12, 0, 0)] = m1 markets[DateTime(2018, 1, 1, 12, 15, 0)] = m2 markets[DateTime(2018, 1, 1, 12, 30, 0)] = m3 self.area._markets = MagicMock(spec=AreaMarkets) self.area._markets.markets = markets m1.sorted_offers = [o1, o1] m2.sorted_offers = [o2, o2] m3.sorted_offers = [o3, o3] assert self.area.market_with_most_expensive_offer is m1 o1.price = 19 o2.price = 20 o3.price = 18 assert self.area.market_with_most_expensive_offer is m2 o1.price = 18 o2.price = 19 o3.price = 20 assert self.area.market_with_most_expensive_offer is m3 def test_cycle_markets(self): self.area = Area(name="Street", children=[Area(name="House")]) self.area.parent = Area(name="GRID") self.area.config.market_count = 5 self.area.activate() assert len(self.area.all_markets) == 5 assert len(self.area.balancing_markets) == 5 self.area.current_tick = 900 self.area.tick(is_root_area=True) assert len(self.area.past_markets) == 1 assert len(self.area.past_balancing_markets) == 1 assert len(self.area.all_markets) == 5 assert len(self.area.balancing_markets) == 5