Skip to content

Commit

Permalink
Merge pull request #274 from Dallinger/serialization-tests-and-fixes
Browse files Browse the repository at this point in the history
Serialization tests and fixes
  • Loading branch information
jessesnyder authored Oct 12, 2023
2 parents 92dcd0d + 8bf68ce commit 630d01f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
13 changes: 9 additions & 4 deletions dlgr/griduniverse/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,14 @@ def deserialize(self, state):

self.players = {}
for player_state in state["players"]:
player_state["color_name"] = player_state.pop("color", None)
# Avoid mutating the caller's data
new_state = player_state.copy()
new_state["color_name"] = new_state.pop("color", None)
player = Player(
pseudonym_locale=self.pseudonyms_locale,
pseudonym_gender=self.pseudonyms_gender,
grid=self,
**player_state,
**new_state,
)
self.players[player.id] = player

Expand All @@ -587,9 +589,12 @@ def deserialize(self, state):
if "items" in state:
self.item_locations = {}
for item_state in state["items"]:
# TODO verify this works at some point!
item_props = self.item_config[item_state["item_id"]]
obj = Item(item_config=item_props, **item_state)
invalid_params = ["item_id", "maturity"]
item_params = {
k: v for k, v in item_state.items() if k not in invalid_params
}
obj = Item(item_props, **item_params)
self.item_locations[tuple(obj.position)] = obj

def instructions(self):
Expand Down
14 changes: 9 additions & 5 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def stub_config():
for key in default_keys:
config.register(*key)
config.extend(defaults.copy())
# Patch load() so we don't update any key/value pairs from actual files:
# (Note: this is blindly cargo-culted in from dallinger's equivalent fixture.
# Not certain it's necessary here.)
config.load = mock.Mock(side_effect=lambda: setattr(config, "ready", True))
config.ready = True

return config
Expand All @@ -99,12 +103,12 @@ def active_config(stub_config):
"""Loads the standard config as the active configuration returned by
dallinger.config.get_config() and returns it.
"""
from dallinger.config import get_config
from dallinger import config as c

config = get_config()
config.data = stub_config.data
config.ready = True
return config
orig_config = c.config
c.config = stub_config
yield c.config
c.config = orig_config


@pytest.fixture
Expand Down
41 changes: 41 additions & 0 deletions test/test_gridworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@ def test_spawn_item_at_random(self, gridworld):
assert gridworld.items_updated is True
assert len(gridworld.item_locations.keys()) == 1

def test_replenish_items_boosts_item_count_to_target(self, gridworld):
target = sum(item.get("item_count") for item in gridworld.item_config.values())

gridworld.replenish_items()

target == len(gridworld.item_locations)


@pytest.mark.usefixtures("env")
class TestSerialize(object):
def test_serializes_players(self, gridworld):
player = mock.Mock()
player.serialize.return_value = "Serialized Player"
gridworld.players = {1: player}

values = gridworld.serialize()

assert values["players"] == ["Serialized Player"]

def test_serializes_game_state(self, gridworld):
values = gridworld.serialize()

assert values["round"] == 0
assert values["donation_active"] == gridworld.donation_active
assert values["rows"] == gridworld.rows
Expand Down Expand Up @@ -59,6 +72,34 @@ def test_food_and_wall_serialization_disabled(self, gridworld):
assert values.get("food") is None


class TestDeserialize(object):
def test_round_trip(self, gridworld):
# Walls ("classic")
gridworld.walls_density = 0.0001
gridworld.build_labyrinth()
# Items
gridworld.replenish_items()
# Players
gridworld.spawn_player(1)
gridworld.spawn_player(2)

# Save state, so we can verify that we restore all the same values
saved = gridworld.serialize()

# Clear everything, so we can verify it's restored via
# deserialization:
gridworld.wall_locations.clear()
gridworld.item_locations.clear()
gridworld.players.clear()
gridworld.round = None

gridworld.deserialize(saved)

refetched = gridworld.serialize()

assert saved == refetched


@pytest.mark.usefixtures("env")
class TestRoundState(object):
def test_check_round_not_started(self, gridworld):
Expand Down

0 comments on commit 630d01f

Please sign in to comment.