Skip to content

Commit 4bd5ba0

Browse files
committed
refactor(tests): enhance command execution in DagsterTestContext
- Updated `_run_command` method to accept an optional `cwd` parameter for dynamic directory changes during command execution. - Adjusted test case to specify the working directory for asset materialization, ensuring correct file placement. - Added a sleep delay in the test to manage timing issues during execution. - Cleaned up print statements for better clarity in command execution context.
1 parent 93cfd09 commit 4bd5ba0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

dagster_sqlmesh/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ class DagsterTestContext:
541541
dagster_project_path: str
542542
sqlmesh_project_path: str
543543

544-
def _run_command(self, cmd: list[str]) -> None:
544+
def _run_command(self, cmd: list[str], cwd: str | None = None) -> None:
545545
"""Execute a command and stream its output in real-time.
546546
547547
Args:
@@ -590,10 +590,10 @@ def stream_output(
590590

591591
print(f"Running command: {' '.join(cmd)}")
592592
print(f"Current working directory: {os.getcwd()}")
593-
print(f"Changing to directory: {self.dagster_project_path}")
594593

595-
# Change to the dagster project directory before running the command
596-
os.chdir(self.dagster_project_path)
594+
if cwd:
595+
print(f"Changing to directory: {cwd}")
596+
os.chdir(cwd)
597597

598598
process = subprocess.Popen(
599599
cmd,
@@ -699,7 +699,8 @@ def asset_materialisation(
699699
config_json,
700700
]
701701

702-
self._run_command(cmd)
702+
# Change to the sqlmesh project directory before running the command (for some reason asset materialize needs to be run from the dirctory you want the db.db file to be in - feel free to investigate)
703+
self._run_command(cmd=cmd, cwd=self.sqlmesh_project_path)
703704

704705
def reset_assets(self) -> None:
705706
"""Resets the assets to the original state"""

dagster_sqlmesh/controller/tests_plan_and_run/test_model_code_change.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import time
23

34
import pytest
45

@@ -237,11 +238,13 @@ def test_given_model_chain_when_running_with_different_flags_then_behaves_as_exp
237238

238239
# sample_dagster_test_context.asset_materialisation(assets=["intermediate_model_1"], plan_options=PlanOptions(skip_backfill=True, enable_preview=True, skip_tests=True))
239240

241+
time.sleep(5)
242+
240243
intermediate_model_1_df = (
241244
sample_sqlmesh_test_context.query(
242245
"""
243246
SELECT *
244-
FROM sqlmesh_example.intermediate_model_1
247+
FROM sqlmesh_example__dev.intermediate_model_1
245248
""",
246249
return_df=True,
247250
)

0 commit comments

Comments
 (0)