def test_update(self, db, manager): s = Uptime() s.update() manager.add(s) rows = db.fetch_all(manager.readings_table_prefix + s.id) assert len(rows) == 0 update(db, s, manager.readings_table_prefix)
def test_add_and_delete_sensor(self, sensor_manager, event_manager): t = YieldTestThread(event_manager.subscribe) t.start() sensor_manager.add(Uptime()) sensor_manager.add(Uptime()) sensor_manager.add(Uptime()) sensor_manager.add(Uptime()) sensor_manager.delete(sensor_manager.items[2].id) time.sleep(0.2) assert t.no_calls == 5 t.raise_errors()
def test_delete(self, db, manager): id1 = add(manager, Uptime()) # add one sensor twice (should not add a duplicate) s = Uptime() add(manager, s) id2 = add(manager, s) id3 = add(manager, Uptime()) assert len(manager.items) == 3 manager.delete(id2) assert len(manager.items) == 2 assert manager[id1].id == id1 assert manager[id2] is None assert manager[id3].id == id3
def test_update_after_load(self, db, manager): s = Uptime() manager.add(s) update(db, s, manager.readings_table_prefix) assert len(db.fetch_all(manager.readings_table_prefix + s.id)) == 2 manager.load() update(db, s, manager.readings_table_prefix) assert len(db.fetch_all(manager.readings_table_prefix + s.id)) == 4
def test_update_sensor(self, sensor_manager, event_manager): t = YieldTestThread(event_manager.subscribe) t.start() sensor_manager.add(Uptime()) sensor_manager.updater.check_actions() # sensor should not be updated again, as it was just updated sensor_manager.updater.check_actions() time.sleep(0.2) assert t.no_calls == 2 t.raise_errors()
def test_remnants(self, db, manager): id = add(manager, Uptime()) # table exits? assert any([id in tab_name for tab_name in db.list_tables()]) manager.delete(id) # table gone? assert not any([id in tab_name for tab_name in db.list_tables()]) rows = db.fetch_all(manager.item_table) for row_id, js in rows: assert id != row_id
para_str = ", ".join(["?"] * len(parameters)) self.execute("INSERT INTO {} VALUES({})".format(table_name, para_str), parameters, commit=True) def drop(self, table_name): table_name = _scrub_table_name(table_name) self.execute("DROP TABLE IF EXISTS {}".format(table_name), commit=True) def remove_old_readings(self, table_name, older_than): table_name = _scrub_table_name(table_name) self.execute("DELETE from {} where time < ?".format(table_name), (older_than, )) if __name__ == "__main__": db = Database(":memory:", echo=True) db.object_table("sensors") db.object_table("cats") from basicmonitor.sensors import Uptime, HTML db.save_object("sensors", Uptime()) print(db.fetch_all("sensors")) sensor = HTML(url="http://google.com") db.channel_table(sensor) db.channel_table(sensor) db.channel_table(sensor) print(db.list_tables())
def make_sensor_with_data(sensor_manager): sensor = Uptime() sensor_manager.add(sensor) for _ in range(100): sensor.update() return sensor