Commit | Line | Data |
---|---|---|
4f8716dc PH |
1 | import sys |
2 | import mock | |
4f8716dc PH |
3 | |
4 | ||
e9c93677 PH |
5 | def force_reimport(module): |
6 | """Force the module under test to be re-imported. | |
7 | ||
8 | Because pytest runs all tests within the same scope (this makes me cry) | |
9 | we have to do some manual housekeeping to avoid tests polluting each other. | |
10 | ||
11 | Since conftest.py already does some sys.modules mangling I see no reason not to | |
12 | do the same thing here. | |
13 | """ | |
14 | if "." in module: | |
15 | steps = module.split(".") | |
16 | else: | |
17 | steps = [module] | |
18 | ||
19 | for i in range(len(steps)): | |
20 | module = ".".join(steps[0:i + 1]) | |
21 | try: | |
22 | del sys.modules[module] | |
23 | except KeyError: | |
24 | pass | |
25 | ||
26 | ||
be4d0fc9 | 27 | def test_gas_setup(GPIO, smbus): |
8ab4cd2d | 28 | from enviroplus import gas |
8c9d0615 | 29 | gas._is_setup = False |
4f8716dc PH |
30 | gas.setup() |
31 | gas.setup() | |
32 | ||
33 | ||
be4d0fc9 | 34 | def test_gas_read_all(GPIO, smbus): |
8ab4cd2d | 35 | from enviroplus import gas |
8c9d0615 | 36 | gas._is_setup = False |
4f8716dc PH |
37 | result = gas.read_all() |
38 | ||
39 | assert type(result.oxidising) == float | |
40 | assert int(result.oxidising) == 16641 | |
41 | ||
42 | assert type(result.reducing) == float | |
43 | assert int(result.reducing) == 16727 | |
44 | ||
45 | assert type(result.nh3) == float | |
46 | assert int(result.nh3) == 16813 | |
47 | ||
48 | assert "Oxidising" in str(result) | |
49 | ||
50 | ||
be4d0fc9 | 51 | def test_gas_read_each(GPIO, smbus): |
8ab4cd2d | 52 | from enviroplus import gas |
8c9d0615 | 53 | gas._is_setup = False |
4f8716dc PH |
54 | |
55 | assert int(gas.read_oxidising()) == 16641 | |
56 | assert int(gas.read_reducing()) == 16727 | |
57 | assert int(gas.read_nh3()) == 16813 | |
8c9d0615 PH |
58 | |
59 | ||
be4d0fc9 | 60 | def test_gas_read_adc(GPIO, smbus): |
8c9d0615 PH |
61 | from enviroplus import gas |
62 | gas._is_setup = False | |
63 | ||
64 | gas.enable_adc(True) | |
65 | gas.set_adc_gain(2.048) | |
66 | assert gas.read_adc() == 0.255 | |
67 | ||
68 | ||
be4d0fc9 | 69 | def test_gas_read_adc_default_gain(GPIO, smbus): |
8c9d0615 PH |
70 | from enviroplus import gas |
71 | gas._is_setup = False | |
72 | ||
73 | gas.enable_adc(True) | |
e9c93677 PH |
74 | gas.set_adc_gain(gas.MICS6814_GAIN) |
75 | assert gas.read_adc() == 0.765 | |
8c9d0615 PH |
76 | |
77 | ||
be4d0fc9 | 78 | def test_gas_read_adc_str(GPIO, smbus): |
8c9d0615 PH |
79 | from enviroplus import gas |
80 | gas._is_setup = False | |
81 | ||
82 | gas.enable_adc(True) | |
83 | gas.set_adc_gain(2.048) | |
84 | assert 'ADC' in str(gas.read_all()) | |
e9c93677 PH |
85 | |
86 | ||
87 | def test_gas_cleanup(GPIO, smbus): | |
88 | force_reimport('enviroplus.gas') | |
89 | from enviroplus import gas | |
90 | ||
91 | gas.cleanup() | |
92 | ||
93 | GPIO.output.assert_called_with(gas.MICS6814_HEATER_PIN, 0) |