-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtest_run.py
More file actions
85 lines (63 loc) · 3.1 KB
/
test_run.py
File metadata and controls
85 lines (63 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import typing as t
import pytest
from pathlib import Path
from click.testing import Result
import time_machine
from sqlmesh_dbt.operations import create
from tests.cli.test_cli import FREEZE_TIME
from tests.dbt.conftest import EmptyProjectCreator
pytestmark = pytest.mark.slow
def test_run(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result]):
result = invoke_cli(["run"])
assert result.exit_code == 0
assert not result.exception
assert "Model batches executed" in result.output
def test_run_with_selectors(jaffle_shop_duckdb: Path, invoke_cli: t.Callable[..., Result]):
with time_machine.travel(FREEZE_TIME):
# do an initial run to create the objects
# otherwise the selected subset may depend on something that hasnt been created
result = invoke_cli(["run"])
assert result.exit_code == 0
assert "main.orders" in result.output
result = invoke_cli(["run", "--select", "raw_customers+", "--exclude", "orders"])
assert result.exit_code == 0
assert not result.exception
assert "main.stg_customers" in result.output
assert "main.stg_orders" in result.output
assert "main.stg_payments" in result.output
assert "main.customers" in result.output
assert "main.orders" not in result.output
assert "Model batches executed" in result.output
def test_run_with_changes_and_full_refresh(
create_empty_project: EmptyProjectCreator, invoke_cli: t.Callable[..., Result]
):
project_path, models_path = create_empty_project(project_name="test")
engine_adapter = create(project_path).context.engine_adapter
engine_adapter.execute("create table external_table as select 'foo' as a, 'bar' as b")
(models_path / "model_a.sql").write_text("select a, b from external_table")
(models_path / "model_b.sql").write_text("select a, b from {{ ref('model_a') }}")
# populate initial env
result = invoke_cli(["run"])
assert result.exit_code == 0
assert not result.exception
assert engine_adapter.fetchall("select a, b from model_b") == [("foo", "bar")]
engine_adapter.execute("insert into external_table (a, b) values ('baz', 'bing')")
(project_path / "models" / "model_b.sql").write_text(
"select a, b, 'changed' as c from {{ ref('model_a') }}"
)
# Clear dbt's partial parse cache to ensure file changes are detected
# Without it dbt may use stale cached model definitions, causing flakiness
partial_parse_file = project_path / "target" / "sqlmesh_partial_parse.msgpack"
if partial_parse_file.exists():
partial_parse_file.unlink()
# run with --full-refresh. this should:
# - fully refresh model_a (pick up the new records from external_table)
# - deploy the local change to model_b (introducing the 'changed' column)
result = invoke_cli(["run", "--full-refresh"])
assert result.exit_code == 0
assert not result.exception
assert engine_adapter.fetchall("select a, b from model_a") == [("foo", "bar"), ("baz", "bing")]
assert engine_adapter.fetchall("select a, b, c from model_b") == [
("foo", "bar", "changed"),
("baz", "bing", "changed"),
]