diff --git a/tests/test_agent.py b/tests/test_agent.py index 23c5158067b..068397b2593 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -12,18 +12,18 @@ def get_unique_identifier(self): class PositionalAgent(Agent): + """Old behavior of Agent creation with unique_id and model as positional arguments. + Can be removed in the future.""" + def __init__(self, unique_id, model): super().__init__(unique_id, model) -class KeywordAgent(Agent): - def __init__(self, unique_id, model, attribute_1=0): - super().__init__(unique_id=unique_id, model=model) - - -class MixedAgent(Agent): - def __init__(self, unique_id, model): - super().__init__(unique_id, model=model) +class NewAgent(Agent): + def __init__(self, model, some_other, arguments=1): + super().__init__(model) + self.some_other = some_other + self.arguments = arguments @pytest.fixture @@ -34,24 +34,21 @@ def model(): def test_creation_with_positional_arguments(model): - """Test agent creation using only positional arguments.""" + """Old behavior of Agent creation with unique_id and model as positional arguments. + Can be removed/updated in the future.""" agent = PositionalAgent(1, model) - assert agent.unique_id == 1 + assert isinstance(agent.unique_id, int) assert agent.model == model + assert isinstance(agent.model, Model) -def test_creation_with_positional_and_keyword_argument(model): - """Test agent creation with one positional and one keyword argument.""" - agent = KeywordAgent(1, model) - assert agent.unique_id == 1 - assert agent.model == model - - -def test_creation_with_keyword_arguments(model): - """Test agent creation using only keyword arguments.""" - agent = MixedAgent(1, model) - assert agent.unique_id == 1 +def test_creation_with_new_arguments(model): + """New behavior of Agent creation with model as the first argument and additional arguments.""" + agent = NewAgent(model, "some_other", 2) assert agent.model == model + assert isinstance(agent.model, Model) + assert agent.some_other == "some_other" + assert agent.arguments == 2 class TestAgentDo(Agent): @@ -88,7 +85,7 @@ def test_agent_removal(): def test_agentset(): # create agentset model = Model() - agents = [TestAgent(model.next_id(), model) for _ in range(10)] + agents = [TestAgent(model) for _ in range(10)] agentset = AgentSet(agents, model) @@ -100,7 +97,7 @@ def test_agentset(): assert a1 == a2 def test_function(agent): - return agent.unique_id > 5 + return agent.unique_id >= 5 assert len(agentset.select(test_function)) == 5 assert len(agentset.select(test_function, n=2)) == 2 diff --git a/tests/test_model.py b/tests/test_model.py index 874d45f935f..cfb236bfed9 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -7,8 +7,6 @@ def test_model_set_up(): assert model.running is True assert model.schedule is None assert model.current_id == 0 - assert model.current_id + 1 == model.next_id() - assert model.current_id == 1 model.step() diff --git a/tests/test_time.py b/tests/test_time.py index a9d8e2e6055..13b781f9876 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -25,8 +25,9 @@ class MockAgent(Agent): Minimalistic agent for testing purposes. """ - def __init__(self, unique_id, model): - super().__init__(unique_id, model) + def __init__(self, model, name=""): + super().__init__(model) + self.name = name self.steps = 0 self.advances = 0 @@ -38,10 +39,10 @@ def kill_other_agent(self): def stage_one(self): if self.model.enable_kill_other_agent: self.kill_other_agent() - self.model.log.append(self.unique_id + "_1") + self.model.log.append(f"{self.name}_1") def stage_two(self): - self.model.log.append(self.unique_id + "_2") + self.model.log.append(f"{self.name}_2") def advance(self): self.advances += 1 @@ -50,7 +51,7 @@ def step(self): if self.model.enable_kill_other_agent: self.kill_other_agent() self.steps += 1 - self.model.log.append(self.unique_id) + self.model.log.append(f"{self.name}") class MockModel(Model): @@ -90,7 +91,7 @@ def __init__(self, shuffle=False, activation=STAGED, enable_kill_other_agent=Fal # Make agents for name in ["A", "B"]: - agent = MockAgent(name, self) + agent = MockAgent(self, name) self.schedule.add(agent) def step(self): @@ -168,7 +169,7 @@ class TestRandomActivation(TestCase): def test_init(self): model = Model() - agents = [MockAgent(model.next_id(), model) for _ in range(10)] + agents = [MockAgent(model) for _ in range(10)] scheduler = RandomActivation(model, agents) assert all(agent in scheduler.agents for agent in agents) @@ -227,7 +228,7 @@ def test_not_sequential(self): model = MockModel(activation=RANDOM) # Create 10 agents for _ in range(10): - model.schedule.add(MockAgent(model.next_id(), model)) + model.schedule.add(MockAgent(model)) # Run 3 steps for _ in range(3): model.step() @@ -273,7 +274,7 @@ class TestRandomActivationByType(TestCase): def test_init(self): model = Model() - agents = [MockAgent(model.next_id(), model) for _ in range(10)] + agents = [MockAgent(model) for _ in range(10)] agents += [Agent(model.next_id(), model) for _ in range(10)] scheduler = RandomActivationByType(model, agents)