444
|
1 #!/usr/bin/env python2.7 |
529
|
2 import io |
444
|
3 |
443
|
4 import unittest |
|
5 import sensor_ds18b20 |
444
|
6 import params |
443
|
7 |
|
8 class TestSensors(unittest.TestCase): |
|
9 def setUp(self): |
|
10 self.sensors = sensor_ds18b20.DS18B20s(None) |
|
11 |
444
|
12 def test_sensors_regex(self): |
443
|
13 f1 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES |
|
14 6e 01 4b 46 7f ff 02 10 71 t=22875 |
|
15 """ |
447
|
16 val = self.sensors.do_sensor('blank', f1) |
443
|
17 self.assertEqual(val, 22.875) |
|
18 |
|
19 f2 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 NO |
|
20 6e 01 4b 46 7f ff 02 10 71 t=22875 |
|
21 """ |
447
|
22 val = self.sensors.do_sensor('blank', f2) |
443
|
23 self.assertEqual(val, None) |
|
24 |
|
25 f3 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES |
|
26 6e 01 4b 46 7f ff 02 10 71 t=-1 |
|
27 """ |
447
|
28 val = self.sensors.do_sensor('blank', f3) |
443
|
29 self.assertEqual(val, -0.001) |
|
30 |
444
|
31 class TestParams(unittest.TestCase): |
|
32 def setUp(self): |
|
33 self.params = params.Params() |
|
34 |
|
35 def test_params_basic(self): |
|
36 defparams = params.Params() |
|
37 self.assertEqual(defparams.overshoot_factor, |
|
38 params._FIELD_DEFAULTS['overshoot_factor']) |
|
39 |
|
40 # fetching a bad parameter fails |
|
41 with self.assertRaises(KeyError): |
|
42 x = self.params.param_that_doesnt_exist |
|
43 |
|
44 # setting a parameter |
|
45 defparams.overshoot_factor = 8877 |
|
46 self.assertEqual(defparams.overshoot_factor, 8877) |
|
47 |
|
48 # setting a bad parameter fails |
|
49 with self.assertRaises(KeyError): |
|
50 self.params.somewrongthing = 5 |
|
51 |
|
52 def test_params_load(self): |
529
|
53 jsbuf = io.StringIO('{"fridge_setpoint": 999}') |
444
|
54 |
|
55 self.params.load(f=jsbuf) |
|
56 self.assertEqual(self.params.fridge_setpoint, 999) |
|
57 |
|
58 with self.assertRaises(params.Params.Error): |
529
|
59 jsbuf = io.StringIO('{"something_else": 999}') |
444
|
60 self.params.load(f=jsbuf) |
|
61 |
|
62 with self.assertRaises(KeyError): |
|
63 x = self.params.something_else |
|
64 |
|
65 def test_params_save(self): |
529
|
66 jsbuf = io.StringIO() |
444
|
67 |
|
68 self.params.overshoot_delay = 123 |
|
69 self.params.save(f=jsbuf) |
|
70 |
|
71 s = jsbuf.getvalue() |
|
72 self.assertTrue('"overshoot_delay": 123' in s, msg=s) |
|
73 |
443
|
74 unittest.main() |