def test_invalid_payload_handled(self):
        '''
        test that invalid payloads are handled
        
        messages should not be processed and added to db if their payloads are not valid for the db
           - payload missing meta-data
           - payload with missing measures
           - empty dict payload
           - non dict payloads
        '''
        payloads = [
            self.payload_missing_metadata, self.payload_missing_device,
            self.payload_missing_measures, self.payload_empty,
            self.payload_int, self.payload_string
        ]

        for payload in payloads:
            # reset to empty queue
            forwarder.incoming_queue = []

            # add sensor-readings messsage
            message = {'topic': 'test/sensor-reading', 'payload': payload}
            forwarder.incoming_queue.append(message)

            # process queue
            response_payload = forwarder.process_queue()

            with self.subTest(payloads.index(payload)):
                self.assertEqual(len(response_payload), 0)
    def test_sensor_topics_map_to_measure(self):
        '''
        test that sensor-reading topic maps to corresponding table insert in db payload
        '''
        topics = ['sensor-reading']

        for topic in topics:
            # reset to empty queue
            forwarder.incoming_queue = []

            # add sensor-readings messsage
            message = {
                'topic': 'test/' + topic,
                'payload': self.payload_timestamped
            }
            forwarder.incoming_queue.append(message)

            # process queue
            response_statements = forwarder.process_queue()

            # test response
            response = response_statements[0][:len("INSERT INTO SensorReadings"
                                                   )]
            expected = "INSERT INTO SensorReadings"
            with self.subTest(topic):
                self.assertEqual(len(response_statements), 1)
                self.assertEqual(response, expected)
    def test_queue_reset(self):
        '''
        test that queue is emptied
        '''
        # reset to empty queue
        forwarder.incoming_queue = []

        # added some messages to queue
        for _ in range(2):
            message = {
                'topic': 'test/sensor-reading',
                'payload': self.payload_timestamped
            }
            forwarder.incoming_queue.append(message)

        # incoming_queue should be empty after process_queue
        forwarder.process_queue()
        self.assertFalse(forwarder.incoming_queue)
    def test_timestamp_maps(self):
        '''
        test that if timestamp exists and is current it is mapped to time
        '''
        # reset to empty queue
        forwarder.incoming_queue = []

        # add sensor-readings messsage
        message = {
            'topic': 'test/sensor-reading',
            'payload': self.payload_timestamped
        }
        forwarder.incoming_queue.append(message)

        # process queue
        response = forwarder.process_queue()

        i = response[0].find(') VALUES (') + len(') VALUES (') + 1

        # test response
        response_time = response[0][i:i + 26]
        expected_time = self.now_ms
        self.assertEqual(response_time, expected_time)
    def test_not_sensor_topics_dont_map_to_any_measure(self):
        '''
        test that none sensor-reading topics don't map to a measure in db payload
        '''
        topics = [
            'nonsense', 'test/nonsense', 'messages', 'test/messages/location',
            'test/sensor-error'
        ]

        for topic in topics:
            # reset to empty queue
            forwarder.incoming_queue = []

            # add sensor-readings messsage
            message = {'topic': topic, 'payload': self.payload_timestamped}
            forwarder.incoming_queue.append(message)

            # process queue
            response = forwarder.process_queue()

            # test response payload is empty
            with self.subTest(topic):
                self.assertFalse(response)