-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly test basic examples (#2390)
- Loading branch information
Showing
4 changed files
with
34 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,40 @@ | ||
# noqa: D100 | ||
import contextlib | ||
import importlib | ||
import os.path | ||
import sys | ||
import unittest | ||
from mesa.examples import ( | ||
BoidFlockers, | ||
BoltzmannWealthModel, | ||
ConwaysGameOfLife, | ||
Schelling, | ||
VirusOnNetwork, | ||
) | ||
|
||
|
||
def test_examples_imports(): | ||
"""Test examples imports.""" | ||
from mesa.examples import ( | ||
BoidFlockers, | ||
BoltzmannWealthModel, | ||
ConwaysGameOfLife, | ||
Schelling, | ||
VirusOnNetwork, | ||
) | ||
def test_boltzmann_model(): # noqa: D103 | ||
model = BoltzmannWealthModel(seed=42) | ||
|
||
BoltzmannWealthModel() | ||
Schelling() | ||
BoidFlockers() | ||
ConwaysGameOfLife() | ||
VirusOnNetwork() | ||
for _i in range(10): | ||
model.step() | ||
|
||
|
||
def classcase(name): # noqa: D103 | ||
return "".join(x.capitalize() for x in name.replace("-", "_").split("_")) | ||
def test_conways_game_model(): # noqa: D103 | ||
model = ConwaysGameOfLife(seed=42) | ||
for _i in range(10): | ||
model.step() | ||
|
||
|
||
@unittest.skip( | ||
"Skipping TextExamples, because examples folder was moved. More discussion needed." | ||
) | ||
class TestExamples(unittest.TestCase): | ||
"""Test examples' models. | ||
def test_schelling_model(): # noqa: D103 | ||
model = Schelling(seed=42) | ||
for _i in range(10): | ||
model.step() | ||
|
||
This creates a model object and iterates it through | ||
some steps. The idea is to get code coverage, rather than to test the | ||
details of each example's model. | ||
""" | ||
|
||
EXAMPLES = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "../mesa/examples") | ||
) | ||
def test_virus_on_network(): # noqa: D103 | ||
model = VirusOnNetwork(seed=42) | ||
for _i in range(10): | ||
model.step() | ||
|
||
@contextlib.contextmanager | ||
def active_example_dir(self, example): | ||
"""Save and restore sys.path and sys.modules.""" | ||
old_sys_path = sys.path[:] | ||
old_sys_modules = sys.modules.copy() | ||
old_cwd = os.getcwd() | ||
example_path = os.path.abspath(os.path.join(self.EXAMPLES, example)) | ||
try: | ||
sys.path.insert(0, example_path) | ||
os.chdir(example_path) | ||
yield | ||
finally: | ||
os.chdir(old_cwd) | ||
added = [m for m in sys.modules if m not in old_sys_modules] | ||
for mod in added: | ||
del sys.modules[mod] | ||
sys.modules.update(old_sys_modules) | ||
sys.path[:] = old_sys_path | ||
|
||
def test_examples(self): # noqa: D102 | ||
for example in os.listdir(self.EXAMPLES): | ||
if not os.path.isdir(os.path.join(self.EXAMPLES, example)): | ||
continue | ||
if hasattr(self, f"test_{example.replace('-', '_')}"): | ||
# non-standard example; tested below | ||
continue | ||
def test_boid_flockers(): # noqa: D103 | ||
model = BoidFlockers(seed=42) | ||
|
||
print(f"testing example {example!r}") | ||
with self.active_example_dir(example): | ||
try: | ||
# epstein_civil_violence.py at the top level | ||
mod = importlib.import_module("model") | ||
server = importlib.import_module("server") | ||
server.server.render_model() | ||
except ImportError: | ||
# <example>/epstein_civil_violence.py | ||
mod = importlib.import_module(f"{example.replace('-', '_')}.model") | ||
server = importlib.import_module( | ||
f"{example.replace('-', '_')}.server" | ||
) | ||
server.server.render_model() | ||
model_class = getattr(mod, classcase(example)) | ||
model = model_class() | ||
for _ in range(10): | ||
model.step() | ||
self.assertEqual(model.steps, 10) | ||
for _i in range(10): | ||
model.step() |