def test_biz_get_todo_patch_good(self, mocked_get): # NOW WE ARE COOKING WITH GAS! # This single mocked test tests the exception fork in the client code # and the happy path. No other tests are needed 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.get_todo(1) mocked_get.return_value = MagicMock(status_code=200, text=""" { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """) # DOES THIS PROVIDE VALUE? Only in so far as it shows the non-error code fork todo = client.get_todo(1) assert todo.id == 1
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 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_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 test_do_a_thing_with_retries_fail_all(self): client = TodoClient('https://jsonplaceholder.typicode.com') biz_logic = TodoBiz(client) failer = lambda: (_ for _ in ()).throw(Exception('foobar')) with self.assertRaises(FunctionFailedException) as ex: result = FunctionRunner.do_a_thing_with_retries(failer, 3) print(ex)
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 test_biz_get_todo_patch(self, mocked_get): # lower level mocks that might get me some value, but this method does not # show the value as it is not testing the forks in the actual client method # which is what matters mocked_get.return_value = MagicMock(status_code=200, text=""" { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } """) client = TodoClient("foobar") biz = TodoBiz(client) todo = biz.get_todo(1) assert todo.id == 1
def test_do_a_thing_with_retries_pass(self): client = TodoClient('https://jsonplaceholder.typicode.com') biz_logic = TodoBiz(client) FunctionRunner.do_a_thing_with_retries(print, 3, "foobar")
def setUpClass(cls): cls.client = TodoClient('https://jsonplaceholder.typicode.com')