def test_get_all_robots(self): Robot.create(None, self.docking_station.id) response = self.client.get(BASE_URL) data = json.loads(response.get_data()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 2)
def test_create_robot_with_docking_station(self, create): robot = Robot.create(None, self.docking_station.id) db_robot = Robot.get(robot.id) create.assert_not_called() self.assertIsNotNone(db_robot) self.assertIsNotNone(db_robot.created_ts) self.assertIsNone(db_robot.modified_ts) self.assertIsNone(db_robot.route_id) self.assertEqual(db_robot.docking_station_id, self.docking_station.id)
def test_update_robot_at_docking_station_to_new_docking_station(self): robot = Robot.create(None, self.docking_station.id) new_docking_station = DockingStation.create(-44, -11) robot.update(docking_station_id=new_docking_station.id) self.assertEqual(robot.docking_station_id, new_docking_station.id) self.assertIsNone(robot.route_id)
def tearDown(self): for robot in Robot.get_all(): robot.delete() for docking_station in DockingStation.get_all(): docking_station.delete() for route in Route.get_all(): route.delete() for robot_route in RobotRoute.get_all(): robot_route.delete()
def test_update_robot_to_same_route(self): robot = Robot.create(self.route.id, None) old_robot_route = robot.current_robot_route() robot.update(route_id=self.route.id) updated_robot_route = robot.current_robot_route() self.assertEqual(old_robot_route, updated_robot_route) self.assertIsNone(robot.docking_station_id)
def test_get_all_routes_with_invalid_target_ts(self): dt = 'abc' robot = Robot.create(self.route.id, None) json_data = json.dumps({'target_ts': dt, 'robot_id': robot.id}) response = self.client.get(BASE_URL, query_string=json_data) data = json.loads(response.get_data()) self.assertEqual(response.status_code, 400) self.assertEqual(data, {'errors': 'timestamp arguments must be UTC timestamps'})
def test_get_all_routes_with_target_ts(self): dt = datetime.datetime(2018, 1, 1).timestamp() robot = Robot.create(self.route.id, None) json_data = json.dumps({'target_ts': dt, 'robot_id': robot.id}) response = self.client.get(BASE_URL, query_string=json_data) data = json.loads(response.get_data()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 1)
def test_get_all_routes_with_ts_range(self): robot = Robot.create(self.route.id, None) robot.update(self.route2.id, None) json_data = json.dumps({'robot_id': robot.id}) response = self.client.get(BASE_URL, query_string=json_data) data = json.loads(response.get_data()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 2)
def test_update_robot_at_docking_station_to_route(self): robot = Robot.create(None, self.docking_station.id) current_robot_route = robot.current_robot_route() robot.update(route_id=self.route.id) updated_robot_route = robot.current_robot_route() self.assertEqual(robot.route_id, self.route.id) self.assertIsNone(robot.docking_station_id) self.assertIsNone(current_robot_route) self.assertIsNotNone(updated_robot_route) self.assertIsNone(updated_robot_route.end_ts)
def test_update_robot_on_route_to_docking_station(self): robot = Robot.create(self.route.id, None) old_robot_route = robot.current_robot_route() robot.update(docking_station_id=self.docking_station.id) updated_robot_route = robot.current_robot_route() self.assertIsNone(robot.route_id) self.assertEqual(robot.docking_station_id, self.docking_station.id) self.assertIsNotNone(old_robot_route.end_ts) self.assertIsNone(updated_robot_route) self.assertTrue(robot in self.docking_station.robots)
def test_update_robot_on_route_to_new_route(self): robot = Robot.create(self.route.id, None) old_robot_route = robot.current_robot_route() new_route = Route.create([[-11, -11], [9, 19], [10, 15]]) robot.update(route_id=new_route.id) updated_robot_route = robot.current_robot_route() self.assertEqual(robot.route_id, new_route.id) self.assertNotEqual(old_robot_route, updated_robot_route) self.assertIsNotNone(old_robot_route.end_ts) self.assertIsNone(updated_robot_route.end_ts) self.assertTrue(robot in new_route.robots) self.assertIsNone(robot.docking_station_id)
def _create_robot_routes(self): robot = Robot.create(self.route.id, None) robot.robot_routes[-1].delete() route_1 = Route.create([[-13, 15], [-13, 14]]) route_2 = Route.create([[-11, 15], [-12, 14]]) route_3 = Route.create([[-10, 15], [-11, 14]]) route_4 = Route.create([[-10, 14], [-11, 14]]) t1 = datetime(2018, 1, 1) t2 = datetime(2018, 3, 3) t3 = datetime(2018, 5, 27) t4 = datetime(2018, 10, 11) t5 = datetime(2018, 10, 12) t6 = datetime(2019, 1, 3) t7 = datetime(2019, 2, 2) t8 = datetime(2019, 5, 9) t9 = datetime(2019, 6, 6) robot_route_1 = RobotRoute.create(robot.id, self.route.id, t1) robot_route_1.update(t2) robot_route_2 = RobotRoute.create(robot.id, route_1.id, t3) robot_route_2.update(t4) robot_route_3 = RobotRoute.create(robot.id, route_2.id, t5) robot_route_3.update(t6) robot_route_4 = RobotRoute.create(robot.id, route_3.id, t7) robot_route_4.update(t8) robot_route_5 = RobotRoute.create(robot.id, route_4.id, t9) return { 'robot': robot, 'robot_route_1_route_id': robot_route_1.route_id, 'robot_route_2_route_id': robot_route_2.route_id, 'robot_route_3_route_id': robot_route_3.route_id, 'robot_route_4_route_id': robot_route_4.route_id, 'robot_route_5_route_id': robot_route_5.route_id }
def get_all(cls, query_string=None): from app.models.robot import Robot query_string = json.loads(query_string) if query_string else {} if not query_string.get('robot_id'): return cls.query.all() robot = Robot.get(query_string.get('robot_id')) if not robot: raise NotFound({ 'resource_name': 'Robot', 'id': query_string.get('robot_id') }) target_dt, start_dt, end_dt = validate_and_extract_datetimes( query_string) if target_dt: robot.find_route_by_ts(target_dt) return robot.find_routes_by_ts_range(start_ts=start_dt, end_ts=end_dt)
def setUp(self): self.docking_station = DockingStation.create(-74.0478, 12.4687) self.route = Route.create([[-75.10, 12.50], [-81.42, 10.92]]) self.robot_schema = RobotSchema() self.robot = Robot.create(self.route.id, None)
def test_delete_robot(self): robot = Robot.create(None, self.docking_station.id) robot.delete() robot_db = Robot.get(robot.id) self.assertIsNone(robot_db)
def test_get_nonexistent_robot(self): robot = Robot.get(100) self.assertIsNone(robot)
def test_get_robot(self): robot = Robot.create(None, self.docking_station.id) db_robot = Robot.get(robot.id) self.assertEqual(db_robot.id, robot.id)
def test_create_robot_with_route_and_docking_station(self): robot = None with self.assertRaises(InvalidArguments): robot = Robot.create(self.route.id, self.docking_station.id) self.assertIsNone(robot)