annotate py/test.py @ 292:28eb733cb803

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