def test_delete_entries_older_than_10_days_from_invalid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=40), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 10 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/invalid_topic' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/days', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual('OK', json.loads(self.client.last_publish)['result']) self.assertEqual('0', json.loads(self.client.last_publish)['values'])
def test_get_entries_with_invalid_time_range(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 25 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/query/invalid_time', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual('KO', json.loads(self.client.last_publish)['result']) self.assertEqual('Invalid unit time', json.loads(self.client.last_publish)['error'])
def test_private_method_get_log_from_desired_topic_newer_than(self): with test_database(test_db, (Log, Topic), create_tables=True): Log.create(timestamp=datetime.now() - timedelta(seconds=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic2') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=10), value="12", topic='/test/topic') logs = LogController() query_result = logs._LogController__get_logs_newer_than('/test/topic', 25) self.assertEqual(2, len(query_result))
def test_add_log_entry(self): with test_database(test_db, (Log, Topic), create_tables=True): message = msg(topic='/test/home/sensor', payload='123445') logs = LogController() result = logs.add_entry(message) parsedResponse = json.loads(result) self.assertEqual('OK', parsedResponse['result']) self.assertEqual(1, Log.select().count())
def test_private_method_get_last_entry_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): Log.create(timestamp=datetime.now() - timedelta(seconds=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic2') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic') timestamp = datetime.now() Log.create(timestamp=timestamp, value="12", topic='/test/topic') logs = LogController() query_result = logs._LogController__get_last_entry_from_topic('/test/topic') self.assertEqual(timestamp.strftime("%Y-%m-%d %H:%M:%S"), query_result['timestamp'])
def test_on_message_delete_last_entry_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='10') Log.create(timestamp=datetime.now(), topic='/test/topic/test2', value='11') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='12') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/last', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) result_logs = Log.select().where(Log.topic == '/test/topic/test1') self.assertTrue('OK', json.loads(self.client.last_publish)['result']) self.assertEqual(1, result_logs.count()) self.assertEqual('10', result_logs[0].value)
def __delete_entries_from_topic_older_than(self, topic, date): query = Log.delete().where( (Log.timestamp <= datetime.now() - timedelta(seconds=date)) & (Log.topic == topic)) result = query.execute() if result: return result else: return '0'
def test_on_message_add_log_entry_to_not_valid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') Topic.create(name='/test/topic/test1') self.msg = msg(topic='/test/topic/test2', payload='12') mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual(0, Log.select().count())
def test_get_entries_newer_than_25_days(self): with test_database(test_db, (Log, Topic), create_tables=True): self.msg.topic = Settings.ROOT_TOPIC + '/log/days' Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic') logs = LogController() query_result = logs.get_topic_entries(self.msg) dic_result = json.loads(query_result) self.assertEqual(2, len(dic_result['values']))
def test_get_last_entry_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.msg.topic = Settings.ROOT_TOPIC + '/log/last' Log.create(timestamp=datetime.now() - timedelta(seconds=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic2') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic') timestamp = datetime.now() Log.create(timestamp=timestamp, value="12", topic='/test/topic') logs = LogController() query_result = logs.get_topic_entries(self.msg) dic_result = json.loads(query_result) self.assertEqual(timestamp.strftime("%Y-%m-%d %H:%M:%S"), dic_result['values'][0]['timestamp'])
def __get_last_entry_from_topic(self, topic): try: result = Log.select().where(Log.topic == topic).order_by( Log.timestamp.desc()).get() return { 'timestamp': result.timestamp.strftime("%Y-%m-%d %H:%M:%S"), 'value': result.value } except: return {}
def add_entry(self, msg): payload = Payload() if hasattr(msg, 'topic') and hasattr(msg, 'payload'): log_register = Log.create(timestamp=datetime.now(), topic=msg.topic, value=str(msg.payload)) payload.topic = 'topic' payload.result = 'OK' else: payload.result = 'KO' return payload.get_json()
def test_private_method_delete_last_entry_from_non_existing_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic') logs = LogController() result = logs._LogController__delete_last_entry_from_topic('/test/topic2') self.assertTrue(result) data_after_delete = Log.select() self.assertEqual(3, data_after_delete.count())
def test_get_entries_newer_than_25_days_invalid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.msg.topic = Settings.ROOT_TOPIC + '/log/days' self.payload['topic'] = '/test/invalid/topic' self.msg.payload = json.dumps(self.payload) Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic') logs = LogController() query_result = logs.get_topic_entries(self.msg) dic_result = json.loads(query_result) self.assertEqual('OK', dic_result['result']) self.assertFalse('values' in dic_result)
def test_on_message_get_last_entry_from_invalid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='10') Log.create(timestamp=datetime.now(), topic='/test/topic/test2', value='11') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='12') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test_invalid' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/query/last', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertTrue('OK', json.loads(self.client.last_publish)['result']) self.assertFalse(json.loads(self.client.last_publish)['values'][0])
def __delete_last_entry_from_topic(self, topic): try: last_entry = Log.select().where(Log.topic == topic).order_by( Log.id.desc()).get() result = last_entry.delete_instance() if result == 1: return '1' else: return '0' except Log.DoesNotExist: return '1' except: return '0'
def __get_logs_newer_than(self, topic, date_initial): query = Log.select().where((Log.timestamp.between( datetime.now() - timedelta(seconds=date_initial), datetime.now())) & (Log.topic == topic)) query_logs = [] for log in query: query_logs.append({ 'timestamp': log.timestamp.strftime("%Y-%m-%d %H:%M:%S"), 'value': log.value }) return query_logs
def test_delete_older_than_x_days_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.msg.topic = Settings.ROOT_TOPIC + '/delete/days' Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic') logs = LogController() query_result = logs.delete_topic_entries(self.msg) dic_result = json.loads(query_result) self.assertEqual(1, dic_result['values']) data_after_delete = Log.select() self.assertEqual(2, data_after_delete.count())
def test_delete_older_than_x_invalid_unit_time_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.msg.topic = Settings.ROOT_TOPIC + '/delete/years' Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic') logs = LogController() query_result = logs.delete_topic_entries(self.msg) dic_result = json.loads(query_result) self.assertEqual('KO', dic_result['result'])
def test_delete_entries_older_than_25_days(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=40), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 25 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/days', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) result_logs = Log.select().where(Log.topic == '/test/topic/test1') self.assertEqual(2, result_logs.count())
def test_private_method_delete_entries_older_than_from_non_existing_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): Log.create(timestamp=datetime.now() - timedelta(seconds=50), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=40), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=30), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=20), value="12", topic='/test/topic') Log.create(timestamp=datetime.now() - timedelta(seconds=10), value="12", topic='/test/topic') logs = LogController() result = logs._LogController__delete_entries_from_topic_older_than('/test/topic2', 25) self.assertEqual('0', result) data_after_delete = Log.select() self.assertEqual(5, data_after_delete.count())