otree-server / tests /test_session_matrix_and_rooms.py
BPEL Bot
Initial Hugging Face deployment
e40dce0
import os
import subprocess
import time
from pathlib import Path
import pytest
PROJECT_DIR = Path(__file__).resolve().parents[1]
OTREE_BIN = PROJECT_DIR.parents[1] / 'venv' / 'bin' / 'otree'
def run_otree_test_with_n(session: str, n: int, expect_ok: bool = True):
env = os.environ.copy()
db_name = f"tmp_{session}_{n}_{int(time.time()*1000)}.sqlite3"
env['OTREE_DATABASE_URL'] = f"sqlite:///{db_name}"
# Remove default project DB to avoid accidental reuse
default_db = PROJECT_DIR / 'db.sqlite3'
if default_db.exists():
default_db.unlink()
proc = subprocess.run(
[str(OTREE_BIN), 'test', session, str(n)],
cwd=str(PROJECT_DIR),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
env=env,
)
if expect_ok and proc.returncode != 0:
print(proc.stdout)
if not expect_ok and proc.returncode == 0:
print(proc.stdout)
if expect_ok:
assert proc.returncode == 0, f"Expected success for {session} with N={n}"
else:
assert proc.returncode != 0, f"Expected failure for {session} with N={n}"
return proc
@pytest.mark.parametrize(
'session,n',
[
('classic_baseline', 6),
('classic_baseline', 12),
('policy_nudges', 6),
('policy_nudges', 12),
('guessing_game_demo', 3),
('guessing_game_demo', 6),
('guessing_game_demo', 9),
('survey_biases_full', 1),
('survey_biases_full', 3),
],
)
def test_session_matrix_valid_groupings(session, n):
run_otree_test_with_n(session, n, expect_ok=True)
@pytest.mark.parametrize(
'session,n',
[
# LCM of (2,2,3) = 6, so non-multiples should fail to complete
('classic_baseline', 5),
('policy_nudges', 4),
],
)
def test_session_matrix_invalid_groupings_fail(session, n):
proc = run_otree_test_with_n(session, n, expect_ok=False)
# Heuristic check: output should mention failure/timeout/wait
out = proc.stdout.lower()
assert ('failed' in out) or ('error' in out) or ('timeout' in out) or ('traceback' in out)
def test_rooms_and_labels_exist():
# settings.py declares ROOMS with lab_demo; ensure label file exists and is non-empty
labels = PROJECT_DIR / '_rooms' / 'lab_demo.txt'
assert labels.exists(), 'Expected _rooms/lab_demo.txt to exist'
lines = [ln.strip() for ln in labels.read_text(encoding='utf-8').splitlines() if ln.strip()]
assert len(lines) >= 2, 'Expected at least 2 participant labels in lab_demo.txt'
def test_static_assets_present():
css = PROJECT_DIR / '_static' / 'custom.css'
assert css.exists() and css.stat().st_size > 0, 'custom.css is missing or empty'