Skip to content

Commit a5c5178

Browse files
committed
Fix jupyter test
1 parent 19520c2 commit a5c5178

2 files changed

Lines changed: 218 additions & 4 deletions

File tree

test.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
from unittest import skip
2+
from sqlmesh.utils import enable_debug_mode
3+
from sqlmesh.cli.example_project import init_example_project
4+
from sqlmesh.core.config.loader import load_config_from_paths
5+
from sqlmesh.core.context import Context
6+
from sqlglot import parse_one, transpile, exp
7+
from pathlib import Path
8+
from sqlmesh.core.config import Config, DuckDBConnectionConfig, ModelDefaultsConfig
9+
import duckdb
10+
from sqlmesh.dbt.context import DbtContext
11+
12+
#load_config_from_paths(Path("./sqlmesh-example/config.yaml"))
13+
14+
#### sqlmesh example
15+
16+
project_dir = "sqlmesh-example-fresh"
17+
gateway = "inttest_databricks"
18+
state_file = "duckdb.db"
19+
20+
import os
21+
import shutil
22+
#os.environ["SQLMESH_DEBUG"] = "1"
23+
24+
os.chdir(f"/Users/trey/tobiko/sqlmesh/{project_dir}")
25+
#os.chdir("/users/trey/desktop/temp")
26+
27+
if os.path.exists(".cache"):
28+
shutil.rmtree(".cache")
29+
30+
if os.path.exists(state_file):
31+
os.remove(state_file)
32+
if os.path.exists(state_file + ".wal"):
33+
os.remove(state_file + ".wal")
34+
35+
temp = Context(paths=f"/Users/trey/tobiko/sqlmesh/{project_dir}")
36+
#temp.print_info()
37+
#temp.run(ignore_cron=True)
38+
#temp.test()
39+
temp.plan(no_prompts=True, auto_apply=True, skip_tests=True)
40+
41+
# #### sushi dbt
42+
43+
# import os
44+
# os.chdir("./examples/sushi_dbt")
45+
# #enable_debug_mode()
46+
# temp = Context(paths="/users/trey/tobiko/sqlmesh/examples/sushi_dbt")
47+
48+
#### sushi
49+
50+
# import os
51+
# os.chdir("./examples/sushi")
52+
# #enable_debug_mode()
53+
# temp = Context(paths="/users/trey/tobiko/sqlmesh/examples/sushi", gateway="inttest_snowflake")
54+
55+
# temp.plan(no_prompts=True, auto_apply=True, skip_tests=True)
56+
57+
#case_2 = duckdb.connect("/users/trey/tobiko/sqlmesh/duckdb_attach/case_2.duckdb")
58+
59+
# temp.render("sqlmesh_example.full_model")
60+
61+
# temp.plan(include_unmodified=True, auto_apply=True, no_prompts=True, skip_tests=True)
62+
63+
# #temp.evaluate("sqlmesh_example.select_external", start = "2020-01-01", end="today", execution_time="now")
64+
65+
# #temp.table_diff(source="prod", target="dev", model_or_snapshot="sqlmesh_example.incremental_model")
66+
67+
# #from sqlglot import exp
68+
69+
# #exp.func("IF", exp.and_(*((exp.column(c, "s").eq(exp.column(c, "t")), exp.column(c, "s").not_().is_(exp.Null()), exp.column(c, "t").not_().is_(exp.Null())) for c in list(set([a.name for a in s_index]))),), 1, 0).sql()
70+
71+
# #exp.func("IF", exp.and_(*(exp.column(c, "s").eq(exp.column(c, "t")), exp.column(c, "s").not_().is_(exp.Null()), exp.column(c, "t").not_().is_(exp.Null()) for c in list(set([a.name for a in s_index]))),), 1, 0).sql()
72+
# #exp.func("IF", exp.and_(*(exp.and_(exp.column(c, "s").eq(exp.column(c, "t")), exp.column(c, "s").not_().is_(exp.Null()), exp.column(c, "t").not_().is_(exp.Null())) for c in list(set([a.name for a in s_index]))),), 1, 0).sql()
73+
74+
# #-------------
75+
76+
# ################
77+
# # MS SQL
78+
# ################
79+
80+
# import pymssql
81+
# import pandas as pd
82+
83+
# conn = pymssql.connect("localhost", "SA", "1StrongPwd@@", as_dict=True)
84+
85+
# cur = conn.cursor()
86+
87+
# # tables
88+
# cur.execute("select * from information_schema.tables where table_schema like '%sqlmesh%'")
89+
# tables = cur.fetchall()
90+
# to_delete = [f"{tbl['TABLE_SCHEMA']}.{tbl['TABLE_NAME']}" for tbl in tables]
91+
92+
# for tbl in to_delete:
93+
# cur.execute(f"drop table {tbl};")
94+
95+
# # views
96+
# cur.execute("select * from information_schema.views where table_schema like '%sqlmesh%'")
97+
# tables = cur.fetchall()
98+
# to_delete = [f"{tbl['TABLE_SCHEMA']}.{tbl['TABLE_NAME']}" for tbl in tables]
99+
100+
# for tbl in to_delete:
101+
# cur.execute(f"drop view {tbl};")
102+
103+
# # schemas
104+
# cur.execute("select * from information_schema.schemata where schema_name like '%sqlmesh%'")
105+
# schemas = cur.fetchall()
106+
# to_delete = [schema['SCHEMA_NAME'] for schema in schemas]
107+
108+
# for schema in to_delete:
109+
# cur.execute(f"drop schema {schema};")
110+
111+
# cur.execute("select * from information_schema.schemata where schema_name like '%sqlmesh'")
112+
# pd.DataFrame(cur.fetchall())
113+
114+
# cur.execute("select * from information_schema.tables where table_schema like '%sqlmesh%'")
115+
# pd.DataFrame(cur.fetchall())
116+
117+
# cur.close()
118+
# conn.close()
119+
# exit()
120+
121+
# #######################
122+
# # METRICS
123+
# #######################
124+
125+
# from sqlmesh.core import Context
126+
127+
# context = Context(paths="/users/trey/tobiko/sqlmesh/examples/sushi")
128+
129+
# query = context.rewrite(
130+
# """
131+
# SELECT
132+
# ds,
133+
# METRIC(total_orders_from_active_customers),
134+
# FROM
135+
# __semantic.__table -- special table for simple metric queries
136+
# GROUP BY
137+
# ds
138+
# """,
139+
# )
140+
141+
# from time import sleep
142+
# from rich.console import Console
143+
# from rich.live import Live
144+
# from rich.table import Table
145+
# from rich.progress import (
146+
# Progress,
147+
# TextColumn,
148+
# BarColumn,
149+
# SpinnerColumn,
150+
# TimeElapsedColumn,
151+
# )
152+
153+
# PROGRESS_BAR_WIDTH = 40
154+
155+
# def make_overall_progress(console: Console) -> Progress:
156+
# return Progress(
157+
# TextColumn("[bold blue]Overall:", justify="right"),
158+
# BarColumn(bar_width=PROGRESS_BAR_WIDTH),
159+
# "[progress.percentage]{task.percentage:>3.1f}%",
160+
# TimeElapsedColumn(),
161+
# console=console,
162+
# transient=False,
163+
# )
164+
165+
# def make_model_progress(console: Console) -> Progress:
166+
# return Progress(
167+
# TextColumn("{task.fields[model_name]}", justify="right"),
168+
# SpinnerColumn(spinner_name="dots"),
169+
# console=console,
170+
# transient=False,
171+
# )
172+
173+
# def demo_table_stateless(models: dict[str, int], delay: float = 0.5):
174+
# console = Console()
175+
# total_batches = sum(models.values())
176+
# completed_total = 0
177+
# completed_msgs: list[str] = [] # store completed model messages
178+
179+
# with Live(console=console, refresh_per_second=10) as live:
180+
# for model_name, batch_count in models.items():
181+
# for batch_index in range(1, batch_count + 1):
182+
# # compute new totals
183+
# completed_total += 1
184+
185+
# # recreate Progress widgets with absolute values
186+
# overall = make_overall_progress(console)
187+
# overall.add_task("", total=total_batches, completed=completed_total)
188+
189+
# model = make_model_progress(console)
190+
# task_id = model.add_task(
191+
# "", total=batch_count, completed=batch_index, model_name=model_name
192+
# )
193+
# # remove the per‑model bar once it's done
194+
# if batch_index >= batch_count:
195+
# model.remove_task(task_id)
196+
# completed_msgs.append(model_name)
197+
198+
# # assemble grid and re‑render
199+
# grid = Table.grid()
200+
# for msg in completed_msgs:
201+
# grid.add_row(msg)
202+
# grid.add_row(overall)
203+
# grid.add_row(model)
204+
# live.update(grid)
205+
206+
# sleep(delay)
207+
208+
# if __name__ == "__main__":
209+
# # simulate three “models” with 5, 3, and 4 batches
210+
# demo_table_stateless({"Model A": 5, "Model B": 3, "Model C": 4})

