def test_create_should_throw_ex_when_empty_instance(self, mock_hook): with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID, body=CREATE_BODY, instance="", task_id="id") op.execute(None) err = cm.exception self.assertIn("The required parameter 'instance' is empty", str(err)) mock_hook.assert_not_called()
def test_create_should_throw_ex_when_empty_instance(self, mock_hook): with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator( project_id=PROJECT_ID, body=CREATE_BODY, instance="", task_id="id" ) op.execute(None) err = cm.exception self.assertIn("The required parameter 'instance' is empty", str(err)) mock_hook.assert_not_called()
def test_create_should_validate_non_empty_fields(self, mock_hook): empty_tier_body = { "name": INSTANCE_NAME, "settings": { "tier": "", # Field can't be empty (defined in CLOUD_SQL_VALIDATION). # Testing if the validation catches this. } } with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID, body=empty_tier_body, instance=INSTANCE_NAME, task_id="id") op.execute(None) err = cm.exception self.assertIn( "The body field 'settings.tier' can't be empty. " "Please provide a value.", str(err)) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default")
def test_create_should_validate_non_empty_fields(self, mock_hook): empty_tier_body = { "name": INSTANCE_NAME, "settings": { "tier": "", # Field can't be empty (defined in CLOUD_SQL_VALIDATION). # Testing if the validation catches this. } } with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator( project_id=PROJECT_ID, body=empty_tier_body, instance=INSTANCE_NAME, task_id="id" ) op.execute(None) err = cm.exception self.assertIn("The body field 'settings.tier' can't be empty. " "Please provide a value.", str(err)) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default")
def test_instance_create_idempotent(self, mock_hook, _check_if_instance_exists): _check_if_instance_exists.return_value = True mock_hook.return_value.create_instance.return_value = True op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID, instance=INSTANCE_NAME, body=CREATE_BODY, task_id="id") result = op.execute(context={'task_instance': mock.Mock()}) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default") mock_hook.return_value.create_instance.assert_not_called() self.assertIsNone(result)
def test_create_should_validate_list_type(self, mock_hook): wrong_list_type_body = { "name": INSTANCE_NAME, "settings": { "tier": "db-n1-standard-1", "ipConfiguration": { "authorizedNetworks": {} # Should be a list, not a dict. # Testing if the validation catches this. } } } with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator(project_id=PROJECT_ID, body=wrong_list_type_body, instance=INSTANCE_NAME, task_id="id") op.execute(None) err = cm.exception self.assertIn( "The field 'settings.ipConfiguration.authorizedNetworks' " "should be of list type according to the specification", str(err)) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default")
def test_create_should_validate_list_type(self, mock_hook): wrong_list_type_body = { "name": INSTANCE_NAME, "settings": { "tier": "db-n1-standard-1", "ipConfiguration": { "authorizedNetworks": {} # Should be a list, not a dict. # Testing if the validation catches this. } } } with self.assertRaises(AirflowException) as cm: op = CloudSqlInstanceCreateOperator( project_id=PROJECT_ID, body=wrong_list_type_body, instance=INSTANCE_NAME, task_id="id" ) op.execute(None) err = cm.exception self.assertIn("The field 'settings.ipConfiguration.authorizedNetworks' " "should be of list type according to the specification", str(err)) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default")
def test_instance_create_missing_project_id(self, mock_hook, _check_if_instance_exists): _check_if_instance_exists.return_value = False mock_hook.return_value.create_instance.return_value = True op = CloudSqlInstanceCreateOperator(instance=INSTANCE_NAME, body=CREATE_BODY, task_id="id") result = op.execute( context={ # pylint: disable=assignment-from-no-return 'task_instance': mock.Mock() }) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default") mock_hook.return_value.create_instance.assert_called_once_with( project_id=None, body=CREATE_BODY) self.assertIsNone(result)
def test_instance_create_idempotent(self, mock_hook, _check_if_instance_exists): _check_if_instance_exists.return_value = True mock_hook.return_value.create_instance.return_value = True op = CloudSqlInstanceCreateOperator( project_id=PROJECT_ID, instance=INSTANCE_NAME, body=CREATE_BODY, task_id="id" ) result = op.execute(context={ 'task_instance': mock.Mock() }) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default") mock_hook.return_value.create_instance.assert_not_called() self.assertIsNone(result)