예제 #1
0
    def constructor_test_helper(
        self,
        config,
        hook_type,
        hook_registry_name=None,
        hook_kwargs=None,
        invalid_configs=None,
    ):
        hook_kwargs = config if hook_kwargs is None else hook_kwargs
        hook1 = hook_type(**hook_kwargs)
        self.assertTrue(isinstance(hook1, hook_type))

        hook2 = hook_type.from_config(config)
        self.assertTrue(isinstance(hook2, hook_type))

        if hook_registry_name is not None:
            config["name"] = hook_registry_name
            hook3 = build_hook(config)
            del config["name"]
            self.assertTrue(isinstance(hook3, hook_type))

        if invalid_configs is not None:
            # Verify assert logic works correctly
            for cfg in invalid_configs:
                with self.assertRaises((AssertionError, TypeError)):
                    hook_type.from_config(cfg)
    def test_hook_registry_and_builder(self):
        config = {"name": "test_hook", "a": 1, "b": 2}
        hook1 = build_hook(hook_config=config)
        self.assertTrue(isinstance(hook1, TestHook))
        self.assertTrue(hook1.state.a == 1)
        self.assertTrue(hook1.state.b == 2)

        hook_configs = [copy.deepcopy(config), copy.deepcopy(config)]
        hooks = build_hooks(hook_configs=hook_configs)
        for hook in hooks:
            self.assertTrue(isinstance(hook, TestHook))
            self.assertTrue(hook.state.a == 1)
            self.assertTrue(hook.state.b == 2)