comparison py/test.py @ 145:6517ddee3187

few more bits
author Matt Johnston <matt@ucc.asn.au>
date Thu, 29 Nov 2012 23:50:40 +0800
parents 482d7852b511
children b32e5a11a4cb
comparison
equal deleted inserted replaced
144:482d7852b511 145:6517ddee3187
1 #!/usr/bin/env python2.7
2 import StringIO
3
1 import unittest 4 import unittest
2 import sensor_ds18b20 5 import sensor_ds18b20
6 import params
3 7
4 class TestSensors(unittest.TestCase): 8 class TestSensors(unittest.TestCase):
5 def setUp(self): 9 def setUp(self):
6 self.sensors = sensor_ds18b20.DS18B20s(None) 10 self.sensors = sensor_ds18b20.DS18B20s(None)
7 11
8 def test_re(self): 12 def test_sensors_regex(self):
9 f1 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES 13 f1 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
10 6e 01 4b 46 7f ff 02 10 71 t=22875 14 6e 01 4b 46 7f ff 02 10 71 t=22875
11 """ 15 """
12 val = self.sensors.do_sensor_name('blank', f1) 16 val = self.sensors.do_sensor_name('blank', f1)
13 self.assertEqual(val, 22.875) 17 self.assertEqual(val, 22.875)
22 6e 01 4b 46 7f ff 02 10 71 t=-1 26 6e 01 4b 46 7f ff 02 10 71 t=-1
23 """ 27 """
24 val = self.sensors.do_sensor_name('blank', f3) 28 val = self.sensors.do_sensor_name('blank', f3)
25 self.assertEqual(val, -0.001) 29 self.assertEqual(val, -0.001)
26 30
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):
53 jsbuf = StringIO.StringIO('{"fridge_setpoint": 999}')
54
55 self.params.load(f=jsbuf)
56 self.assertEqual(self.params.fridge_setpoint, 999)
57
58 with self.assertRaises(params.Params.Error):
59 jsbuf = StringIO.StringIO('{"something_else": 999}')
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):
66 jsbuf = StringIO.StringIO()
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
27 unittest.main() 74 unittest.main()