From 2ebfff06fa1eda86c02be3a68e651e2921a22b33 Mon Sep 17 00:00:00 2001 From: Mason Zarns Date: Mon, 15 Jul 2024 18:34:04 -0500 Subject: [PATCH] Add json test cases; linting --- tests/integration_tests/test_speed.py | 9 ++++- tests/test_game.py | 48 ++++++++++++------------- tests/test_json.py | 51 ++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/tests/integration_tests/test_speed.py b/tests/integration_tests/test_speed.py index 2659ac11..d875ab40 100644 --- a/tests/integration_tests/test_speed.py +++ b/tests/integration_tests/test_speed.py @@ -5,7 +5,10 @@ from catanatron.models.player import Color, SimplePlayer, RandomPlayer from catanatron.players.weighted_random import WeightedRandomPlayer from catanatron_gym.features import create_sample -from catanatron_experimental.machine_learning.players.minimax import AlphaBetaPlayer, SameTurnAlphaBetaPlayer +from catanatron_experimental.machine_learning.players.minimax import ( + AlphaBetaPlayer, + SameTurnAlphaBetaPlayer, +) RANDOM_SEED = 0 @@ -62,6 +65,7 @@ def test_simpleplayer_speed(benchmark): SimplePlayer(Color.ORANGE), ] game = Game(players, seed=RANDOM_SEED) + def _play_game(game): for _ in range(100): game.play_tick() @@ -78,6 +82,7 @@ def test_weightedrandom_speed(benchmark): WeightedRandomPlayer(Color.ORANGE), ] game = Game(players, seed=RANDOM_SEED) + def _play_game(game): for _ in range(100): game.play_tick() @@ -94,6 +99,7 @@ def test_alphabeta_speed(benchmark): AlphaBetaPlayer(Color.ORANGE), ] game = Game(players, seed=RANDOM_SEED) + def _play_game(game): for _ in range(100): game.play_tick() @@ -110,6 +116,7 @@ def test_same_turn_alphabeta_speed(benchmark): SameTurnAlphaBetaPlayer(Color.ORANGE), ] game = Game(players, seed=RANDOM_SEED) + def _play_game(game): for _ in range(100): game.play_tick() diff --git a/tests/test_game.py b/tests/test_game.py index 3bfcfab8..c00d7561 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -13,9 +13,7 @@ player_num_resource_cards, ) from catanatron.state_functions import player_key -from catanatron.models.actions import ( - generate_playable_actions -) +from catanatron.models.actions import generate_playable_actions from catanatron.models.enums import ( BRICK, ORE, @@ -358,8 +356,8 @@ def test_longest_road_steal(): board.build_road(p0.color, (7, 8)) board.build_road(p0.color, (8, 9)) board.build_road(p0.color, (9, 10)) - game.state.player_state[f'{p0_key}_VICTORY_POINTS'] = 1 - game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] = 1 + game.state.player_state[f"{p0_key}_VICTORY_POINTS"] = 1 + game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] = 1 # p1 has longest road of lenght 5 board.build_settlement(p1.color, 28, True) @@ -368,14 +366,14 @@ def test_longest_road_steal(): board.build_road(p1.color, (29, 30)) board.build_road(p1.color, (30, 31)) board.build_road(p1.color, (31, 32)) - game.state.player_state[f'{p1_key}_VICTORY_POINTS'] = 3 - game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] = 3 - game.state.player_state[f'{p1_key}_HAS_ROAD'] = True + game.state.player_state[f"{p1_key}_VICTORY_POINTS"] = 3 + game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] = 3 + game.state.player_state[f"{p1_key}_HAS_ROAD"] = True # Required to be able to apply actions other than rolling or initial build phase. game.state.current_prompt = ActionPrompt.PLAY_TURN game.state.is_initial_build_phase = False - game.state.player_state[f'{p0_key}_HAS_ROLLED'] = True + game.state.player_state[f"{p0_key}_HAS_ROLLED"] = True game.state.playable_actions = generate_playable_actions(game.state) # Set up player0 to build two roads and steal longest road. @@ -386,25 +384,25 @@ def test_longest_road_steal(): # Matching length of longest road does not steal longest road. apply_action(game.state, Action(p0.color, ActionType.BUILD_ROAD, road1)) - assert game.state.player_state[f'{p0_key}_LONGEST_ROAD_LENGTH'] == 5 - assert game.state.player_state[f'{p0_key}_HAS_ROAD'] == False - assert game.state.player_state[f'{p0_key}_VICTORY_POINTS'] == 1 - assert game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] == 1 - assert game.state.player_state[f'{p1_key}_LONGEST_ROAD_LENGTH'] == 5 - assert game.state.player_state[f'{p1_key}_HAS_ROAD'] == True - assert game.state.player_state[f'{p1_key}_VICTORY_POINTS'] == 3 - assert game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] == 3 + assert game.state.player_state[f"{p0_key}_LONGEST_ROAD_LENGTH"] == 5 + assert game.state.player_state[f"{p0_key}_HAS_ROAD"] == False + assert game.state.player_state[f"{p0_key}_VICTORY_POINTS"] == 1 + assert game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] == 1 + assert game.state.player_state[f"{p1_key}_LONGEST_ROAD_LENGTH"] == 5 + assert game.state.player_state[f"{p1_key}_HAS_ROAD"] == True + assert game.state.player_state[f"{p1_key}_VICTORY_POINTS"] == 3 + assert game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] == 3 # Surpassing length of longest road steals longest road and VPs. apply_action(game.state, Action(p0.color, ActionType.BUILD_ROAD, road2)) - assert game.state.player_state[f'{p0_key}_LONGEST_ROAD_LENGTH'] == 6 - assert game.state.player_state[f'{p0_key}_HAS_ROAD'] == True - assert game.state.player_state[f'{p0_key}_VICTORY_POINTS'] == 3 - assert game.state.player_state[f'{p0_key}_ACTUAL_VICTORY_POINTS'] == 3 - assert game.state.player_state[f'{p1_key}_LONGEST_ROAD_LENGTH'] == 5 - assert game.state.player_state[f'{p1_key}_HAS_ROAD'] == False - assert game.state.player_state[f'{p1_key}_VICTORY_POINTS'] == 1 - assert game.state.player_state[f'{p1_key}_ACTUAL_VICTORY_POINTS'] == 1 + assert game.state.player_state[f"{p0_key}_LONGEST_ROAD_LENGTH"] == 6 + assert game.state.player_state[f"{p0_key}_HAS_ROAD"] == True + assert game.state.player_state[f"{p0_key}_VICTORY_POINTS"] == 3 + assert game.state.player_state[f"{p0_key}_ACTUAL_VICTORY_POINTS"] == 3 + assert game.state.player_state[f"{p1_key}_LONGEST_ROAD_LENGTH"] == 5 + assert game.state.player_state[f"{p1_key}_HAS_ROAD"] == False + assert game.state.player_state[f"{p1_key}_VICTORY_POINTS"] == 1 + assert game.state.player_state[f"{p1_key}_ACTUAL_VICTORY_POINTS"] == 1 def test_second_placement_takes_cards_from_bank(): diff --git a/tests/test_json.py b/tests/test_json.py index 4d66eca4..08418b6c 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1,3 +1,4 @@ +import pytest import json from catanatron.game import Game @@ -27,9 +28,57 @@ def test_serialization(): assert isinstance(result["actions"], list) -def test_action_from_json(): +def test_action_from_json_maritime_trade(): data = ["RED", "MARITIME_TRADE", ["SHEEP", "SHEEP", "SHEEP", "SHEEP", "ORE"]] action = action_from_json(data) assert action.color == Color.RED assert action.action_type == ActionType.MARITIME_TRADE assert action.value == ("SHEEP", "SHEEP", "SHEEP", "SHEEP", "ORE") + + +def test_action_from_json_play_year_of_plenty_two_resources(): + data = ["RED", "PLAY_YEAR_OF_PLENTY", ["WOOD", "BRICK"]] + action = action_from_json(data) + assert action.color == Color.RED + assert action.action_type == ActionType.PLAY_YEAR_OF_PLENTY + assert action.value == ("WOOD", "BRICK") + + +def test_action_from_json_play_year_of_plenty_one_resource(): + data = ["BLUE", "PLAY_YEAR_OF_PLENTY", ["SHEEP"]] + action = action_from_json(data) + assert action.color == Color.BLUE + assert action.action_type == ActionType.PLAY_YEAR_OF_PLENTY + assert action.value == ("SHEEP",) + + +def test_action_from_json_play_year_of_plenty_invalid(): + data = ["WHITE", "PLAY_YEAR_OF_PLENTY", ["WOOD", "BRICK", "SHEEP"]] + with pytest.raises( + ValueError, match="Year of Plenty action must have 1 or 2 resources" + ): + action_from_json(data) + + +def test_action_from_json_move_robber_with_victim(): + data = ["ORANGE", "MOVE_ROBBER", [[0, 0, 0], "RED", None]] + action = action_from_json(data) + assert action.color == Color.ORANGE + assert action.action_type == ActionType.MOVE_ROBBER + assert action.value == ((0, 0, 0), Color.RED, None) + + +def test_action_from_json_move_robber_without_victim(): + data = ["RED", "MOVE_ROBBER", [[1, -1, 0], None, None]] + action = action_from_json(data) + assert action.color == Color.RED + assert action.action_type == ActionType.MOVE_ROBBER + assert action.value == ((1, -1, 0), None, None) + + +def test_action_from_json_build_road(): + data = ["BLUE", "BUILD_ROAD", [0, 1]] + action = action_from_json(data) + assert action.color == Color.BLUE + assert action.action_type == ActionType.BUILD_ROAD + assert action.value == (0, 1)