File: test_config_instance.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (157 lines) | stat: -rw-r--r-- 4,965 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Test for the configuration module / Instance."""

import pytest

from asusrouter.config import (
    CONFIG_DEFAULT,
    CONFIG_DEFAULT_BOOL,
    ARConfig,
    ARConfigKey as ARConfKey,
    ARInstanceConfig,
    safe_bool_config,
)

TEST_INSTANCE_DEFAULT_BOOL = not CONFIG_DEFAULT_BOOL
TEST_INSTANCE_DEFAULTS = {
    ARConfKey.OPTIMISTIC_TEMPERATURE: TEST_INSTANCE_DEFAULT_BOOL
}


@pytest.fixture(autouse=True)
def reset_global() -> None:
    """Reset the configuration before each test."""

    ARConfig.set(ARConfKey.OPTIMISTIC_DATA, CONFIG_DEFAULT_BOOL)
    ARConfig.set(ARConfKey.OPTIMISTIC_TEMPERATURE, CONFIG_DEFAULT_BOOL)
    ARConfig.set(ARConfKey.ROBUST_BOOTTIME, CONFIG_DEFAULT_BOOL)


class TestInstance:
    """Tests for the empty instance configuration."""

    def test_reset(self) -> None:
        """Test resetting the instance configuration."""

        inst = ARInstanceConfig(TEST_INSTANCE_DEFAULTS)

        # Instance value applied
        assert (
            inst.get(ARConfKey.OPTIMISTIC_TEMPERATURE)
            is TEST_INSTANCE_DEFAULT_BOOL
        )

        # Make sure that it did not change the global config
        assert ARConfig.get(
            ARConfKey.OPTIMISTIC_TEMPERATURE
        ) is CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)

    def test_get(self) -> None:
        """Test getting the instance configuration."""

        inst = ARInstanceConfig()

        # Global value is returned
        assert inst.get(
            ARConfKey.OPTIMISTIC_TEMPERATURE
        ) is CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)

    def test_remove(self) -> None:
        """Test removing the instance configuration."""

        inst = ARInstanceConfig(TEST_INSTANCE_DEFAULTS)

        # Remove instance configuration
        inst.remove(ARConfKey.OPTIMISTIC_TEMPERATURE)

        # Global value is returned
        assert inst.get(
            ARConfKey.OPTIMISTIC_TEMPERATURE
        ) is CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)

    def test_set_nonexistent(self) -> None:
        """Test setting a non-existent instance configuration."""

        inst = ARInstanceConfig()

        # Set a non-existent configuration
        with pytest.raises(KeyError, match="Register it before setting."):
            inst.set(ARConfKey.OPTIMISTIC_TEMPERATURE, True)

        # Global value is returned
        assert inst.get(
            ARConfKey.OPTIMISTIC_TEMPERATURE
        ) is CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)

    def test_register_and_set(self) -> None:
        """Test registering and setting an instance configuration."""

        inst = ARInstanceConfig()

        # Register a new configuration option
        inst.register(ARConfKey.OPTIMISTIC_TEMPERATURE, safe_bool_config)

        # Set the instance configuration
        inst.set(ARConfKey.OPTIMISTIC_TEMPERATURE, TEST_INSTANCE_DEFAULT_BOOL)

        # Instance value is returned
        assert (
            inst.get(ARConfKey.OPTIMISTIC_TEMPERATURE)
            is TEST_INSTANCE_DEFAULT_BOOL
        )

        # Global value is unchanged
        assert ARConfig.get(
            ARConfKey.OPTIMISTIC_TEMPERATURE
        ) is CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)

    def test_register_does_not_change_global(self) -> None:
        """Test registering a configuration does influence global config."""

        inst = ARInstanceConfig()
        key = ARConfKey.OPTIMISTIC_TEMPERATURE

        # Register a new configuration option
        inst.register(key, safe_bool_config)

        # Global value is returned
        assert inst.get(key) is CONFIG_DEFAULT.get(key)

    def test_global_dynamic(self) -> None:
        """Test that global dynamic changes are reflected."""

        inst = ARInstanceConfig()

        global_default = CONFIG_DEFAULT.get(ARConfKey.OPTIMISTIC_TEMPERATURE)
        global_not_default = not global_default

        # Global value is returned
        assert inst.get(ARConfKey.OPTIMISTIC_TEMPERATURE) is global_default

        # Change the global configuration
        ARConfig.set(ARConfKey.OPTIMISTIC_TEMPERATURE, global_not_default)

        # Instance reflects the global change
        assert inst.get(ARConfKey.OPTIMISTIC_TEMPERATURE) is global_not_default

    def test_instance_independence(self) -> None:
        """Test that instance configurations are independent."""

        inst1 = ARInstanceConfig(TEST_INSTANCE_DEFAULTS)
        inst2 = ARInstanceConfig(TEST_INSTANCE_DEFAULTS)

        # Change instance 1 configuration
        inst1.set(
            ARConfKey.OPTIMISTIC_TEMPERATURE, not TEST_INSTANCE_DEFAULT_BOOL
        )

        # Instance 1 reflects the change
        assert (
            inst1.get(ARConfKey.OPTIMISTIC_TEMPERATURE)
            is not TEST_INSTANCE_DEFAULT_BOOL
        )

        # Instance 2 is unaffected
        assert (
            inst2.get(ARConfKey.OPTIMISTIC_TEMPERATURE)
            is TEST_INSTANCE_DEFAULT_BOOL
        )