Skip to content

Commit

Permalink
Merge pull request #1339 from gridsingularity/fix-create-future-markets
Browse files Browse the repository at this point in the history
Fix create_future_markets to avoid creating any future market if the global future duration is 0
  • Loading branch information
Hassan754 authored Dec 16, 2021
2 parents d31969d + 54734bb commit 03a1937
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/gsy_e/models/market/future.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ 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 +
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
Expand Down
28 changes: 25 additions & 3 deletions tests/market/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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
Expand Down Expand Up @@ -84,12 +86,32 @@ 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()) == 5
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]:
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 +
Expand All @@ -108,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):
Expand Down

0 comments on commit 03a1937

Please sign in to comment.