File size: 2,303 Bytes
55ce00a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8427cff
55ce00a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21f3f57
55ce00a
 
21f3f57
55ce00a
 
21f3f57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55ce00a
 
 
 
 
 
 
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
"""Run against a built bundle: SQUADDS_ML_API_ROOT (default: the folder above tests/). Each session uses a fresh
Hugging Face cache, so the tests can see which weights were downloaded."""
import os
import sys
import tempfile
import time
from pathlib import Path

import pytest
import yaml

ROOT = Path(os.environ.get("SQUADDS_ML_API_ROOT", Path(__file__).resolve().parents[1]))
sys.path[:0] = [str(ROOT), str(ROOT / "src")]
TOKEN = None
try:
    from huggingface_hub import get_token

    TOKEN = get_token()
except Exception:  # noqa: BLE001
    pass
os.environ["HF_HUB_CACHE"] = tempfile.mkdtemp(prefix="squadds_ml_api_hub_")
os.environ.setdefault("KERAS_BACKEND", "torch")
os.environ.setdefault("SQUADDS_ML_API_RATE_PER_MIN", "0")
GDS_DIR = Path(os.environ.get("SQUADDS_ML_API_TEST_GDS",
                              "/Users/shanto/LFL/tl_squadds/deploy/build_flagship_docker/PlanarCapacitanceModel/examples"))
CAP = ("qcap", "qcap-fast")


def _client(registry_file=None):
    from fastapi.testclient import TestClient

    from squadds_ml_api.api import create_app

    app = create_app(ROOT, registry_file)
    c = TestClient(app)
    c.__enter__()
    reg = app.state.registry
    t = time.time()
    while any(m.state == "loading" for m in reg.models.values()) and time.time() - t < 600:
        time.sleep(0.2)
    return c


@pytest.fixture(scope="session")
def client():
    c = _client()
    c.hub_listing = sorted(os.listdir(os.environ["HF_HUB_CACHE"]))
    yield c
    c.__exit__(None, None, None)


def _forced(tmp_path_factory, visibility):
    reg = yaml.safe_load((ROOT / "registry.yaml").read_text())
    for k in CAP:
        reg["models"][k]["visibility"] = visibility
    p = tmp_path_factory.mktemp("reg") / "registry.yaml"
    p.write_text(yaml.safe_dump(reg, sort_keys=False))
    return _client(p)


@pytest.fixture(scope="session")
def gated_client(tmp_path_factory):
    if not TOKEN:
        pytest.skip("no Hugging Face token")
    c = _forced(tmp_path_factory, "gated")
    yield c
    c.__exit__(None, None, None)


@pytest.fixture(scope="session")
def hidden_client(tmp_path_factory):
    c = _forced(tmp_path_factory, "hidden")
    yield c
    c.__exit__(None, None, None)


@pytest.fixture(scope="session")
def auth():
    return {"Authorization": f"Bearer {TOKEN}"}