def __init__(self): self.items: Dict[int, TodoItem] = { 1: TodoItem(id=1, msg="Item 1", is_done=False), 2: TodoItem(id=2, msg="Item 2", is_done=False), 3: TodoItem(id=3, msg="Item 3", is_done=False), 4: TodoItem(id=4, msg="Item 4", is_done=True), 5: TodoItem(id=5, msg="Item 5", is_done=True), } self.items_bk = deepcopy(self.items)
def test_max_length_lte_100(self, valid_data): with pytest.raises(ValidationError) as excinfo: valid_data.update({"msg": "a" * 101}) TodoItem(**valid_data) self.assert_validation_error("value_error.any_str.max_length", excinfo)
async def create(self, dto: CreateTodoItemDto): self.items, new_item = pipe( self.items.keys(), last, lambda key: key + 1, lambda new_key: TodoItem( id=new_key, msg=dto.msg, is_done=dto.is_done), lambda item: (assoc(self.items, item.id, item), item), ) return new_item
async def update(self, dto: UpdateTodoItemDto, id_: int): item = get_in([id_], self.items) if not item: return None self.items, new_item = pipe( (item, dto), lambda items: { **items[0].dict(), **items[1].dict(exclude_defaults=True) }, lambda data: TodoItem(**data), lambda todo: (assoc(self.items, id_, todo), todo), ) return new_item
async def replace(self, dto: CreateTodoItemDto, id_: int): if not get_in([id_], self.items): return None self.items = assoc(self.items, id_, TodoItem(id=id_, msg=dto.msg, is_done=dto.is_done)) return get_in([id_], self.items)
def test_must_be_str(self, valid_data): with pytest.raises(ValidationError) as excinfo: valid_data.update({"msg": ["some string"]}) TodoItem(**valid_data) self.assert_validation_error("type_error.str", excinfo)
def test_is_required(self, valid_data): with pytest.raises(ValidationError) as excinfo: valid_data.pop("msg") TodoItem(**valid_data) self.assert_validation_error("value_error.missing", excinfo)
def test_must_be_int(self, valid_data): with pytest.raises(ValidationError) as excinfo: valid_data.update({"id": "some integer"}) TodoItem(**valid_data) self.assert_validation_error("type_error.integer", excinfo)
def test_immutability(self, valid_data): tdi = TodoItem(**valid_data) for key in tdi.dict().keys(): with pytest.raises(TypeError): setattr(tdi, key, "some value")
def test_invalidation(self, invalid_data): with pytest.raises(ValidationError): TodoItem(**invalid_data)
def test_validation(self, valid_data): assert TodoItem(**valid_data)
def test_must_be_bool(self, valid_data): with pytest.raises(ValidationError) as excinfo: valid_data.update({"is_done": "some bool"}) TodoItem(**valid_data) self.assert_validation_error("type_error.bool", excinfo)