diff --git a/tests/test_space.py b/tests/test_space.py index 0ec4932a8de..7d9c8484ac8 100644 --- a/tests/test_space.py +++ b/tests/test_space.py @@ -449,6 +449,122 @@ def test_iter_cell_list_contents(self): assert len(cell_list_4) == 1 +class TestSingleGridWithPropertyGrid(unittest.TestCase): + def setUp(self): + self.grid = SingleGrid(10, 10, False) + self.property_layer1 = PropertyLayer("layer1", 10, 10, 0) + self.property_layer2 = PropertyLayer("layer2", 10, 10, 1) + self.grid.add_property_layer(self.property_layer1) + self.grid.add_property_layer(self.property_layer2) + + # Test adding and removing property layers + def test_add_property_layer(self): + self.assertIn("layer1", self.grid.properties) + self.assertIn("layer2", self.grid.properties) + + def test_remove_property_layer(self): + self.grid.remove_property_layer("layer1") + self.assertNotIn("layer1", self.grid.properties) + + def test_add_property_layer_mismatched_dimensions(self): + with self.assertRaises(ValueError): + self.grid.add_property_layer(PropertyLayer("layer3", 5, 5, 0)) + + def test_add_existing_property_layer(self): + with self.assertRaises(ValueError): + self.grid.add_property_layer(self.property_layer1) + + def test_remove_nonexistent_property_layer(self): + with self.assertRaises(ValueError): + self.grid.remove_property_layer("nonexistent_layer") + + # Test selecting cells + def test_select_cells_multi_properties(self): + condition = lambda x: x == 0 + selected_cells = self.grid.select_cells_multi_properties({"layer1": condition}) + self.assertEqual(len(selected_cells), 100) # All cells should be selected + + def test_select_cells_with_multiple_properties(self): + condition1 = lambda x: x == 0 + condition2 = lambda x: x == 1 + selected_cells = self.grid.select_cells_multi_properties( + {"layer1": condition1, "layer2": condition2} + ) + self.assertEqual( + len(selected_cells), 100 + ) # All cells should meet both conditions + + def test_select_cells_with_neighborhood(self): + condition = lambda x: x == 0 + selected_cells = self.grid.select_cells_multi_properties( + {"layer1": condition}, only_neighborhood=True, pos=(5, 5), radius=1 + ) + # Expect a selection of cells around (5, 5) + expected_selection = [ + (4, 4), + (4, 5), + (4, 6), + (5, 4), + (5, 6), + (6, 4), + (6, 5), + (6, 6), + ] + self.assertCountEqual(selected_cells, expected_selection) + + def test_select_no_cells_due_to_conflicting_conditions(self): + condition1 = lambda x: x == 0 # All cells in layer1 meet this + condition2 = lambda x: x != 1 # No cells in layer2 meet this + selected_cells = self.grid.select_cells_multi_properties( + {"layer1": condition1, "layer2": condition2} + ) + self.assertEqual(len(selected_cells), 0) + + # Test moving agents to cells + def test_move_agent_to_random_cell(self): + agent = MockAgent(1, self.grid) + self.grid.place_agent(agent, (5, 5)) + conditions = {"layer1": lambda x: x == 0} + self.grid.move_agent_to_random_cell(agent, conditions) + self.assertNotEqual(agent.pos, (5, 5)) + + def test_move_agent_no_eligible_cells(self): + agent = MockAgent(3, self.grid) + self.grid.place_agent(agent, (5, 5)) + conditions = {"layer1": lambda x: x != 0} # No cell meets this condition + self.grid.move_agent_to_random_cell(agent, conditions) + # Agent should not move + self.assertEqual(agent.pos, (5, 5)) + + # Move to cells with the highest or lowest value in a layer + def test_move_agent_to_extreme_value_cell(self): + agent = MockAgent(2, self.grid) + self.grid.place_agent(agent, (5, 5)) + self.grid.properties["layer2"].set_cell((3, 1), 1.1) + self.grid.move_agent_to_extreme_value_cell(agent, "layer2", "highest") + self.assertEqual(agent.pos, (3, 1)) + + def test_move_agent_to_extreme_value_cell_lowest(self): + agent = MockAgent(4, self.grid) + self.grid.place_agent(agent, (5, 5)) + self.grid.properties["layer2"].set_cell((6, 7), 0) + self.grid.move_agent_to_extreme_value_cell(agent, "layer2", "lowest") + # Agent should move to a cell with the lowest value in layer2 (which is 1 for all cells, so position should not change) + self.assertEqual(agent.pos, (6, 7)) + + # Edge Cases: Invalid property name or mode + def test_invalid_property_name_in_conditions(self): + condition = lambda x: x == 0 + with self.assertRaises(KeyError): + self.grid.select_cells_multi_properties({"nonexistent_layer": condition}) + + def test_invalid_mode_in_move_to_extreme(self): + agent = MockAgent(6, self.grid) + self.grid.place_agent(agent, (5, 5)) + with self.assertRaises(ValueError): + self.grid.move_agent_to_extreme_value_cell(agent, "layer1", "invalid_mode") + + class TestSingleNetworkGrid(unittest.TestCase): GRAPH_SIZE = 10