File: test_system_network.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (91 lines) | stat: -rw-r--r-- 2,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import freeOrionAIInterface as fo
from unittest.mock import patch

import pytest

from AIDependencies import INVALID_ID
from universe import system_network


class TestUniverse:
    connected_systems = {
        1: {2, 7},
        2: {1, 3},
        3: {2, 4, 5, 6},
        4: {3, 5},
        5: {3, 4, 6},
        6: {3, 5, 7},
        7: {1, 6, 8},
        8: {7, 9},
        9: {8, 10},
        10: {9},
        11: {12},
        12: {11},
    }

    # fo interface asks for an empire_id, so we need two arguments as well
    def getImmediateNeighbors(self, sys_id, _):
        return TestUniverse.connected_systems.get(sys_id, {})

    @staticmethod
    def systemsConnected(sx, sy, _):
        return sx in system_network.systems_connected_to_system(sy)


@pytest.fixture(autouse=True)
def connected_systems(monkeypatch):
    monkeypatch.setattr(fo, "getUniverse", TestUniverse)


def test_connections():
    """
    Test that the graph is bi-directional.
    This simplifies the implementation of TestUniverse and makes it easier to determine the expected results.
    """
    for i1, lanes in TestUniverse.connected_systems.items():
        for i2 in lanes:
            assert i1 in TestUniverse.connected_systems[i2]


@pytest.mark.parametrize(
    ("sys_id", "expected"),
    (
        (1, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
        (4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
        (12, {11, 12}),
        (INVALID_ID, set()),
    ),
)
def test_systems_connected_to_system(sys_id, expected):
    assert system_network.systems_connected_to_system(sys_id) == expected


@pytest.mark.parametrize(
    ("sys_id", "n", "expected"),
    (
        (1, 0, {1}),
        (1, 1, {1, 2, 7}),
        (1, 2, {1, 2, 3, 6, 7, 8}),
        (1, 3, {1, 2, 3, 4, 5, 6, 7, 8, 9}),
        (1, 4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
        (1, 5, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
        (10, 1, {9, 10}),
        (10, 2, {8, 9, 10}),
        (10, 3, {7, 8, 9, 10}),
        (10, 4, {1, 6, 7, 8, 9, 10}),
        (10, 5, {1, 2, 3, 5, 6, 7, 8, 9, 10}),
        (11, 5, {11, 12}),
    ),
)
def test_within_n_jumps(sys_id, n, expected):
    assert system_network.within_n_jumps(sys_id, n) == expected


@pytest.mark.parametrize(
    ("s1", "s2", "expected"), ((1, 2, True), (1, 12, False), (11, 1, False), (11, 12, True), (11, 13, False))
)
def test_systems_connected(s1, s2, expected, monkeypatch):
    with patch("universe.system_network.get_capital_sys_id") as get_capital_sys_id:
        get_capital_sys_id.return_value = 1
        assert system_network.systems_connected(s1, s2) == expected
        assert system_network.systems_connected(s2, s1) == expected