diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..98aed8a --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,35 @@ +from pathlib import Path + +import pytest +import yaml + +from simulator.__main__ import _build_world +from simulator.core.carrier import Carrier +from simulator.core.greenhouse import Greenhouse, Node +from simulator.core.spider import Spider +from simulator.core.world import World +from simulator.dtn.epidemic import EpidemicRouter + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.fixture +def small_world() -> World: + n0 = Node(id="n0", x=0.0, y=0.0, hotspot=0.0) + n1 = Node(id="n1", x=1.0, y=0.0, hotspot=0.0) + greenhouse = Greenhouse(width=5, height=5, nodes=[n0, n1], edges=[("n0", "n1")]) + spiders = [Spider(id="S0", node=n0, storage_capacity=1_000_000)] + carriers = [Carrier(id="C0", node=n0, storage_capacity=10_000_000)] + return World( + greenhouse=greenhouse, + spiders=spiders, + carriers=carriers, + router=EpidemicRouter(), + ) + + +@pytest.fixture +def loaded_world() -> World: + with (ROOT / "simulator/config/default.yaml").open(encoding="utf-8") as config_file: + return _build_world(yaml.safe_load(config_file)) + diff --git a/tests/test_world.py b/tests/test_world.py index d39265a..59871e8 100644 --- a/tests/test_world.py +++ b/tests/test_world.py @@ -1,40 +1,30 @@ -from simulator.core.carrier import Carrier -from simulator.core.greenhouse import Greenhouse, Node -from simulator.core.spider import Spider from simulator.core.world import World -from simulator.dtn.epidemic import EpidemicRouter -def _simple_world() -> World: - n0 = Node(id="n0", x=0.0, y=0.0, hotspot=0.0) - n1 = Node(id="n1", x=1.0, y=0.0, hotspot=0.0) - gh = Greenhouse(width=5, height=5, nodes=[n0, n1], edges=[("n0", "n1")]) - spiders = [Spider(id="S0", node=n0, storage_capacity=1_000_000)] - carriers = [Carrier(id="C0", node=n0, storage_capacity=10_000_000)] - return World(greenhouse=gh, spiders=spiders, carriers=carriers, router=EpidemicRouter()) +def test_world_step_increments_tick(small_world: World): + small_world.step() + assert small_world.tick == 1 -def test_world_step_increments_tick(): - world = _simple_world() - world.step() - assert world.tick == 1 - - -def test_spider_accumulates_data_over_time(): - world = _simple_world() +def test_spider_accumulates_data_over_time(small_world: World): for _ in range(10): - world.step() - assert world.spiders[0].storage_used > 0 + small_world.step() + assert small_world.spiders[0].storage_used > 0 -def test_carrier_receives_dump_when_in_range(): - world = _simple_world() - spider = world.spiders[0] - carrier = world.carriers[0] +def test_carrier_receives_dump_when_in_range(small_world: World): + spider = small_world.spiders[0] + carrier = small_world.carriers[0] # Force spider onto carrier's node and fill storage spider.node = carrier.node spider.storage_used = spider.storage_capacity * 0.8 initial_carrier_storage = carrier.storage_used - world._run_wifi_dumps() + small_world._run_wifi_dumps() assert carrier.storage_used > initial_carrier_storage assert spider.storage_ratio < 0.8 + + +def test_loaded_world_uses_default_config(loaded_world: World): + assert loaded_world.greenhouse.nodes + assert loaded_world.spiders + assert loaded_world.carriers