def test_must_be_str(self, valid_create_hero_dto): with pytest.raises(ValidationError) as excinfo: CreateHeroDto(**{ **valid_create_hero_dto, "nickname": ["Some nickname"] }) self.assert_validation_error("type_error.str", excinfo)
def test_max_length(self, valid_create_hero_dto): with pytest.raises(ValidationError) as excinfo: CreateHeroDto(**{ **valid_create_hero_dto, "nickname": "a" * 101 }) self.assert_validation_error("value_error.any_str.max_length", excinfo)
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})
def test_is_required(self, valid_create_hero_dto): with pytest.raises(ValidationError) as excinfo: CreateHeroDto(**dissoc(valid_create_hero_dto, "location")) self.assert_validation_error("value_error.missing", excinfo)
def test_must_be_power_class(self, valid_create_hero_dto): with pytest.raises(ValidationError) as excinfo: CreateHeroDto(**{**valid_create_hero_dto, "power_class": 9000}) self.assert_validation_error("type_error.enum", excinfo)
def test_defaults(self, valid_create_hero_dto): assert (CreateHeroDto( **dissoc(valid_create_hero_dto, "name")).name == "Unknown")
def test_immutability(self, valid_create_hero_dto): entity = CreateHeroDto(**valid_create_hero_dto) for key in entity.dict().keys(): with pytest.raises(TypeError): setattr(entity, key, "some value")
def test_invalidation(self, invalid_create_hero_dto): with pytest.raises(ValidationError): CreateHeroDto(**invalid_create_hero_dto)
def test_validation(self, valid_create_hero_dto): assert CreateHeroDto(**valid_create_hero_dto)