diff --git a/scripts/test/ISISPowderCommonTest.py b/scripts/test/ISISPowderCommonTest.py index 1538e0fcbdfdd9c93fb16a7a7e296dda5bcff9c6..532bfff2fee30504795e2bd15987bacc6a78f2a1 100644 --- a/scripts/test/ISISPowderCommonTest.py +++ b/scripts/test/ISISPowderCommonTest.py @@ -16,17 +16,16 @@ class ISISPowderCommonTest(unittest.TestCase): def test_cal_map_dict_helper(self): missing_key_name = "wrong_key" correct_key_name = "right_key" - dict_without_key = {correct_key_name: None} dict_with_key = {correct_key_name: 123} # Check it correctly raises with self.assertRaisesRegexp(KeyError, "The field '" + missing_key_name + "' is required"): - common.cal_map_dictionary_key_helper(dictionary=dict_without_key, key=missing_key_name) + common.cal_map_dictionary_key_helper(dictionary=dict_with_key, key=missing_key_name) # Check it correctly appends the passed error message when raising appended_e_msg = "test append message" with self.assertRaisesRegexp(KeyError, appended_e_msg): - common.cal_map_dictionary_key_helper(dictionary=dict_without_key, key=missing_key_name, + common.cal_map_dictionary_key_helper(dictionary=dict_with_key, key=missing_key_name, append_to_error_message=appended_e_msg) # Check that it correctly returns the key value where it exists @@ -42,7 +41,7 @@ class ISISPowderCommonTest(unittest.TestCase): for i in range(0, 3): out_name = "crop_banks_in_tof-" + str(i) cropping_value_list.append(cropping_value) - bank_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=20000, BinWidth=1)) + bank_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=1100, BinWidth=1)) # Check a list of WS and single cropping value is detected with self.assertRaisesRegexp(ValueError, "The cropping values were not in a list type"): @@ -75,7 +74,7 @@ class ISISPowderCommonTest(unittest.TestCase): for i in range(0, 3): out_name = "crop_banks_in_tof-" + str(i) - ws_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=20000, BinWidth=1)) + ws_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=600, BinWidth=1)) # Crop a single workspace in TOF tof_single_ws = common.crop_in_tof(ws_to_crop=ws_list[0], x_min=x_min, x_max=x_max) @@ -92,7 +91,7 @@ class ISISPowderCommonTest(unittest.TestCase): # Checks that crop_in_tof converts to TOF before cropping ws_list = [] x_min = 100 - x_max = 500 # Crop to 0-500 microseconds for unit tests + x_max = 200 expected_number_of_bins = 20000 # Hard code number of expected bins for dSpacing for i in range(0, 3): @@ -111,5 +110,33 @@ class ISISPowderCommonTest(unittest.TestCase): self.assertEqual(ws.getNumberBins(), expected_number_of_bins) mantid.DeleteWorkspace(ws) + def test_dictionary_key_helper(self): + good_key_name = "key_exists" + bad_key_name = "key_does_not_exist" + + test_dictionary = {good_key_name: 123} + + e_msg = "test message" + + with self.assertRaises(KeyError): + common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name) + + with self.assertRaisesRegexp(KeyError, e_msg): + common.dictionary_key_helper(dictionary=test_dictionary, key=bad_key_name, exception_msg=e_msg) + + self.assertEqual(common.dictionary_key_helper(dictionary=test_dictionary, key=good_key_name), 123) + + def test_extract_ws_spectra(self): + number_of_expected_banks = 5 + ws_to_split = mantid.CreateSampleWorkspace(XMin=0, XMax=1, BankPixelWidth=1, + NumBanks=number_of_expected_banks) + input_name = ws_to_split.getName() + + extracted_banks = common.extract_ws_spectra(ws_to_split=ws_to_split) + self.assertEqual(len(extracted_banks), number_of_expected_banks) + for i, ws in enumerate(extracted_banks): + expected_name = input_name + '-' + str(i + 1) + self.assertEqual(expected_name, ws.getName()) + if __name__ == "__main__": unittest.main()