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 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 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 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
Exemple #5
0
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 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_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_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)