Merge pull request #57 from pimoroni/test-tweaks-and-linting
[EVA-2020-02-2.git] / library / tests / test_setup.py
1 import sys
2 import mock
3
4
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
27 def test_gas_setup(GPIO, smbus):
28 from enviroplus import gas
29 gas._is_setup = False
30 gas.setup()
31 gas.setup()
32
33
34 def test_gas_read_all(GPIO, smbus):
35 from enviroplus import gas
36 gas._is_setup = False
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
51 def test_gas_read_each(GPIO, smbus):
52 from enviroplus import gas
53 gas._is_setup = False
54
55 assert int(gas.read_oxidising()) == 16641
56 assert int(gas.read_reducing()) == 16727
57 assert int(gas.read_nh3()) == 16813
58
59
60 def test_gas_read_adc(GPIO, smbus):
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
69 def test_gas_read_adc_default_gain(GPIO, smbus):
70 from enviroplus import gas
71 gas._is_setup = False
72
73 gas.enable_adc(True)
74 gas.set_adc_gain(gas.MICS6814_GAIN)
75 assert gas.read_adc() == 0.765
76
77
78 def test_gas_read_adc_str(GPIO, smbus):
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())
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)