Skip to content

Commit

Permalink
Add and update tests cases
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Aug 29, 2024
1 parent d69cca9 commit 6e383cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
43 changes: 20 additions & 23 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
19 changes: 10 additions & 9 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6e383cf

Please sign in to comment.