annotate py/test.py @ 579:4a6e75ed2ed4

fix gpio chown script
author Matt Johnston <matt@ucc.asn.au>
date Mon, 30 Nov 2015 21:40:45 +0800
parents 453c4c0eab3a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
1 import io
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
2
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import unittest
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import sensor_ds18b20
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
5 import params
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 class TestSensors(unittest.TestCase):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 def setUp(self):
572
453c4c0eab3a syntax fixes, doesn't make sense yet for asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
9 self.sensors = sensor_ds18b20.SensorDS18B20(None)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
11 def test_sensors_regex(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 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
13 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
14 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
15 val = self.sensors.do_sensor('blank', f1)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 self.assertEqual(val, 22.875)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 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
19 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
20 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
21 val = self.sensors.do_sensor('blank', f2)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.assertEqual(val, None)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 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
25 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
26 """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
27 val = self.sensors.do_sensor('blank', f3)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 self.assertEqual(val, -0.001)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
30 class TestParams(unittest.TestCase):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
31 def setUp(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
32 self.params = params.Params()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
33
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
34 def test_params_basic(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
35 defparams = params.Params()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
36 self.assertEqual(defparams.overshoot_factor,
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
37 params._FIELD_DEFAULTS['overshoot_factor'])
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
38
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
39 # fetching a bad parameter fails
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
40 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
41 x = self.params.param_that_doesnt_exist
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
42
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
43 # setting a parameter
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
44 defparams.overshoot_factor = 8877
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
45 self.assertEqual(defparams.overshoot_factor, 8877)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
46
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
47 # setting a bad parameter fails
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
48 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
49 self.params.somewrongthing = 5
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
50
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
51 def test_params_load(self):
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
52 jsbuf = io.StringIO('{"fridge_setpoint": 999}')
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
53
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
54 self.params.load(f=jsbuf)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
55 self.assertEqual(self.params.fridge_setpoint, 999)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
56
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
57 with self.assertRaises(params.Params.Error):
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
58 jsbuf = io.StringIO('{"something_else": 999}')
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
59 self.params.load(f=jsbuf)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
60
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
61 with self.assertRaises(KeyError):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
62 x = self.params.something_else
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
63
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
64 def test_params_save(self):
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
65 jsbuf = io.StringIO()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
66
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
67 self.params.overshoot_delay = 123
572
453c4c0eab3a syntax fixes, doesn't make sense yet for asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
68 s = self.params.save_string()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
69 self.assertTrue('"overshoot_delay": 123' in s, msg=s)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
70
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 unittest.main()