def setUp(self): self.hook = OpenFaasHook(function_name=FUNCTION_NAME) self.mock_response = {'ans': 'a'}
class TestOpenFaasHook(unittest.TestCase): GET_FUNCTION = "/system/function/" INVOKE_ASYNC_FUNCTION = "/async-function/" DEPLOY_FUNCTION = "/system/functions" UPDATE_FUNCTION = "/system/functions" def setUp(self): self.hook = OpenFaasHook(function_name=FUNCTION_NAME) self.mock_response = {'ans': 'a'} @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_is_function_exist_false(self, mock_get_connection, m): m.get( "http://open-faas.io" + self.GET_FUNCTION + FUNCTION_NAME, json=self.mock_response, status_code=404, ) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection does_function_exist = self.hook.does_function_exist() self.assertFalse(does_function_exist) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_is_function_exist_true(self, mock_get_connection, m): m.get( "http://open-faas.io" + self.GET_FUNCTION + FUNCTION_NAME, json=self.mock_response, status_code=202, ) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection does_function_exist = self.hook.does_function_exist() self.assertTrue(does_function_exist) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_update_function_true(self, mock_get_connection, m): m.put("http://open-faas.io" + self.UPDATE_FUNCTION, json=self.mock_response, status_code=202) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection self.hook.update_function({}) # returns None @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_update_function_false(self, mock_get_connection, m): m.put("http://open-faas.io" + self.UPDATE_FUNCTION, json=self.mock_response, status_code=400) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection with self.assertRaises(AirflowException) as context: self.hook.update_function({}) self.assertIn('failed to update ' + FUNCTION_NAME, str(context.exception)) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_invoke_async_function_false(self, mock_get_connection, m): m.post( "http://open-faas.io" + self.INVOKE_ASYNC_FUNCTION + FUNCTION_NAME, json=self.mock_response, status_code=400, ) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection with self.assertRaises(AirflowException) as context: self.hook.invoke_async_function({}) self.assertIn('failed to invoke function', str(context.exception)) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_invoke_async_function_true(self, mock_get_connection, m): m.post( "http://open-faas.io" + self.INVOKE_ASYNC_FUNCTION + FUNCTION_NAME, json=self.mock_response, status_code=202, ) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection self.assertEqual(self.hook.invoke_async_function({}), None) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_deploy_function_function_already_exist(self, mock_get_connection, m): m.put("http://open-faas.io/" + self.UPDATE_FUNCTION, json=self.mock_response, status_code=202) mock_connection = Connection(host="http://open-faas.io/") mock_get_connection.return_value = mock_connection self.assertEqual(self.hook.deploy_function(True, {}), None) @mock.patch.object(BaseHook, 'get_connection') @requests_mock.mock() def test_deploy_function_function_not_exist(self, mock_get_connection, m): m.post("http://open-faas.io" + self.DEPLOY_FUNCTION, json={}, status_code=202) mock_connection = Connection(host="http://open-faas.io") mock_get_connection.return_value = mock_connection self.assertEqual(self.hook.deploy_function(False, {}), None)