60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
|
|
from ploop.cli import _parser, main
|
|
from ploop.infra.db.models import Base
|
|
|
|
FIXTURES_ROOT = Path(__file__).resolve().parents[1] / "fixtures" / "imports"
|
|
|
|
|
|
def test_import_dataset_cli_accepts_only_an_operator_manifest_path() -> None:
|
|
arguments = _parser().parse_args(
|
|
["import-dataset", "--manifest", "operator-staging/demo/manifest.json"]
|
|
)
|
|
|
|
assert arguments.command == "import-dataset"
|
|
assert arguments.manifest == Path("operator-staging/demo/manifest.json")
|
|
assert not hasattr(arguments, "manifest_json")
|
|
assert not hasattr(arguments, "target_command")
|
|
|
|
|
|
def test_import_dataset_cli_rejects_inline_manifest_json() -> None:
|
|
with pytest.raises(SystemExit):
|
|
_parser().parse_args(["import-dataset", "--manifest-json", "{}"])
|
|
|
|
|
|
def test_import_dataset_cli_uses_explicit_operator_environment(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
capsys: pytest.CaptureFixture[str],
|
|
) -> None:
|
|
staging_root = tmp_path / "operator-staging"
|
|
import_root = staging_root / "demo"
|
|
shutil.copytree(FIXTURES_ROOT / "demo", import_root)
|
|
database_path = tmp_path / "ploop.db"
|
|
engine = create_engine(f"sqlite+pysqlite:///{database_path}")
|
|
Base.metadata.create_all(engine)
|
|
engine.dispose()
|
|
artifact_root = tmp_path / "artifacts"
|
|
monkeypatch.setenv("PLOOP_DATABASE_URL", f"sqlite+pysqlite:///{database_path}")
|
|
monkeypatch.setenv("PLOOP_ARTIFACT_ROOT", str(artifact_root))
|
|
monkeypatch.setenv("PLOOP_OPERATOR_STAGING_ROOT", str(staging_root))
|
|
monkeypatch.setattr(
|
|
sys,
|
|
"argv",
|
|
["ploop", "import-dataset", "--manifest", str(import_root / "manifest.json")],
|
|
)
|
|
|
|
main()
|
|
|
|
payload = json.loads(capsys.readouterr().out)
|
|
assert payload["dataset_version_id"].startswith("dataset-")
|
|
assert str(artifact_root) not in json.dumps(payload)
|