tests/integrations/jupyter/test_magics.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,6 @@ def test_plan(
289289
with capture_output() as output:
290290
notebook.run_line_magic(magic_name="plan", line="--no-prompts --auto-apply")
291291

292-
# TODO: Should this be going to stdout? This is printing the status updates for when each batch finishes for
293-
# the models and how long it took
294-
assert len(output.stdout.strip().split("\n")) == 46
295292
assert not output.stderr
296293
assert len(output.outputs) == 4
297294
text_output = convert_all_html_output_to_text(output)
@@ -324,8 +321,15 @@ def test_run_dag(
324321
"'Evaluating models ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 18/18"
325322
)
326323
assert not output.stderr
327-
assert len(output.outputs) == 6
324+
assert len(output.outputs) == 10
328325
assert convert_all_html_output_to_text(output) == [
326+
"[2K",
327+
"Executing model batches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━ 93.8% • "
328+
"15/16 • 0:00:01\n"
329+
"sushi.waiter_as_customer_by_day",
330+
"[1/1] sushi.waiter_as_customer_by_day [insert 2023-01-01 - 2023-01-02, "
331+
"audits ✔2] 0.15s",
332+
"",
329333
"✔ Model batches executed",
330334
"Run finished for environment 'prod'",
331335
]

0 commit comments

Comments
 (0)