def test_event_filter(test_client, test_db): new_events = [ LocationEvent(robot="Bender", x=0, y=0, timestamp=0), LocationEvent(robot="Bender", x=2, y=0, timestamp=2), LocationEvent(robot="Bender", x=0, y=4, timestamp=3), LocationEvent(robot="Bender", x=1, y=0, timestamp=5), LocationEvent(robot="Bender", x=0, y=0, timestamp=7) ] [test_db.session.add(event) for event in new_events] test_db.session.commit() assert events.get_events(start_time=3).count() == 3 assert events.get_events(start_time=5).count() == 2 assert events.get_events(start_time=7).count() == 1 assert events.get_events(start_time=10).count() == 0 assert events.get_events(start_time=0, end_time=5).count() == 4 assert events.get_events(end_time=5).count() == 4 assert events.get_events(end_time=2).count() == 2 with pytest.raises(ValueError): events.get_events(end_time="2a").count() with pytest.raises(ValueError): events.get_events(start_time="2b").count() with pytest.raises(ValueError): events.get_events(end_time=-2).count() with pytest.raises(ValueError): events.get_events(start_time=-20).count() with pytest.raises(ValueError): events.get_events(end_time="2c").count()
def test_odometer_two_events(test_client, test_db): new_events = [ LocationEvent(robot="Bender", x=0, y=0, timestamp=0), LocationEvent(robot="Bender", x=1, y=0, timestamp=1) ] [test_db.session.add(event) for event in new_events] test_db.session.commit() assert events.get_events().count() == 2 assert events.get_odometer("Bender") == 1
def push_event(msg, socket): try: start = datetime.datetime.utcnow() csv_data = msg.split(",") if len(csv_data) != 4: current_app.logger.error("%s Data does not have 4 parts" % msg) socket.send("ERR") return False robot_data = { "robot": csv_data[3], "x": float(csv_data[0]), "y": float(csv_data[1]), "timestamp": int(csv_data[2]) } location_event = LocationEvent(**robot_data) robot = Robot.query.get(robot_data["robot"]) if robot is not None: current_app.logger.debug("%s Updated" % robot_data["robot"]) last_event_id = robot.last_event_id current_app.logger.debug(last_event_id) last_event = LocationEvent.query.get(last_event_id) odometer = events.calculate_distance(last_event.x, last_event.y, robot_data["x"], robot_data["y"]) db.session.add(location_event) db.session.flush() robot.odometer = odometer + robot.odometer robot.last_event_id = location_event.id robot.last_x = robot_data["x"] robot.last_y = robot_data["y"] db.session.commit() else: current_app.logger.debug("%s Added" % robot_data["robot"]) db.session.add(location_event) db.session.flush() new_robot = Robot(name=robot_data["robot"], last_x=robot_data["x"], last_y=robot_data["y"], last_event_id=location_event.id) db.session.add(new_robot) db.session.commit() socket.send("OK") except Exception as e: db.session.rollback() current_app.logger.exception(e) finally: end = datetime.datetime.utcnow() current_app.logger.info("Took %s ms" % (end - start))
def test_api_db(): db.create_all() db.session.add(LocationEvent(robot="Bender", x=0, y=0, timestamp=0)) db.session.add(LocationEvent(robot="Bender", x=1, y=0, timestamp=1)) new_event = LocationEvent(robot="Bender", x=0, y=0, timestamp=2) db.session.add(new_event) db.session.flush() new_robot = Robot(name="Bender", last_x=0.0, last_y=0.0, last_event_id=new_event.id, odometer=2.0) db.session.add(new_robot) db.session.commit() yield db db.session.rollback() db.drop_all()
def add_event(robot_name, x, y, timestamp): if timestamp < 0: raise ValueError("Timestamp should be positive.") if not isinstance(timestamp, int): raise TypeError("Timestamp should be an integer.") if not isinstance(x, (int, float)): raise TypeError("x-coordinate should be an integer or float.") if not isinstance(y, (int, float)): raise TypeError("y-coordinate should be an integer or float.") if not isinstance(robot_name, str): raise TypeError("Robot name should be a string.") location_event = LocationEvent(robot=robot_name, x=x, y=y, timestamp=timestamp) db.session.add(location_event) db.session.commit()
def test_event_filter_multiple_robots(test_client, test_db): new_events = [ LocationEvent(robot="Bender", x=0, y=0, timestamp=0), LocationEvent(robot="Bender", x=2, y=0, timestamp=2), LocationEvent(robot="Bender", x=0, y=4, timestamp=3), LocationEvent(robot="Bender", x=1, y=0, timestamp=5), LocationEvent(robot="Bender", x=0, y=0, timestamp=7), LocationEvent(robot="Calculon", x=3, y=2, timestamp=0), LocationEvent(robot="Calculon", x=4, y=3, timestamp=1), LocationEvent(robot="Calculon", x=5, y=6, timestamp=4), LocationEvent(robot="Calculon", x=1, y=0, timestamp=8), LocationEvent(robot="Calculon", x=0, y=0, timestamp=9), LocationEvent(robot="Angleyne", x=0, y=0, timestamp=19), LocationEvent(robot="Angleyne", x=0, y=0, timestamp=20), LocationEvent(robot="Angleyne", x=0, y=0, timestamp=21), ] [test_db.session.add(event) for event in new_events] test_db.session.commit() assert events.get_events(start_time=3).count() == 9 assert events.get_events(start_time=30).count() == 0 assert events.get_events(start_time=0, end_time=5).count() == 7 assert events.get_events(end_time=3).count() == 5 assert events.get_events(robots='Bender', start_time=7).count() == 1 assert events.get_events(robots=['Bender'], start_time=7).count() == 1 assert events.get_events(robots=['Bender'], start_time=10).count() == 0 assert events.get_events(robots=['Bender'], start_time=0, end_time=5).count() == 4 assert events.get_events(robots=['Bender', 'Angleyne'], start_time=7).count() == 4 assert events.get_events(robots=['Bender', 'Angleyne'], start_time=0, end_time=5).count() == 4 assert events.get_events(robots=['Bender', 'Calculon'], start_time=0, end_time=5).count() == 7