def test_close_sensor_history_element(self): """ Test case for close_sensor_history_element """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) ser.remove_assignment("room", 1, 0) # OK history = ser.get_sensor_history(sensor.id)["positions"][0] self.assertEqual( { "room": position.room, "bus": position.bus, "index": position.index }, history["position"]) self.assertIsNotNone(history["end_of_service"]) db.session.delete(sensor) db.session.delete(position) db.session.commit()
def test_get_position_history(self): """ Test case for get_position_history """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() sensor2 = Sensor("name2", "mac2", "type", "model", 0) db.session.add(sensor2) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) ser.remove_assignment("room", 1, 0) ser.add_assignment("room", 1, 0, dict(sensor=sensor2.id)) # OK history = sorted(ser.get_position_history("room", 1, 0)["sensors"], key=lambda element: element["start_of_service"]) self.assertEqual(2, len(history)) self.assertEqual(sensor.id, history[0]["sensor"]["id"]) self.assertIsNotNone(history[0]["end_of_service"]) self.assertEqual(sensor2.id, history[1]["sensor"]["id"]) self.assertIsNone(history[1]["end_of_service"]) db.session.delete(sensor) db.session.delete(sensor2) db.session.delete(position) db.session.commit()
def test_get_assigned_sensor(self): """ Test case for get_assigned_sensor """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() # No sensor assigned to position with self.assertRaises(AssignmentNotFoundException): ser.get_assigned_sensor(position.room, position.bus, position.index) ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) # OK found_sensor = ser.get_assigned_sensor(position.room, position.bus, position.index) self.assertEqual(sensor, found_sensor) db.session.delete(sensor) db.session.delete(position) db.session.commit()
def test_add_assignment(self): """ Test case for add_assignment """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() other_sensor = Sensor("other name", "other mac", "type", "model", 0) db.session.add(other_sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() # OK same_sensor = ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) self.assertEqual(sensor, same_sensor) found_position = ser.get_sensor_position(sensor.id) self.assertEqual(position, found_position) found_sensor = ser.get_assigned_sensor(position.room, position.bus, position.index) self.assertEqual(sensor, found_sensor) # Trying to assign a sensor but there is already one with self.assertRaises(AssignmentNotValidException): ser.add_assignment("room", 1, 0, dict(sensor=other_sensor.id)) db.session.delete(sensor) db.session.delete(other_sensor) db.session.delete(position) db.session.commit()
def test_get_sensor_position(self): """ Test case for get_sensor_position """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() # Sensor not assigned with self.assertRaises(AssignmentNotFoundException): ser.get_sensor_position(sensor.id) ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) # OK found_position = ser.get_sensor_position(sensor.id) self.assertEqual(position, found_position) db.session.delete(sensor) db.session.delete(position) db.session.commit()
def test_get_sensor_history(self): """ Test case for get_sensor_history """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() position2 = Position("room", 1, 1) db.session.add(position2) db.session.commit() ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) ser.add_assignment("room", 1, 1, dict(sensor=sensor.id)) # OK history = sorted(ser.get_sensor_history(sensor.id)["positions"], key=lambda element: element["start_of_service"]) self.assertEqual(2, len(history)) self.assertEqual( { "room": position.room, "bus": position.bus, "index": position.index }, history[0]["position"]) self.assertIsNotNone(history[0]["end_of_service"]) self.assertEqual( { "room": position2.room, "bus": position2.bus, "index": position2.index }, history[1]["position"]) self.assertIsNone(history[1]["end_of_service"]) db.session.delete(sensor) db.session.delete(position) db.session.delete(position2) db.session.commit()
def test_remove_assignment(self): """ Test case for remove_assignment """ with self.app.app_context(): sensor = Sensor("name", "mac", "type", "model", 0) db.session.add(sensor) db.session.commit() position = Position("room", 1, 0) db.session.add(position) db.session.commit() ser.add_assignment("room", 1, 0, dict(sensor=sensor.id)) # OK ser.remove_assignment(position.room, position.bus, position.index) with self.assertRaises(AssignmentNotFoundException): ser.get_assigned_sensor(position.room, position.bus, position.index) with self.assertRaises(AssignmentNotFoundException): ser.get_sensor_position(sensor.id) db.session.delete(sensor) db.session.delete(position) db.session.commit()
def put(self, room, bus, index): """ Adds a sensor to a given position. """ return add_assignment(room, bus, index, request.json), 201