Source code for toolbox_scs.test.test_top_level
import unittest
import logging
import os
import sys
import argparse
import toolbox_scs as tb
from toolbox_scs.util.exceptions import *
import extra_data as ed
logging.basicConfig(level=logging.DEBUG)
[docs]suites = {"packaging": (
"test_constant",
),
"load": (
"test_load",
"test_openrun",
"test_openrunpath",
"test_loadbinnedarray",
)
}
[docs]class TestToolbox(unittest.TestCase):
@classmethod
[docs] def setUpClass(cls):
log_root.info("Start global setup")
cls._mnentry = 'SCS_RR_UTC/TSYS/TIMESERVER'
cls._ed_run = ed.open_run(2212, 235)
log_root.info("Finished global setup, start tests")
@classmethod
[docs] def test_constant(self):
cls = self.__class__
self.assertEqual(tb.mnemonics['bunchPatternTable']['source'],cls._mnentry)
[docs] def test_load(self):
fields = ["SCS_XGM"]
# normal behavior
run_tb = None
proposalNB = 2511
runNB = 176
run_tb, data = tb.load(proposalNB, runNB, fields)
self.assertEqual(data['bunchPatternTable'].values[0, 0], 2113321)
# exception raised
run_tb = None
proposalNB = 2511
runNB = 1766
with self.assertRaises(ToolBoxPathError) as cm:
run_tb, data = tb.load(proposalNB, runNB, fields)
tb_exception = cm.exception
constr_path = f'/gpfs/exfel/exp/SCS/202001/p002511/raw/r{runNB}'
exp_msg = f"Invalid path: {constr_path}. " + \
"The constructed path does not exist."
self.assertEqual(tb_exception.message, exp_msg)
[docs] def test_openrun(self):
run, _ = tb.load(2212, 235)
src = 'SCS_DET_DSSC1M-1/DET/0CH0:xtdf'
self.assertTrue(src in run.all_sources)
[docs] def test_openrunpath(self):
run = tb.run_by_path(
"/gpfs/exfel/exp/SCS/201901/p002212/raw/r0235")
src = 'SCS_DET_DSSC1M-1/DET/0CH0:xtdf'
self.assertTrue(src in run.all_sources)
[docs] def test_loadbinnedarray(self):
cls = self.__class__
# Normal use
mnemonic = 'PP800_PhaseShifter'
data = tb.get_array(cls._ed_run, mnemonic, 0.5)
self.assertTrue = (data)
# unknown mnemonic
mnemonic = 'blabla'
with self.assertRaises(ToolBoxValueError) as cm:
scan_variable = tb.get_array(cls._ed_run, mnemonic, 0.5)
excp = cm.exception
self.assertEqual(excp.value, mnemonic)
[docs]def list_suites():
print("\nPossible test suites:\n" + "-" * 79)
for key in suites:
print(key)
print("-" * 79 + "\n")
[docs]def suite(*tests):
suite = unittest.TestSuite()
for test in tests:
suite.addTest(TestToolbox(test))
return suite
[docs]def main(*cliargs):
try:
for test_suite in cliargs:
if test_suite in suites:
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite(*suites[test_suite]))
else:
log_root.warning(
"Unknown suite: '{}'".format(test_suite))
pass
except Exception as err:
log_root.error("Unecpected error: {}".format(err),
exc_info=True)
pass
if __name__ == '__main__':
parser.add_argument('--list-suites',
action='store_true',
help='list possible test suites')
parser.add_argument('--run-suites', metavar='S',
nargs='+', action='store',
help='a list of valid test suites')
args = parser.parse_args()
if args.list_suites:
list_suites()
if args.run_suites:
main(*args.run_suites)