145
|
1 #!/usr/bin/env python2.7 |
230
|
2 import io |
145
|
3 |
144
|
4 import unittest |
|
5 import sensor_ds18b20 |
145
|
6 import params |
144
|
7 |
|
8 class TestSensors(unittest.TestCase): |
|
9 def setUp(self): |
|
10 self.sensors = sensor_ds18b20.DS18B20s(None) |
|
11 |
145
|
12 def test_sensors_regex(self): |
144
|
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 """ |
148
|
16 val = self.sensors.do_sensor('blank', f1) |
144
|
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 """ |
148
|
22 val = self.sensors.do_sensor('blank', f2) |
144
|
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 """ |
148
|
28 val = self.sensors.do_sensor('blank', f3) |
144
|
29 self.assertEqual(val, -0.001) |
|
30 |
145
|
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): |
230
|
53 jsbuf = io.StringIO('{"fridge_setpoint": 999}') |
145
|
54 |
|
55 self.params.load(f=jsbuf) |
|
56 self.assertEqual(self.params.fridge_setpoint, 999) |
|
57 |
|
58 with self.assertRaises(params.Params.Error): |
230
|
59 jsbuf = io.StringIO('{"something_else": 999}') |
145
|
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): |
230
|
66 jsbuf = io.StringIO() |
145
|
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 |
144
|
74 unittest.main() |