def find_reviews(
     self, input_dto: FindReviewsInputDto
 ) -> Union[FailedOutputDto, FindReviewsOutputDto]:
     try:
         reviews = self._review_repository.find_all(
             query_param=input_dto.query_param)
         return FindReviewsOutputDto(items=[
             FindReviewsOutputDto.Item(
                 review_id=str(review.id),
                 drink_id=str(review.drink_id),
                 user_id=str(review.user_id),
                 rating=int(review.rating),
                 comment=review.comment,
                 created_at=review.created_at,
                 updated_at=review.updated_at,
             ) for review in reviews
         ])
     except InvalidParamInputError as e:
         return FailedOutputDto.build_parameters_error(message=str(e))
     except Exception as e:
         return FailedOutputDto.build_system_error(message=str(e))
Ejemplo n.º 2
0
def test_get_reviews(client, app):
    application_service_mock = mock.Mock(spec=ReviewApplicationService)
    application_service_mock.find_reviews.return_value = FailedOutputDto.build_parameters_error(
    )

    # valid request but no resource
    with app.container.review_application_service.override(
            application_service_mock):
        response = client.get("/reviews")
    assert response.status_code == 400
    assert response.json() == {
        "error_type": "Parameters Error",
        "message": "",
    }

    # valid request
    application_service_mock.find_reviews.return_value = FindReviewsOutputDto(
        items=[
            FindReviewsOutputDto.Item(
                review_id="review_id_uuid",
                drink_id="drink_id_uuid",
                user_id="user_id_uuid",
                rating=4,
                comment="tastes good",
                created_at=737373737.6,
                updated_at=737373737.6,
            ),
            FindReviewsOutputDto.Item(
                review_id="review_id_uuid2",
                drink_id="drink_id_uuid2",
                user_id="user_id_uuid2",
                rating=3,
                comment="tastes good2",
                created_at=123.123,
                updated_at=123.123,
            ),
        ])
    with app.container.review_application_service.override(
            application_service_mock):
        response = client.get("/reviews?drink_id=drink_id_uuid&order=newest")
    assert response.status_code == 200
    assert response.json() == [
        {
            "review_id": "review_id_uuid",
            "drink_id": "drink_id_uuid",
            "user_id": "user_id_uuid",
            "rating": 4,
            "comment": "tastes good",
            "created_at": 737373737.6,
            "updated_at": 737373737.6,
        },
        {
            "review_id": "review_id_uuid2",
            "drink_id": "drink_id_uuid2",
            "user_id": "user_id_uuid2",
            "rating": 3,
            "comment": "tastes good2",
            "created_at": 123.123,
            "updated_at": 123.123,
        },
    ]