Esempio n. 1
0
    def test_single_measurement(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"0a45","+":2,"vac|h":9}'
        
        measurements = Measurement.create_from_udp(msg)

        MockMeasurement.assert_called_once_with(datetime=get_current_datetime(), sensor_id="0a45", type="vacancy", value=9)
        self.assertEqual(measurements['success'][0], MockMeasurement.return_value)
Esempio n. 2
0
    def test_supplied_datetime(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"0a45","+":2,"vac|h":9}'
        mock_datetime = mock.Mock()

        measurements = Measurement.create_from_udp(msg, mock_datetime)

        MockMeasurement.assert_called_once_with(datetime=mock_datetime, sensor_id="0a45", type="vacancy", value=9)
Esempio n. 3
0
    def test_battery_with_centivolts(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"819c","T|C16":71,"L":5,"B|cV":256}'
        expected_calls = [
            mock.call(datetime=get_current_datetime(), sensor_id="819c", type="temperature", value=4.4375),
            mock.call(datetime=get_current_datetime(), sensor_id="819c", type="light", value=5),
            mock.call(datetime=get_current_datetime(), sensor_id="819c", type="battery", value=2.56)
        ]
        mock_measurements = [mock.Mock(), mock.Mock(), mock.Mock()]
        MockMeasurement.side_effect = mock_measurements

        measurements = Measurement.create_from_udp(msg)
        
        self.assertEqual(MockMeasurement.call_args_list, expected_calls)
        self.assertEqual(measurements['success'], mock_measurements)
Esempio n. 4
0
    def test_multiple_measurements(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"0a45","+":2,"vac|h":9,"T|C16":201,"L":0}'
        expected_calls = [
            mock.call(datetime=get_current_datetime(), sensor_id="0a45", type="vacancy", value=9),
            mock.call(datetime=get_current_datetime(), sensor_id="0a45", type="temperature", value=12.5625),
            mock.call(datetime=get_current_datetime(), sensor_id="0a45", type="light", value=0)
        ]
        mock_measurements = [mock.Mock(), mock.Mock(), mock.Mock()]
        MockMeasurement.side_effect = mock_measurements

        measurements = Measurement.create_from_udp(msg)
        
        self.assertEqual(MockMeasurement.call_args_list, expected_calls)
        self.assertEqual(measurements['success'], mock_measurements)
Esempio n. 5
0
    def test_battery_millivolts(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"414a","+":4,"B|mV":3}'
        expected_calls = [
            mock.call(datetime=get_current_datetime(), sensor_id="414a", type="battery", value=0.003),
        ]
        mock_measurements = [mock.Mock()]
        MockMeasurement.side_effect = mock_measurements
        expected_measurements = {
            'success': mock_measurements,
            'failure': []
        }

        measurements = Measurement.create_from_udp(msg)

        self.assertEqual(MockMeasurement.call_args_list, expected_calls)
        self.assertEqual(measurements['success'], expected_measurements['success'])
        self.assertEqual(measurements['failure'], expected_measurements['failure'])
Esempio n. 6
0
    def test_cumulative_valve_travel(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"414a","+":4,"vac|h":3,"v|%":0,"tT|C":7,"vC|%":50}'
        expected_calls = [
            mock.call(datetime=get_current_datetime(), sensor_id="414a", type="vacancy", value=3),
            mock.call(datetime=get_current_datetime(), sensor_id="414a", type="valve_open_percent", value=0),
            mock.call(datetime=get_current_datetime(), sensor_id="414a", type="target_temperature", value=7),
            mock.call(datetime=get_current_datetime(), sensor_id="414a", type="valve_travel", value=50),
        ]
        mock_measurements = [mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock()]
        MockMeasurement.side_effect = mock_measurements
        expected_measurements = {
            'success': mock_measurements,
            'failure': []
        }

        measurements = Measurement.create_from_udp(msg)

        for call in MockMeasurement.call_args_list:
            self.assertIn(call, expected_calls)
        self.assertEqual(measurements['success'], expected_measurements['success'])
        for failure in expected_measurements['failure']:
            self.assertIn(failure, measurements['failure'])
Esempio n. 7
0
from opentrv_sensor.models import Measurement

sample_data_filepath = os.path.join(sample_data_dir, '201501.json')

if __name__ == '__main__':

    f = open(sample_data_filepath, 'rb')

    lines = [line.replace('\n', '') for line in f]

    Measurement.objects.all().delete()

    n_successes = 0
    n_failures = 0
    success_types = set()
    failure_types = set()
    for line in lines[:]:
        print line
        measurements = Measurement.create_from_udp(line)
        n_successes += len(measurements['success'])
        n_failures += len(measurements['failure'])
        for x in measurements['failure']:
            failure_types.add(x['type'])
        # for x in measurements['success']:
        #     success_types.add(x.type)

    print 'n_successes:', n_successes
    print 'n_failures:', n_failures
    print 'failure_types:', failure_types
    # print 'success_types:', success_types
Esempio n. 8
0
from opentrv_sensor.models import Measurement

sample_data_filepath = os.path.join(sample_data_dir, "201501.json")

if __name__ == "__main__":

    f = open(sample_data_filepath, "rb")

    lines = [line.replace("\n", "") for line in f]

    Measurement.objects.all().delete()

    n_successes = 0
    n_failures = 0
    success_types = set()
    failure_types = set()
    for line in lines[:]:
        print line
        measurements = Measurement.create_from_udp(line)
        n_successes += len(measurements["success"])
        n_failures += len(measurements["failure"])
        for x in measurements["failure"]:
            failure_types.add(x["type"])
        # for x in measurements['success']:
        #     success_types.add(x.type)

    print "n_successes:", n_successes
    print "n_failures:", n_failures
    print "failure_types:", failure_types
    # print 'success_types:', success_types
Esempio n. 9
0
    def test_invalid_boiler_measurement_raises_error(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"0a45","+":2,"b":2}'

        with self.assertRaises(Exception) as e:
            measurements = Measurement.create_from_udp(msg)
Esempio n. 10
0
    def test_boiler_measurement(self, MockMeasurement, get_current_datetime):
        msg = '{"@":"0a45","+":2,"b":0}'

        measurements = Measurement.create_from_udp(msg)

        MockMeasurement.assert_called_once_with(datetime=get_current_datetime.return_value, sensor_id="0a45", type="boiler", value=0)