Skip to content

Commit 5cdc62e

Browse files
Fix unit tests for versioned Streamlit deployment
- Add mock for describe() method to return versioned stage path - Update test_deploy to use legacy=True for testing old behavior - Replace experimental parameter with legacy parameter in SPCS runtime v2 tests - Fix test_deploy_with_spcs_runtime_v2_and_legacy_flag_raises_error invalid parameters Changes reflect that versioned deployment is now the default, and the experimental flag has been replaced with a legacy flag to opt into old behavior.
1 parent 64c0ba0 commit 5cdc62e

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

tests/streamlit/streamlit_test_class.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def setup_method(self):
5353
lambda _, **kwargs: False,
5454
).start()
5555

56+
# Mock describe() to return a versioned stage path for versioned deployments
57+
self.mock_describe = mock.patch(
58+
"snowflake.cli._plugins.streamlit.streamlit_entity.StreamlitEntity.describe"
59+
).start()
60+
mock_cursor = mock.Mock()
61+
mock_cursor.fetchone.return_value = {
62+
"live_version_location_uri": f"snow://streamlit/DB.PUBLIC.{STREAMLIT_NAME}/versions/live/"
63+
}
64+
self.mock_describe.return_value = mock_cursor
65+
5666
def teardown_method(self):
5767
mock.patch.stopall()
5868

tests/streamlit/test_streamlit_entity.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def test_deploy(self, mock_get_url, mock_describe, example_entity, action_contex
5555
mock_describe.return_value = False
5656
mock_get_url.return_value = "https://snowflake.com"
5757

58-
example_entity.action_deploy(action_context, _open=False, replace=False)
58+
# Test legacy deployment behavior
59+
example_entity.action_deploy(
60+
action_context, _open=False, replace=False, legacy=True
61+
)
5962

6063
self.mock_execute.assert_called_with(
6164
f"CREATE STREAMLIT IDENTIFIER('{STREAMLIT_NAME}')\nROOT_LOCATION = '@streamlit/test_streamlit'\nMAIN_FILE = 'streamlit_app.py'\nQUERY_WAREHOUSE = test_warehouse\nTITLE = 'My Fancy Streamlit';"
@@ -149,10 +152,8 @@ def test_get_deploy_sql_with_spcs_runtime_v2(self, workspace_context):
149152

150153
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
151154

152-
# Test with FROM syntax (artifacts_dir provided)
153-
sql = entity.get_deploy_sql(
154-
artifacts_dir=Path("/tmp/artifacts"), experimental=True
155-
)
155+
# Test with FROM syntax (artifacts_dir provided) - versioned deployment
156+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=False)
156157

157158
assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql
158159
assert "COMPUTE_POOL = 'MYPOOL'" in sql
@@ -172,16 +173,16 @@ def test_get_deploy_sql_spcs_runtime_v2_with_stage(self, workspace_context):
172173

173174
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
174175

175-
# Test with stage-based deployment - should NOT include SPCS runtime fields
176-
# even when experimental=True, as stage-based deployments are old-style
177-
sql = entity.get_deploy_sql(from_stage_name="@stage/path", experimental=True)
176+
# Test with stage-based deployment (ROOT_LOCATION) - should NOT include SPCS runtime fields
177+
# as stage-based deployments are old-style
178+
sql = entity.get_deploy_sql(from_stage_name="@stage/path", legacy=False)
178179

179180
assert "ROOT_LOCATION = '@stage/path'" in sql
180181
assert "RUNTIME_NAME" not in sql
181182
assert "COMPUTE_POOL" not in sql
182183

183184
def test_get_deploy_sql_without_spcs_runtime_v2(self, workspace_context):
184-
"""Test that get_deploy_sql works normally when experimental is False"""
185+
"""Test that get_deploy_sql works normally when legacy is True"""
185186
model = StreamlitEntityModel(
186187
type="streamlit",
187188
identifier="test_streamlit",
@@ -194,10 +195,8 @@ def test_get_deploy_sql_without_spcs_runtime_v2(self, workspace_context):
194195

195196
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
196197

197-
# Test without experimental flag enabled
198-
sql = entity.get_deploy_sql(
199-
artifacts_dir=Path("/tmp/artifacts"), experimental=False
200-
)
198+
# Test with legacy flag - should not add SPCS runtime v2 fields
199+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=True)
201200

202201
assert "RUNTIME_NAME" not in sql
203202
assert "COMPUTE_POOL" not in sql
@@ -216,26 +215,20 @@ def test_spcs_runtime_v2_requires_correct_runtime_name(self, workspace_context):
216215

217216
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
218217

219-
# Test with experimental=True and correct runtime_name
220-
sql = entity.get_deploy_sql(
221-
artifacts_dir=Path("/tmp/artifacts"), experimental=True
222-
)
218+
# Test with versioned deployment (default, legacy=False) and correct runtime_name
219+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=False)
223220
assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql
224221
assert "COMPUTE_POOL = 'MYPOOL'" in sql
225222

226-
# Test with experimental=False, should not add SPCS fields
227-
sql = entity.get_deploy_sql(
228-
artifacts_dir=Path("/tmp/artifacts"), experimental=False
229-
)
223+
# Test with legacy=True, should not add SPCS fields
224+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=True)
230225
assert "RUNTIME_NAME" not in sql
231226
assert "COMPUTE_POOL" not in sql
232227

233228
# Test with wrong runtime_name
234229
model.runtime_name = "SOME_OTHER_RUNTIME"
235230
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
236-
sql = entity.get_deploy_sql(
237-
artifacts_dir=Path("/tmp/artifacts"), experimental=True
238-
)
231+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=False)
239232
assert "RUNTIME_NAME" not in sql
240233
assert "COMPUTE_POOL" not in sql
241234

@@ -253,9 +246,7 @@ def test_spcs_runtime_v2_requires_runtime_and_pool(self, workspace_context):
253246
)
254247
model.set_entity_id("test_streamlit")
255248
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
256-
sql = entity.get_deploy_sql(
257-
artifacts_dir=Path("/tmp/artifacts"), experimental=True
258-
)
249+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=False)
259250
assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql
260251
assert "COMPUTE_POOL = 'MYPOOL'" in sql
261252

@@ -269,9 +260,7 @@ def test_spcs_runtime_v2_requires_runtime_and_pool(self, workspace_context):
269260
)
270261
model.set_entity_id("test_streamlit")
271262
entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model)
272-
sql = entity.get_deploy_sql(
273-
artifacts_dir=Path("/tmp/artifacts"), experimental=True
274-
)
263+
sql = entity.get_deploy_sql(artifacts_dir=Path("/tmp/artifacts"), legacy=False)
275264
# Warehouse runtime should not trigger SPCS runtime v2 mode
276265
assert "RUNTIME_NAME" not in sql
277266
assert "COMPUTE_POOL" not in sql
@@ -347,12 +336,11 @@ def test_deploy_with_spcs_runtime_v2_and_legacy_flag_raises_error(
347336
main_file="streamlit_app.py",
348337
artifacts=["streamlit_app.py"],
349338
)
339+
model.set_entity_id("test_streamlit")
350340

351341
entity = StreamlitEntity(
352342
workspace_ctx=workspace_context,
353343
entity_model=model,
354-
project_root=Path(__file__).parent / "test_data" / "projects",
355-
entity_id="test_streamlit",
356344
)
357345

358346
with pytest.raises(

0 commit comments

Comments
 (0)