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 test_find_by_review_id(orm_drink_repository): with pytest.raises(ResourceNotFoundError): orm_drink_repository.find_by_drink_id( DrinkId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbd8")) a = DrinkOrm.from_drink( Drink( id=DrinkId.build(drink_name="soju1", created_at=1234), name="soju1", image_url="soju_image1", type=DrinkType.SOJU, avg_rating=DrinkRating(), num_of_reviews=0, num_of_wish=0, )) actual = orm_drink_repository.find_by_drink_id( drink_id=DrinkId.build(drink_name="soju1", created_at=1234)) expected = Drink( id=DrinkId.build(drink_name="soju1", created_at=1234), name="soju1", image_url="soju_image1", type=DrinkType.from_str("soju"), avg_rating=DrinkRating(), num_of_reviews=0, num_of_wish=0, ) assert actual == expected
def create_wish( self, input_dto: CreateWishInputDto, drink_application_service: DrinkApplicationService, ) -> Union[CreateWishOutputDto, FailedOutputDto]: try: wish = Wish( id=WishId.build(user_id=str(input_dto.user_id), drink_id=str(input_dto.drink_id)), user_id=UserId(value=input_dto.user_id), drink_id=DrinkId.from_str(input_dto.drink_id), created_at=time.time(), ) self._wish_repository.add(wish) add_drink_wish_input_dto = AddDrinkWishInputDto( drink_id=input_dto.drink_id) add_drink_wish_output_dto = drink_application_service.add_drink_wish( input_dto=add_drink_wish_input_dto) if not add_drink_wish_output_dto.status: return add_drink_wish_output_dto return CreateWishOutputDto(id=str(wish.id), user_id=str(wish.user_id), drink_id=str(wish.drink_id), created_at=wish.created_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 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_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 test_find_all(orm_wish_repository): actual = orm_wish_repository.find_all( QueryParam(drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c")) expected = [ Wish( id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667.0, ), Wish( id=WishId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbd9"), user_id=UserId(value="joon"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667.0, ), ] assert actual == expected
def wish_application_service(): wish_repository_mock = mock.Mock(spec=WishRepository) wish_repository_mock.find_all.return_value = [ Wish( id=WishId.build(user_id="heumsi", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613113664.931505, ), Wish( id=WishId.build(user_id="joon", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c"), user_id=UserId(value="joon"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613113664.931505, ), ] return WishApplicationService(wish_repository=wish_repository_mock)
def test_find(orm_wish_repository): actual = orm_wish_repository.find(QueryParam(user_id="heumsi")) expected = Wish( id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667.0, ) assert actual == expected actual = orm_wish_repository.find( QueryParam(user_id="heumsi", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c")) expected = Wish( id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667.0, ) assert actual == expected
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 add_drink_wish(self, input_dto: AddDrinkWishInputDto) -> Union[AddDrinkWishOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.add_wish() self._drink_repository.update(drink) return AddDrinkWishOutputDto() 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 setup(database): with database.session() as session: session.query(WishOrm).delete() session.add_all([ WishOrm.from_wish( Wish( id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str( "335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667, )), WishOrm.from_wish( Wish( id=WishId.from_str("35a05a4b-d9ba-5122-af75-7c0022b8bbd9"), user_id=UserId(value="joon"), drink_id=DrinkId.from_str( "335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667, )), ]) session.commit()
def delete_drink(self, input_dto: DeleteDrinkInputDto) -> Union[DeleteDrinkOutputDto, FailedOutputDto]: try: drink_id = DrinkId.from_str(input_dto.drink_id) if self._drink_repository.find_by_drink_id(drink_id) is None: return FailedOutputDto.build_resource_not_found_error(f"{str(drink_id)}의 술을 찾을 수 없습니다.") self._drink_repository.delete_by_drink_id(drink_id) return DeleteDrinkOutputDto() 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 delete_drink_review( self, input_dto: DeleteDrinkReviewInputDto ) -> Union[DeleteDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.delete_rating(input_dto.drink_rating) self._drink_repository.update(drink) return DeleteDrinkReviewOutputDto() 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_delete(orm_wish_repository): with pytest.raises(ResourceNotFoundError): orm_wish_repository.find(QueryParam(user_id="not exist user")) actual = orm_wish_repository.delete_by_wish_id( wish_id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0")) expected = Wish( id=WishId.from_str("bcbfdb54-acb7-5443-926a-42e882ef7db0"), user_id=UserId(value="heumsi"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667.0, ) assert actual == expected with pytest.raises(ResourceNotFoundError): orm_wish_repository.find(query_param=QueryParam(user_id="heumsi"))
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_add(orm_wish_repository): wish = Wish( id=WishId.build(user_id="siheum", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c"), user_id=UserId(value="siheum"), drink_id=DrinkId.from_str("335ca1a4-5175-5e41-8bac-40ffd840834c"), created_at=1613807667, ) orm_wish_repository.add(wish) actual = orm_wish_repository.find( QueryParam(user_id="siheum", drink_id="335ca1a4-5175-5e41-8bac-40ffd840834c")) expected = wish assert actual == expected with pytest.raises(ResourceAlreadyExistError): orm_wish_repository.add(wish)
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 find_drink(self, input_dto: FindDrinkInputDto) -> Union[FindDrinkOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(drink_id=DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) return FindDrinkOutputDto( drink_id=str(drink.id), drink_name=drink.name, drink_image_url=drink.image_url, drink_type=drink.type.value, avg_rating=float(drink.avg_rating), num_of_reviews=drink.num_of_reviews, num_of_wish=drink.num_of_wish, ) 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 update_drink_review( self, input_dto: UpdateDrinkReviewInputDto ) -> Union[UpdateDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) drink.update_rating( old_rating=input_dto.old_drink_rating, new_rating=input_dto.new_drink_rating, ) self._drink_repository.update(drink) return UpdateDrinkReviewOutputDto() 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 update_drink(self, input_dto: UpdateDrinkInputDto) -> Union[UpdateDrinkOutputDto, FailedOutputDto]: try: drink_id = DrinkId.from_str(input_dto.drink_id) if self._drink_repository.find_by_drink_id(drink_id) is None: return FailedOutputDto.build_resource_not_found_error(f"{str(drink_id)}의 술을 찾을 수 없습니다.") drink = Drink( id=drink_id, name=input_dto.drink_name, image_url=input_dto.drink_image_url, type=input_dto.drink_type, avg_rating=DrinkRating(value=input_dto.avg_rating), num_of_reviews=input_dto.num_of_reviews, num_of_wish=input_dto.num_of_wish, ) self._drink_repository.update(drink) return UpdateDrinkOutputDto() 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))
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(): return mock.Mock(spec=DrinkApplicationService)