64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""A fresh platform per test, on SQLite and on PostgreSQL, and the way an operator runs it:
|
|
save a pipeline, run the `ingest` job (each run in its own process, as Dagster does)."""
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine, make_url, text
|
|
|
|
|
|
def run(env, *args):
|
|
subprocess.run(args, env=env, check=True)
|
|
|
|
|
|
def ingest(env, pipeline="site"):
|
|
config = Path(env["ISSSTELEON_DATA"]) / "run.json"
|
|
config.write_text(json.dumps({"ops": {"run_pipeline": {"config": {"pipeline": pipeline}}}}))
|
|
run(env, "dagster", "job", "execute", "-m", "models.definitions", "-j", "ingest",
|
|
"--config", str(config)) # fmt: skip
|
|
|
|
|
|
@pytest.fixture(params=["sqlite", "postgres"])
|
|
def platform(request, tmp_path):
|
|
"""Env for a fresh platform on each backend, and a SQL connection to read it back."""
|
|
(tmp_path / "dagster").mkdir()
|
|
env = {
|
|
**os.environ,
|
|
"ISSSTELEON_DATA": str(tmp_path),
|
|
"DLT_DATA_DIR": str(tmp_path / "dlt"),
|
|
"DAGSTER_HOME": str(tmp_path / "dagster"),
|
|
}
|
|
if request.param == "sqlite":
|
|
url = env["PLATFORM_DATABASE_URL"] = f"sqlite:///{tmp_path / 'platform.db'}"
|
|
attach = [f"ATTACH '{tmp_path}/platform__{s}.db' AS {s}" for s in ("bronze", "silver")]
|
|
else:
|
|
pg = os.environ["POSTGRES_TEST_URL"] # required: run with `docker compose run --rm test`
|
|
env.update(PLATFORM_DATABASE_URL=pg, DBT_TARGET="postgres")
|
|
url = make_url(pg).set(drivername="postgresql+psycopg2")
|
|
attach = []
|
|
with create_engine(url).begin() as con:
|
|
for schema in ("bronze", "silver", "config"):
|
|
con.exec_driver_sql(f"drop schema if exists {schema} cascade")
|
|
|
|
def count(table: str) -> int:
|
|
with create_engine(url).connect() as con:
|
|
for statement in attach:
|
|
con.exec_driver_sql(statement)
|
|
return con.execute(text(f"select count(*) from {table}")).scalar()
|
|
|
|
def query(sql: str):
|
|
with create_engine(url).connect() as con:
|
|
for statement in attach:
|
|
con.exec_driver_sql(statement)
|
|
return con.execute(text(sql)).fetchall()
|
|
|
|
return SimpleNamespace(
|
|
env=env, count=count, query=query,
|
|
run=lambda *args: run(env, *args),
|
|
ingest=lambda pipeline="site": ingest(env, pipeline),
|
|
) # fmt: skip
|