Repository navigation
Expand file tree
/
Copy pathtest_catalog_runtime.py
More file actions
126 lines (110 loc) · 6.2 KB
/
Copy pathtest_catalog_runtime.py
File metadata and controls
126 lines (110 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Opt-in schema/catalog checks run without provider network access."""
import json
import os
import subprocess
import sys
from pathlib import Path
import pytest
from qopenbb_ci.catalog import canonical, generate
from qopenbb_ci.catalog_runtime import (
capture_schema,
docker,
image_identity,
upstream_widgets,
verified_sources,
)
ROOT = Path(__file__).resolve().parents[2]
IMAGE = os.environ.get("QOPENBB_DATA_IMAGE")
def test_container_output_preserves_utf8_description(monkeypatch):
description = "Volume in millions of euro (€)."
payload = json.dumps({"description": description}, ensure_ascii=False).encode("utf-8")
command = [sys.executable, "-c", f"import sys; sys.stdout.buffer.write({payload!r})"]
monkeypatch.setattr("qopenbb_ci.catalog_runtime.container_command", lambda args: command)
assert json.loads(docker("image", "inspect", "fixture"))["description"] == description
@pytest.mark.parametrize("prefix", ["sha256:", ""])
def test_image_identity_accepts_exact_engine_id(monkeypatch, prefix):
digest = "a" * 64
identity = {"Id": prefix + digest, "Config": {"Labels": {"revision": "accepted"}}}
calls = []
def inspect(*args):
calls.append(args)
return json.dumps([identity])
monkeypatch.setattr("qopenbb_ci.catalog_runtime.docker", inspect)
assert image_identity("sha256:" + digest) == identity
assert calls == [("image", "inspect", "sha256:" + digest)]
@pytest.mark.parametrize("reported", [
"sha256:" + "b" * 64, "b" * 64, "sha512:" + "a" * 64,
"a" * 63, "a" * 65, "A" * 64, "sha256:sha256:" + "a" * 64,
"a" * 64 + "\n", "latest", None, [], {}, 123,
])
def test_image_identity_rejects_different_or_malformed_engine_id(monkeypatch, reported):
monkeypatch.setattr("qopenbb_ci.catalog_runtime.docker", lambda *args: json.dumps([{"Id": reported}]))
with pytest.raises(ValueError, match="image ID mismatch"):
image_identity("sha256:" + "a" * 64)
@pytest.mark.parametrize("image", ["latest", "a" * 64, "sha256:" + "a" * 63])
def test_image_identity_requires_exact_external_id_before_inspection(monkeypatch, image):
def unexpected_inspection(*args):
pytest.fail("invalid external image must fail before inspection")
monkeypatch.setattr("qopenbb_ci.catalog_runtime.docker", unexpected_inspection)
with pytest.raises(ValueError, match="exact local image ID"):
image_identity(image)
def test_modified_generator_is_rejected_before_execution(tmp_path):
with pytest.raises(ValueError, match="source"):
verified_sources(tmp_path, "0" * 40)
def test_changed_source_blob_fails_closed(tmp_path):
from qopenbb_ci.catalog_runtime import ASSET, UTILS
subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
for relative in [f"{UTILS}/widgets.py", f"{UTILS}/openapi.py", ASSET]:
target = tmp_path / relative
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text("accepted source\n")
subprocess.run(["git", "-C", str(tmp_path), "add", UTILS, ASSET], check=True)
subprocess.run(["git", "-C", str(tmp_path), "-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid",
"commit", "-qm", "accepted fixture"], check=True)
(tmp_path / UTILS / "widgets.py").write_text("modified source\n")
with pytest.raises(ValueError, match="differs"):
verified_sources(tmp_path, "HEAD")
@pytest.mark.skipif(not IMAGE, reason="requires exact accepted loaded data image")
def test_served_schema_and_upstream_generator_repeat(tmp_path):
revision = json.loads((ROOT / "sources.lock.json").read_text())["data_api"]["revision"]
source = ROOT / "vendor/data-api"
provenance = verified_sources(source, revision)
schema, identity = capture_schema(IMAGE)
inventory = json.loads((ROOT / "contracts/datasets.json").read_text())
raw = upstream_widgets(IMAGE, source, schema, inventory, tmp_path)
bundled = json.loads((source / "openbb_platform/providers/fred/openbb_fred/assets/apps.json").read_text())
first = generate(schema, inventory, raw, bundled, "/data")
second_raw = upstream_widgets(IMAGE, source, schema, inventory, tmp_path)
assert canonical(first) == canonical(generate(schema, inventory, second_raw, bundled, "/data"))
endpoint_widgets = {key: widget for key, widget in first["widgets.json"].items()
if "endpoint" in widget}
assert len(endpoint_widgets) == len(inventory["datasets"])
assert {widget["endpoint"] for widget in endpoint_widgets.values()} == {
dataset["path"] for dataset in inventory["datasets"]}
native_widgets = {key: widget for key, widget in first["widgets.json"].items()
if key not in endpoint_widgets}
assert set(native_widgets) == {"company_research"}
assert native_widgets["company_research"] == {
"widgetId": "company_research", "type": "company_research", "name": "Peer Research",
"description": "Add candidate peers and inspect provider observations. Saved choices are not frozen data.",
"category": "Equity", "gridData": {"w": 40, "h": 20}, "params": [],
}
assert {"equity_profile_fmp_obb", "equity_fundamental_income_fmp_obb",
"economy_fred_series_fred_obb"} <= set(first["widgets.json"])
assert [app["name"] for app in first["apps.json"]] == ["QOpenBB starter", "FRED", "Company Research"]
research = first["apps.json"][-1]
assert list(research["tabs"]) == ["Research"]
assert [item["i"] for item in research["tabs"]["Research"]["layout"]] == [
"equity_profile_fmp_obb", "equity_fundamental_income_fmp_obb",
"equity_fundamental_balance_fmp_obb", "equity_fundamental_cash_fmp_obb",
"equity_price_historical_fmp_obb", "company_research",
]
assert set(identity["Config"]["Labels"].values()) >= {revision}
assert provenance["revision"] == revision
evidence = os.environ.get("QOPENBB_CATALOG_EVIDENCE")
if evidence:
destination = Path(evidence)
destination.mkdir(parents=True, exist_ok=True)
for name, value in {**first, "source-provenance.json": provenance,
"image-identity.json": identity, "upstream-widgets.json": raw}.items():
(destination / name).write_bytes(canonical(value))