def add_car(): """ Endpoint to add car to db Returns: car added to db, or 400 """ schema = CarSchema(exclude=['id']) try: new_car = schema.loads(request.get_json()) except ValidationError as ve: return abort(400, description=ve.messages) if Car.query.filter_by(reg_number=new_car.reg_number).first() is not None: return abort(403, description='Conflict, reg number exists!') db.session.add(new_car) handle_db_operation(db.session.commit) return jsonify(schema.dumps(new_car)), 201
def test_form_post_get_filtered_cars(self): with self.app.app_context(): with self.app.test_client() as app: colour1 = CarColour.query.get(self.colour1) manufacturer1 = CarManufacturer.query.get(self.manufacturer1) car1 = Car.query.get(self.car1) car4 = DummyCar.create_random() car4.car_colour = self.colour1 car4.car_manufacturer = self.manufacturer1 add_to_db(car4) filters = { 'car_colour': colour1.id, 'car_manufacturer': manufacturer1.id } response = app.post('/api/car', data=filters) self.assertEqual(200, response.status_code) response_cars = CarSchema(many=True).loads(response.get_json()) self.assertTrue(type(Car), type(response_cars[0])) self.assertTrue( all([ car.car_colour == self.colour1 for car in response_cars ])) self.assertFalse(car1 in response_cars) self.assertTrue(car4.id in [c.id for c in response_cars])
def test_get_all_cars(self): with self.app.app_context(): with self.app.test_client() as app: response = app.get('/api/car', query_string={}) self.assertEqual(200, response.status_code) response_cars = CarSchema(many=True).loads(response.get_json()) self.assertTrue(type(Car), type(response_cars[0]))
def car(id: int): """ Get, update, or delete car Args: id (int): car id Returns: Error or 200 """ car = Car.query.get(id) if car is None: return abort(404, description='Car not found') schema = CarSchema() if request.method == 'GET': # Dump to dict, then add issue text car_data = schema.dump(car) if car.issue is not None: car_data['issue'] = car.issue.issue return jsonify(json.dumps(car_data)), 200 elif request.method == 'PUT': try: new_car = schema.loads(request.get_json()) except ValidationError as ve: return abort(400, description=ve.messages) car.reg_number = new_car.reg_number car.car_manufacturer = new_car.car_manufacturer car.car_colour = new_car.car_colour car.car_type = new_car.car_type car.seats = new_car.seats car.hour_rate = new_car.hour_rate car.longitude = new_car.longitude car.latitude = new_car.latitude handle_db_operation(db.session.commit) return jsonify('Car updated'), 200 elif request.method == 'DELETE': Car.query.filter_by(id=id).delete() handle_db_operation(db.session.commit) return jsonify('Car deleted'), 200
def test_get_booking_for_person(self): with self.app.app_context(): with self.app.test_client() as app: person1 = Person.query.get(self.person1) booking1 = Booking.query.get(self.booking1) response = app.get(f'api/person/{person1.username}/booking/{booking1.id}') self.assertEqual(200, response.status_code) data = response.get_json() booking = BookingSchema().loads(data['booking']) person = PersonSchema().loads(data['person']) car = CarSchema().loads(data['car']) self.assertEqual(type(booking1), type(booking))
def test_update_car_location(self): with self.app.app_context(): with self.app.test_client() as app: car = Car.query.get(self.car1) lat = '123.321' long = '321.123' data = {'latitude': lat, 'longitude': long} response = app.put(f'api/car/{car.id}/location', data=data) response_car = CarSchema().loads(response.get_json()) self.assertEqual(200, response.status_code) self.assertEqual(lat, response_car.latitude) self.assertEqual(long, response_car.longitude) self.assertEqual(lat, car.latitude)
def test_get_bookings_for_person(self): with self.app.app_context(): with self.app.test_client() as app: person1 = Person.query.get(self.person1) booking1 = Booking.query.get(self.booking1) car1 = Car.query.get(self.car1) response = app.get(f'api/person/{person1.username}/booking') self.assertEqual(200, response.status_code) data = response.get_json() self.assertIsNotNone(data) for booking_info in data: self.assertEqual(type(booking1), type(BookingSchema().loads(booking_info['booking']))) self.assertEqual(type(person1), type(PersonSchema().loads(booking_info['person']))) self.assertEqual(type(car1), type(CarSchema().loads(booking_info['car'])))
def get_all_cars(): """ Endpoint to get all cars with extra data Returns: car data """ cars = Car.query.all() car_data = [{ 'car': CarSchema().dump(car), 'type': CarTypeSchema().dump(car.type), 'manufacturer': CarManufacturerSchema().dump(car.manufacturer), 'colour': CarColourSchema().dump(car.color), 'issue': CarIssueSchema().dump(car.issue) } for car in cars] return jsonify(car_data), 200
def get_all_bookings_details(): """ Get all bookings Returns: All bookings details in list of objects """ bookings = Booking.query.all() booking_data = [{ 'booking': BookingSchema().dump(booking), 'person': PersonSchema().dump(booking.person), 'car': CarSchema().dump(booking.car), 'manufacturer': CarManufacturerSchema().dump(booking.car.manufacturer) } for booking in bookings] return jsonify(booking_data), 200
def get_all_cars_with_issue(): """ Endpoint to get all cars with issue data Returns: car data """ issues = CarIssue.query.all() car_issue = [issue.car for issue in issues] car_data = [{ 'car': CarSchema().dump(car), 'issue': CarIssueSchema().dump(car.issue), 'type': CarTypeSchema().dump(car.type), 'manufacturer': CarManufacturerSchema().dump(car.manufacturer), 'colour': CarColourSchema().dump(car.color) } for car in car_issue] return jsonify(car_data), 200