def test_delete(orm_user_repository): with pytest.raises(ResourceNotFoundError): orm_user_repository.find_by_user_id(user_id=UserId(value="not exist user")) orm_user_repository.delete_by_user_id(UserId(value="heumsi")) with pytest.raises(ResourceNotFoundError): orm_user_repository.find_by_user_id(user_id=UserId(value="heumsi"))
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 setup(database): with database.session() as session: session.query(UserOrm).delete() session.add_all( [ UserOrm.from_user( User( id=UserId(value="heumsi"), name=UserName(value="heumsi"), description="hi, I am heumsi", password="******", image_url="", ) ), UserOrm.from_user( User( id=UserId(value="joon"), name=UserName(value="joon"), description="hello, I am joon", password="******", image_url="", ) ), ] ) session.commit()
def test_find_review_success( review_repository_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.find_by_review_id.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_application_service = ReviewApplicationService( review_repository=review_repository_mock) input_dto = FindReviewInputDto(review_id=str(review_id)) actual = review_application_service.find_review(input_dto) expected = FindReviewOutputDto( review_id=str(review_id), drink_id=str(drink_id), user_id=str(UserId(value=user_id)), rating=int(ReviewRating(value=rating)), comment="hello", created_at=created_at, updated_at=created_at, ) assert actual == expected
def test_update(orm_user_repository): orm_user_repository.update( user=User( id=UserId(value="heumsi"), name=UserName(value="heumsi jeon"), description="Hi, I'm heumsi jeon!", password="******", image_url="", ) ) actual = orm_user_repository.find_by_user_id(user_id=UserId(value="heumsi")) expected = User( id=UserId(value="heumsi"), name=UserName(value="heumsi jeon"), description="Hi, I'm heumsi jeon!", password="******", image_url="", ) assert actual == expected with pytest.raises(ResourceNotFoundError): orm_user_repository.update( user=User( id=UserId(value="not exist user"), name=UserName(value="heumsi jeon"), description="Hi, I'm heumsi jeon!", password="******", image_url="", ) )
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_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, )
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_create_review_fail( review_repository_mock, drink_application_service_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.add.side_effect = ResourceAlreadyExistError() review_application_service = ReviewApplicationService( review_repository=review_repository_mock) drink_application_service_mock.add_drink_review.return_value = AddDrinkReviewOutputDto( ) input_dto = CreateReviewInputDto( drink_id=str(drink_id), user_id=str(UserId(value=user_id)), rating=int(ReviewRating(value=rating)), comment="", ) actual = review_application_service.create_review( input_dto, drink_application_service_mock) expected = FailedOutputDto(type="Resource Conflict Error", message="") assert actual == expected
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 test_delete_review_success( review_repository_mock, drink_application_service_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.find_by_review_id.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_repository_mock.delete_by_review_id.return_value = None review_application_service = ReviewApplicationService( review_repository=review_repository_mock) drink_application_service_mock.delete_drink_review.return_value = DeleteDrinkReviewOutputDto( ) input_dto = DeleteReviewInputDto(review_id=str(review_id)) actual = review_application_service.delete_review( input_dto, drink_application_service_mock) expected = DeleteReviewOutputDto() assert actual == expected
def test_delete_review_fail( review_repository_mock, drink_application_service_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.find_by_review_id.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_repository_mock.delete_by_review_id.side_effect = ResourceNotFoundError( ) review_application_service = ReviewApplicationService( review_repository=review_repository_mock) drink_application_service_mock.delete_drink_review.return_value = DeleteDrinkReviewOutputDto( ) input_dto = DeleteReviewInputDto(review_id=str(review_id)) actual = review_application_service.delete_review( input_dto, drink_application_service_mock) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
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 to_user(self) -> User: return User( id=UserId(value=self.id), name=UserName(value=self.name), description=self.description, password=self.password, image_url=self.image_url, )
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 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 test_find_all(orm_user_repository): actual = orm_user_repository.find_all() expected = [ User( id=UserId(value="heumsi"), name=UserName(value="heumsi"), description="hi, I am heumsi", password="******", image_url="", ), User( id=UserId(value="joon"), name=UserName(value="joon"), description="hello, I am joon", password="******", image_url="", ), ] 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 test_add(orm_user_repository): orm_user_repository.add(user=User(id=UserId(value="siheum"), name=UserName(value="siheum"), password="******")) actual = orm_user_repository.find_by_user_id(user_id=UserId(value="siheum")) expected = User( id=UserId(value="siheum"), name=UserName(value="siheum"), description="", password="******", image_url="", ) assert actual == expected with pytest.raises(ResourceAlreadyExistError): orm_user_repository.add( user=User( id=UserId(value="siheum"), name=UserName(value="siheum"), password="******", ) )
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 delete_user( self, input_dto: DeleteUserInputDto ) -> Union[DeleteUserOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) self._user_repository.delete_by_user_id(user_id) return DeleteUserOutputDto() 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 test_login_success(user_repository_mock, user_id, user_name, password): user_repository_mock.find_by_user_id.return_value = User( id=UserId(value=user_id), name=UserName(value=user_name), password="******", ) user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = LoginInputDto(user_id=user_id, password=password) actual = user_application_service.login(input_dto) expected = LoginOutputDto() assert actual == expected
def test_find_user_success(user_repository_mock, user_id, user_name, password): user_repository_mock.find_by_user_id.return_value = User( id=UserId(value=user_id), name=UserName(value=user_name), password=password) user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = FindUserInputDto(user_id=user_id) actual = user_application_service.find_user(input_dto) expected = FindUserOutputDto(user_id=user_id, user_name=user_name, description="", image_url="") assert actual == expected
def login( self, input_dto: LoginInputDto ) -> Union[LoginOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) user = self._user_repository.find_by_user_id(user_id=user_id) if not self._verify_password(input_dto.password, user.password): return FailedOutputDto.build_unauthorized_error( f"잘못된 비밀번호 입니다.") return LoginOutputDto() 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_login_fail_with_wrong_password(user_repository_mock, user_id, user_name, password): wrong_password = "******" user_repository_mock.find_by_user_id.return_value = User( id=UserId(value=user_id), name=UserName(value=user_name), password="******", ) user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = LoginInputDto(user_id=user_id, password=wrong_password) actual = user_application_service.login(input_dto) expected = FailedOutputDto.build_unauthorized_error( message=f"잘못된 비밀번호 입니다.") assert actual == expected
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 update_user( self, input_dto: UpdateUserInputDto ) -> Union[UpdateUserOutputDto, FailedOutputDto]: try: user = User( id=UserId(value=input_dto.user_id), name=UserName(value=input_dto.user_name), description=input_dto.description, password=input_dto.password, ) self._user_repository.update(user) return UpdateUserOutputDto() 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))