4 from importlib
import import_module
5 from inspect
import getmembers, isclass
6 from pathlib
import Path
9 from basf2
import find_file
11 from skimExpertFunctions
import BaseSkim
13 __authors__ = [
"Sam Cunliffe",
"Phil Grace"]
17 """Test case for skim registry."""
19 ExistentModulePaths = Path(find_file(
"skim/scripts/skim")).glob(
"*.py")
22 for module
in ExistentModulePaths
23 if module.stem
not in [
"__init__",
"registry"]
27 """Fail if `cls` is not a subclass of `parent_cls`."""
28 if not issubclass(cls, parent_cls):
29 standardMsg =
"%r is not a subclass of %r" % (cls, parent_cls)
30 self.fail(self._formatMessage(msg, standardMsg))
33 """Check the codes are the correct format (8 digits)."""
35 for code
in Registry.codes:
36 self.assertEqual(len(code), 8,
"Incorrect length skim code")
37 self.assertTrue(code.isdigit(),
"Must consist of digits")
40 """Check that there aren't two skims registered with the same code."""
42 len(Registry.codes), len(set(Registry.codes)),
"Duplicated skim code"
46 """Check that there aren't two skims registered with the same name."""
48 len(Registry.names), len(set(Registry.names)),
"Duplicated skim name"
52 """Check that that no registered skims have invalid names."""
53 for name
in Registry.names:
55 name.startswith(
"Base"),
57 f
"Invalid skim name in registry: {name}. Registed skim names cannot"
58 " begin with 'Base'; this word is reserved for subclassing purposes."
63 """Check that we raise a LookupError if the skim name doesn't exist."""
64 with self.assertRaises(LookupError):
65 Registry.encode_skim_name(
"SomeNonExistentSkimName")
68 """Check that we raise a LookupError if the skim code doesn't exist."""
69 with self.assertRaises(LookupError):
70 Registry.decode_skim_code(
"1337")
73 """Check that all modules listed in registry exist in skim/scripts/skim/."""
74 for module
in Registry.modules:
79 f
"Module {module} listed in registry, but does not exist in "
85 """Check that there is no overlap between skim and module names."""
86 duplicates = set(Registry.modules).intersection(Registry.names)
88 set(), duplicates, f
"Name used for both a skim and a module: {', '.join(duplicates)}"
92 """Check that the registry is correct about the location of every skim.
94 This test uses the information from the registry, and checks for missing skims
97 for ModuleName
in Registry.modules:
98 SkimModule = import_module(f
"skim.{ModuleName}")
99 for SkimName
in Registry.get_skims_in_module(ModuleName):
103 SkimModule.__dict__.keys(),
105 f
"Registry lists {SkimName} as existing in skim.{ModuleName}, "
106 "but no such skim found!"
111 SkimClass = getattr(SkimModule, SkimName)
115 f
"Skim {SkimName} must be defined as a subclass of BaseSkim.",
119 """Check that every skim defined in a module is listed in the registry.
121 This test uses the information from the modules, and checks for missing or
122 incorrect skim information in the registry.
125 SkimModule = import_module(f
"skim.{ModuleName}")
130 for obj
in getmembers(SkimModule, isclass)
132 issubclass(obj[1], BaseSkim)
133 and obj[1]
is not BaseSkim
134 and obj[0] !=
"CombinedSkim"
136 and not obj[0].startswith(
"Base")
141 for SkimName
in SkimNames:
146 f
"Skim {SkimName} defined in skim/scripts/skim/{ModuleName}.py, "
147 "but not listed in registry."
152 for SkimName
in SkimNames:
153 ExpectedModuleName = Registry.get_skim_module(SkimName)
158 f
"Skim {SkimName} defined in "
159 f
"skim/scripts/skim/{ModuleName}.py, but listed in registry as "
160 f
"belonging to skim/scripts/skim/{ExpectedModuleName}.py."
165 if __name__ ==
"__main__":