Skip to content

Commit 0216ba7

Browse files
committed
Parametrize database *configurations*, put connection params in env to parametrize api tests
1 parent c17addf commit 0216ba7

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

citatio/tests/conftest.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
from os import environ
33
from pathlib import Path
44

5+
import asyncpg
56
import numpy as np
67
import pytest
8+
from confidence import Configuration
79
from testcontainers.postgres import PostgresContainer
810

911
from citatio.db import PostgreSQLDatabase, SQLiteDatabase
@@ -81,16 +83,50 @@ def connect_pgvector(request, monkeypatch):
8183
pytest.param('postgresql', marks=pytest.mark.postgresql),
8284
]
8385
)
84-
async def database(request):
86+
async def database_config(request):
8587
match request.param:
8688
case 'sqlite':
87-
# create an in-memory database that will be empty after use by design
88-
async with await SQLiteDatabase.connect(':memory:') as db:
89-
yield db
89+
# use a in-memory sqlite database
90+
yield Configuration({'database.sqlite': ':memory:'})
9091
case 'postgresql':
91-
# request pgvector connection settings from a named fixture
92-
# (a pytest.param cannot use pytest.mark.usefixtures, it seems)
92+
# run a session-scope ephemeral database
9393
connect = request.getfixturevalue('connect_pgvector')
94+
yield Configuration({'database': connect})
95+
96+
97+
@pytest.fixture
98+
async def database_env(monkeypatch, database_config):
99+
match database_config:
100+
case {'database.sqlite': ':memory:'}:
101+
# NB: add quotes to avoid the configuration's format misparsing :memory:
102+
monkeypatch.setenv('CITATIO_DATABASE_SQLITE', '":memory:"')
103+
yield
104+
case {'database.sqlite': fname}:
105+
monkeypatch.setenv('CITATIO_DATABASE_SQLITE', fname)
106+
yield
107+
case {'database': connect}:
108+
for var, value in connect.items():
109+
monkeypatch.setenv(f'CITATIO_DATABASE_{var}'.upper(), str(value))
110+
yield
111+
# empty the database after use
112+
connection = await asyncpg.connect(**connect)
113+
await connection.execute("""
114+
DELETE FROM labels;
115+
DELETE FROM functions;
116+
""")
117+
await connection.close()
118+
case _:
119+
raise ValueError
120+
121+
122+
@pytest.fixture
123+
async def database(database_config):
124+
match database_config:
125+
case {'database.sqlite': name}:
126+
# create an in-memory database that will be empty after use by design
127+
async with await SQLiteDatabase.connect(name) as db:
128+
yield db
129+
case {'database': connect}:
94130
async with await PostgreSQLDatabase.connect(**connect) as db:
95131
yield db
96132
# empty the database after use

citatio/tests/test_api.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import os
22

3-
import asyncpg
43
import pytest
54
from fastapi.testclient import TestClient
65

76
from citatio.api import app
87

98

10-
pytestmark = pytest.mark.postgresql
11-
12-
139
@pytest.fixture
14-
async def client(connect_pgvector):
10+
async def client(database_env):
1511
with TestClient(app) as client:
1612
yield client
1713

18-
# after the test (post-yield), make sure to empty the tables that we might have inserted into during the test
19-
connection = await asyncpg.connect(**connect_pgvector)
20-
await connection.execute("""
21-
DELETE FROM labels;
22-
DELETE FROM functions;
23-
""")
24-
2514

2615
@pytest.mark.skipif(os.environ.get('CI') == 'true', reason="don't run this test on CI")
2716
def test_add_function(client, functions):

0 commit comments

Comments
 (0)