def test_post_drinks(client, app):
    application_service_mock = mock.Mock(spec=DrinkApplicationService)
    application_service_mock.create_drink.return_value = FailedOutputDto.build_resource_conflict_error(
    )

    with app.container.drink_application_service.override(
            application_service_mock):
        response = client.post(
            "/drinks",
            json={
                "drink_id":
                str(
                    DrinkId(
                        value=uuid.uuid5(uuid.NAMESPACE_DNS, "drink_num_1"))),
                "drink_name":
                "wine_1",
                "drink_image_url":
                "picture_wine_1",
                "drink_type":
                "wine",
            },
        )

        assert response.status_code == 409
        assert response.json() == {
            "error_type": "Resource Conflict Error",
            "message": "",
        }

    application_service_mock.create_drink.return_value = CreateDrinkOutputDto()
    with app.container.drink_application_service.override(
            application_service_mock):
        response = client.post(
            "/drinks",
            json={
                "drink_id":
                str(
                    DrinkId(
                        value=uuid.uuid5(uuid.NAMESPACE_DNS, "drink_num_1"))),
                "drink_name":
                "wine_1",
                "drink_image_url":
                "picture_wine_1",
                "drink_type":
                "wine",
            },
        )
        assert response.status_code == 201
Пример #2
0
    def find_all(self, query_param: QueryParam) -> List[Review]:
        if not query_param.user_id and not query_param.drink_id:
            raise InvalidParamInputError(
                f"drink_id: {query_param.drink_id} or user_id:{query_param.user_id}에 해당하는 값이 없습니다."
            )
        _query_param = {
            attr: value
            for attr, value in query_param
            if value and not isinstance(value, OrderType)
        }
        with self._session_factory() as session:

            order_type = desc(ReviewOrm.updated_at)
            # if order_type == OrderType.LIKE_DESC:
            #     order_type = desc(ReviewOrm.num_likes)
            # elif order_type == OrderType.LIKE_ASC:
            #     order_type = asc(ReviewOrm.num_likes)

            query = session.query(ReviewOrm)
            review_orms = query.filter_by(
                **_query_param).order_by(order_type).all()

            return [
                Review(
                    id=ReviewId(value=review_orm.id),
                    drink_id=DrinkId(value=review_orm.drink_id),
                    user_id=UserId(value=review_orm.user_id),
                    rating=ReviewRating(value=review_orm.rating),
                    comment=review_orm.comment,
                    created_at=review_orm.created_at,
                    updated_at=review_orm.updated_at,
                ) for review_orm in review_orms
            ]
 def to_wish(self) -> Wish:
     return Wish(
         id=WishId(value=self.id),
         user_id=UserId(value=self.user_id),
         drink_id=DrinkId(value=self.drink_id),
         created_at=self.created_at,
     )
Пример #4
0
 def to_review(self) -> Review:
     return Review(
         id=ReviewId(value=self.id),
         user_id=UserId(value=self.user_id),
         drink_id=DrinkId(value=self.drink_id),
         rating=ReviewRating(value=self.rating),
         comment=self.comment,
         created_at=self.created_at,
         updated_at=self.updated_at,
     )
    def create_review(
        self,
        input_dto: CreateReviewInputDto,
        drink_application_service: DrinkApplicationService,
    ) -> Union[CreateReviewOutputDto, FailedOutputDto]:
        try:
            review = Review(
                id=ReviewId.build(user_id=input_dto.user_id,
                                  drink_id=input_dto.drink_id),
                drink_id=DrinkId(value=input_dto.drink_id),
                user_id=UserId(value=input_dto.user_id),
                rating=ReviewRating(value=input_dto.rating),
                comment=input_dto.comment,
                created_at=time.time(),
                updated_at=time.time(),
            )
            self._review_repository.add(review)
            input_dto = AddDrinkReviewInputDto(drink_id=input_dto.drink_id,
                                               drink_rating=input_dto.rating)
            drink_add_review_output_dto = drink_application_service.add_drink_review(
                input_dto=input_dto)

            if not drink_add_review_output_dto.status:
                return drink_add_review_output_dto
            return CreateReviewOutputDto(
                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,
            )
        except ResourceAlreadyExistError as e:
            return FailedOutputDto.build_resource_conflict_error(
                message=str(e))
        except Exception as e:
            return FailedOutputDto.build_system_error(message=str(e))