61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import io
|
|
import os
|
|
import threading
|
|
import time
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
from werkzeug.serving import make_server
|
|
|
|
from app import create_app
|
|
from app.config import Config
|
|
from photoflow.core import Store
|
|
from photoflow.core.models import Asset, Cluster, ClusterMember
|
|
|
|
|
|
def _jpeg(color):
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (48, 48), color).save(buf, format="JPEG")
|
|
return buf.getvalue()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def data_dir(tmp_path_factory):
|
|
d = str(tmp_path_factory.mktemp("data"))
|
|
thumbs = os.path.join(d, "thumbs")
|
|
os.makedirs(thumbs, exist_ok=True)
|
|
s = Store(os.path.join(d, "trip-cluster.db")).connect()
|
|
palette = {"a": (10, 20, 30), "b": (40, 80, 120), "c": (200, 50, 90)}
|
|
for i, t in [("a", "2019-06-01"), ("b", "2019-06-02"), ("c", "2019-07-10")]:
|
|
s.upsert_asset(Asset(immich_id=i, taken_at=t))
|
|
with open(os.path.join(thumbs, f"{i}.jpg"), "wb") as f:
|
|
f.write(_jpeg(palette[i]))
|
|
s.insert_cluster(
|
|
Cluster(start_at="2019-06-01", end_at="2019-06-02", suggested_name="Venice",
|
|
confidence=0.9, kind_guess="trip", status="pending"),
|
|
[ClusterMember(cluster_id=0, immich_id="a"),
|
|
ClusterMember(cluster_id=0, immich_id="b")])
|
|
s.insert_cluster(
|
|
Cluster(start_at="2019-07-10", end_at="2019-07-10", suggested_name="Rome",
|
|
confidence=0.3, kind_guess="trip", status="pending"),
|
|
[ClusterMember(cluster_id=0, immich_id="c")])
|
|
s.close()
|
|
return d
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def flask_app(data_dir):
|
|
cfg = Config(immich_url="http://127.0.0.1:1", immich_api_key="k",
|
|
anthropic_api_key="", data_dir=data_dir)
|
|
return create_app(cfg)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def base_url(flask_app):
|
|
server = make_server("127.0.0.1", 8095, flask_app)
|
|
t = threading.Thread(target=server.serve_forever, daemon=True)
|
|
t.start()
|
|
time.sleep(0.2)
|
|
yield "http://127.0.0.1:8095"
|
|
server.shutdown()
|