From 30491311e3c9f28d551c20b52451340ca612c543 Mon Sep 17 00:00:00 2001 From: Federico Stagni Date: Fri, 4 Dec 2026 11:42:40 +0100 Subject: [PATCH] fix(test): make Test_scitag.py independent of the live scitags.org API get_scitag() fetches https://scitags.org/api.json, but the unit tests asserted against VO/activity ids hardcoded from a past snapshot of that API. scitags.org has since evolved (e.g. atlas 'Analysis Output' moved from activityId 18 to 12, 'Debug' no longer matches), and the fetch can also fail outright in CI, so the tests were flaky. Mock the network call with a static fixture, clear the module-level caches around each test, and add coverage for the network-failure fallback and the fetch-once caching behaviour. --- .../Client/test/Test_scitag.py | 86 +++++++++++++++++-- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/src/DIRAC/DataManagementSystem/Client/test/Test_scitag.py b/src/DIRAC/DataManagementSystem/Client/test/Test_scitag.py index bcfc033b049..3af32da32c6 100644 --- a/src/DIRAC/DataManagementSystem/Client/test/Test_scitag.py +++ b/src/DIRAC/DataManagementSystem/Client/test/Test_scitag.py @@ -1,21 +1,83 @@ -from unittest.mock import Mock, patch +"""Tests for :func:`get_scitag`. + +``get_scitag`` normally fetches the VO/activity definitions from +https://scitags.org/api.json. That live API evolves over time (activities +can be renamed or get new ids) and can be unreachable from CI, so the tests +below mock the network call with a static fixture and never depend on the +remote content. +""" + +from unittest.mock import Mock import pytest +from DIRAC.DataManagementSystem.Client import FTS3Job from DIRAC.DataManagementSystem.Client.FTS3Job import get_scitag +# Static stand-in for https://scitags.org/api.json with only the fields used +# by get_scitag(). The ids are arbitrary: they no longer need to match the +# live API since the network call is always mocked. +SCITAGS_API = { + "experiments": [ + { + "expId": 1, + "expName": "default", + "activities": [{"activityId": 1, "activityName": "default"}], + }, + { + "expId": 2, + "expName": "atlas", + "activities": [ + {"activityId": 1, "activityName": "default"}, + {"activityId": 3, "activityName": "Data Brokering"}, + {"activityId": 9, "activityName": "Debug"}, + {"activityId": 17, "activityName": "Analysis Input"}, + {"activityId": 18, "activityName": "Analysis Output"}, + ], + }, + { + "expId": 3, + "expName": "cms", + "activities": [ + {"activityId": 1, "activityName": "default"}, + {"activityId": 3, "activityName": "Cache"}, + ], + }, + ] +} + + +def _clear_caches(): + # get_scitag() caches its results, and the fetched JSON, at module level: + # empty them so each test runs against a fresh (mocked) fetch + FTS3Job._scitag_cache.clear() + FTS3Job._scitag_json_cache.clear() + + +@pytest.fixture(autouse=True) +def mocked_scitags_api(monkeypatch): + """Serve SCITAGS_API instead of hitting scitags.org.""" + response = Mock() + response.json.return_value = SCITAGS_API + response.raise_for_status.return_value = None + get = Mock(return_value=response) + monkeypatch.setattr(FTS3Job.requests, "get", get) + _clear_caches() + yield get + _clear_caches() + class TestGetScitag: def test_valid_vo_and_activity(self): """Test get_scitag with valid VO and activity.""" result = get_scitag("atlas", "Analysis Input") - expected = 2 << 6 | 17 # atlas expId=2, analysis activityId=17 + expected = 2 << 6 | 17 assert result == expected def test_valid_vo_no_activity(self): """Test get_scitag with valid VO but no specific activity (should use default).""" result = get_scitag("cms") - expected = 3 << 6 | 1 # cms expId=200, default activityId=1 + expected = 3 << 6 | 1 assert result == expected def test_invalid_vo(self): @@ -27,13 +89,13 @@ def test_invalid_vo(self): def test_valid_vo_invalid_activity(self): """Test get_scitag with valid VO but invalid activity.""" result = get_scitag("atlas", "nonexistent_activity") - expected = 2 << 6 | 1 # atlas expId=2, default activity_id=1 + expected = 2 << 6 | 1 # default activity_id=1 assert result == expected def test_case_insensitive_vo(self): """Test that VO matching is case insensitive.""" result = get_scitag("ATLAS", "Data Brokering") - expected = 2 << 6 | 3 # atlas expId=2, production activityId=3 + expected = 2 << 6 | 3 assert result == expected @@ -67,3 +129,17 @@ def test_no_activity_parameter(vo, expected_result): """Test behavior when no activity parameter is provided.""" result = get_scitag(vo) assert result == expected_result + + +def test_network_failure_falls_back_to_defaults(mocked_scitags_api): + """If scitags.org cannot be reached, the default values are used.""" + mocked_scitags_api.side_effect = ConnectionError("no network") + assert get_scitag("atlas", "Analysis Input") == 1 << 6 | 1 + + +def test_api_is_fetched_only_once(mocked_scitags_api): + """Repeated calls are served from the caches, not from the network.""" + assert get_scitag("atlas", "Debug") == 2 << 6 | 9 + assert get_scitag("atlas", "Debug") == 2 << 6 | 9 + assert get_scitag("cms", "Cache") == 3 << 6 | 3 + assert mocked_scitags_api.call_count == 1