Skip to content

Commit 1ec74ba

Browse files
committed
Add testing
1 parent 889a744 commit 1ec74ba

File tree

3 files changed

+141
-14
lines changed

3 files changed

+141
-14
lines changed

sqlmesh/core/console.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ class SignalConsole(abc.ABC):
335335
def start_signal_progress(
336336
self,
337337
snapshot: Snapshot,
338-
total_signals: int,
339338
default_catalog: t.Optional[str],
340339
environment_naming_info: EnvironmentNamingInfo,
341340
) -> None:
@@ -355,7 +354,7 @@ def update_signal_progress(
355354
"""Updates the signal checking progress."""
356355

357356
@abc.abstractmethod
358-
def stop_signal_progress(self, snapshot: Snapshot) -> None:
357+
def stop_signal_progress(self) -> None:
359358
"""Indicates that signal checking has completed for a snapshot."""
360359

361360

@@ -569,7 +568,6 @@ def stop_evaluation_progress(self, success: bool = True) -> None:
569568
def start_signal_progress(
570569
self,
571570
snapshot: Snapshot,
572-
total_signals: int,
573571
default_catalog: t.Optional[str],
574572
environment_naming_info: EnvironmentNamingInfo,
575573
) -> None:
@@ -587,7 +585,7 @@ def update_signal_progress(
587585
) -> None:
588586
pass
589587

590-
def stop_signal_progress(self, snapshot: Snapshot) -> None:
588+
def stop_signal_progress(self) -> None:
591589
pass
592590

593591
def start_creation_progress(
@@ -1112,7 +1110,6 @@ def stop_evaluation_progress(self, success: bool = True) -> None:
11121110
def start_signal_progress(
11131111
self,
11141112
snapshot: Snapshot,
1115-
total_signals: int,
11161113
default_catalog: t.Optional[str],
11171114
environment_naming_info: EnvironmentNamingInfo,
11181115
) -> None:
@@ -1183,7 +1180,7 @@ def update_signal_progress(
11831180
if self.signal_status_tree is not None:
11841181
self.signal_status_tree.add(tree)
11851182

1186-
def stop_signal_progress(self, snapshot: Snapshot) -> None:
1183+
def stop_signal_progress(self) -> None:
11871184
"""Indicates that signal checking has completed for a snapshot."""
11881185
if self.signal_status_tree is not None:
11891186
self._print(self.signal_status_tree)

sqlmesh/core/snapshot/definition.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ def check_ready_intervals(
991991
if console:
992992
console.start_signal_progress(
993993
self,
994-
len(signals),
995994
default_catalog,
996995
environment_naming_info or EnvironmentNamingInfo(),
997996
)
@@ -1033,7 +1032,7 @@ def check_ready_intervals(
10331032

10341033
# Stop signal progress tracking
10351034
if console:
1036-
console.stop_signal_progress(self)
1035+
console.stop_signal_progress()
10371036

10381037
return intervals
10391038

tests/cli/test_cli.py

Lines changed: 137 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import json
12
import logging
3+
import pytest
24
import string
5+
import time_machine
36
from contextlib import contextmanager
47
from os import getcwd, path, remove
58
from pathlib import Path
69
from shutil import rmtree
10+
from unittest.mock import MagicMock
11+
712
from click import ClickException
8-
import pytest
913
from click.testing import CliRunner
10-
import time_machine
11-
import json
12-
from unittest.mock import MagicMock
1314
from sqlmesh import RuntimeEnv
1415
from sqlmesh.cli.project_init import ProjectTemplate, init_example_project
1516
from sqlmesh.cli.main import cli
@@ -42,13 +43,13 @@ def disable_logging():
4243
logging.disable(logging.NOTSET)
4344

4445

45-
def create_example_project(temp_dir) -> None:
46+
def create_example_project(temp_dir, template=ProjectTemplate.DEFAULT) -> None:
4647
"""
4748
Sets up CLI tests requiring a real SQLMesh project by:
4849
- Creating the SQLMesh example project in the temp_dir directory
4950
- Overwriting the config.yaml file so the duckdb database file will be created in the temp_dir directory
5051
"""
51-
init_example_project(temp_dir, engine_type="duckdb")
52+
init_example_project(temp_dir, engine_type="duckdb", template=template)
5253
with open(temp_dir / "config.yaml", "w", encoding="utf-8") as f:
5354
f.write(
5455
f"""gateways:
@@ -2044,3 +2045,133 @@ def test_render(runner: CliRunner, tmp_path: Path):
20442045
"""
20452046

20462047
assert expected in cleaned_output
2048+
2049+
2050+
@time_machine.travel(FREEZE_TIME)
2051+
def test_signals(runner: CliRunner, tmp_path: Path):
2052+
create_example_project(tmp_path, template=ProjectTemplate.EMPTY)
2053+
2054+
# Create signals module
2055+
signals_dir = tmp_path / "signals"
2056+
signals_dir.mkdir(exist_ok=True)
2057+
2058+
# Create signal definitions
2059+
(signals_dir / "signal.py").write_text(
2060+
"""from sqlmesh import signal
2061+
@signal()
2062+
def only_first_two_ready(batch):
2063+
if len(batch) > 2:
2064+
return batch[:2]
2065+
return batch
2066+
2067+
@signal()
2068+
def none_ready(batch):
2069+
return False
2070+
"""
2071+
)
2072+
2073+
# Create model with signals
2074+
(tmp_path / "models" / "model_with_signals.sql").write_text(
2075+
"""MODEL (
2076+
name sqlmesh_example.model_with_signals,
2077+
kind INCREMENTAL_BY_TIME_RANGE (
2078+
time_column ds
2079+
),
2080+
start '2022-12-28',
2081+
cron '@daily',
2082+
signals [
2083+
only_first_two_ready()
2084+
]
2085+
);
2086+
2087+
SELECT
2088+
ds::DATE as ds,
2089+
'test' as value
2090+
FROM VALUES
2091+
('2022-12-28'),
2092+
('2022-12-29'),
2093+
('2022-12-30'),
2094+
('2022-12-31'),
2095+
('2023-01-01')
2096+
AS t(ds)
2097+
WHERE ds::DATE BETWEEN @start_ds AND @end_ds
2098+
"""
2099+
)
2100+
2101+
# Create model with no ready intervals
2102+
(tmp_path / "models" / "model_with_unready.sql").write_text(
2103+
"""MODEL (
2104+
name sqlmesh_example.model_with_unready,
2105+
kind INCREMENTAL_BY_TIME_RANGE (
2106+
time_column ds
2107+
),
2108+
start '2022-12-28',
2109+
cron '@daily',
2110+
signals [
2111+
none_ready()
2112+
]
2113+
);
2114+
2115+
SELECT
2116+
ds::DATE as ds,
2117+
'unready' as value
2118+
FROM VALUES
2119+
('2022-12-28'),
2120+
('2022-12-29'),
2121+
('2022-12-30'),
2122+
('2022-12-31'),
2123+
('2023-01-01')
2124+
AS t(ds)
2125+
WHERE ds::DATE BETWEEN @start_ds AND @end_ds
2126+
"""
2127+
)
2128+
2129+
# Test 1: Normal plan flow with --no-prompts --auto-apply
2130+
result = runner.invoke(
2131+
cli,
2132+
[
2133+
"--paths",
2134+
str(tmp_path),
2135+
"plan",
2136+
"--no-prompts",
2137+
"--auto-apply",
2138+
],
2139+
)
2140+
assert result.exit_code == 0
2141+
2142+
assert "Checking signals for sqlmesh_example.model_with_signals" in result.output
2143+
assert "[1/1] only_first_two_ready" in result.output
2144+
assert "check: 2022-12-28 - 2022-12-31" in result.output
2145+
assert "ready: 2022-12-28 - 2022-12-29" in result.output
2146+
2147+
assert "Checking signals for sqlmesh_example.model_with_unready" in result.output
2148+
assert "[1/1] none_ready" in result.output
2149+
assert "ready: no intervals" in result.output
2150+
2151+
# Test 2: Run command with start and end dates
2152+
result = runner.invoke(
2153+
cli,
2154+
[
2155+
"--paths",
2156+
str(tmp_path),
2157+
"run",
2158+
"--start",
2159+
"2022-12-29",
2160+
"--end",
2161+
"2022-12-31",
2162+
],
2163+
)
2164+
assert result.exit_code == 0
2165+
2166+
assert "Checking signals for sqlmesh_example.model_with_signals" in result.output
2167+
assert "[1/1] only_first_two_ready" in result.output
2168+
assert "check: 2022-12-30 - 2022-12-31" in result.output
2169+
assert "ready: 2022-12-30 - 2022-12-31" in result.output
2170+
2171+
assert "Checking signals for sqlmesh_example.model_with_unready" in result.output
2172+
assert "[1/1] none_ready" in result.output
2173+
assert "check: 2022-12-29 - 2022-12-31" in result.output
2174+
assert "ready: no intervals" in result.output
2175+
2176+
# Only one model was executed
2177+
assert "100.0% • 1/1 • 0:00:00" in result.output

0 commit comments

Comments
 (0)