Skip to content

Commit

Permalink
Allow automatic transition to None - add test for automatic transition
Browse files Browse the repository at this point in the history
  • Loading branch information
silviot committed Oct 12, 2023
1 parent da7479f commit 219de29
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dlgr/griduniverse/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,27 +837,29 @@ def items_changed(self, last_items):
return True
return False

def trigger_transitions(self):
now = time.time()
def trigger_transitions(self, time=time.time):
now = time()
to_change = []
for position, item in self.item_locations.items():
item_type = self.item_config.get(item.item_id)
if not item_type:
continue
if "auto_transition_time" in item_type:
if now - item.creation_timestamp >= item_type["auto_transition_time"]:
new_target_item = Item(
target = item_type.get("auto_transition_target")
new_target_item = target and Item(
id=item.id,
position=position,
item_config=self.item_config[
item_type["auto_transition_target"]
],
item_config=self.item_config[target],
)
to_change.append((position, new_target_item))
if to_change:
self.items_updated = True
for position, new_target_item in to_change:
self.item_locations[position] = new_target_item
if new_target_item is None:
del self.item_locations[position]
else:
self.item_locations[position] = new_target_item

def replenish_items(self):
items_by_type = collections.defaultdict(list)
Expand Down
57 changes: 57 additions & 0 deletions test/test_automatic_transitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import time

import pytest


class TestAutoTransition(object):
messages = []

def test_automatic_transition(self, exp):
from dlgr.griduniverse.experiment import Item

item = Item(
{
"id": 1,
"item_id": "sunflower_sprout",
"name": "Sunflower Sprout",
"n_uses": 1,
}
)
exp.grid.item_locations[(2, 2)] = item

exp.grid.trigger_transitions(time=lambda: time.time() + 5)
assert exp.grid.item_locations[(2, 2)].item_id == "sunflower_bud"

def test_null_target(self, exp):
from dlgr.griduniverse.experiment import Item

item = Item(
{
"id": 1,
"item_id": "sunflower_dried",
"name": "Sunflower Dried",
"n_uses": 1,
}
)
exp.grid.item_locations[(2, 2)] = item

exp.item_config["sunflower_sprout"]["auto_transition_target"] = None
exp.grid.trigger_transitions(time=lambda: time.time() + 5)
assert (2, 2) not in exp.grid.item_locations


@pytest.fixture(autouse=True)
def add_item_config(exp):
"""Make sure item configs needed for these tests are set"""
exp.grid.item_config["sunflower_dried"] = {
"name": "Sunflower dried",
"item_id": "sunflower_dried",
"n_uses": 1,
"auto_transition_time": 5,
}
exp.grid.item_config["sunflower_sprout"] = {
"auto_transition_target": "sunflower_bud",
"auto_transition_time": 5,
"n_uses": 1,
"name": "Sunflower sprout",
}

0 comments on commit 219de29

Please sign in to comment.