From a0b2a81c481d0b7841df2a981024755da8fb9599 Mon Sep 17 00:00:00 2001 From: Michael Walsh Date: Mon, 9 Dec 2024 12:31:10 -0500 Subject: [PATCH] fix failing unit tests --- src/snapred/backend/data/LocalDataService.py | 4 ++- .../backend/data/test_DataFactoryService.py | 4 +-- tests/unit/backend/data/test_Indexer.py | 2 +- .../backend/data/test_LocalDataService.py | 28 +++++++++++++------ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/snapred/backend/data/LocalDataService.py b/src/snapred/backend/data/LocalDataService.py index fec072a5a..2d7b5d00f 100644 --- a/src/snapred/backend/data/LocalDataService.py +++ b/src/snapred/backend/data/LocalDataService.py @@ -850,8 +850,10 @@ def readCalibrationState(self, runId: str, useLiteMode: bool, version: Optional[ return parameters @validate_call - def readNormalizationState(self, runId: str, useLiteMode: bool, version: Optional[Version] = VersionState.LATEST): + def readNormalizationState(self, runId: str, useLiteMode: bool, version: Version = VersionState.LATEST): indexer = self.normalizationIndexer(runId, useLiteMode) + if version is VersionState.LATEST: + version = indexer.latestApplicableVersion(runId) return indexer.readParameters(version) def writeCalibrationState(self, calibration: Calibration): diff --git a/tests/unit/backend/data/test_DataFactoryService.py b/tests/unit/backend/data/test_DataFactoryService.py index 62f10add0..ab9000e2b 100644 --- a/tests/unit/backend/data/test_DataFactoryService.py +++ b/tests/unit/backend/data/test_DataFactoryService.py @@ -67,12 +67,12 @@ def setUpClass(cls): cls.mockLookupService.calibrationIndexer.return_value = mock.Mock( versionPath=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Calibration", *x)), getIndex=mock.Mock(return_value=[cls.expected(cls, "Calibration")]), - getLatestApplicableVersion=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Calibration", *x)), + latestApplicableVersion=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Calibration", *x)), ) cls.mockLookupService.normalizationIndexer.return_value = mock.Mock( versionPath=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Normalization", *x)), getIndex=mock.Mock(return_value=[cls.expected(cls, "Normalization")]), - getLatestApplicableVersion=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Normalization", *x)), + latestApplicableVersion=mock.Mock(side_effect=lambda *x: cls.expected(cls, "Normalization", *x)), ) def setUp(self): diff --git a/tests/unit/backend/data/test_Indexer.py b/tests/unit/backend/data/test_Indexer.py index 6afbf3d93..c6d11e398 100644 --- a/tests/unit/backend/data/test_Indexer.py +++ b/tests/unit/backend/data/test_Indexer.py @@ -10,6 +10,7 @@ from unittest import mock import pytest +from util.dao import DAOFactory from snapred.backend.dao.calibration.Calibration import Calibration from snapred.backend.dao.calibration.CalibrationRecord import CalibrationRecord @@ -275,7 +276,6 @@ def test_flattenVersion(self): indexer.currentVersion = lambda: 3 indexer.nextVersion = lambda: 4 assert indexer._flattenVersion(VersionState.DEFAULT) == indexer.defaultVersion() - assert indexer._flattenVersion(VersionState.LATEST) == indexer.currentVersion() assert indexer._flattenVersion(VersionState.NEXT) == indexer.nextVersion() assert indexer._flattenVersion(3) == 3 diff --git a/tests/unit/backend/data/test_LocalDataService.py b/tests/unit/backend/data/test_LocalDataService.py index d971e3d18..2152c3f71 100644 --- a/tests/unit/backend/data/test_LocalDataService.py +++ b/tests/unit/backend/data/test_LocalDataService.py @@ -315,8 +315,11 @@ def test_readStateConfig_default(): indexer = localDataService.calibrationIndexer("57514", True) parameters = DAOFactory.calibrationParameters("57514", True, indexer.defaultVersion()) tmpRoot.saveObjectAt(groupingMap, localDataService._groupingMapPath(tmpRoot.stateId)) - tmpRoot.saveObjectAt(parameters, indexer.parametersPath(VersionState.DEFAULT)) - indexer.index = {VersionState.DEFAULT: mock.Mock()} # NOTE manually update the Indexer + tmpRoot.saveObjectAt(parameters, indexer.parametersPath(indexer.defaultVersion())) + + indexer.index = { + VersionState.DEFAULT: mock.MagicMock(appliesTo="57514", version=indexer.defaultVersion()) + } # NOTE manually update the Indexer actual = localDataService.readStateConfig("57514", True) assert actual is not None assert actual.stateId == DAOFactory.magical_state_id @@ -332,7 +335,9 @@ def test_readStateConfig_previous(): indexer = localDataService.calibrationIndexer("57514", True) tmpRoot.saveObjectAt(groupingMap, localDataService._groupingMapPath(tmpRoot.stateId)) tmpRoot.saveObjectAt(parameters, indexer.parametersPath(version)) - indexer.index = {version: mock.Mock()} # NOTE manually update the Indexer + indexer.index = { + version: mock.MagicMock(appliesTo="57514", version=version) + } # NOTE manually update the Indexer actual = localDataService.readStateConfig("57514", True) assert actual is not None assert actual.stateId == DAOFactory.magical_state_id @@ -348,7 +353,9 @@ def test_readStateConfig_attaches_grouping_map(): indexer = localDataService.calibrationIndexer("57514", True) tmpRoot.saveObjectAt(groupingMap, localDataService._groupingMapPath(tmpRoot.stateId)) tmpRoot.saveObjectAt(parameters, indexer.parametersPath(version)) - indexer.index = {version: mock.Mock()} # NOTE manually update the Indexer + indexer.index = { + version: mock.MagicMock(appliesTo="57514", version=version) + } # NOTE manually update the Indexer actual = localDataService.readStateConfig("57514", True) expectedMap = DAOFactory.groupingMap_SNAP() assert actual.groupingMap == expectedMap @@ -365,7 +372,9 @@ def test_readStateConfig_invalid_grouping_map(): indexer = localDataService.calibrationIndexer("57514", True) tmpRoot.saveObjectAt(groupingMap, localDataService._groupingMapPath(tmpRoot.stateId)) tmpRoot.saveObjectAt(parameters, indexer.parametersPath(version)) - indexer.index = {version: mock.Mock()} # NOTE manually update the Indexer + indexer.index = { + version: mock.MagicMock(appliesTo="57514", version=version) + } # NOTE manually update the Indexer # 'GroupingMap.defaultStateId' is _not_ a valid grouping-map 'stateId' for an existing `StateConfig`. with pytest.raises( # noqa: PT012 RuntimeError, @@ -383,7 +392,9 @@ def test_readStateConfig_calls_prepareStateRoot(): with state_root_redirect(localDataService, stateId=expected.instrumentState.id.hex) as tmpRoot: indexer = localDataService.calibrationIndexer("57514", True) tmpRoot.saveObjectAt(expected, indexer.parametersPath(version)) - indexer.index = {version: mock.Mock()} # NOTE manually update the Indexer + indexer.index = { + version: mock.MagicMock(appliesTo="57514", version=version) + } # NOTE manually update the Indexer assert not localDataService._groupingMapPath(tmpRoot.stateId).exists() localDataService._prepareStateRoot = mock.Mock( side_effect=lambda x: tmpRoot.saveObjectAt( # noqa ARG005 @@ -720,7 +731,8 @@ def test_write_model_pretty_StateConfig_excludes_grouping_map(): # move the calculation parameters into correct folder indexer = localDataService.calibrationIndexer("57514", True) indexer.writeParameters(DAOFactory.calibrationParameters("57514", True, indexer.defaultVersion())) - indexer.index = {indexer.defaultVersion(): mock.Mock()} + indexer.index = {indexer.defaultVersion(): mock.MagicMock(appliesTo="57514", version=indexer.defaultVersion())} + # move the grouping map into correct folder write_model_pretty(DAOFactory.groupingMap_SNAP(), localDataService._groupingMapPath(tmpRoot.stateId)) @@ -2120,7 +2132,7 @@ def test_readWriteNormalizationState(): ans = localDataService.readNormalizationState(runNumber, True, VersionState.LATEST) assert ans == mockNormalizationIndexer.readParameters.return_value - mockNormalizationIndexer.readParameters.assert_called_once_with(VersionState.LATEST) + mockNormalizationIndexer.readParameters.assert_called_once_with(1) def test_readDetectorState():