Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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))

42 changes: 16 additions & 26 deletions tests/test_world.py
Original file line number Diff line number Diff line change
@@ -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
Loading