# -*- coding: utf-8 -*- import random import pytest import os from flaky import flaky if not hasattr(random, "choices"): def choices(population, *, k=1): return [random.choice(population) for _ in range(k)] random.choices = choices @pytest.fixture(scope="module") def py_router(stars_path): from ed_lrr_gui import PyRouter stars_path, names = stars_path router = PyRouter(lambda status: None) router.load(stars_path) resolved_systems = router.resolve_systems(*names) for name in names: err = "Failed to resolve {}".format(name) assert name in resolved_systems, err yield router, resolved_systems class Test_ED_LRR(object): # noqa: H601 @pytest.mark.dependency() @flaky(max_runs=10, min_passes=5) def test_load_and_resolve(self, stars_path): stars_path, names = stars_path from ed_lrr_gui import PyRouter router = PyRouter(lambda status: None) router.load(stars_path) resolved_systems = router.resolve_systems(*names) for name in names: err = "Failed to resolve {}".format(name) assert name in resolved_systems, err @pytest.mark.dependency(depends=["Test_ED_LRR::test_load_and_resolve"]) @flaky(max_runs=10, min_passes=5) def test_zero_range_fails(self, py_router): r, resolved_systems = py_router waypoints = random.choices(list(resolved_systems.values()), k=2) err = pytest.raises(RuntimeError, r.route, waypoints, 0) err.match(r"No route from .* to .* found!") beam_widths = [256, 512, 1024, 0] greedyness = [0, 0.5, 1] n_workers = [0, os.cpu_count()] ranges = [30, 50] @pytest.mark.dependency(depends=["Test_ED_LRR::test_load_and_resolve"]) @flaky(max_runs=10, min_passes=2) @pytest.mark.parametrize("workers", n_workers, ids=lambda v: "workers:{}".format(v)) @pytest.mark.parametrize("jump_range", ranges, ids=lambda v: "range:{}".format(v)) @pytest.mark.parametrize( "greedyness", greedyness, ids=lambda v: "greedyness:{}".format(v) ) @pytest.mark.parametrize( "beam_width", beam_widths, ids=lambda v: "beam_width:{}".format(v) ) def test_route(self, py_router, jump_range, workers, greedyness, beam_width): r, resolved_systems = py_router waypoints = random.choices(list(resolved_systems.values()), k=2) args = waypoints, jump_range, greedyness, beam_width, workers err = "Failed to route for waypoints: {}".format(waypoints) route_len = r.route(*args) assert route_len != 0, err del r # TODO: verify hop distance and inclusion of waypoints