|
1 | 1 | import typing as t |
| 2 | +from unittest import mock |
| 3 | + |
2 | 4 | import pytest |
| 5 | +import time_machine |
3 | 6 | from pathlib import Path |
4 | 7 | from sqlglot import exp |
5 | 8 | from sqlglot.optimizer.qualify_columns import quote_identifiers |
@@ -441,7 +444,7 @@ def test_table_diff_table_name_matches_column_name(ctx: TestContext): |
441 | 444 | assert row_diff.full_match_count == 1 |
442 | 445 |
|
443 | 446 |
|
444 | | -def test_correlation_id_in_job_labels(ctx: TestContext): |
| 447 | +def test_plan_correlation_id_in_job_labels(ctx: TestContext): |
445 | 448 | model_name = ctx.table("test") |
446 | 449 |
|
447 | 450 | sqlmesh = ctx.create_context() |
@@ -469,3 +472,55 @@ def test_correlation_id_in_job_labels(ctx: TestContext): |
469 | 472 | labels = adapter._job_params.get("labels") |
470 | 473 | correlation_id = CorrelationId.from_plan_id(plan.plan_id) |
471 | 474 | assert labels == {correlation_id.job_type.value.lower(): correlation_id.job_id} |
| 475 | + |
| 476 | + |
| 477 | +@time_machine.travel("2023-01-08 15:00:00 UTC") |
| 478 | +def test_run_correlation_id_in_job_labels(ctx: TestContext): |
| 479 | + run_id = "test_run_id" |
| 480 | + model_name = ctx.table("run_test") |
| 481 | + |
| 482 | + sqlmesh = ctx.create_context() |
| 483 | + sqlmesh.upsert_model( |
| 484 | + load_sql_based_model( |
| 485 | + d.parse( |
| 486 | + f""" |
| 487 | +MODEL ( |
| 488 | + name {model_name}, |
| 489 | + kind INCREMENTAL_BY_TIME_RANGE ( |
| 490 | + time_column event_ts |
| 491 | + ), |
| 492 | + cron '@daily', |
| 493 | + start '2023-01-07' |
| 494 | +); |
| 495 | +SELECT 1 AS col, '2023-01-07' AS event_ts |
| 496 | +""" |
| 497 | + ) |
| 498 | + ) |
| 499 | + ) |
| 500 | + sqlmesh.plan(auto_apply=True, no_prompts=True) |
| 501 | + |
| 502 | + captured_evaluators: t.List = [] |
| 503 | + original_scheduler = sqlmesh.scheduler |
| 504 | + |
| 505 | + def scheduler_wrapper( |
| 506 | + environment: t.Optional[str] = None, |
| 507 | + snapshot_evaluator: t.Optional[t.Any] = None, |
| 508 | + ): |
| 509 | + if snapshot_evaluator is not None: |
| 510 | + captured_evaluators.append(snapshot_evaluator) |
| 511 | + return original_scheduler(environment=environment, snapshot_evaluator=snapshot_evaluator) |
| 512 | + |
| 513 | + with time_machine.travel("2023-01-09 00:00:00 UTC"): |
| 514 | + with mock.patch( |
| 515 | + "sqlmesh.core.context.analytics.collector.on_run_start", return_value=run_id |
| 516 | + ): |
| 517 | + with mock.patch.object(sqlmesh, "scheduler", scheduler_wrapper): |
| 518 | + sqlmesh.run() |
| 519 | + |
| 520 | + assert captured_evaluators |
| 521 | + adapter = t.cast(BigQueryEngineAdapter, captured_evaluators[-1].adapter) |
| 522 | + |
| 523 | + assert adapter.correlation_id is not None |
| 524 | + labels = adapter._job_params.get("labels") |
| 525 | + correlation_id = CorrelationId.from_run_id(run_id) |
| 526 | + assert labels == {correlation_id.job_type.value.lower(): correlation_id.job_id} |
0 commit comments