def test_complex_logic_for_bad_todos(self): """ Test to make sure todos id's 3,4 are marked bad""" todo3_json = """ { "userId": 1, "id": 3, "title": "delectus aut autem", "completed": false } """ todo3 = Todo.from_json(json.loads(todo3_json)) todo4_json = """ { "userId": 1, "id": 4, "title": "delectus aut autem", "completed": false } """ todo4 = Todo.from_json(json.loads(todo4_json)) client = TodoClient('https://jsonplaceholder.typicode.com') biz_logic = TodoBiz(client) new_todo3 = biz_logic.do_complex_logic_on_todo(todo3) assert "bad todo" in new_todo3.title assert new_todo3.completed == True new_todo4 = biz_logic.do_complex_logic_on_todo(todo4) assert "bad todo" in new_todo4.title assert new_todo4.completed == True
def complete_todo_with_complex_logic_bad(self, todo: Todo): #pragma: no cover # BAD because requires API call to test biz logic REFACTOR todo.completed = True if todo.id in (3, 4): todo.title = todo.title + " is a bad todo!" self.todo_client.put_todo(todo)
def get_todo(self, todo_id: int) -> Todo: uri = f"""{self.base_uri}/todos/{todo_id}""" response = requests.get(uri) if response.status_code != 200: raise requests.HTTPError(response.text) return Todo.from_json(json.loads(response.text))
def test_biz_get_todo_mock(self): # with mocks, but what does this tell me? NOTHING client = TodoClient("foobar") client.get_todo = MagicMock( return_value=Todo(id=1, userId=3, title="foobar", completed=False)) biz = TodoBiz(client) todo = biz.get_todo(1) assert todo.id == 1
def complete_todo_complex_with_retries_bad(self, todo: Todo): #pragma: no cover # BADNESS: ╭∩╮(Ο_Ο)╭∩╮ needs refactor # need to be able to test this separately todo.completed = True if todo.id in (3, 4): todo.title = todo.title + " is a bad todo!" # how do I test this retry logic? for tries in range(3): try: if (tries < 2): print("will retry") self.todo_client.put_todo(todo) else: # now I have to have qeueu somewhere too print("Put this in a failure queue now") return except Exception as ex: print(f"Bad things have happened {ex}") pass
def test_todo_from_json(self): todo_json = """ { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """ todo = Todo.from_json(json.loads(todo_json)) assert todo.id == 1
def test_biz_put_todo_patch_good(self, mocked_get): mocked_get.return_value = MagicMock(status_code=500, text=""" { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """) client = TodoClient("foobar") with self.assertRaises(requests.HTTPError): client.put_todo( Todo(id=1, userId=1, title="foobar", completed=False))
def test_update_todo(self): """ Test with low value. I don't need to test remote API """ todo_json = """ { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """ todo = Todo.from_json(json.loads(todo_json)) client = TodoClient('https://jsonplaceholder.typicode.com') biz_logic = TodoBiz(client) biz_logic.complete_todo(todo)
def test_complex_logic_good_todo(self): """ Test to make sure ids that are not 3,4, are not bad """ todo1_json = """ { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """ todo1 = Todo.from_json(json.loads(todo1_json)) client = TodoClient('https://jsonplaceholder.typicode.com') biz_logic = TodoBiz(client) new_todo1 = biz_logic.do_complex_logic_on_todo(todo1) assert "bad todo" not in new_todo1.title assert new_todo1.completed == True
def complete_todo(self, todo: Todo): """ Simplest completion method possible """ todo.completed = True self.todo_client.put_todo(todo)
def test_todo_put(self): """ Simple TODO put integration test """ todo = Todo(id=2, userId=2, title="Sandy", completed=False) self.client.put_todo(todo)
def put_todo(self, todo: Todo): data = todo.dump() uri = f"""{self.base_uri}/todos/{todo.id}""" response = requests.put(uri, data=data) if response.status_code != 200: raise requests.HTTPError(response.text)