Example #1
0
    async def test_found(self, database, make_user, make_hero):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        insert_hero(make_hero(id=id_, user_id=user.id).dict())

        async with database.transaction():
            assert await hero_repository.exists(id_)
Example #2
0
    def test_found(self, test_client, logged_user, make_hero):
        id_ = 1
        hero = make_hero(id=id_, user_id=logged_user.user.id)
        insert_hero(hero.dict())

        with test_client as client:
            response = client.delete("/hero/1",
                                     headers=auth_headers(
                                         logged_user.access_token))
        assert response.status_code == 204
Example #3
0
    def test_success(self, test_client, logged_user, make_hero):
        id_ = 1
        hero = make_hero(id=id_, user_id=logged_user.user.id)
        insert_hero(hero.dict())

        with test_client as client:
            response = client.get(f"/hero/{id_}",
                                  headers=auth_headers(
                                      logged_user.access_token))
        assert response.status_code == 200
        assert response.json() == hero.dict()
Example #4
0
    async def test_found(self, database, make_user, make_hero):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        async with database.transaction():
            result = await hero_repository.fetch(user_id, id_)

        assert result == hero
Example #5
0
async def test_fetch_all(database, make_user, make_heroes):
    user = make_user()
    insert_user(user.dict())

    heroes = make_heroes(10, user_id=user.id)
    for hero in heroes:
        insert_hero(hero.dict())

    async with database.transaction():
        result = list(await hero_repository.fetch_all())

    assert len(result) == len(heroes)
    for hero in result:
        assert hero in heroes
Example #6
0
    def test_from_other_user(self, test_client, logged_user, make_user,
                             make_hero):
        other_user = make_user()
        insert_user(other_user.dict())

        id_ = 1
        hero = make_hero(id=id_, user_id=other_user.id)
        insert_hero(hero.dict())

        with test_client as client:
            response = client.get(f"/hero/{id_}",
                                  headers=auth_headers(
                                      logged_user.access_token))
        assert response.status_code == 404
Example #7
0
    async def test_hero_not_unique_error(self, database, make_user, make_hero,
                                         create_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        dto = create_hero_dto_factory()

        async with database.transaction():
            with pytest.raises(HeroNotUniqueError):
                await hero_repository.persist(user_id, dto)
Example #8
0
    def test_success(self, test_client, logged_user, make_hero,
                     make_update_hero_dto):
        id_ = 1
        hero = make_hero(id=id_, user_id=logged_user.user.id)
        insert_hero(hero.dict())

        dto = make_update_hero_dto()
        with test_client as client:
            response = client.put(
                f"/hero/{id_}",
                headers=auth_headers(logged_user.access_token),
                json=dto.dict(),
            )
        assert response.status_code == 200
        assert response.json() == {**hero.dict(), **dto.dict()}
Example #9
0
async def test_fetch_all_by_user(database, make_user, make_heroes):
    users = make_user(), make_user()
    for user in users:
        insert_user(user.dict())

    users_heroes = pipe(
        users,
        lambda users:
        {user.id: make_heroes(10, user_id=user.id)
         for user in users},
        do(lambda mapping: [
            insert_hero(hero.dict()) for user, heroes in mapping.items()
            for hero in heroes
        ]),
    )

    async with database.transaction():
        for user in users:
            user_id = user.id
            heroes = users_heroes[user_id]

            result = list(await hero_repository.fetch_all_by_user(user_id))
            assert len(result) == len(heroes)
            for hero in result:
                assert hero in heroes
Example #10
0
    async def test_hero_not_unique_error(self, database, make_user, make_hero,
                                         update_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        other_hero = make_hero(user_id=user_id)
        insert_hero([hero.dict(), other_hero.dict()])

        dto = update_hero_dto_factory(name=other_hero.name,
                                      nickname=other_hero.nickname)

        async with database.transaction():
            with pytest.raises(HeroNotUniqueError):
                await hero_repository.update(user_id, dto, id_)
Example #11
0
    def test_conflict(self, test_client, logged_user, make_hero,
                      make_create_hero_dto):
        hero = make_hero(user_id=logged_user.user.id)
        insert_hero(hero.dict())
        dto = make_create_hero_dto(name=hero.name, nickname=hero.nickname)

        with test_client as client:
            response = client.post("/hero",
                                   json=dto.dict(),
                                   headers=auth_headers(
                                       logged_user.access_token))
        assert response.status_code == 409
        assert response.json() == {
            "detail": {
                "msg": "hero already exists",
                "type": "conflict_error.not_unique",
            }
        }
Example #12
0
    async def test_found(self, database, make_user, make_hero,
                         update_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        dto = update_hero_dto_factory()

        async with database.transaction():
            result = await hero_repository.update(user_id, dto, id_)

        assert result
        assert (result.id, result.user_id) == (hero.id, hero.user_id)

        getter = attrgetter("name", "nickname", "power_class", "location")
        assert getter(result) != getter(hero)
Example #13
0
    def test_success(self, test_client, logged_user, make_user, make_heroes):
        other_user = make_user()
        insert_user(other_user.dict())
        heroes = [
            hero.dict() for hero in [
                *make_heroes(10, user_id=logged_user.user.id),
                *make_heroes(10, user_id=other_user.id),
            ]
        ]
        insert_hero(heroes)

        with test_client as client:
            response = client.get("/hero",
                                  headers=auth_headers(
                                      logged_user.access_token))
        assert response.status_code == 200

        response_body = response.json()
        assert len(response_body) == len(heroes)
        for hero in response_body:
            assert hero in heroes
Example #14
0
    def test_conflict(self, test_client, logged_user, make_hero,
                      make_update_hero_dto):
        id_ = 1
        hero = make_hero(id=id_, user_id=logged_user.user.id)
        other_hero = make_hero(user_id=logged_user.user.id)
        insert_hero([hero.dict(), other_hero.dict()])

        dto = make_update_hero_dto(name=other_hero.name,
                                   nickname=other_hero.nickname)
        with test_client as client:
            response = client.put(
                f"/hero/{id_}",
                headers=auth_headers(logged_user.access_token),
                json=dto.dict(),
            )
        assert response.status_code == 409
        assert response.json() == {
            "detail": {
                "msg": "hero already exists",
                "type": "conflict_error.not_unique",
            }
        }
Example #15
0
    def test_success(self, test_client, logged_user, make_user, make_heroes):
        other_user = make_user()
        insert_user(other_user.dict())
        heroes = [
            hero.dict() for hero in [
                *make_heroes(10, user_id=logged_user.user.id),
                *make_heroes(10, user_id=other_user.id),
            ]
        ]
        insert_hero(heroes)

        with test_client as client:
            response = client.get("/hero/me",
                                  headers=auth_headers(
                                      logged_user.access_token))

        assert response.status_code == 200

        response_body = response.json()
        cond = itemgetter("id")
        user_heroes = [
            hero for hero in heroes if hero["user_id"] == logged_user.user.id
        ]
        assert sorted(response_body, key=cond) == sorted(user_heroes, key=cond)