def __init__(self, data: BaseModel, *, path: Optional[Path] = None) -> None: """Instantiate class. Args: data: The data model of the config file. path: Path to the config file. """ self._data = data.copy() self.file_path = path.resolve() if path else Path.cwd()
def patch(self, id: int, data: BaseModel) -> Alchemy: if hasattr(data, "id") and data.id is not None and data.id != id: e = ExcBuilder(status.HTTP_422_UNPROCESSABLE_ENTITY) e.add(("id",), EMsg.E_002_NOT_EDIT, EType.E_002_VAL_ERR_IMMUTABLE) raise e.build() obj = data.copy() obj.id = id result = self.rep.update(obj) if result is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) return result
def test_update(client, url: str = URL, model: BaseModel = basic_potato, id_key: str = 'id'): res = client.post(url, json=model.dict()) data = res.json() assert res.status_code == 200 tuber = model.copy() tuber.color = 'yellow' res = client.put(f'{url}/{data[id_key]}', json=tuber.dict()) assert res.status_code == 200 assert compare_dict(res.json(), tuber.dict(), exclude=[id_key]) assert not compare_dict(res.json(), model.dict(), exclude=[id_key]) res = client.get(f'{url}/{data[id_key]}') assert res.status_code == 200 assert compare_dict(res.json(), tuber.dict(), exclude=[id_key]) assert not compare_dict(res.json(), model.dict(), exclude=[id_key])