Beispiel #1
0
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
Beispiel #2
0
def test_find_all(orm_drink_repository):
    actual = orm_drink_repository.find_all(query_param=QueryParam(
        type=DrinkType.from_str("soju"),
        filter=FilterType.from_str("review"),
        order=OrderType.from_str("descending"),
    ))
    expected = [
        Drink(
            id=DrinkId.build(drink_name="soju2", created_at=1234),
            name="soju2",
            image_url="soju_image2",
            type=DrinkType.from_str("soju"),
            avg_rating=DrinkRating(value=5),
            num_of_reviews=1,
            num_of_wish=1,
        ),
        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
Beispiel #3
0
def test_add(orm_drink_repository):
    drink = Drink(
        id=DrinkId.build(drink_name="wine", created_at=1234),
        name="wine",
        image_url="wine_image",
        type=DrinkType.from_str("wine"),
        avg_rating=DrinkRating(value=3),
        num_of_reviews=1,
        num_of_wish=1,
    )
    orm_drink_repository.add(drink)

    actual = orm_drink_repository.find_by_drink_id(
        drink_id=DrinkId.build(drink_name="wine", created_at=1234))
    expected = Drink(
        id=DrinkId.build(drink_name="wine", created_at=1234),
        name="wine",
        image_url="wine_image",
        type=DrinkType.from_str("wine"),
        avg_rating=DrinkRating(value=3),
        num_of_reviews=1,
        num_of_wish=1,
    )

    assert actual == expected

    with pytest.raises(ResourceAlreadyExistError):
        orm_drink_repository.add(drink)
def test_drink_update_rating(drink_no_review, drink_one_review, drink_two_reviews):
    drink_no_review.update_rating(old_rating=5, new_rating=4)
    assert drink_no_review.avg_rating == DrinkRating(value=0)
    assert drink_no_review.num_of_reviews == 0

    drink_one_review.update_rating(old_rating=5, new_rating=4)
    assert drink_one_review.avg_rating == DrinkRating(value=4)
    assert drink_one_review.num_of_reviews == 1

    drink_two_reviews.update_rating(old_rating=4, new_rating=5)
    assert drink_two_reviews.avg_rating == DrinkRating(value=5)
    assert drink_two_reviews.num_of_reviews == 2
def test_drink_delete_rating(drink_no_review, drink_one_review, drink_two_reviews):
    drink_no_review.delete_rating(input_rating=5)
    assert drink_no_review.avg_rating == DrinkRating(value=0)
    assert drink_no_review.num_of_reviews == 0

    drink_one_review.delete_rating(input_rating=5)
    assert drink_one_review.avg_rating == DrinkRating(value=0)
    assert drink_one_review.num_of_reviews == 0

    drink_two_reviews.delete_rating(input_rating=5)
    assert drink_two_reviews.avg_rating == DrinkRating(value=4)
    assert drink_two_reviews.num_of_reviews == 1
Beispiel #6
0
    def delete_rating(self, input_rating: int) -> None:
        if self.num_of_reviews <= 0:
            return

        total_rating_value = (float(self.avg_rating) *
                              self.num_of_reviews) - input_rating
        self.num_of_reviews -= 1

        if self.num_of_reviews == 0:
            self.avg_rating = DrinkRating(value=0)
        else:
            self.avg_rating = DrinkRating(value=(total_rating_value /
                                                 self.num_of_reviews))
def test_find_drink_success(
    client,
    app,
    drink_repository_mock,
    drink_id,
    drink_name,
    drink_image_url,
    drink_type,
):
    drink_repository_mock.find_by_drink_id.return_value = Drink(
        id=drink_id,
        name=drink_name,
        image_url=drink_image_url,
        type=drink_type)
    drink_application_service = DrinkApplicationService(
        drink_repository=drink_repository_mock)
    input_dto = FindDrinkInputDto(drink_id=str(drink_id))
    actual = drink_application_service.find_drink(input_dto)
    expected = FindDrinkOutputDto(
        drink_id=str(drink_id),
        drink_name=drink_name,
        drink_image_url=drink_image_url,
        drink_type=drink_type.value,
        avg_rating=float(DrinkRating()),
        num_of_reviews=0,
        num_of_wish=0,
    )
    assert actual == expected
Beispiel #8
0
    def update_rating(self, old_rating: int, new_rating: int) -> None:
        if self.num_of_reviews <= 0:
            return

        sum_rating_value = (float(self.avg_rating) *
                            self.num_of_reviews) - old_rating + new_rating
        self.avg_rating = DrinkRating(value=(sum_rating_value /
                                             self.num_of_reviews))
def drink_one_review():
    return Drink(
        id=DrinkId.build(drink_name="soju2", created_at=1234),
        name="soju2",
        image_url="soju_image2",
        type=DrinkType.from_str("soju"),
        avg_rating=DrinkRating(value=5),
        num_of_reviews=1,
        num_of_wish=1,
    )
def drink_two_reviews():
    return Drink(
        id=DrinkId.build(drink_name="soju3", created_at=1234),
        name="soju3",
        image_url="soju_image3",
        type=DrinkType.from_str("soju"),
        avg_rating=DrinkRating(value=4.5),
        num_of_reviews=2,
        num_of_wish=2,
    )
def drink_no_review():
    return 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,
    )
