|
| 1 | +from pathlib import Path |
1 | 2 | from unittest import mock |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from snowflake.cli._plugins.streamlit.streamlit_entity import StreamlitEntity |
| 6 | +from snowflake.cli._plugins.streamlit.streamlit_entity_model import ( |
| 7 | + SPCS_RUNTIME_V2_NAME, |
| 8 | + StreamlitEntityModel, |
| 9 | +) |
4 | 10 |
|
5 | 11 | from tests.streamlit.streamlit_test_class import STREAMLIT_NAME, StreamlitTestClass |
6 | 12 |
|
@@ -112,3 +118,212 @@ def test_if_attribute_is_not_set_correct_error_is_raised( |
112 | 118 | with pytest.raises(ValueError) as e: |
113 | 119 | result = getattr(example_entity, attribute) |
114 | 120 | assert str(e.value) == f"Could not determine {attribute} for {STREAMLIT_NAME}" |
| 121 | + |
| 122 | + def test_spcs_runtime_v2_model_fields(self, workspace_context): |
| 123 | + """Test that StreamlitEntityModel accepts runtime_name and compute_pool fields""" |
| 124 | + model = StreamlitEntityModel( |
| 125 | + type="streamlit", |
| 126 | + identifier="test_streamlit", |
| 127 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 128 | + compute_pool="MYPOOL", |
| 129 | + main_file="streamlit_app.py", |
| 130 | + artifacts=["streamlit_app.py"], |
| 131 | + ) |
| 132 | + model.set_entity_id("test_streamlit") |
| 133 | + |
| 134 | + assert model.runtime_name == SPCS_RUNTIME_V2_NAME |
| 135 | + assert model.compute_pool == "MYPOOL" |
| 136 | + |
| 137 | + def test_get_deploy_sql_with_spcs_runtime_v2(self, workspace_context): |
| 138 | + """Test that get_deploy_sql includes RUNTIME_NAME and COMPUTE_POOL when experimental is True""" |
| 139 | + |
| 140 | + model = StreamlitEntityModel( |
| 141 | + type="streamlit", |
| 142 | + identifier="test_streamlit", |
| 143 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 144 | + compute_pool="MYPOOL", |
| 145 | + main_file="streamlit_app.py", |
| 146 | + artifacts=["streamlit_app.py"], |
| 147 | + ) |
| 148 | + model.set_entity_id("test_streamlit") |
| 149 | + |
| 150 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 151 | + |
| 152 | + # Test with FROM syntax (artifacts_dir provided) |
| 153 | + sql = entity.get_deploy_sql( |
| 154 | + artifacts_dir=Path("/tmp/artifacts"), experimental=True |
| 155 | + ) |
| 156 | + |
| 157 | + assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql |
| 158 | + assert "COMPUTE_POOL = 'MYPOOL'" in sql |
| 159 | + |
| 160 | + def test_get_deploy_sql_spcs_runtime_v2_with_stage(self, workspace_context): |
| 161 | + """Test that SPCS runtime v2 clauses are NOT added with stage-based deployment (old-style streamlits)""" |
| 162 | + |
| 163 | + model = StreamlitEntityModel( |
| 164 | + type="streamlit", |
| 165 | + identifier="test_streamlit", |
| 166 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 167 | + compute_pool="MYPOOL", |
| 168 | + main_file="streamlit_app.py", |
| 169 | + artifacts=["streamlit_app.py"], |
| 170 | + ) |
| 171 | + model.set_entity_id("test_streamlit") |
| 172 | + |
| 173 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 174 | + |
| 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) |
| 178 | + |
| 179 | + assert "ROOT_LOCATION = '@stage/path'" in sql |
| 180 | + assert "RUNTIME_NAME" not in sql |
| 181 | + assert "COMPUTE_POOL" not in sql |
| 182 | + |
| 183 | + 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 | + model = StreamlitEntityModel( |
| 186 | + type="streamlit", |
| 187 | + identifier="test_streamlit", |
| 188 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 189 | + compute_pool="MYPOOL", |
| 190 | + main_file="streamlit_app.py", |
| 191 | + artifacts=["streamlit_app.py"], |
| 192 | + ) |
| 193 | + model.set_entity_id("test_streamlit") |
| 194 | + |
| 195 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 196 | + |
| 197 | + # Test without experimental flag enabled |
| 198 | + sql = entity.get_deploy_sql( |
| 199 | + artifacts_dir=Path("/tmp/artifacts"), experimental=False |
| 200 | + ) |
| 201 | + |
| 202 | + assert "RUNTIME_NAME" not in sql |
| 203 | + assert "COMPUTE_POOL" not in sql |
| 204 | + |
| 205 | + def test_spcs_runtime_v2_requires_correct_runtime_name(self, workspace_context): |
| 206 | + """Test that SPCS runtime v2 requires correct runtime name to be enabled""" |
| 207 | + model = StreamlitEntityModel( |
| 208 | + type="streamlit", |
| 209 | + identifier="test_streamlit", |
| 210 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 211 | + compute_pool="MYPOOL", |
| 212 | + main_file="streamlit_app.py", |
| 213 | + artifacts=["streamlit_app.py"], |
| 214 | + ) |
| 215 | + model.set_entity_id("test_streamlit") |
| 216 | + |
| 217 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 218 | + |
| 219 | + # Test with experimental=True and correct runtime_name |
| 220 | + sql = entity.get_deploy_sql( |
| 221 | + artifacts_dir=Path("/tmp/artifacts"), experimental=True |
| 222 | + ) |
| 223 | + assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql |
| 224 | + assert "COMPUTE_POOL = 'MYPOOL'" in sql |
| 225 | + |
| 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 | + ) |
| 230 | + assert "RUNTIME_NAME" not in sql |
| 231 | + assert "COMPUTE_POOL" not in sql |
| 232 | + |
| 233 | + # Test with wrong runtime_name |
| 234 | + model.runtime_name = "SOME_OTHER_RUNTIME" |
| 235 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 236 | + sql = entity.get_deploy_sql( |
| 237 | + artifacts_dir=Path("/tmp/artifacts"), experimental=True |
| 238 | + ) |
| 239 | + assert "RUNTIME_NAME" not in sql |
| 240 | + assert "COMPUTE_POOL" not in sql |
| 241 | + |
| 242 | + def test_spcs_runtime_v2_requires_runtime_and_pool(self, workspace_context): |
| 243 | + """Test that SPCS runtime v2 SQL generation works with valid models""" |
| 244 | + |
| 245 | + # Test with valid container runtime and compute_pool |
| 246 | + model = StreamlitEntityModel( |
| 247 | + type="streamlit", |
| 248 | + identifier="test_streamlit", |
| 249 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 250 | + compute_pool="MYPOOL", |
| 251 | + main_file="streamlit_app.py", |
| 252 | + artifacts=["streamlit_app.py"], |
| 253 | + ) |
| 254 | + model.set_entity_id("test_streamlit") |
| 255 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 256 | + sql = entity.get_deploy_sql( |
| 257 | + artifacts_dir=Path("/tmp/artifacts"), experimental=True |
| 258 | + ) |
| 259 | + assert f"RUNTIME_NAME = '{SPCS_RUNTIME_V2_NAME}'" in sql |
| 260 | + assert "COMPUTE_POOL = 'MYPOOL'" in sql |
| 261 | + |
| 262 | + # Test with warehouse runtime (no compute_pool needed) |
| 263 | + model = StreamlitEntityModel( |
| 264 | + type="streamlit", |
| 265 | + identifier="test_streamlit", |
| 266 | + runtime_name="SYSTEM$WAREHOUSE_RUNTIME", |
| 267 | + main_file="streamlit_app.py", |
| 268 | + artifacts=["streamlit_app.py"], |
| 269 | + ) |
| 270 | + model.set_entity_id("test_streamlit") |
| 271 | + entity = StreamlitEntity(workspace_ctx=workspace_context, entity_model=model) |
| 272 | + sql = entity.get_deploy_sql( |
| 273 | + artifacts_dir=Path("/tmp/artifacts"), experimental=True |
| 274 | + ) |
| 275 | + # Warehouse runtime should not trigger SPCS runtime v2 mode |
| 276 | + assert "RUNTIME_NAME" not in sql |
| 277 | + assert "COMPUTE_POOL" not in sql |
| 278 | + |
| 279 | + def test_spcs_runtime_validation(self, workspace_context): |
| 280 | + """Test validation for SPCS runtime configuration""" |
| 281 | + |
| 282 | + # Test: SYSTEM$ST_CONTAINER_RUNTIME_PY3_11 requires compute_pool |
| 283 | + escaped_runtime_name = SPCS_RUNTIME_V2_NAME.replace("$", r"\$") |
| 284 | + with pytest.raises( |
| 285 | + ValueError, |
| 286 | + match=rf"compute_pool is required when using {escaped_runtime_name}", |
| 287 | + ): |
| 288 | + StreamlitEntityModel( |
| 289 | + type="streamlit", |
| 290 | + identifier="test_streamlit", |
| 291 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 292 | + main_file="streamlit_app.py", |
| 293 | + artifacts=["streamlit_app.py"], |
| 294 | + ) |
| 295 | + |
| 296 | + # Test: compute_pool without runtime_name is invalid |
| 297 | + with pytest.raises( |
| 298 | + ValueError, match="compute_pool is specified without runtime_name" |
| 299 | + ): |
| 300 | + StreamlitEntityModel( |
| 301 | + type="streamlit", |
| 302 | + identifier="test_streamlit", |
| 303 | + compute_pool="MYPOOL", |
| 304 | + main_file="streamlit_app.py", |
| 305 | + artifacts=["streamlit_app.py"], |
| 306 | + ) |
| 307 | + |
| 308 | + # Test: warehouse runtime without compute_pool is valid |
| 309 | + model = StreamlitEntityModel( |
| 310 | + type="streamlit", |
| 311 | + identifier="test_streamlit", |
| 312 | + runtime_name="SYSTEM$WAREHOUSE_RUNTIME", |
| 313 | + main_file="streamlit_app.py", |
| 314 | + artifacts=["streamlit_app.py"], |
| 315 | + ) |
| 316 | + assert model.runtime_name == "SYSTEM$WAREHOUSE_RUNTIME" |
| 317 | + assert model.compute_pool is None |
| 318 | + |
| 319 | + # Test: container runtime with compute_pool is valid |
| 320 | + model = StreamlitEntityModel( |
| 321 | + type="streamlit", |
| 322 | + identifier="test_streamlit", |
| 323 | + runtime_name=SPCS_RUNTIME_V2_NAME, |
| 324 | + compute_pool="MYPOOL", |
| 325 | + main_file="streamlit_app.py", |
| 326 | + artifacts=["streamlit_app.py"], |
| 327 | + ) |
| 328 | + assert model.runtime_name == SPCS_RUNTIME_V2_NAME |
| 329 | + assert model.compute_pool == "MYPOOL" |
0 commit comments