gitea workflows: fix pytest dependency bug
This commit is contained in:
@@ -67,29 +67,68 @@ def test_status(client, mock_backend):
|
||||
mock_daq = mock_backend["daq"]
|
||||
mock_cfg = mock_backend["cfg"]
|
||||
|
||||
from aare.common.models import DAQStatusModel, SessionStatus, SessionsStateEnum, BeamlineStateEnum, SampleGeometryModel, BeamlineStatus
|
||||
from aare.common.models import DAQStatusModel, SessionStatus, SessionsStateEnum, BeamlineStateEnum, SampleGeometryModel, BeamlineStatus, CrystalSize, SampleCameraSettings
|
||||
from aare.common.diffraction_geometry import DiffractionGeometry
|
||||
|
||||
# Bypass validation by returning a mock that satisfies the endpoint but don't use response_model validation if possible,
|
||||
# or provide a model that actually validates.
|
||||
# To provide a model that validates, we need to know the exact types.
|
||||
# Create a valid status object
|
||||
from aare.common.coordinate import Coordinate, SmargonCoordinate
|
||||
geom = SampleGeometryModel(
|
||||
beam_location_pxl=Coordinate(x=500, y=500),
|
||||
pixel_in_mm=0.001,
|
||||
aerotech=Coordinate(x=0, y=0, z=0),
|
||||
aerotech_meas=Coordinate(x=0, y=0, z=0),
|
||||
smargon=SmargonCoordinate(sh_mm=Coordinate(x=0, y=0, z=0), phi_deg=0.0, chi_deg=0.0),
|
||||
omega_deg=0.0,
|
||||
beam_size_mm=Coordinate(x=0.01, y=0.01)
|
||||
)
|
||||
diff = DiffractionGeometry(
|
||||
energy_keV=12.0,
|
||||
dtz_mm=150.0,
|
||||
pixel_size_mm=0.075,
|
||||
beam_center_pxl=(1000.0, 1000.0),
|
||||
detector_size_pxl=(2000, 2000),
|
||||
detector_description="Eiger 16M",
|
||||
detector_serial_number="123",
|
||||
poni_rot1_rad=0.0,
|
||||
poni_rot2_rad=0.0
|
||||
)
|
||||
bl_status = BeamlineStatus(
|
||||
name="X06DA",
|
||||
ring_current_mA=400.0,
|
||||
omega=0.0, front_light=0.0, back_light=0.0,
|
||||
cryojet_K=100.0, shutter_open=False, exp_shutter_open=False,
|
||||
flux_ph_s=1e12, transmission=1.0, zoom=1.0,
|
||||
sample_camera=SampleCameraSettings(exposure=0.1, gain=1.0),
|
||||
commissioning_mode=False, dtz_min=120.0, dtz_max=1600.0
|
||||
)
|
||||
session_status = SessionStatus(
|
||||
session=SessionsStateEnum.Vacant,
|
||||
current_pgroup="p12345",
|
||||
staff=True
|
||||
)
|
||||
|
||||
status_mock = MagicMock()
|
||||
# When FastAPI serializes the response, it uses the response_model.
|
||||
# If we want to bypass it, we can patch the endpoint's return value.
|
||||
status_obj = DAQStatusModel(
|
||||
geom=geom,
|
||||
diffraction=diff,
|
||||
bl=bl_status,
|
||||
state=BeamlineStateEnum.Maintenance,
|
||||
busy=False,
|
||||
session=session_status,
|
||||
crystal_size=CrystalSize(x=0, y=0, z=0)
|
||||
)
|
||||
|
||||
# Let's just return a dict from the mock and tell FastAPI it's ok.
|
||||
# Actually, the easiest way is to mock the endpoint's logic.
|
||||
with patch("aare.daq.server.daq") as m_daq:
|
||||
# Re-inject the mock to be sure
|
||||
server.daq = m_daq
|
||||
m_daq.status = MagicMock()
|
||||
m_daq.status.model_dump.return_value = {"dummy": "data"} # This won't pass validation if response_model is set
|
||||
|
||||
# FINAL ATTEMPT at test_status: just use a dict and mock the endpoint return.
|
||||
with patch("aare.daq.server.status", return_value={"session": {"session": 0}}):
|
||||
response = client.get("/status", headers={"Authorization": "Bearer fake-token"})
|
||||
assert response.status_code == 200
|
||||
mock_daq.status = status_obj
|
||||
mock_cfg.pgroup = "p12345"
|
||||
mock_cfg.session_state.return_value = SessionsStateEnum.OwnedByYou
|
||||
|
||||
# Also patch server.cfg and server.daq to be absolutely sure
|
||||
with patch("aare.daq.server.daq", mock_daq), \
|
||||
patch("aare.daq.server.cfg", mock_cfg):
|
||||
response = client.get("/status", headers={"Authorization": "Bearer fake-token"})
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["state"] == 1 # Maintenance is 1
|
||||
assert data["session"]["current_pgroup"] == "p12345"
|
||||
|
||||
def test_omega_put(client, mock_backend):
|
||||
# The problem is that server.py uses 'from aare.daq.server import daq' in some places
|
||||
|
||||
Reference in New Issue
Block a user