Skip to content

Commit

Permalink
Ewm6552 fix incorrect error (#437)
Browse files Browse the repository at this point in the history
* Fix the error shown when NormalizationRecord is none.

* Get tests to work

* Updates to tests.

* Make code cov happy
  • Loading branch information
darshdinger authored Aug 12, 2024
1 parent e903c1b commit c57dfc4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/snapred/backend/service/SousChef.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ def prepReductionIngredients(
normalizationRecord = self.dataFactoryService.getNormalizationRecord(
ingredients.runNumber, ingredients.useLiteMode, version
)
# check for records
if calibrationRecord is None:
raise ValueError(
f"No calibration record found for run {ingredients.runNumber}, please run a calibration for this run."
)
elif normalizationRecord is None:
raise ValueError(
f"No normalization record found for run {ingredients.runNumber}, please run a normalization for this run." # noqa: E501
)
# grab information from records
ingredients.calibrantSamplePath = calibrationRecord.calibrationFittingIngredients.calibrantSamplePath
ingredients.cifPath = self.dataFactoryService.getCifFilePath(Path(ingredients.calibrantSamplePath).stem)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/backend/data/test_GroceryService.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def setUp(self):
# each test, ensure a random version is drawn
self.version = randint(1, 120)
self.instance.dataService.latestVersion = self.version
self.rtolValue = 1.0e-10
return super().setUp()

def clearoutWorkspaces(self) -> None:
Expand Down Expand Up @@ -425,6 +426,7 @@ def test_getCloneOfWorkspace_works(self):
assert_wksp_almost_equal(
Workspace1=wsname1,
Workspace2=wsname2,
rtol=self.rtolValue,
)

def test_updateNeutronCacheFromADS_noop(self):
Expand Down Expand Up @@ -697,6 +699,7 @@ def test_fetchWorkspace(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

# make sure it won't load same workspace name again
Expand All @@ -712,6 +715,7 @@ def test_fetchWorkspace(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

def test_fetch_failed(self):
Expand Down Expand Up @@ -760,6 +764,7 @@ def test_fetch_dirty_nexus_native(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

# test that it will use a raw workspace if one exists
Expand All @@ -783,6 +788,7 @@ def test_fetch_dirty_nexus_native(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

# test calling with Lite data, that it will call to lite service
Expand Down Expand Up @@ -835,6 +841,7 @@ def test_fetch_cached_native(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=workspaceNameCopy1,
rtol=self.rtolValue,
)

# run with a raw workspace in cache -- it will copy it
Expand All @@ -850,6 +857,7 @@ def test_fetch_cached_native(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=workspaceNameCopy2,
rtol=self.rtolValue,
)

def test_fetch_cached_lite(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def setUpClass(cls):
InputWorkspace=cls.sampleWS,
Filename=cls.filepath,
)
cls.rtolValue = 1.0e-10
assert os.path.exists(cls.filepath)

def tearDown(self) -> None:
Expand Down Expand Up @@ -159,6 +160,7 @@ def test_loadNexusNoLoader(self):
assert_wksp_almost_equal(
Workspace1=self.fetchedWS,
Workspace2=self.sampleWS,
rtol=self.rtolValue,
)
assert "LoadNexusProcessed" == algo.getPropertyValue("LoaderType")

Expand All @@ -173,6 +175,7 @@ def test_loadNexusLoader(self):
assert_wksp_almost_equal(
Workspace1=self.fetchedWS,
Workspace2=self.sampleWS,
rtol=self.rtolValue,
)
assert "LoadNexus" == algo.getPropertyValue("LoaderType")

Expand All @@ -196,6 +199,7 @@ def test_loadNexusProcessedLoader(self):
assert_wksp_almost_equal(
Workspace1=self.fetchedWS,
Workspace2=self.sampleWS,
rtol=self.rtolValue,
)
assert "LoadNexusProcessed" == algo.getPropertyValue("LoaderType")

Expand All @@ -219,6 +223,7 @@ def test_loadGroupings(self):
assert_wksp_almost_equal(
Workspace1=f"_{self.runNumber}_grouping_file",
Workspace2=f"_{self.runNumber}_grouping_name",
rtol=self.rtolValue,
)
algo.setProperty("InstrumentName", "")

Expand All @@ -228,6 +233,7 @@ def test_loadGroupings(self):
assert_wksp_almost_equal(
Workspace1=f"_{self.runNumber}_grouping_file",
Workspace2=f"_{self.runNumber}_grouping_donor",
rtol=self.rtolValue,
)

def test_loadGroupingTwice(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
class TestSaveGroupingDefinition(unittest.TestCase):
@classmethod
def setUpClass(cls):
# rtol value
cls.rtolValue = 1.0e-10

# file location for instrument definition
cls.localInstrumentFilename = Resource.getPath("inputs/testInstrument/fakeSNAP_Definition.xml")
cls.localGroupingFilename = Resource.getPath("inputs/testInstrument/fakeSNAPFocGroup_Natural.xml")
Expand Down Expand Up @@ -222,7 +225,7 @@ def do_test_local_from_workspace_test(self, workspaceName):
assert loadingAlgo.execute()

# retrieve the loaded workspace and compare it with the workspace created from the input grouping file
assert_wksp_almost_equal(loaded_ws_name, workspaceName)
assert_wksp_almost_equal(loaded_ws_name, workspaceName, rtol=self.rtolValue)

def test_local_from_groupingfile_test(self):
groupingFile = Resource.getPath("inputs/testInstrument/fakeSNAPFocGroup_Natural.xml")
Expand Down Expand Up @@ -272,6 +275,7 @@ def replaceInADS(msg, **kwargs): # noqa: ARG001
assert_wksp_almost_equal(
loaded_ws_name,
self.localReferenceWorkspace["Natural"],
rtol=self.rtolValue,
)

def test_local_from_workspace_test_column(self):
Expand All @@ -280,6 +284,7 @@ def test_local_from_workspace_test_column(self):
assert_wksp_almost_equal(
self.columnGroupingWorkspace,
self.localReferenceWorkspace["Column"],
rtol=self.rtolValue,
)

def test_local_from_workspace_test_natural(self):
Expand All @@ -288,6 +293,7 @@ def test_local_from_workspace_test_natural(self):
assert_wksp_almost_equal(
self.columnGroupingWorkspace,
self.localReferenceWorkspace["Natural"],
rtol=self.rtolValue,
)

## REMOTE CHECKS WITH FULL INSTRUMENT
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/backend/recipe/test_FetchGroceriesRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUpClass(cls):
cls.instrumentFilepath = Resource.getPath("inputs/testInstrument/fakeSNAP_Definition.xml")
cls.fetchedWSname = "_fetched_grocery"
cls.groupingScheme = "Native"
cls.rtolValue = 1.0e-10
# create some sample data
cls.sampleWS = "_grocery_to_fetch"
CreateWorkspace(
Expand Down Expand Up @@ -102,6 +103,7 @@ def test_fetch(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

# make sure it won't load same workspace name again
Expand All @@ -114,6 +116,7 @@ def test_fetch(self):
assert_wksp_almost_equal(
Workspace1=self.sampleWS,
Workspace2=res["workspace"],
rtol=self.rtolValue,
)

@mock.patch("snapred.backend.recipe.FetchGroceriesRecipe.FetchAlgo")
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/backend/recipe/test_GenericRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class TestMatrixProp(GenericRecipe[MatrixPropertyAlgo]):
# run the recipe and make sure correct result is given
CreateSingleValuedWorkspace(OutputWorkspace="okay")
res = TestMatrixProp().executeRecipe(InputWorkspace="okay", OutputWorkspace="hurray")
assert_wksp_almost_equal(Workspace1="okay", Workspace2=res)
assert_wksp_almost_equal(Workspace1="okay", Workspace2=res, rtol=1.0e-10)

def test_primitives(self):
# register the algorithm and define the recipe
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/backend/service/test_SousChef.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
from unittest import mock

import pytest
from mantid.simpleapi import DeleteWorkspace, mtd
from snapred.backend.dao.request.FarmFreshIngredients import FarmFreshIngredients
from snapred.backend.service.SousChef import SousChef
Expand Down Expand Up @@ -347,6 +348,46 @@ def test_prepReductionIngredients(self, ReductionIngredients):
)
assert res == ReductionIngredients.return_value

@mock.patch(thisService + "ReductionIngredients")
def test_prepReductionIngredients_noCalibrationRecord(self, ReductionIngredients): # noqa: ARG002
record = mock.Mock(
smoothingParameter=1.0,
calibrationFittingIngredients=mock.Mock(calibrantSamplePath="a/b.x"),
)
self.instance.prepRunConfig = mock.Mock()
self.instance.prepManyPixelGroups = mock.Mock()
self.instance.prepManyDetectorPeaks = mock.Mock()
self.instance.dataFactoryService.getCifFilePath = mock.Mock()
self.instance.dataFactoryService.getReductionState = mock.Mock()
self.instance.dataFactoryService.getNormalizationRecord = mock.Mock(return_value=record)
self.instance.dataFactoryService.getCalibrationRecord = mock.Mock(return_value=None)

with pytest.raises(
ValueError,
match=f"No calibration record found for run {self.ingredients.runNumber}, please run a calibration for this run.", # noqa: E501
):
self.instance.prepReductionIngredients(self.ingredients)

@mock.patch(thisService + "ReductionIngredients")
def test_prepReductionIngredients_noNormalizationRecord(self, ReductionIngredients): # noqa: ARG002
record = mock.Mock(
smoothingParameter=1.0,
calibrationFittingIngredients=mock.Mock(calibrantSamplePath="a/b.x"),
)
self.instance.prepRunConfig = mock.Mock()
self.instance.prepManyPixelGroups = mock.Mock()
self.instance.prepManyDetectorPeaks = mock.Mock()
self.instance.dataFactoryService.getCifFilePath = mock.Mock()
self.instance.dataFactoryService.getReductionState = mock.Mock()
self.instance.dataFactoryService.getNormalizationRecord = mock.Mock(return_value=None)
self.instance.dataFactoryService.getCalibrationRecord = mock.Mock(return_value=record)

with pytest.raises(
ValueError,
match=f"No normalization record found for run {self.ingredients.runNumber}, please run a normalization for this run.", # noqa: E501
):
self.instance.prepReductionIngredients(self.ingredients)

@mock.patch(thisService + "NormalizationIngredients")
def test_prepNormalizationIngredients(self, NormalizationIngredients):
self.instance.prepCalibrantSample = mock.Mock()
Expand Down

0 comments on commit c57dfc4

Please sign in to comment.