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 with missing measures - empty dict payload - non dict payloads ''' payloads = [ 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_metadata_and_measures_map(self): ''' test that meta-data maps to tags and measures map to fileds ''' # 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_payload = forwarder.process_queue() response_tags = response_payload[0]['tags'] expected_tags = self.payload_timestamped['meta-data'] response_fields = response_payload[0]['fields'] expected_fields = self.payload_timestamped['measures'] # test response with self.subTest('tags'): self.assertEqual(response_tags, expected_tags) with self.subTest('fields'): self.assertEqual(response_fields, expected_fields)
def test_sensor_topics_map_to_measure(self): ''' test that sensor-reading and sensor-error topics map to corresponding measures in db payload ''' topics = ['sensor-reading', 'sensor-error'] 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_payload = forwarder.process_queue() # test response response_measurement = response_payload[0]['measurement'] expected_measurement = topic + 's' with self.subTest(topic): self.assertEqual(response_measurement, expected_measurement)
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_partial_payload_handled(self): ''' test that missing parts of payload is handled ''' with self.subTest('no meta-data'): # message should process, with no tags added to db_payload object # reset to empty queue forwarder.incoming_queue = [] # add sensor-readings messsage message = { 'topic': 'test/sensor-reading', 'payload': self.payload_missing_metadata } forwarder.incoming_queue.append(message) # process queue response_payload = forwarder.process_queue() self.assertEqual(len(response_payload), 1) self.assertFalse( response_payload[0]['tags']) # tags should be empty
def test_not_sensor_topics_dont_map_to_any_measure(self): ''' test that none sensor topics don't map to a measure in db payload ''' topics = [ 'nonsense', 'test/nonsense', 'messages', 'test/messages/location' ] 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_payload = forwarder.process_queue() # test response payload is empty with self.subTest(topic): self.assertFalse(response_payload)
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_payload = forwarder.process_queue() # print(response_payload) # test response response_time = response_payload[0]['time'] expected_time = self.now self.assertEqual(response_time, expected_time)
def test_notimestamp_maps(self): ''' test that if no timestamp exists one is created and mapped to time ''' # reset to empty queue forwarder.incoming_queue = [] # add sensor-readings messsage message = { 'topic': 'test/sensor-reading', 'payload': self.payload_notimestamp } forwarder.incoming_queue.append(message) # process queue response_payload = forwarder.process_queue() print(response_payload) # test response # timestamp compare # response_time = datetime.datetime.timestamp(datetime.datetime.strptime(response_payload[0]['time'], "%Y-%m-%dT%H:%M:%S.%f")) # expected_time = datetime.datetime.timestamp(datetime.datetime.utcnow()) # print(response_time) # print(expected_time) # self.assertAlmostEqual(response_time, expected_time, delta=2) # datetime compare response_time = datetime.datetime.strptime(response_payload[0]['time'], "%Y-%m-%dT%H:%M:%S.%f") expected_time = datetime.datetime.utcnow() # print(response_time) # print(expected_time) self.assertAlmostEqual(response_time, expected_time, delta=datetime.timedelta(seconds=15))