Files
AareDAQ/tests/conftest.py
T
appleb_m 947e142f6e
Build and Publish / test (push) Successful in 1m26s
Build and Publish / build (push) Successful in 15s
Build and Publish / Build and Deploy Docs (push) Successful in 38s
DAQ/GUI updated: gui session handling in daq and gui so staff can now close erroneous GUIs and see who has the baton. Added tests
2026-04-30 14:03:10 +02:00

86 lines
2.1 KiB
Python

import os
import sys
from unittest.mock import MagicMock, patch
import numpy as np
import pytest
from PySide6.QtWidgets import QApplication
from fastapi.testclient import TestClient
from aare.common.models import SampleShortInfo, DewarAddress
# Ensure safe defaults during tests
os.environ.setdefault("BEAMLINE", "SIMULATED")
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
os.environ.setdefault("JWT_AAREDAQ_KEY", "test_key_for_unit_testing")
# -------------------------
# Qt app fixture
# -------------------------
@pytest.fixture(scope="session")
def qapp():
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
return app
@pytest.fixture
def sample_info():
return SampleShortInfo(
db_id=1,
puck_name="puck1",
dewar_name="dew1",
sample_name="sample1",
run_number=1,
user="group1",
pin=3,
location=DewarAddress(segment="A", pos=2),
)
# -------------------------
# Shared DAQ server fixtures
# -------------------------
@pytest.fixture
def mock_backend():
import aare.daq.server as server
with patch("aare.daq.server.daq") as m_daq, \
patch("aare.daq.server.bl") as m_bl, \
patch("aare.daq.server.cfg") as m_cfg:
server.daq = m_daq
server.bl = m_bl
server.cfg = m_cfg
m_daq.busy = False
m_cfg.pgroup = "p12345"
m_cfg.session_state.return_value = "ACTIVE"
yield {
"daq": m_daq,
"bl": m_bl,
"cfg": m_cfg,
}
@pytest.fixture
def client(mock_backend):
import aare.daq.server as server
with patch("aare.daq.auth.parse_token") as mock_parse:
from aare.daq.auth import TokenData
mock_parse.return_value = TokenData(
sub="testuser",
staff=True,
pgroups=["p12345"],
session=123,
)
with patch("cv2.imencode") as mock_imencode:
mock_imencode.return_value = (True, np.array([1, 2, 3], dtype=np.uint8))
with TestClient(server.app) as c:
yield c