def test_application(self): # Basic test making sure application() is returning the output of # Update.get_output(). Have to mock Update(): otherwise, the real # database would be hit, not the test one, because of how services # use a different setting and database connection APIs. environ = { 'QUERY_STRING': '' } self.start_response_call_count = 0 expected_headers = [ ('FakeHeader', 'FakeHeaderValue') ] expected_output = b'{"fake": "output"}' def start_response_inspector(status, headers): self.start_response_call_count += 1 assert status == '200 OK' assert headers == expected_headers with mock.patch('services.update.Update') as UpdateMock: update_instance = UpdateMock.return_value update_instance.get_headers.return_value = expected_headers update_instance.get_output.return_value = expected_output output = update.application(environ, start_response_inspector) assert self.start_response_call_count == 1 # Output is an array with a single string containing the body of the # response. assert output == [expected_output]
def test_application(self): # Basic test making sure application() is returning the output of # Update.get_output(). Have to mock Update(): otherwise, even though # we're setting SERVICES_DATABASE to point to the test database in # settings_test.py, we wouldn't see results because the data wouldn't # exist with the cursor the update service is using, which is different # from the one used by django tests. environ = {'QUERY_STRING': ''} self.start_response_call_count = 0 expected_headers = [('FakeHeader', 'FakeHeaderValue')] expected_output = b'{"fake": "output"}' def start_response_inspector(status, headers): self.start_response_call_count += 1 assert status == '200 OK' assert headers == expected_headers with mock.patch('services.update.Update') as UpdateMock: update_instance = UpdateMock.return_value update_instance.get_headers.return_value = expected_headers update_instance.get_output.return_value = expected_output output = update.application(environ, start_response_inspector) assert self.start_response_call_count == 1 # Output is an array with a single string containing the body of the # response. assert output == [expected_output]
def test_exception_handling(self, UpdateMock, log_mock): """Test ensuring exceptions are raised and logged properly.""" class CustomException(Exception): pass self.inspector_call_count = 0 update_instance = UpdateMock.return_value update_instance.get_output.side_effect = CustomException('Boom!') def inspector(status, headers): self.inspector_call_count += 1 with self.assertRaises(CustomException): update.application({'QUERY_STRING': ''}, inspector) assert self.inspector_call_count == 0 # The log should be present. assert log_mock.exception.call_count == 1 log_mock.exception.assert_called_with( update_instance.get_output.side_effect)