Beispiel #12
0
class Drink(BaseModel):
    MIN_NUM_OF_REVIEWS: ClassVar[int] = 0
    MIN_NUM_OF_WISH: ClassVar[int] = 0

    id: DrinkId
    name: str
    image_url: str
    type: DrinkType
    avg_rating: DrinkRating = Field(default=DrinkRating())
    num_of_reviews: int = Field(default=0, ge=MIN_NUM_OF_REVIEWS)
    num_of_wish: int = Field(default=0, ge=MIN_NUM_OF_WISH)

    def add_rating(self, input_rating: int) -> None:
        sum_rating_value = (float(self.avg_rating) *
                            self.num_of_reviews) + input_rating
        self.num_of_reviews += 1
        self.avg_rating = DrinkRating(value=(sum_rating_value /
                                             self.num_of_reviews))

    def update_rating(self, old_rating: int, new_rating: int) -> None:
        if self.num_of_reviews <= 0:
            return

        sum_rating_value = (float(self.avg_rating) *
                            self.num_of_reviews) - old_rating + new_rating
        self.avg_rating = DrinkRating(value=(sum_rating_value /
                                             self.num_of_reviews))

    def delete_rating(self, input_rating: int) -> None:
        if self.num_of_reviews <= 0:
            return

        total_rating_value = (float(self.avg_rating) *
                              self.num_of_reviews) - input_rating
        self.num_of_reviews -= 1

        if self.num_of_reviews == 0:
            self.avg_rating = DrinkRating(value=0)
        else:
            self.avg_rating = DrinkRating(value=(total_rating_value /
                                                 self.num_of_reviews))

    def add_wish(self) -> None:
        self.num_of_wish += 1

    def delete_wish(self) -> None:
        if self.num_of_wish <= 0:
            return
        self.num_of_wish -= 1
Beispiel #13
0
def setup(database):
    with database.session() as session:
        session.query(DrinkOrm).delete()
        session.add_all([
            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,
                )),
            DrinkOrm.from_drink(
                Drink(
                    id=DrinkId.build(drink_name="soju2", created_at=1234),
                    name="soju2",
                    image_url="soju_image2",
                    type=DrinkType.SOJU,
                    avg_rating=DrinkRating(value=5),
                    num_of_reviews=1,
                    num_of_wish=1,
                )),
            DrinkOrm.from_drink(
                Drink(
                    id=DrinkId.build(drink_name="beer", created_at=1234),
                    name="beer",
                    image_url="beer_image",
                    type=DrinkType.BEER,
                    avg_rating=DrinkRating(value=4.5),
                    num_of_reviews=2,
                    num_of_wish=2,
                )),
        ])
        session.commit()
Beispiel #14
0
def test_update(orm_drink_repository):
    old_drink = orm_drink_repository.find_by_drink_id(
        DrinkId.build(drink_name="soju1", created_at=1234))

    new_drink = Drink(
        id=old_drink.id,
        name=old_drink.name,
        image_url=old_drink.image_url,
        type=old_drink.type,
        avg_rating=DrinkRating(value=4.5),
        num_of_reviews=2,
        num_of_wish=1,
    )
    orm_drink_repository.update(new_drink)

    actual = orm_drink_repository.find_by_drink_id(
        DrinkId.build(drink_name="soju1", created_at=1234))
    expected = new_drink
    assert actual == expected
Beispiel #15
0
    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))
def test_update_drink_success(
    drink_repository_mock,
    drink_id,
    drink_name,
    drink_image_url,
    drink_type,
):
    drink_repository_mock.update.return_value = None
    drink_application_service = DrinkApplicationService(
        drink_repository=drink_repository_mock)

    input_dto = UpdateDrinkInputDto(
        drink_id=str(drink_id),
        drink_name="Tequila",
        drink_image_url="tequila image url",
        drink_type=DrinkType.LIQUOR.value,
        avg_rating=float(DrinkRating(value=3.65)),
        num_of_reviews=10,
        num_of_wish=20,
    )

    actual = drink_application_service.update_drink(input_dto)
    expected = UpdateDrinkOutputDto()
    assert actual == expected
def test_update_drink_fail(
    drink_repository_mock,
    drink_id,
    drink_name,
    drink_image_url,
    drink_type,
):
    drink_repository_mock.update.side_effect = ResourceNotFoundError()
    drink_application_service = DrinkApplicationService(
        drink_repository=drink_repository_mock)

    input_dto = UpdateDrinkInputDto(
        drink_id=str(drink_id),
        drink_name="Tequila",
        drink_image_url="tequila image url",
        drink_type=DrinkType.LIQUOR.value,
        avg_rating=float(DrinkRating(value=3.65)),
        num_of_reviews=10,
        num_of_wish=20,
    )

    actual = drink_application_service.update_drink(input_dto)
    expected = FailedOutputDto(type="Resource Not Found Error", message="")
    assert actual == expected
Beispiel #18
0
 def add_rating(self, input_rating: int) -> None:
     sum_rating_value = (float(self.avg_rating) *
                         self.num_of_reviews) + input_rating
     self.num_of_reviews += 1
     self.avg_rating = DrinkRating(value=(sum_rating_value /
                                          self.num_of_reviews))
def test_drink_add_rating(drink_no_review, drink_one_review, drink_two_reviews):
    drink_no_review.add_rating(input_rating=5)
    assert drink_no_review.avg_rating == DrinkRating(value=5)
    assert drink_no_review.num_of_reviews == 1