Skip to content

Commit a144f25

Browse files
authored
Test load_config() (hardbyte#1396)
* listener_test: Do not modify environment for other tests * network_test: Do not modify environment for other tests Also do not skip the test since there will always be an interface (previously set globally and now prepared in setUp function. * test_load_file_config: Test with different values Testing with the same interface values would not show possible issues in case there were any. * tests: Add test for load_config Add tests for load_config with contexts and environment variables. Closes hardbyte#345 Signed-off-by: Martin Kletzander <[email protected]> Signed-off-by: Martin Kletzander <[email protected]>
1 parent 89c395f commit a144f25

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

test/listener_test.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
from .data.example_data import generate_message
1717

18-
channel = "virtual_channel_0"
19-
can.rc["interface"] = "virtual"
20-
2118
logging.basicConfig(level=logging.DEBUG)
2219

2320
# makes the random number generator deterministic
@@ -55,10 +52,15 @@ def testClassesImportable(self):
5552

5653
class BusTest(unittest.TestCase):
5754
def setUp(self):
55+
# Save all can.rc defaults
56+
self._can_rc = can.rc
57+
can.rc = {"interface": "virtual"}
5858
self.bus = can.interface.Bus()
5959

6060
def tearDown(self):
6161
self.bus.shutdown()
62+
# Restore the defaults
63+
can.rc = self._can_rc
6264

6365

6466
class ListenerTest(BusTest):

test/network_test.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
import can
1515

1616
channel = "vcan0"
17-
can.rc["interface"] = "virtual"
1817

1918

20-
@unittest.skipIf("interface" not in can.rc, "Need a CAN interface")
2119
class ControllerAreaNetworkTestCase(unittest.TestCase):
2220
"""
2321
This test ensures that what messages go in to the bus is what comes out.
@@ -42,6 +40,15 @@ class ControllerAreaNetworkTestCase(unittest.TestCase):
4240
for b in range(num_messages)
4341
)
4442

43+
def setUp(self):
44+
# Save all can.rc defaults
45+
self._can_rc = can.rc
46+
can.rc = {"interface": "virtual"}
47+
48+
def tearDown(self):
49+
# Restore the defaults
50+
can.rc = self._can_rc
51+
4552
def producer(self, ready_event, msg_read):
4653
self.client_bus = can.interface.Bus(channel=channel)
4754
ready_event.wait()

test/test_load_config.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import shutil
5+
import tempfile
6+
import unittest
7+
from tempfile import NamedTemporaryFile
8+
9+
import can
10+
11+
12+
class LoadConfigTest(unittest.TestCase):
13+
configuration = {
14+
"default": {"interface": "serial", "channel": "0"},
15+
"one": {"interface": "kvaser", "channel": "1", "bitrate": 100000},
16+
"two": {"channel": "2"},
17+
}
18+
19+
def setUp(self):
20+
# Create a temporary directory
21+
self.test_dir = tempfile.mkdtemp()
22+
23+
def tearDown(self):
24+
# Remove the directory after the test
25+
shutil.rmtree(self.test_dir)
26+
27+
def _gen_configration_file(self, sections):
28+
with NamedTemporaryFile(
29+
mode="w", dir=self.test_dir, delete=False
30+
) as tmp_config_file:
31+
content = []
32+
for section in sections:
33+
content.append("[{}]".format(section))
34+
for k, v in self.configuration[section].items():
35+
content.append("{} = {}".format(k, v))
36+
tmp_config_file.write("\n".join(content))
37+
return tmp_config_file.name
38+
39+
def _dict_to_env(self, d):
40+
return {f"CAN_{k.upper()}": str(v) for k, v in d.items()}
41+
42+
def test_config_default(self):
43+
tmp_config = self._gen_configration_file(["default"])
44+
config = can.util.load_config(path=tmp_config)
45+
self.assertEqual(config, self.configuration["default"])
46+
47+
def test_config_whole_default(self):
48+
tmp_config = self._gen_configration_file(self.configuration)
49+
config = can.util.load_config(path=tmp_config)
50+
self.assertEqual(config, self.configuration["default"])
51+
52+
def test_config_whole_context(self):
53+
tmp_config = self._gen_configration_file(self.configuration)
54+
config = can.util.load_config(path=tmp_config, context="one")
55+
self.assertEqual(config, self.configuration["one"])
56+
57+
def test_config_merge_context(self):
58+
tmp_config = self._gen_configration_file(self.configuration)
59+
config = can.util.load_config(path=tmp_config, context="two")
60+
expected = self.configuration["default"]
61+
expected.update(self.configuration["two"])
62+
self.assertEqual(config, expected)
63+
64+
def test_config_merge_environment_to_context(self):
65+
tmp_config = self._gen_configration_file(self.configuration)
66+
env_data = {"interface": "serial", "bitrate": 125000}
67+
env_dict = self._dict_to_env(env_data)
68+
with unittest.mock.patch.dict("os.environ", env_dict):
69+
config = can.util.load_config(path=tmp_config, context="one")
70+
expected = self.configuration["one"]
71+
expected.update(env_data)
72+
self.assertEqual(config, expected)
73+
74+
def test_config_whole_environment(self):
75+
tmp_config = self._gen_configration_file(self.configuration)
76+
env_data = {"interface": "socketcan", "channel": "3", "bitrate": 250000}
77+
env_dict = self._dict_to_env(env_data)
78+
with unittest.mock.patch.dict("os.environ", env_dict):
79+
config = can.util.load_config(path=tmp_config, context="one")
80+
expected = self.configuration["one"]
81+
expected.update(env_data)
82+
self.assertEqual(config, expected)
83+
84+
85+
if __name__ == "__main__":
86+
unittest.main()

test/test_load_file_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class LoadFileConfigTest(unittest.TestCase):
1212
configuration = {
1313
"default": {"interface": "virtual", "channel": "0"},
14-
"one": {"interface": "virtual", "channel": "1"},
14+
"one": {"interface": "kvaser", "channel": "1"},
1515
"two": {"channel": "2"},
1616
"three": {"extra": "extra value"},
1717
}

0 commit comments

Comments
 (0)