def test_get_instance(self): my_module = self.__module__ obj = factory.get_instance(str(self.__module__) + ".Counter") self.assertIsInstance(obj, Counter) obj.increment() obj.increment() self.assertEqual(2, obj.counter)
def register_comparators(self, comparators): """Populate a dictionary of comparators, keyed by comparator name, and a dictionary of comparators, keyed by format. `comparators` must hold a list of comparator configurations keyed by name. Each configuration consists of: - ``class``: name of class that manages invocations of that comparator. - Configuration values required by the value of ``class``. A valid value for `comparators` is:: { "ProvPyComparator": { "class": "prov_interop.provpy.comparator.ProvPyComparator", "executable": "prov-compare", "arguments": "-f FORMAT1 -F FORMAT2 FILE1 FILE2", "formats": ["provx", "json"], } } This method populates `comparators`, a dictionary of :class:`prov_interop.comparator.Comparator` objects, keyed by comparator name. The `class` determines the comparator object to create and the associated configuration is used to configure it - this uses dynamic object creation (see :mod:`prov_interop.factory`). Using the above configuration there would be a mapping from ``ProvPyComparator`` to an instance of :class:`prov_interop.provpy.comparator.ProvPyComparator`. It also populates `format_comparators`, a dictionary of :class:`prov_interop.comparator.Comparator` objects, keyed by formats in :mod:`prov_interop.standards`. Using the above configuration there would be mappings from both ``provx`` and ``json`` to an instance of :class:`prov_interop.provpy.comparator.ProvPyComparator`. :param comparators: Mapping of comparator names to class names and comparator-specific configuration :type config: dict """ if (comparators == None) or (len(comparators) == 0): return for comparator_name in comparators: config = comparators[comparator_name] if HarnessResources.CLASS not in config: raise ConfigError("Missing " + HarnessResources.CLASS + " for " + comparator_name) class_name = config[HarnessResources.CLASS] comparator = factory.get_instance(class_name) comparator.configure(config) self._comparators[comparator_name] = comparator for format in comparator.formats: self._format_comparators[format] = comparator
def test_get_instance_no_such_class(self): with self.assertRaises(AttributeError): factory.get_instance(str(self.__module__) + ".NoSuchClass")
def test_get_instance_no_such_module(self): with self.assertRaises(ImportError): factory.get_instance("nosuchmodule.NoSuchClass")
def test_get_instance_no_prefix(self): with self.assertRaises(ValueError): factory.get_instance("Factory")
def test_get_instance_non_zero_arity_constructor(self): with self.assertRaises(TypeError): factory.get_instance(str(self.__module__) + ".Value")