|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
| 14 | +import json |
14 | 15 | from typing import List, Optional |
15 | 16 |
|
16 | 17 | import typer |
17 | 18 | from snowflake.cli._plugins.dcm.manager import DCMProjectManager |
| 19 | +from snowflake.cli._plugins.dcm.utils import format_test_failures |
18 | 20 | from snowflake.cli._plugins.object.command_aliases import add_object_command_aliases |
19 | 21 | from snowflake.cli._plugins.object.commands import scope_option |
20 | 22 | from snowflake.cli._plugins.object.manager import ObjectManager |
@@ -235,6 +237,47 @@ def drop_deployment( |
235 | 237 | ) |
236 | 238 |
|
237 | 239 |
|
| 240 | +@app.command(requires_connection=True) |
| 241 | +def test( |
| 242 | + identifier: FQN = dcm_identifier, |
| 243 | + **options, |
| 244 | +): |
| 245 | + """ |
| 246 | + Test all expectations set for tables, views and dynamic tables defined |
| 247 | + in DCM project. |
| 248 | + """ |
| 249 | + with cli_console.spinner() as spinner: |
| 250 | + spinner.add_task(description=f"Testing dcm project {identifier}", total=None) |
| 251 | + result = DCMProjectManager().test(project_identifier=identifier) |
| 252 | + |
| 253 | + row = result.fetchone() |
| 254 | + if not row: |
| 255 | + return MessageResult("No data.") |
| 256 | + |
| 257 | + result_data = row[0] |
| 258 | + result_json = ( |
| 259 | + json.loads(result_data) if isinstance(result_data, str) else result_data |
| 260 | + ) |
| 261 | + |
| 262 | + expectations = result_json.get("expectations", []) |
| 263 | + |
| 264 | + if not expectations: |
| 265 | + return MessageResult("No expectations defined in the project.") |
| 266 | + |
| 267 | + if result_json.get("status") == "EXPECTATION_VIOLATED": |
| 268 | + failed_expectations = [ |
| 269 | + exp for exp in expectations if exp.get("expectation_violated", False) |
| 270 | + ] |
| 271 | + total_tests = len(expectations) |
| 272 | + failed_count = len(failed_expectations) |
| 273 | + error_message = format_test_failures( |
| 274 | + failed_expectations, total_tests, failed_count |
| 275 | + ) |
| 276 | + raise CliError(error_message) |
| 277 | + |
| 278 | + return MessageResult(f"All {len(expectations)} expectation(s) passed successfully.") |
| 279 | + |
| 280 | + |
238 | 281 | def _get_effective_stage(identifier: FQN, from_location: Optional[str]): |
239 | 282 | manager = DCMProjectManager() |
240 | 283 | if not from_location: |
|
0 commit comments