tests: refactord tests based on changes to auth and sample info
Build and Publish / test (push) Successful in 1m41s
Build and Publish / build (push) Successful in 18s
Build and Publish / Build and Deploy Docs (push) Successful in 40s

This commit is contained in:
appleb_m
2026-06-10 15:19:53 +02:00
parent 40f2835aca
commit e95eff9751
2 changed files with 56 additions and 62 deletions
+51 -59
View File
@@ -1,5 +1,6 @@
import pytest
import json
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from aare.daq.spreadsheetupdater import on_message, get_ws_headers, set_spreadsheet_in_redis
from aare.common.models import SampleShortInfoList
@@ -47,69 +48,58 @@ def test_set_spreadsheet_in_redis(mock_config):
def test_on_message_success(mock_config):
message = json.dumps({
"samples": [
{
"id": 1,
"puck_name": "P1",
"puck_type": "UniPuck",
"puck_location_in_dewar": 1,
"dewar_id": 1,
"pgroup": "p12345",
"dewar_name": "D1",
"tell_position": "A1",
"samples": [
{
"id": 1,
"sample_name": "S1",
"run_number": 100,
"pgroup": "p12345",
"position": 1,
"priority": 1,
"mount_count": 0,
"rotation_count": 0,
"raster_count": 0,
"screening_count": 0,
"data_collection_parameters": {}
}
]
},
{
"id": 2,
"puck_name": "Ref",
"puck_type": "UniPuck",
"puck_location_in_dewar": 2,
"dewar_id": 1,
"pgroup": "p12345",
"dewar_name": "D1",
"tell_position": "X1",
"samples": [
{
"id": 2,
"sample_name": "R1",
"run_number": 1,
"pgroup": "p12345",
"position": 1,
"priority": 1,
"mount_count": 0,
"rotation_count": 0,
"raster_count": 0,
"screening_count": 0,
"data_collection_parameters": {}
}
]
}
]
})
normal_sample = SimpleNamespace(
id=1,
sample_name="S1",
run_number=100,
pgroup="p12345",
position=1,
priority=1,
mount_count=0,
rotation_count=0,
raster_count=0,
screening_count=0,
data_collection_parameters={},
)
ref_sample = SimpleNamespace(
id=2,
sample_name="R1",
run_number=1,
pgroup="p12345",
position=1,
priority=1,
mount_count=0,
rotation_count=0,
raster_count=0,
screening_count=0,
data_collection_parameters={},
)
on_message(None, message)
mock_pucks = [
SimpleNamespace(
puck_name="P1",
dewar_name="D1",
tell_position="A1",
samples=[normal_sample],
),
SimpleNamespace(
puck_name="Ref",
dewar_name="D1",
tell_position="X1",
samples=[ref_sample],
),
]
message = json.dumps({"samples": [{}, {}]})
with patch("aare.daq.spreadsheetupdater.PuckWithTellPosition", side_effect=mock_pucks):
on_message(None, message)
normal_key = "X10SA:sample_spreadsheet"
calls = mock_config._BeamlineConfig__client.set.call_args_list
assert any(call.args[0] == normal_key for call in calls)
written_keys = [call.args[0] for call in calls]
ref_key = "X10SA:reference-tools"
assert any(call.args[0] == ref_key for call in calls)
assert "X10SA:sample_spreadsheet" in written_keys
assert "X10SA:reference-tools" in written_keys
def test_on_message_empty_ref(mock_config):
@@ -117,6 +107,8 @@ def test_on_message_empty_ref(mock_config):
"samples": [
{
"id": 1,
"barcode": "B1",
"position": "P1",
"puck_name": "P1",
"puck_type": "UniPuck",
"puck_location_in_dewar": 1,
+5 -3
View File
@@ -14,12 +14,14 @@ def test_auth_success(mocker):
stderr="",
)
token = auth("http://test-server")
token = auth("http://test-server", "/tmp/test-cert.pem")
assert token == "fake_token_abc.123.xyz"
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert "curl" in args[0]
assert "--cacert" in args[0]
assert "/tmp/test-cert.pem" in args[0]
assert "http://test-server/token" in args[0]
@@ -28,7 +30,7 @@ def test_auth_network_failure(mocker):
mock_run.side_effect = OSError("Connection refused")
with pytest.raises(RuntimeError) as excinfo:
auth("http://test-server")
auth("http://test-server", "/tmp/test-cert.pem")
assert "Cannot reach AareDAQ server" in str(excinfo.value)
@@ -37,7 +39,7 @@ def test_auth_no_url_returns_dummy_jwt(mocker):
mock_run = mocker.patch("aare.gui.auth.subprocess.run")
mocker.patch("aare.gui.auth.get_user", return_value="testuser")
token = auth(None)
token = auth(None, None)
assert isinstance(token, str)
assert token.count('.') == 2