annotate py/test.py @ 631:c57821a60e51 rust

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