def test_delete(orm_review_repository): orm_review_repository.find_by_review_id(review_id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367")) orm_review_repository.delete_by_review_id(review_id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367")) with pytest.raises(ResourceNotFoundError): orm_review_repository.delete_by_review_id(review_id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"))
def test_add(orm_review_repository): review = Review( id=ReviewId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbc1"), user_id=UserId(value="ME"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), rating=ReviewRating(value=4), comment="456", created_at=1613807667, updated_at=1613807667, ) orm_review_repository.add(review) actual = orm_review_repository.find_by_review_id( review_id=ReviewId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbc1") ) expected = Review( id=ReviewId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbc1"), user_id=UserId(value="ME"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), rating=ReviewRating(value=4), comment="456", created_at=1613807667, updated_at=1613807667, ) assert actual == expected with pytest.raises(ResourceAlreadyExistError): orm_review_repository.add(review)
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 delete_review( self, input_dto: DeleteReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[DeleteReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) if review is None: return FailedOutputDto.build_resource_not_found_error( f"{str(input_dto.review_id)}의 리뷰를 찾을 수 없습니다.") self._review_repository.delete_by_review_id(review_id) drinks_input_dto = DeleteDrinkReviewInputDto( drink_id=str(review.drink_id), drink_rating=int(review.rating)) drink_delete_review_output_dto = drink_application_service.delete_drink_review( input_dto=drinks_input_dto) if not drink_delete_review_output_dto.status: return drink_delete_review_output_dto return DeleteReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_find_all(orm_review_repository): actual = orm_review_repository.find_all(QueryParam(drink_id="07a9627a-c930-4292-afd0-b6a2d55de3b2")) expected = [ Review( id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=4), comment="first jun", created_at=1613807667, updated_at=3, ), Review( id=ReviewId.from_str("b329c178-46e3-4d67-9ae4-6624b651d3d1"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=3), comment="first heumsi", created_at=1613807667, updated_at=1, ), ] assert actual == expected actual = orm_review_repository.find_all(QueryParam(user_id="jun")) expected = [ Review( id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=4), comment="first jun", created_at=1613807667, updated_at=3, ), Review( id=ReviewId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834d"), rating=ReviewRating(value=4), comment="second jun", created_at=1613807667, updated_at=2, ), ] assert actual == expected
def test_find_by_review_id(orm_review_repository): with pytest.raises(ResourceNotFoundError): orm_review_repository.find_by_review_id(ReviewId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbd8")) actual = orm_review_repository.find_by_review_id( review_id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367") ) expected = Review( id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=4), comment="first jun", created_at=1613807667, updated_at=3, ) assert actual == expected
def test_find_reviews_success( review_repository_mock, review_id, drink_id, user_id, rating, created_at, ): drink_id_2 = "335ca1a4-5175-5e41-8bac-40ffd840835c" user_id_2 = "meme" review_id_2 = ReviewId.build(user_id=user_id_2, drink_id=drink_id_2) review_repository_mock.find_all.return_value = [ Review( id=review_id, drink_id=drink_id, user_id=UserId(value=user_id), rating=ReviewRating(value=rating), comment="hello", created_at=created_at, updated_at=created_at, ), Review( id=review_id_2, drink_id=DrinkId.from_str(drink_id_2), user_id=UserId(value=user_id_2), rating=ReviewRating(value=rating), comment="olleh", created_at=created_at, updated_at=created_at, ), ] review_application_service = ReviewApplicationService( review_repository=review_repository_mock) input_dto = FindReviewsInputDto(query_param=QueryParam()) actual = review_application_service.find_reviews(input_dto) expected = FindReviewsOutputDto(items=[ FindReviewsOutputDto.Item( review_id=str(review_id), drink_id=str(drink_id), user_id=str(user_id), rating=int(rating), comment="hello", created_at=created_at, updated_at=created_at, ), FindReviewsOutputDto.Item( review_id=str(review_id_2), drink_id=str(drink_id_2), user_id=str(user_id_2), rating=int(rating), comment="olleh", created_at=created_at, updated_at=created_at, ), ]) assert actual == expected
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 setup(database): with database.session() as session: session.query(ReviewOrm).delete() session.add_all( [ ReviewOrm.from_review( Review( id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=4), comment="first jun", created_at=1613807667, updated_at=3, ) ), ReviewOrm.from_review( Review( id=ReviewId.from_str("b329c178-46e3-4d67-9ae4-6624b651d3d1"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=3), comment="first heumsi", created_at=1613807667, updated_at=1, ) ), ReviewOrm.from_review( Review( id=ReviewId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834d"), rating=ReviewRating(value=4), comment="second jun", created_at=1613807667, updated_at=2, ) ), ] ) session.commit()
def find_review( self, input_dto: FindReviewInputDto ) -> Union[FindReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) return FindReviewOutputDto( 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 ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_find(orm_review_repository): actual = orm_review_repository.find( query_param=QueryParam(drink_id="07a9627a-c930-4292-afd0-b6a2d55de3b2", user_id="jun") ) expected = Review( id=ReviewId.from_str("afa213dd-d705-4f4d-953d-ad25e7181367"), user_id=UserId(value="jun"), drink_id=DrinkId.from_str("07a9627a-c930-4292-afd0-b6a2d55de3b2"), rating=ReviewRating(value=4), comment="first jun", created_at=1613807667, updated_at=3, ) assert actual == expected with pytest.raises(InvalidParamInputError): orm_review_repository.find(query_param=QueryParam(user_id="jun")) with pytest.raises(InvalidParamInputError): orm_review_repository.find(query_param=QueryParam(drink_id="07a9627a-c930-4292-afd0-b6a2d55de3b2"))
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))
def update_review( self, input_dto: UpdateReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[UpdateReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) old_review = self._review_repository.find_by_review_id(review_id) old_rating = int(old_review.rating) new_review = Review( id=old_review.id, drink_id=old_review.drink_id, user_id=old_review.user_id, rating=ReviewRating(value=input_dto.rating), comment=input_dto.comment, created_at=old_review.created_at, updated_at=time.time(), ) self._review_repository.update(new_review) drinks_input_dto = UpdateDrinkReviewInputDto( drink_id=str(new_review.drink_id), old_drink_rating=old_rating, new_drink_rating=input_dto.rating, ) drink_update_review_output_dto = drink_application_service.update_drink_review( input_dto=drinks_input_dto) if not drink_update_review_output_dto.status: return drink_update_review_output_dto return UpdateReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
FindReviewsInputDto, FindReviewsOutputDto, UpdateReviewInputDto, UpdateReviewOutputDto, ) from reviews.application.service import ReviewApplicationService from reviews.domain.entities import Review from reviews.domain.repository import QueryParam, ReviewRepository from reviews.domain.value_objects import ReviewRating from shared_kernel.application.dtos import FailedOutputDto from shared_kernel.domain.exceptions import InvalidParamInputError, ResourceAlreadyExistError, ResourceNotFoundError from shared_kernel.domain.value_objects import DrinkId, ReviewId, UserId review_data = [( ReviewId.build( user_id="Jun", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c"), # review_id DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), # drink_id "Jun", # user_id 4, # rating 1355563265.81, # created_at )] @pytest.fixture(scope="function") def review_repository_mock(): return mock.Mock(spec=ReviewRepository) @pytest.fixture(scope="function") def drink_application_service_mock():