Пример #1
0
    def test_rental_delete_by_id(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()

        res = RentalService.delete_by_id(1234)
        self.assertEqual(res.status_code, 404)

        res = RentalService.delete_by_id(1)
        self.assertEqual(res.status_code, 204)
Пример #2
0
    def create(req_data):

        req_data['user_id'] = RentalService.get_user()
        try:
            data = rental_schema.load(req_data)
        except ValidationError as e:
            return custom_response(e.messages, 400)
        rental = RentalModel(data)
        rental.save()
        data = rental_schema.dump(rental)
        return custom_response(data, 201)
Пример #3
0
    def test_rental_get_by_id(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()

        res = RentalService.get_by_id(13)
        self.assertEqual(res.status_code, 404)

        res = RentalService.get_by_id(1)

        json_data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(json_data.get('movie_id'), 1)
Пример #4
0
    def test_rental_get_all(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()
        res = RentalService.get_all()

        json_data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(json_data.__len__(), 2)
Пример #5
0
    def get_by_id(rental_id: int):

        rental = RentalModel.get_by_id(rental_id)
        if not rental:
            return custom_response({'error': 'not found'}, 404)
        data = rental_schema.dump(rental)
        if data.get('user_id') != RentalService.get_user():
            return custom_response({'error': 'permission denied'}, 400)
        return custom_response(data, 200)
Пример #6
0
    def return_movie(req_data):

        rental = RentalModel.get_by_id(req_data["rental_id"])
        if not rental:
            return custom_response({'error': 'rental not found'}, 404)
        data = rental_schema.dump(rental)
        if data.get('user_id') != RentalService.get_user():
            return custom_response({'error': 'permission denied'}, 400)
        rental = rental.return_movie()

        data = rental_schema.dump(rental)
        return custom_response(data, 200)
Пример #7
0
    def update(rental_id: int, req_data):

        rental = RentalModel.get_by_id(rental_id)
        if not rental:
            return custom_response({'error': 'not found'}, 404)
        data = rental_schema.dump(rental)
        if data.get('user_id') != RentalService.get_user():
            return custom_response({'error': 'permission denied'}, 400)

        try:
            data = rental_schema.load(req_data, partial=True)
        except ValidationError as e:
            return custom_response(e.messages, 400)
        rental.update(data)

        data = rental_schema.dump(rental)
        return custom_response(data, 200)
Пример #8
0
 def create_rental(self):
     rental = RentalModel({'user_id': 1, 'movie_id': 1})
     db.session.add(rental)
     db.session.commit()
     return rental
Пример #9
0
    def test_rental_update_without_permission(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()

        res = RentalService.update(1, rental_schema.load({}, partial=True))
        self.assertEqual(res.status_code, 400)
Пример #10
0
    def test_rental_update_not_found(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()

        res = RentalService.update(33, rental_schema.load({}, partial=True))
        self.assertEqual(res.status_code, 404)
Пример #11
0
    def test_rental_get_by_id_without_permission(self):
        rental = RentalModel(rental_schema.load(self.rental1))
        rental.save()

        res = RentalService.get_by_id(1)
        self.assertEqual(res.status_code, 400)
Пример #12
0
    def get_all():

        rentals = RentalModel.get_all_rentals()
        data = rental_schema.dump(rentals, many=True)
        return custom_response(data, 200)