예제 #1
0
async def fetch(user_id: int, id_: int) -> Optional[Hero]:
    query = (
        HeroModel.select()
        .where(HeroModel.c.user_id == user_id)
        .where(HeroModel.c.id == id_)
    )

    result = await database.fetch_one(query)
    return Hero.parse_obj(dict(result)) if result else None
예제 #2
0
async def persist(user_id: int, dto: CreateHeroDto) -> Hero:
    values = {**dto.dict(), "user_id": user_id}
    query = HeroModel.insert().values(**values)

    try:
        last_record_id = await database.execute(query)
    except UniqueViolationError:
        raise HeroNotUniqueError()

    return Hero.parse_obj({**values, "id": last_record_id})
예제 #3
0
        def test_is_required(self, valid_hero):
            with pytest.raises(ValidationError) as excinfo:
                Hero(**dissoc(valid_hero, "location"))

            self.assert_validation_error("value_error.missing", excinfo)
예제 #4
0
        def test_must_be_power_class(self, valid_hero):
            with pytest.raises(ValidationError) as excinfo:
                Hero(**{**valid_hero, "power_class": 9000})

            self.assert_validation_error("type_error.enum", excinfo)
예제 #5
0
        def test_max_length(self, valid_hero):
            with pytest.raises(ValidationError) as excinfo:
                Hero(**{**valid_hero, "nickname": "a" * 101})

            self.assert_validation_error("value_error.any_str.max_length",
                                         excinfo)
예제 #6
0
        def test_must_be_str(self, valid_hero):
            with pytest.raises(ValidationError) as excinfo:
                Hero(**{**valid_hero, "nickname": ["Some nickname"]})

            self.assert_validation_error("type_error.str", excinfo)
예제 #7
0
 def test_defaults(self, valid_hero):
     assert Hero(**dissoc(valid_hero, "name")).name == "Unknown"
예제 #8
0
        def test_must_be_int(self, valid_hero):
            with pytest.raises(ValidationError) as excinfo:
                Hero(**{**valid_hero, "user_id": "some_id"})

            self.assert_validation_error("type_error.integer", excinfo)
예제 #9
0
 def test_immutability(self, valid_hero):
     entity = Hero(**valid_hero)
     for key in entity.dict().keys():
         with pytest.raises(TypeError):
             setattr(entity, key, "some value")
예제 #10
0
 def test_invalidation(self, invalid_hero):
     with pytest.raises(ValidationError):
         Hero(**invalid_hero)
예제 #11
0
 def test_validation(self, valid_hero):
     assert Hero(**valid_hero)
예제 #12
0
async def fetch_all_by_user(user_id: int) -> Iterable[Hero]:
    query = HeroModel.select().where(HeroModel.c.user_id == user_id)

    results = await database.fetch_all(query)
    return (Hero.parse_obj(dict(result)) for result in results)
예제 #13
0
async def fetch_all() -> Iterable[Hero]:
    query = HeroModel.select()

    results = await database.fetch_all(query)
    return (Hero.parse_obj(dict(result)) for result in results)