From de539455cf33ef941b0912cfdf802f34af8bdfc8 Mon Sep 17 00:00:00 2001 From: Hassan Hammoud Date: Tue, 14 Dec 2021 13:22:53 +0200 Subject: [PATCH 1/2] Fix create_future_markets to avoid creating any future market if the global future duration is 0 --- src/gsy_e/models/market/future.py | 2 ++ tests/market/test_future.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/gsy_e/models/market/future.py b/src/gsy_e/models/market/future.py index a179e2ca1..3c575d805 100644 --- a/src/gsy_e/models/market/future.py +++ b/src/gsy_e/models/market/future.py @@ -178,6 +178,8 @@ def create_future_markets(self, current_market_time_slot: DateTime, slot_length: duration, config: "SimulationConfig") -> None: """Add sub dicts in order dictionaries for future market slots.""" + if not GlobalConfig.FUTURE_MARKET_DURATION_HOURS: + return future_time_slot = current_market_time_slot.add(minutes=slot_length.total_minutes()) most_future_slot = (future_time_slot + duration(hours=GlobalConfig.FUTURE_MARKET_DURATION_HOURS)) diff --git a/tests/market/test_future.py b/tests/market/test_future.py index 12bed9059..e4becf584 100644 --- a/tests/market/test_future.py +++ b/tests/market/test_future.py @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ +from unittest.mock import patch, MagicMock + import pytest from gsy_framework.constants_limits import GlobalConfig, DATE_TIME_FORMAT from gsy_framework.data_classes import Bid, Offer, Trade, TradeBidOfferInfo @@ -84,8 +86,28 @@ class TestFutureMarkets: """Tests that target the future markets.""" @staticmethod + @patch("gsy_e.models.market.future.is_time_slot_in_simulation_duration", + MagicMock()) def test_create_future_markets(future_market): """Test if all future time_slots are created in the order buffers.""" + future_market.offers = {} + future_market.bids = {} + + with patch("gsy_e.models.market.future.GlobalConfig." + "FUTURE_MARKET_DURATION_HOURS", 0): + future_market.create_future_markets( + DEFAULT_CURRENT_MARKET_SLOT, DEFAULT_SLOT_LENGTH, MagicMock() + ) + for buffer in [future_market.slot_bid_mapping, + future_market.slot_offer_mapping, + future_market.slot_trade_mapping]: + assert len(buffer.keys()) == 0 + + with patch("gsy_e.models.market.future.GlobalConfig." + "FUTURE_MARKET_DURATION_HOURS", 1): + future_market.create_future_markets( + DEFAULT_CURRENT_MARKET_SLOT, DEFAULT_SLOT_LENGTH, MagicMock() + ) for buffer in [future_market.slot_bid_mapping, future_market.slot_offer_mapping, future_market.slot_trade_mapping]: From ac78925a26cee653bc3c4e7026e7d3104708315c Mon Sep 17 00:00:00 2001 From: Hassan Hammoud Date: Tue, 14 Dec 2021 13:38:38 +0200 Subject: [PATCH 2/2] Correct the create_future_markets duration starting point --- src/gsy_e/models/market/future.py | 2 +- tests/market/test_future.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gsy_e/models/market/future.py b/src/gsy_e/models/market/future.py index 3c575d805..11fead2c0 100644 --- a/src/gsy_e/models/market/future.py +++ b/src/gsy_e/models/market/future.py @@ -181,7 +181,7 @@ def create_future_markets(self, current_market_time_slot: DateTime, if not GlobalConfig.FUTURE_MARKET_DURATION_HOURS: return future_time_slot = current_market_time_slot.add(minutes=slot_length.total_minutes()) - most_future_slot = (future_time_slot + + most_future_slot = (current_market_time_slot + duration(hours=GlobalConfig.FUTURE_MARKET_DURATION_HOURS)) while future_time_slot <= most_future_slot: if (future_time_slot not in self.slot_bid_mapping and diff --git a/tests/market/test_future.py b/tests/market/test_future.py index e4becf584..3436e1b1f 100644 --- a/tests/market/test_future.py +++ b/tests/market/test_future.py @@ -111,7 +111,7 @@ def test_create_future_markets(future_market): for buffer in [future_market.slot_bid_mapping, future_market.slot_offer_mapping, future_market.slot_trade_mapping]: - assert len(buffer.keys()) == 5 + assert len(buffer.keys()) == 4 future_time_slot = DEFAULT_CURRENT_MARKET_SLOT.add( minutes=DEFAULT_SLOT_LENGTH.total_minutes()) most_future_slot = (future_time_slot + @@ -130,10 +130,10 @@ def test_delete_old_future_markets(future_market): time_slot=time_slot) future_market.trades.append(trade) - count_orders_in_buffers(future_market, 5) + count_orders_in_buffers(future_market, 4) first_future_market = next(iter(future_market.slot_bid_mapping)) future_market.delete_orders_in_old_future_markets(first_future_market) - count_orders_in_buffers(future_market, 4) + count_orders_in_buffers(future_market, 3) @staticmethod def test_offer_is_posted_correctly(future_market):