Repository: projectmesa/mesa
Pull request #2491 (2024-11-11 13:30:58)

test app init of examples
First stab at testing the exmamples app. 

This already tests if there are any errors in app.py itself and if solara starts without any exceptions by calling app.page. We still don't know if anything is displayed and if the buttons etc. do anything, but initial errors are caught. 

Corvince: @quaquel  this seems to already catch an error in WolfSheep related to the ABMSimulator?
github-actions[bot]: Performance benchmarks:

| Model           | Size   | Init time [95% CI]      | Run time [95% CI]       |
|-----------------|--------|-------------------------|-------------------------|
| BoltzmannWealth | small  | 🔴 +6.1% [+3.7%, +8.5%] | 🔵 -0.4% [-0.6%, -0.1%] |
| BoltzmannWealth | large  | 🔵 +0.3% [-0.7%, +1.2%] | 🔵 +0.2% [-1.4%, +1.5%] |
| Schelling       | small  | 🔵 +1.1% [+0.7%, +1.4%] | 🔵 +0.3% [+0.1%, +0.5%] |
| Schelling       | large  | 🔵 -0.1% [-0.6%, +0.4%] | 🔵 +3.2% [+1.8%, +4.8%] |
| WolfSheep       | small  | 🔵 +0.5% [+0.1%, +0.9%] | 🔵 -2.1% [-2.3%, -1.8%] |
| WolfSheep       | large  | 🔵 -0.8% [-1.5%, -0.2%] | 🟢 -6.5% [-9.0%, -4.1%] |
| BoidFlockers    | small  | 🔵 -0.8% [-1.3%, -0.3%] | 🔵 +0.0% [-0.8%, +0.8%] |
| BoidFlockers    | large  | 🔵 +0.5% [-0.0%, +1.1%] | 🔵 -0.3% [-0.9%, +0.5%] |

EwoutH: I did so way more complex things to try to accomplish this.

Thanks, we really need it.
quaquel: > @quaquel this seems to already catch an error in WolfSheep related to the ABMSimulator?

in app.py, change the line to `model = WolfSheep(simulator=simulator, grass=True)`. I don't understand how this has slipped through. My hunch is the split of #2470 into two separate PRs and the resulting rebase mess. I though I tested everything afterwards, but I most somehow have overlookted this.
Corvince: > I did so way more complex things to try to accomplish this.

Same! Luckily, this is a simple solution



> in app.py, change the line to `model = WolfSheep(simulator=simulator, grass=True)`

I thought I am crazy, because I wanted to do it on GitHub directly and it was already updated. But it was just you being faster. Thanks! 

Patch:
diff --git a/mesa/examples/advanced/wolf_sheep/app.py b/mesa/examples/advanced/wolf_sheep/app.py
index fa66d2bf..878c6797 100644
--- a/mesa/examples/advanced/wolf_sheep/app.py
+++ b/mesa/examples/advanced/wolf_sheep/app.py
@@ -83,7 +83,7 @@ lineplot_component = make_plot_component(
 )
 
 simulator = ABMSimulator()
-model = WolfSheep(simulator, grass=True)
+model = WolfSheep(simulator=simulator, grass=True)
 
 page = SolaraViz(
     model,
diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py
index 8d9a2e25..10718e8a 100644
--- a/mesa/visualization/solara_viz.py
+++ b/mesa/visualization/solara_viz.py
@@ -276,7 +276,7 @@ def SimulatorController(
         running.value = True
         simulator.reset()
         model.value = model.value = model.value.__class__(
-            simulator, **model_parameters.value
+            simulator=simulator, **model_parameters.value
         )
 
     def do_play_pause():


Test patch:
diff --git a/tests/test_examples.py b/tests/test_examples.py
index 98d1d580..0e8a7edc 100644
--- a/tests/test_examples.py
+++ b/tests/test_examples.py
@@ -13,6 +13,10 @@ from mesa.examples import (
 
 
 def test_boltzmann_model():  # noqa: D103
+    from mesa.examples.basic.boltzmann_wealth_model import app
+
+    app.page  # noqa: B018
+
     model = BoltzmannWealth(seed=42)
 
     for _i in range(10):
@@ -20,24 +24,40 @@ def test_boltzmann_model():  # noqa: D103
 
 
 def test_conways_game_model():  # noqa: D103
+    from mesa.examples.basic.conways_game_of_life import app
+
+    app.page  # noqa: B018
+
     model = ConwaysGameOfLife(seed=42)
     for _i in range(10):
         model.step()
 
 
 def test_schelling_model():  # noqa: D103
+    from mesa.examples.basic.schelling import app
+
+    app.page  # noqa: B018
+
     model = Schelling(seed=42)
     for _i in range(10):
         model.step()
 
 
 def test_virus_on_network():  # noqa: D103
+    from mesa.examples.basic.virus_on_network import app
+
+    app.page  # noqa: B018
+
     model = VirusOnNetwork(seed=42)
     for _i in range(10):
         model.step()
 
 
 def test_boid_flockers():  # noqa: D103
+    from mesa.examples.basic.boid_flockers import app
+
+    app.page  # noqa: B018
+
     model = BoidFlockers(seed=42)
 
     for _i in range(10):
@@ -45,6 +65,10 @@ def test_boid_flockers():  # noqa: D103
 
 
 def test_epstein():  # noqa: D103
+    from mesa.examples.advanced.epstein_civil_violence import app
+
+    app.page  # noqa: B018
+
     model = EpsteinCivilViolence(seed=42)
 
     for _i in range(10):
@@ -52,6 +76,10 @@ def test_epstein():  # noqa: D103
 
 
 def test_pd_grid():  # noqa: D103
+    from mesa.examples.advanced.pd_grid import app
+
+    app.page  # noqa: B018
+
     model = PdGrid(seed=42)
 
     for _i in range(10):
@@ -59,6 +87,10 @@ def test_pd_grid():  # noqa: D103
 
 
 def test_sugarscape_g1mt():  # noqa: D103
+    from mesa.examples.advanced.sugarscape_g1mt import app
+
+    app.page  # noqa: B018
+
     model = SugarscapeG1mt(seed=42)
 
     for _i in range(10):
@@ -66,8 +98,11 @@ def test_sugarscape_g1mt():  # noqa: D103
 
 
 def test_wolf_sheep():  # noqa: D103
+    from mesa.examples.advanced.wolf_sheep import app
     from mesa.experimental.devs import ABMSimulator
 
+    app.page  # noqa: B018
+
     simulator = ABMSimulator()
     WolfSheep(seed=42, simulator=simulator)
     simulator.run_for(10)