annotate py/test.py @ 452:25bdf95f0e29

config for sensor IDs
author Matt Johnston <matt@ucc.asn.au>
date Thu, 20 Dec 2012 22:26:18 +0800
parents 8fdf86ea41e7
children 185621f47040
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
1 #!/usr/bin/env python2.7
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
2 import StringIO
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
3
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import unittest
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import sensor_ds18b20
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
6 import params
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 class TestSensors(unittest.TestCase):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 def setUp(self):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 self.sensors = sensor_ds18b20.DS18B20s(None)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
12 def test_sensors_regex(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 f1 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 6e 01 4b 46 7f ff 02 10 71 t=22875
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
16 val = self.sensors.do_sensor('blank', f1)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.assertEqual(val, 22.875)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 f2 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 NO
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 6e 01 4b 46 7f ff 02 10 71 t=22875
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
22 val = self.sensors.do_sensor('blank', f2)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.assertEqual(val, None)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 f3 = """6e 01 4b 46 7f ff 02 10 71 : crc=71 YES
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 6e 01 4b 46 7f ff 02 10 71 t=-1
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
28 val = self.sensors.do_sensor('blank', f3)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 self.assertEqual(val, -0.001)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
31 class TestParams(unittest.TestCase):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
32 def setUp(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
33 self.params = params.Params()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
34
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
35 def test_params_basic(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
36 defparams = params.Params()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
37 self.assertEqual(defparams.overshoot_factor,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
38 params._FIELD_DEFAULTS['overshoot_factor'])
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
39
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
40 # fetching a bad parameter fails
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
41 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
42 x = self.params.param_that_doesnt_exist
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
43
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
44 # setting a parameter
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
45 defparams.overshoot_factor = 8877
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
46 self.assertEqual(defparams.overshoot_factor, 8877)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
47
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
48 # setting a bad parameter fails
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
49 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
50 self.params.somewrongthing = 5
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
51
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
52 def test_params_load(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
53 jsbuf = StringIO.StringIO('{"fridge_setpoint": 999}')
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
54
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
55 self.params.load(f=jsbuf)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
56 self.assertEqual(self.params.fridge_setpoint, 999)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
57
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
58 with self.assertRaises(params.Params.Error):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
59 jsbuf = StringIO.StringIO('{"something_else": 999}')
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
60 self.params.load(f=jsbuf)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
61
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
62 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
63 x = self.params.something_else
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
64
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
65 def test_params_save(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
66 jsbuf = StringIO.StringIO()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
67
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
68 self.params.overshoot_delay = 123
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
69 self.params.save(f=jsbuf)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
70
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
71 s = jsbuf.getvalue()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
72 self.assertTrue('"overshoot_delay": 123' in s, msg=s)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
73
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 unittest.main()