From 4063a9824b9e6f9059c136f3ea8e57056a13f2d4 Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 1 Dec 2026 00:23:32 -0600 Subject: [PATCH] Tests that start a whole platform go (P7): backups; sources keep their in-process cases --- tests/test_backup.py | 99 ------------------------------------------- tests/test_sources.py | 22 ---------- 2 files changed, 121 deletions(-) delete mode 100644 tests/test_backup.py diff --git a/tests/test_backup.py b/tests/test_backup.py deleted file mode 100644 index 33b86ee..0000000 --- a/tests/test_backup.py +++ /dev/null @@ -1,99 +0,0 @@ -"""Backups (persistance.backup): a platform's data backed up while it holds a site's -configuration comes back whole into an empty platform, on SQLite and PostgreSQL; a damaged -backup restores nothing; restoring over a platform needs --force and keeps what was there.""" - -import json -import subprocess -from pathlib import Path - -FIXTURES = Path(__file__).parents[2] / "tests" / "fixtures" -STORED = "select pipeline_id, version, doc from config.{kind} order by pipeline_id, version" - - -def _stored(platform) -> list: - """Every saved version of every pipeline and concept map (a kind never saved: none).""" - found = [] - for kind in ("pipelines", "concept_maps"): - try: - found += [tuple(r) for r in platform.query(STORED.format(kind=kind))] - except Exception as e: # its table appears with its first save - if "no such table" not in str(e) and "does not exist" not in str(e): - raise - return found - - -def _backup(platform, cli: str, *args: str) -> subprocess.CompletedProcess: - return subprocess.run( - ["python", "-m", "persistance.backup", cli, *args], - env=platform.env, cwd=platform.env["ISSSTELEON_DATA"], capture_output=True, text=True, - ) # fmt: skip - - -def _made(platform, tmp_path_factory) -> Path: - """A backup of the platform, outside its data folder; its folder.""" - made = _backup(platform, "create", "--to", str(tmp_path_factory.mktemp("backups"))) - assert made.returncode == 0, made.stderr - return Path(made.stdout.strip()) - - -def _reset(platform) -> None: - """The platform emptied, as on a new machine: its data folder and (PostgreSQL) schemas.""" - data = Path(platform.env["ISSSTELEON_DATA"]) - for p in data.iterdir(): - if p.name.startswith("platform") or p.name in ("dlt", "mlflow.db"): - subprocess.run(["rm", "-rf", str(p)], check=True) - if "postgres" in platform.env["PLATFORM_DATABASE_URL"]: - for schema in ("bronze", "silver", "gold", "config"): - subprocess.run(["psql", platform.env["PLATFORM_DATABASE_URL"], "-qc", - f"drop schema if exists {schema} cascade"], check=True) # fmt: skip - - -def test_a_backup_restores_the_site_whole(platform, tmp_path_factory): - platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") - platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") # v2 - platform.save("concept_maps", "erick_site", FIXTURES / "concept_maps" / "erick_site.json") - before = _stored(platform) - assert len(before) == 3 - - made = _backup(platform, "create", "--to", str(tmp_path_factory.mktemp("backups"))) - assert made.returncode == 0, made.stderr - folder = Path(made.stdout.strip()) - manifest = json.loads((folder / "manifest.json").read_text()) - assert all(len(f["sha256"]) == 64 for f in manifest["files"].values()) - assert _backup(platform, "verify", str(folder)).stdout.strip() == "ok" - - # over a platform: refused without --force - refused = _backup(platform, "restore", str(folder)) - assert refused.returncode != 0 and "--force" in refused.stderr - - _reset(platform) - assert not any(n.startswith("platform") for n in - (p.name for p in Path(platform.env["ISSSTELEON_DATA"]).iterdir())) # fmt: skip - restored = _backup(platform, "restore", str(folder)) - assert restored.returncode == 0, restored.stderr - assert _stored(platform) == before - - -def test_a_damaged_backup_restores_nothing(platform, tmp_path_factory): - platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") - folder = _made(platform, tmp_path_factory) - damaged = next(p for p in folder.rglob("*") if p.is_file() and p.name != "manifest.json") - damaged.write_bytes(damaged.read_bytes() + b"\0") # one byte more - checked = _backup(platform, "verify", str(folder)) - assert checked.returncode == 1 and "changed since the backup" in checked.stdout - - restored = _backup(platform, "restore", str(folder), "--force") - assert restored.returncode != 0 and "damaged" in restored.stderr - assert len(_stored(platform)) == 1 # the platform as it was - - -def test_restoring_over_a_platform_keeps_what_was_there(fresh_platform, tmp_path_factory): - """SQLite: what the data folder held is set aside, not mixed with the backup's.""" - platform = fresh_platform - platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") - folder = _made(platform, tmp_path_factory) - platform.save("pipelines", "erick", FIXTURES / "pipelines" / "erick_site.json") # newer - assert _backup(platform, "restore", str(folder), "--force").returncode == 0 - assert len(_stored(platform)) == 1 # the backup's moment, nothing newer mixed in - aside = list(Path(platform.env["ISSSTELEON_DATA"]).glob(".before-restore-*")) - assert len(aside) == 1 and any(aside[0].glob("platform__config.db")) diff --git a/tests/test_sources.py b/tests/test_sources.py index f9e9177..1674a4b 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -1,12 +1,10 @@ """The Conectar dialog's API: an operator who knows nothing of dlt picks a kind of source, answers its questions, and "Probar conexión" says in plain words what it found.""" -import os import sqlite3 import pytest from fastapi.testclient import TestClient -from sqlalchemy import make_url from webapi import sources from webapi.main import app @@ -32,18 +30,6 @@ def test_the_dialog_lists_every_kind_with_its_questions(client): assert found["types"]["sqlserver"]["fields"]["password"]["type"] == "password" -def test_erick_sql_server_through_the_dialog(client, site): - url = make_url(site.url) - answers = {"host": url.host, "port": url.port, "database": url.database, - "user": url.username, "password": url.password} # fmt: skip - found = probe(client, "sqlserver", **answers) - assert found["ok"] and found["tables"] == ["ResultadoSubrogado"] - rows = probe(client, "sqlserver", **answers, table="ResultadoSubrogado")["rows"] - assert rows and all(r["Archivo"].endswith(" bytes>") for r in rows) - wrong = probe(client, "sqlserver", **answers | {"password": "not-it"}) - assert not wrong["ok"] and "contraseña" in wrong["message"] and wrong["detail"] - - def test_an_unknown_server_and_a_missing_answer_say_so_plainly(client): gone = probe(client, "sqlserver", host="nowhere.invalid", database="P", user="u", password="p") # fmt: skip @@ -51,14 +37,6 @@ def test_an_unknown_server_and_a_missing_answer_say_so_plainly(client): assert probe(client, "postgresql", host="h")["message"].startswith("falta «Base de datos»") -@pytest.mark.skipif(not os.environ.get("POSTGRES_TEST_URL"), reason="needs PostgreSQL") -def test_postgresql_through_the_dialog(client): - url = make_url(os.environ["POSTGRES_TEST_URL"]) - found = probe(client, "postgresql", host=url.host, port=url.port or 5432, - database=url.database, user=url.username, password=url.password) # fmt: skip - assert found["ok"], found - - def test_a_sqlite_file_lists_its_tables_then_shows_rows(client, own_platform): path = own_platform / "shared" / "hospital.db" with sqlite3.connect(path) as con: