13 Tests to ensure that the structure of the skim package is maintained.
17 from importlib
import import_module
18 from inspect
import getmembers, isclass
20 from pathlib
import Path
23 from basf2
import find_file
25 from skim
import BaseSkim
29 """Test case for skim registry."""
31 ExistentModulePaths = Path(find_file(
"skim/scripts/skim/WGs")).glob(
"*.py")
34 for module
in ExistentModulePaths
35 if module.stem
not in [
"__init__",
"registry"]
39 """Fail if `cls` is not a subclass of `parent_cls`."""
40 if not issubclass(cls, parent_cls):
41 standardMsg =
"%r is not a subclass of %r" % (cls, parent_cls)
42 self.fail(self._formatMessage(msg, standardMsg))
45 """Check the codes are the correct format (8 digits)."""
47 for code
in Registry.codes:
48 self.assertEqual(len(code), 8,
"Incorrect length skim code")
49 self.assertTrue(code.isdigit(),
"Must consist of digits")
52 """Check that there aren't two skims registered with the same code."""
54 len(Registry.codes), len(set(Registry.codes)),
"Duplicated skim code"
58 """Check that there aren't two skims registered with the same name."""
60 len(Registry.names), len(set(Registry.names)),
"Duplicated skim name"
64 """Check that that no registered skims have invalid names."""
65 for name
in Registry.names:
67 name.startswith(
"Base"),
69 f
"Invalid skim name in registry: {name}. Registed skim names cannot"
70 " begin with 'Base'; this word is reserved for subclassing purposes."
75 """Check that we raise a LookupError if the skim name doesn't exist."""
76 with self.assertRaises(LookupError):
77 Registry.encode_skim_name(
"SomeNonExistentSkimName")
80 """Check that we raise a LookupError if the skim code doesn't exist."""
81 with self.assertRaises(LookupError):
82 Registry.decode_skim_code(
"1337")
85 """Check that all modules listed in registry exist in skim/scripts/skim/."""
86 for module
in Registry.modules:
91 f
"Module {module} listed in registry, but does not exist in "
97 """Check that there is no overlap between skim and module names."""
98 duplicates = set(Registry.modules).intersection(Registry.names)
102 f
"Name used for both a skim and a module: {', '.join(duplicates)}",
106 """Check that the registry is correct about the location of every skim.
108 This test uses the information from the registry, and checks for missing skims
111 for ModuleName
in Registry.modules:
112 SkimModule = import_module(f
"skim.WGs.{ModuleName}")
113 for SkimName
in Registry.get_skims_in_module(ModuleName):
117 SkimModule.__dict__.keys(),
119 f
"Registry lists {SkimName} as existing in skim.WGs.{ModuleName}, "
120 "but no such skim found!"
125 SkimClass = getattr(SkimModule, SkimName)
129 f
"Skim {SkimName} must be defined as a subclass of BaseSkim.",
133 """Check that every skim defined in a module is listed in the registry.
135 This test uses the information from the modules, and checks for missing or
136 incorrect skim information in the registry.
139 SkimModule = import_module(f
"skim.WGs.{ModuleName}")
144 for obj
in getmembers(SkimModule, isclass)
146 issubclass(obj[1], BaseSkim)
147 and obj[1]
is not BaseSkim
148 and obj[0] !=
"CombinedSkim"
150 and not obj[0].startswith(
"Base")
155 for SkimName
in SkimNames:
160 f
"Skim {SkimName} defined in skim/scripts/skim/{ModuleName}.py, "
161 "but not listed in registry."
166 for SkimName
in SkimNames:
167 ExpectedModuleName = Registry.get_skim_module(SkimName)
172 f
"Skim {SkimName} defined in "
173 f
"skim/scripts/skim/{ModuleName}.py, but listed in registry as "
174 f
"belonging to skim/scripts/skim/{ExpectedModuleName}.py."
182 Check that all skims with a ``validation_histograms`` method defined have a
183 script in skim/validation/, and vice versa. This unit test exists to make sure
184 that the code auto-generated by ``b2skim-generate-validation`` stays up to date.
186 SkimsWithValidationMethod = [
188 for skim
in Registry.names
189 if not Registry.get_skim_function(skim)()._method_unchanged(
190 "validation_histograms"
193 SkimsWithValidationScript = [script.stem
for script
in Path(find_file(
"skim/validation")).glob(
"*.py")]
195 for skim
in SkimsWithValidationMethod:
198 SkimsWithValidationScript,
200 f
"Skim {skim} has a `validation_histograms` method defined, but no "
201 "script has been added to skim/validation/ yet. Please add this "
203 f
" $ b2skim-generate-validation --skims {skim} --in-place"
206 for skim
in SkimsWithValidationScript:
209 SkimsWithValidationMethod,
211 f
"Skim {skim} has a script in skim/validation/, but no "
212 "`validation_histograms` method defined."
216 @unittest.skipIf(
not os.getenv("BELLE2_VALIDATION_DATA_DIR"),
217 "BELLE2_VALIDATION_DATA_DIR environment variable not set."
221 Check that all ``validation_sample`` attributes of skims point to existing files.
223 for skim
in Registry.names:
224 SkimObject = Registry.get_skim_function(skim)()
226 if SkimObject._method_unchanged(
"validation_histograms"):
230 find_file(SkimObject.validation_sample, data_type=
"validation")
231 except FileNotFoundError:
232 self.fail(f
"{skim}.validation_sample does not point to an existing validation file.")
235 if __name__ ==
"__main__":
237
def test_invalid_names(self)
def test_skims_exist(self)
def test_code_format(self)
def test_undocumented_skims(self)
def test_unique_names(self)
def test_unique_codes(self)
def assertIsSubclass(self, cls, parent_cls, msg=None)
def test_modules_exist(self)
def test_clashing_skim_and_module_names(self)
def test_validation_samples(self)
def test_validation_scripts_exist(self)