diff --git a/src/snowflake/cli/_plugins/snowpark/snowpark_entity.py b/src/snowflake/cli/_plugins/snowpark/snowpark_entity.py index c7bbe41c6e..ad8d4ba7a3 100644 --- a/src/snowflake/cli/_plugins/snowpark/snowpark_entity.py +++ b/src/snowflake/cli/_plugins/snowpark/snowpark_entity.py @@ -4,7 +4,6 @@ from typing import Generic, List, Optional, TypeVar from click import ClickException - from snowflake.cli._plugins.nativeapp.feature_flags import FeatureFlag from snowflake.cli._plugins.snowpark import package_utils from snowflake.cli._plugins.snowpark.common import DEFAULT_RUNTIME @@ -37,11 +36,10 @@ class DeployMode( class SnowparkEntity(EntityBase[Generic[T]]): - def __init__(self, *args, **kwargs): if not FeatureFlag.ENABLE_NATIVE_APP_CHILDREN.is_enabled(): - raise NotImplementedError("Streamlit entity is not implemented yet") + raise NotImplementedError("Snowpark entity is not implemented yet") super().__init__(*args, **kwargs) @property diff --git a/tests/snowpark/__snapshots__/test_snowpark_entity.ambr b/tests/snowpark/__snapshots__/test_snowpark_entity.ambr index d623a2f50c..a486d2f274 100644 --- a/tests/snowpark/__snapshots__/test_snowpark_entity.ambr +++ b/tests/snowpark/__snapshots__/test_snowpark_entity.ambr @@ -50,3 +50,14 @@ HANDLER='app.func1_handler' ''' # --- +# name: test_nativeapp_children_interface + ''' + CREATE FUNCTION IDENTIFIER('func1') + COPY GRANTS + RETURNS string + LANGUAGE PYTHON + RUNTIME_VERSION '3.10' + IMPORTS= + HANDLER='app.func1_handler' + ''' +# --- diff --git a/tests/snowpark/test_snowpark_entity.py b/tests/snowpark/test_snowpark_entity.py index 9f584e024a..c87c83e0fe 100644 --- a/tests/snowpark/test_snowpark_entity.py +++ b/tests/snowpark/test_snowpark_entity.py @@ -7,7 +7,11 @@ AnacondaPackages, AvailablePackage, ) -from snowflake.cli._plugins.snowpark.snowpark_entity import DeployMode, FunctionEntity, ProcedureEntity +from snowflake.cli._plugins.snowpark.snowpark_entity import ( + DeployMode, + FunctionEntity, + ProcedureEntity, +) from snowflake.cli._plugins.snowpark.snowpark_entity_model import FunctionEntityModel from snowflake.cli._plugins.workspace.context import ActionContext, WorkspaceContext @@ -45,14 +49,46 @@ def example_function_workspace( ), ) + def test_cannot_instantiate_without_feature_flag(): with pytest.raises(NotImplementedError) as err: FunctionEntity() - assert str(err.value) == "Snowpark entities are not implemented yet" + assert str(err.value) == "Snowpark entity is not implemented yet" with pytest.raises(NotImplementedError) as err: ProcedureEntity() - assert str(err.value) == "Snowpark entities are not implemented yet" + assert str(err.value) == "Snowpark entity is not implemented yet" + + +@mock.patch(ANACONDA_PACKAGES) +def test_nativeapp_children_interface( + mock_anaconda, example_function_workspace, snapshot +): + mock_anaconda.return_value = AnacondaPackages( + { + "pandas": AvailablePackage("pandas", "1.2.3"), + "numpy": AvailablePackage("numpy", "1.2.3"), + "snowflake_snowpark_python": AvailablePackage( + "snowflake_snowpark_python", "1.2.3" + ), + } + ) + + sl, action_context = example_function_workspace + + sl.bundle(None, False, False, None, False) + bundle_artifact = ( + sl.root / "output" / sl.model.stage / "my_snowpark_project" / "app.py" + ) + deploy_sql_str = sl.get_deploy_sql(DeployMode.create) + grant_sql_str = sl.get_usage_grant_sql(app_role="app_role") + + assert bundle_artifact.exists() + assert deploy_sql_str == snapshot + assert ( + grant_sql_str == f"GRANT USAGE ON FUNCTION IDENTIFIER('func1') TO ROLE app_role" + ) + @mock.patch(EXECUTE_QUERY) def test_action_describe(mock_execute, example_function_workspace): @@ -130,6 +166,10 @@ def test_get_deploy_sql(mode, example_function_workspace, snapshot): entity, _ = example_function_workspace assert entity.get_deploy_sql(mode) == snapshot + def test_get_usage_grant_sql(example_function_workspace): entity, _ = example_function_workspace - assert entity.get_usage_grant_sql("test_role") == "GRANT USAGE ON FUNCTION IDENTIFIER('func1') TO ROLE test_role" \ No newline at end of file + assert ( + entity.get_usage_grant_sql("test_role") + == "GRANT USAGE ON FUNCTION IDENTIFIER('func1') TO ROLE test_role" + ) diff --git a/tests/streamlit/test_actions.py b/tests/streamlit/test_actions.py deleted file mode 100644 index 3a170ee187..0000000000 --- a/tests/streamlit/test_actions.py +++ /dev/null @@ -1,138 +0,0 @@ -from pathlib import Path -from unittest import mock - -import pytest -import yaml -from snowflake.cli._plugins.streamlit.streamlit_entity import StreamlitEntity -from snowflake.cli._plugins.streamlit.streamlit_entity_model import StreamlitEntityModel -from snowflake.cli._plugins.workspace.context import ActionContext, WorkspaceContext - -STREAMLIT_NAME = "test_streamlit" -CONNECTOR = "snowflake.connector.connect" -CONTEXT = "" -EXECUTE_QUERY = "snowflake.cli.api.sql_execution.BaseSqlExecutor.execute_query" - -GET_UI_PARAMETERS = "snowflake.cli._plugins.connection.util.get_ui_parameters" - - -@pytest.fixture -def example_streamlit_workspace(project_directory): - with project_directory("example_streamlit_v2") as pdir: - with Path(pdir / "snowflake.yml").open() as definition_file: - definition = yaml.safe_load(definition_file) - model = StreamlitEntityModel( - **definition.get("entities", {}).get("test_streamlit") - ) - - workspace_context = WorkspaceContext( - console=mock.MagicMock(), - project_root=pdir, - get_default_role=lambda: "test_role", - get_default_warehouse=lambda: "test_warehouse", - ) - - return ( - StreamlitEntity(workspace_ctx=workspace_context, entity_model=model), - ActionContext( - get_entity=lambda *args: None, - ), - ) - - -def test_bundle(example_streamlit_workspace): - - entity, action_ctx = example_streamlit_workspace - entity.action_bundle(action_ctx) - - output = entity.root / "output" / entity._entity_model.stage # noqa - assert output.exists() - assert (output / "streamlit_app.py").exists() - assert (output / "environment.yml").exists() - assert (output / "pages" / "my_page.py").exists() - - -@mock.patch(EXECUTE_QUERY) -def test_deploy(mock_execute, example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - entity.action_deploy(action_ctx) - - mock_execute.assert_called_with( - f"CREATE STREAMLIT IDENTIFIER('{STREAMLIT_NAME}') \n MAIN_FILE = 'streamlit_app.py' \n QUERY_WAREHOUSE = 'test_warehouse' \n TITLE = 'My Fancy Streamlit' \n" - ) - - -@mock.patch(EXECUTE_QUERY) -def test_drop(mock_execute, example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - entity.action_drop(action_ctx) - - mock_execute.assert_called_with(f"DROP STREAMLIT {STREAMLIT_NAME}") - - -@mock.patch(CONNECTOR) -@mock.patch( - GET_UI_PARAMETERS, - return_value={"UI_SNOWSIGHT_ENABLE_REGIONLESS_REDIRECT": "false"}, -) -@mock.patch("click.get_current_context") -def test_get_url( - mock_get_ctx, - mock_param, - mock_connect, - mock_cursor, - example_streamlit_workspace, - mock_ctx, -): - ctx = mock_ctx( - mock_cursor( - rows=[ - {"SYSTEM$GET_SNOWSIGHT_HOST()": "https://snowsight.domain"}, - {"SYSTEM$RETURN_CURRENT_ORG_NAME()": "FOOBARBAZ"}, - {"CURRENT_ACCOUNT_NAME()": "https://snowsight.domain"}, - ], - columns=["SYSTEM$GET_SNOWSIGHT_HOST()"], - ) - ) - mock_connect.return_value = ctx - mock_get_ctx.return_value = ctx - - entity, action_ctx = example_streamlit_workspace - result = entity.action_get_url(action_ctx) - - mock_connect.assert_called() - - -@mock.patch(EXECUTE_QUERY) -def test_execute(mock_execute, example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - entity.action_execute(action_ctx) - - mock_execute.assert_called_with(f"EXECUTE STREAMLIT {STREAMLIT_NAME}()") - - -def test_get_execute_sql(example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - execute_sql = entity.get_execute_sql(action_ctx) - - assert execute_sql == f"EXECUTE STREAMLIT {STREAMLIT_NAME}()" - - -def test_get_drop_sql(example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - drop_sql = entity.get_drop_sql(action_ctx) - - assert drop_sql == f"DROP STREAMLIT {STREAMLIT_NAME}" - - -def test_get_deploy_sql(example_streamlit_workspace): - entity, action_ctx = example_streamlit_workspace - deploy_sql = entity.get_deploy_sql(action_ctx) - - assert ( - deploy_sql - == f"""CREATE STREAMLIT IDENTIFIER('{STREAMLIT_NAME}') - MAIN_FILE = 'streamlit_app.py' - QUERY_WAREHOUSE = 'test_warehouse' - TITLE = 'My Fancy Streamlit' -""" - )