Expand test coverage, bugfix
[EVA-2020-02-2.git] / library / tests / test_setup.py
1 import sys
2 import mock
3 from i2cdevice import MockSMBus
4
5
6 class SMBusFakeDevice(MockSMBus):
7 def __init__(self, i2c_bus):
8 MockSMBus.__init__(self, i2c_bus)
9 self.regs[0x00:0x01] = 0x0f, 0x00
10
11
12 def test_gas_setup():
13 sys.modules['RPi'] = mock.Mock()
14 sys.modules['RPi.GPIO'] = mock.Mock()
15 smbus = mock.Mock()
16 smbus.SMBus = SMBusFakeDevice
17 sys.modules['smbus'] = smbus
18 from enviroplus import gas
19 gas._is_setup = False
20 gas.setup()
21 gas.setup()
22
23
24 def test_gas_read_all():
25 sys.modules['RPi'] = mock.Mock()
26 sys.modules['RPi.GPIO'] = mock.Mock()
27 smbus = mock.Mock()
28 smbus.SMBus = SMBusFakeDevice
29 sys.modules['smbus'] = smbus
30 from enviroplus import gas
31 gas._is_setup = False
32 result = gas.read_all()
33
34 assert type(result.oxidising) == float
35 assert int(result.oxidising) == 16641
36
37 assert type(result.reducing) == float
38 assert int(result.reducing) == 16727
39
40 assert type(result.nh3) == float
41 assert int(result.nh3) == 16813
42
43 assert "Oxidising" in str(result)
44
45
46 def test_gas_read_each():
47 sys.modules['RPi'] = mock.Mock()
48 sys.modules['RPi.GPIO'] = mock.Mock()
49 smbus = mock.Mock()
50 smbus.SMBus = SMBusFakeDevice
51 sys.modules['smbus'] = 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():
61 sys.modules['RPi'] = mock.Mock()
62 sys.modules['RPi.GPIO'] = mock.Mock()
63 smbus = mock.Mock()
64 smbus.SMBus = SMBusFakeDevice
65 sys.modules['smbus'] = smbus
66 from enviroplus import gas
67 gas._is_setup = False
68
69 gas.enable_adc(True)
70 gas.set_adc_gain(2.048)
71 assert gas.read_adc() == 0.255
72
73
74 def test_gas_read_adc_default_gain():
75 sys.modules['RPi'] = mock.Mock()
76 sys.modules['RPi.GPIO'] = mock.Mock()
77 smbus = mock.Mock()
78 smbus.SMBus = SMBusFakeDevice
79 sys.modules['smbus'] = smbus
80 from enviroplus import gas
81 gas._is_setup = False
82
83 gas.enable_adc(True)
84 assert gas.read_adc() == 0.255
85
86
87 def test_gas_read_adc_str():
88 sys.modules['RPi'] = mock.Mock()
89 sys.modules['RPi.GPIO'] = mock.Mock()
90 smbus = mock.Mock()
91 smbus.SMBus = SMBusFakeDevice
92 sys.modules['smbus'] = smbus
93 from enviroplus import gas
94 gas._is_setup = False
95
96 gas.enable_adc(True)
97 gas.set_adc_gain(2.048)
98 assert 'ADC' in str(gas.read_all())