Exemplo n.º 1
0
def add_booking_admin():
    """
    Add a new booking

    Returns: Json string of booking, or error

    """

    schema = BookingSchema(exclude=['id'])
    try:
        booking = schema.loads(request.get_json())
    except ValidationError as ve:
        return abort(400, description=ve.messages)  # wow generic message
    # check that references to data in db is valid
    person = Person.query.filter_by(id=booking.person_id).first()
    car = Car.query.filter_by(id=booking.car_id).first()
    if None in [person, car]:
        return abort(403, description='Booking references invalid person/car id(s)')

    # Check that no booking with car is currently active
    if Booking.is_car_busy(booking.start_time, booking.end_time, booking.car_id):
        return abort(403, description=f'A booking with car id {booking.car_id}'
                                      f' is already mad in that time period')

    db.session.add(booking)
    handle_db_operation(db.session.commit)
    return schema.jsonify(booking), 201
Exemplo n.º 2
0
 def test_booking_end_date_before_start_date(self):
     with self.app.app_context():
         schema = BookingSchema(exclude=['id'])
         booking = DummyBooking.person1_car1_available
         booking.start_time = datetime.now() + timedelta(days=100)
         booking.end_time = datetime.now()
         booking_jsonstr = json.dumps(schema.dump(booking))
         with self.assertRaises(ValidationError) as cm:
             schema.loads(booking_jsonstr)
         self.assertEqual(['end_time can not be before start_time'], cm.exception.messages['_schema'])
Exemplo n.º 3
0
    def test_schema_load_data_from_json(self):
        with self.app.app_context():
            person_schema = PersonSchema()
            person = person_schema.loads(DummyPerson.creat_random_json(id=None))
            self.assertEqual(type(person), Person)

            booking_schema = BookingSchema()
            # deserialize to json string
            booking_json = booking_schema.dumps(DummyBooking.person1_car1_available)
            # serialize from json string
            booking = booking_schema.loads(DummyBooking.b1_json)
            self.assertEqual(type(booking), Booking)
Exemplo n.º 4
0
 def test_deactivate_booking(self):
     with self.app.app_context():
         with self.app.test_client() as app:
             person = Person.query.get(self.person1)
             booking = duplicate_db_object(BookingSchema, Booking.query.get(self.booking1))
             booking.car_id = self.car3
             add_to_db(booking)
             booking_jsonstr = json.dumps(BookingSchema(exclude=['id', 'status']).dump(booking))
             response = app.put(f"/api/person/{person.username}/booking/{booking.id}", json=booking_jsonstr)
             self.assertEqual(200, response.status_code)
Exemplo n.º 5
0
 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))
Exemplo n.º 6
0
    def test_get_valid_booking(self):
        with self.app.app_context():
            with self.app.test_client() as app:
                # serialize booking to json str
                booking1 = Booking.query.get(self.booking1)
                person1 = Person.query.get(self.person1)

                booking_jsonstr = json.dumps(BookingSchema(exclude=['id', 'status']).dump(booking1))

                response_get = app.get(
                    f'/api/person/{person1.username}/booking')
                self.assertEqual(response_get.status_code, 200)
Exemplo n.º 7
0
    def test_add_invalid_booking_username_gives_error(self):
        with self.app.app_context():
            with self.app.test_client() as app:
                # serialize booking to json str
                booking1 = Booking.query.get(self.booking1)
                person1 = Person.query.get(self.person1)
                booking_jsonstr = json.dumps(BookingSchema(exclude=['id', 'status']).dump(booking1))

                # post to username that does not match booking person username
                response_wrong_username = app.post(
                    f"/api/person/wrongusername/booking", json=booking_jsonstr)
                self.assertEqual(403, response_wrong_username.status_code)
Exemplo n.º 8
0
    def test_add_booking_with_car_in_use(self):
        with self.app.app_context():
            with self.app.test_client() as app:
                person1 = Person.query.get(self.person1)
                # booking1 already has car 1 in use
                booking2 = duplicate_db_object(BookingSchema, Booking.query.get(self.booking1))

                booking_jsonstr = json.dumps(BookingSchema(exclude=['id', 'status']).dump(booking2))

                # Car is in use, should return error
                response_error = app.post(f"/api/person/{person1.username}/booking", json=booking_jsonstr)
                self.assertEqual(403, response_error.status_code)
Exemplo n.º 9
0
def booking(id: int):
    """
    Get, put, delete booking

    Args:
        id (int): booking id

    Returns: 403 if booking not found, 200 Ok

    """
    booking = Booking.query.get(id)
    if booking is None:
        return abort(403, description='Booking not found')

    if request.method == 'DELETE':
        Booking.query.filter_by(id=id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Booking deleted'), 200
    elif request.method == 'PUT':
        schema = BookingSchema()
        try:
            new_booking = schema.loads(request.get_json())
        except ValidationError as ve:
            return abort(403, description=ve.messages)

        booking.person_id = new_booking.person_id
        booking.car_id = new_booking.car_id
        booking.start_time = new_booking.start_time
        booking.end_time = new_booking.end_time
        booking.status = new_booking.status
        handle_db_operation(db.session.commit)
        return jsonify('Booking updated successfully'), 200
    else:
        schema = BookingSchema()
        return jsonify(schema.dumps(booking)), 200
Exemplo n.º 10
0
 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'])))
Exemplo n.º 11
0
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
Exemplo n.º 12
0
    def test_add_valid_booking(self):
        with self.app.app_context():
            with self.app.test_client() as app:
                person1 = Person.query.get(self.person1)
                #person2 = add_to_db(duplicate_db_object(PersonSchema, person1))
                # hacky way to make a new booking
                booking1 = Booking.query.get(self.booking1)
                booking2 = duplicate_db_object(BookingSchema, booking1)

                # add car not currently in a booking
                booking2.car_id = self.car2
                #booking2.start_time = datetime.now() + timedelta(hours=1)
                #booking2.person_id = person2

                booking_jsonstr = json.dumps(BookingSchema(exclude=['id', 'status']).dump(booking2))

                response_valid = app.post(f"/api/person/{person1.username}/booking", json=booking_jsonstr)
                self.assertEqual(response_valid.status_code, 201)