Skip to content

Commit

Permalink
Move some tests to system tests, reduce use of dummy connection fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
jsouter committed Nov 28, 2024
1 parent aaad89f commit 6f17078
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 425 deletions.
210 changes: 0 additions & 210 deletions tests/conftest.py

This file was deleted.

37 changes: 37 additions & 0 deletions tests/system/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import signal
import subprocess
from pathlib import Path
from time import sleep

import pytest

from fastcs_eiger.eiger_controller import EigerController

HERE = Path(__file__).parent


# Stolen from tickit-devices
# https://docs.pytest.org/en/latest/example/parametrize.html#indirect-parametrization
@pytest.fixture
def sim_eiger_controller(request):
"""Subprocess that runs ``tickit all <config_path>``."""
config_path: str = request.param
proc = subprocess.Popen(
["tickit", "all", config_path],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)

# Wait until ready
while True:
line = proc.stdout.readline()
if "Starting HTTP server..." in line:
break

sleep(3)

yield EigerController("127.0.0.1", 8081)

proc.send_signal(signal.SIGINT)
print(proc.communicate()[0])
35 changes: 0 additions & 35 deletions tests/system/test_introspection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import json
import os
import signal
import subprocess
from pathlib import Path
from time import sleep
from typing import Any

import pytest
Expand Down Expand Up @@ -34,38 +31,6 @@ def _serialise_parameter(parameter: EigerParameter) -> dict:
}


@pytest.fixture
def eiger_controller():
yield EigerController("i04-1-eiger01", 80)


# Stolen from tickit-devices
# https://docs.pytest.org/en/latest/example/parametrize.html#indirect-parametrization
@pytest.fixture
def sim_eiger_controller(request):
"""Subprocess that runs ``tickit all <config_path>``."""
config_path: str = request.param
proc = subprocess.Popen(
["tickit", "all", config_path],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)

# Wait until ready
while True:
line = proc.stdout.readline()
if "Starting HTTP server..." in line:
break

sleep(3)

yield EigerController("127.0.0.1", 8081)

proc.send_signal(signal.SIGINT)
print(proc.communicate()[0])


@pytest.mark.asyncio
@pytest.mark.parametrize(
"sim_eiger_controller", [str(HERE / "eiger.yaml")], indirect=True
Expand Down
58 changes: 58 additions & 0 deletions tests/system/test_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from pathlib import Path

import pytest
from fastcs.attributes import Attribute

from fastcs_eiger.eiger_controller import IGNORED_KEYS, MISSING_KEYS, EigerController

HERE = Path(__file__).parent


@pytest.mark.asyncio
@pytest.mark.parametrize(
"sim_eiger_controller", [str(HERE / "eiger.yaml")], indirect=True
)
async def test_controller_groups_and_parameters(sim_eiger_controller: EigerController):
controller = sim_eiger_controller
await controller.initialise()

for subsystem in MISSING_KEYS:
subcontroller = controller.get_sub_controllers()[subsystem.title()]
parameters = await subcontroller._introspect_detector_subsystem()
if subsystem == "detector":
# ignored keys should not get added to the controller
assert all(param.key not in IGNORED_KEYS for param in parameters)

# threshold parameters should belong to own group
for attr_name in dir(subcontroller):
attr = getattr(subcontroller, attr_name)
if isinstance(attr, Attribute) and "threshold" in attr_name:
if attr_name == "threshold_energy":
continue
assert attr.group and "Threshold" in attr.group

attr = subcontroller.threshold_1_energy
sender = attr.sender
await sender.put(subcontroller, attr, 100.0)
# set parameters to update based on response to put request
assert subcontroller._parameter_updates == {
"flatfield",
"threshold/1/energy",
"threshold/1/flatfield",
"threshold/2/flatfield",
"threshold_energy",
}

subcontroller._parameter_updates.clear()

# make sure API inconsistency for threshold/difference/mode is addressed
attr = subcontroller.threshold_difference_mode
sender = attr.sender
await sender.put(subcontroller, attr, "enabled")
assert subcontroller._parameter_updates == {"threshold/difference/mode"}

for keys in MISSING_KEYS[subsystem].values(): # loop over status, config keys
for key in keys:
assert any(param.key == key for param in parameters)

await controller.connection.close()
Loading

0 comments on commit 6f17078

Please sign in to comment.