App factory, data model, initial migration and HTTP metrics
- Flask app factory with env-driven config (.env loaded at import) - SQLAlchemy models: users, API apps, UI tokens, Last.fm auth tokens, sessions, scrobbles (deduplicated) and now-playing - Initial Alembic migration; CI checks migrations match the models - Prometheus metric definitions, per-endpoint RED hooks with bounded labels, and a standalone metrics server - docker compose Postgres with a test database; CI test job runs against a Postgres service container Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
606348800b
commit
98c8f2e824
21 files changed
+1777
-6
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import pytest
|
||||
from flask_migrate import downgrade, upgrade
|
||||
from prometheus_client import REGISTRY
|
||||
from sqlalchemy import text
|
||||
|
||||
from scrobbler import create_app
|
||||
from scrobbler.config import TestConfig
|
||||
from scrobbler.extensions import db
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app():
|
||||
app = create_app(TestConfig)
|
||||
with app.app_context():
|
||||
downgrade(revision="base") # clean slate if a previous run was interrupted
|
||||
upgrade()
|
||||
yield app
|
||||
db.session.remove()
|
||||
downgrade(revision="base")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean_tables(app):
|
||||
yield
|
||||
db.session.rollback()
|
||||
tables = ", ".join(t.name for t in db.metadata.sorted_tables)
|
||||
db.session.execute(text(f"TRUNCATE {tables} RESTART IDENTITY CASCADE"))
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
class MetricDelta:
|
||||
"""Reads a metric sample now and later, so tests can assert on the increase."""
|
||||
|
||||
def __init__(self, name: str, **labels: str):
|
||||
self.name, self.labels = name, labels
|
||||
self.start = self._value()
|
||||
|
||||
def _value(self) -> float:
|
||||
return REGISTRY.get_sample_value(self.name, self.labels) or 0.0
|
||||
|
||||
@property
|
||||
def delta(self) -> float:
|
||||
return self._value() - self.start
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def metric_delta():
|
||||
return MetricDelta
|
||||
@@ -0,0 +1,33 @@
|
||||
from sqlalchemy import inspect
|
||||
|
||||
from scrobbler import __version__
|
||||
from scrobbler.extensions import db
|
||||
|
||||
|
||||
def test_migrations_create_all_tables(app):
|
||||
tables = set(inspect(db.engine).get_table_names())
|
||||
assert {
|
||||
"users",
|
||||
"api_apps",
|
||||
"ui_tokens",
|
||||
"auth_tokens",
|
||||
"sessions",
|
||||
"scrobbles",
|
||||
"now_playing",
|
||||
} <= tables
|
||||
|
||||
|
||||
def test_unmatched_requests_use_a_fixed_endpoint_label(client, metric_delta):
|
||||
requests = metric_delta(
|
||||
"scrobbler_http_requests_total", endpoint="unmatched", http_method="GET", status="404"
|
||||
)
|
||||
client.get("/no/such/path/123")
|
||||
client.get("/another/random/path")
|
||||
assert requests.delta == 2
|
||||
|
||||
|
||||
def test_build_info_is_exported(metric_delta):
|
||||
from prometheus_client import REGISTRY
|
||||
|
||||
samples = [s for m in REGISTRY.collect() if m.name == "scrobbler_build_info" for s in m.samples]
|
||||
assert samples and samples[0].labels["version"] == __version__
|
||||
@@ -1,5 +0,0 @@
|
||||
import scrobbler
|
||||
|
||||
|
||||
def test_version_is_set():
|
||||
assert scrobbler.__version__
|
||||
Reference in new issue
Block a user