Foxray: a light error tracker for the Sentry SDKs. Runs inside Django (the admin is the UI) or on its own; SQLite or Postgres, nothing else. Not affiliated with Sentry.
danq9
0.1.2: the first PyPI release (v0.1.1 is already tagged at 05cc354, before the PyPI metadata)
58507e3
4 weeks ago
History
conftest.py · 2.7 KB · 86 lines
| 1 | import os |
| 2 | import uuid |
| 3 | from urllib.parse import urlsplit, urlunsplit |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from foxray.core.sql import TABLES, PostgresBackend, SqliteBackend, SqlStore, init_schema |
| 8 | |
| 9 | PG_URL = os.environ.get("FOXRAY_TEST_PG_URL", "") |
| 10 | |
| 11 | |
| 12 | @pytest.fixture |
| 13 | def sqlite_store(tmp_path): |
| 14 | backend = SqliteBackend(str(tmp_path / "foxray.db")) |
| 15 | init_schema(backend) |
| 16 | yield SqlStore(backend) |
| 17 | backend.close() |
| 18 | |
| 19 | |
| 20 | def _with_db(url: str, name: str) -> str: |
| 21 | p = urlsplit(url) |
| 22 | return urlunsplit((p.scheme, p.netloc, "/" + name, p.query, p.fragment)) |
| 23 | |
| 24 | |
| 25 | @pytest.fixture(scope="session") |
| 26 | def pg_url(): |
| 27 | """A throwaway database, created and dropped by the test run.""" |
| 28 | if not PG_URL: |
| 29 | pytest.skip("set FOXRAY_TEST_PG_URL to run the Postgres tests") |
| 30 | import psycopg |
| 31 | name = "foxray_test_" + uuid.uuid4().hex[:10] |
| 32 | with psycopg.connect(PG_URL, autocommit=True) as conn: |
| 33 | conn.execute(f"CREATE DATABASE {name}") |
| 34 | try: |
| 35 | yield _with_db(PG_URL, name) |
| 36 | finally: |
| 37 | with psycopg.connect(PG_URL, autocommit=True) as conn: |
| 38 | conn.execute(f"DROP DATABASE IF EXISTS {name} WITH (FORCE)") |
| 39 | |
| 40 | |
| 41 | @pytest.fixture |
| 42 | def pg_fresh_url(): |
| 43 | """A brand-new database for one test (dropped afterwards).""" |
| 44 | if not PG_URL: |
| 45 | pytest.skip("set FOXRAY_TEST_PG_URL to run the Postgres tests") |
| 46 | import psycopg |
| 47 | name = "foxray_test_" + uuid.uuid4().hex[:10] |
| 48 | with psycopg.connect(PG_URL, autocommit=True) as conn: |
| 49 | conn.execute(f"CREATE DATABASE {name}") |
| 50 | try: |
| 51 | yield _with_db(PG_URL, name) |
| 52 | finally: |
| 53 | with psycopg.connect(PG_URL, autocommit=True) as conn: |
| 54 | conn.execute(f"DROP DATABASE IF EXISTS {name} WITH (FORCE)") |
| 55 | |
| 56 | |
| 57 | @pytest.fixture |
| 58 | def pg_store(pg_url): |
| 59 | backend = PostgresBackend(pg_url) |
| 60 | init_schema(backend) |
| 61 | with backend.tx() as c: |
| 62 | c.execute("TRUNCATE " + ", ".join(TABLES) + ", foxray_user RESTART IDENTITY", ()) |
| 63 | yield SqlStore(backend) |
| 64 | backend.close() |
| 65 | |
| 66 | |
| 67 | @pytest.fixture |
| 68 | def django_store(transactional_db): |
| 69 | from foxray.django.store import get_store |
| 70 | return get_store() |
| 71 | |
| 72 | |
| 73 | @pytest.fixture(params=["sqlite", "postgres", "django"]) |
| 74 | def store(request): |
| 75 | """The storage contract runs against every store.""" |
| 76 | return request.getfixturevalue({"sqlite": "sqlite_store", "postgres": "pg_store", |
| 77 | "django": "django_store"}[request.param]) |
| 78 | |
| 79 | |
| 80 | def pytest_collection_modifyitems(items): |
| 81 | # pytest-django creates only the test databases that collected tests ask |
| 82 | # for, so mark the Django runs of the contract up front. |
| 83 | for item in items: |
| 84 | callspec = getattr(item, "callspec", None) |
| 85 | if callspec is not None and callspec.params.get("store") == "django": |
| 86 | item.add_marker(pytest.mark.django_db(transaction=True)) |