예제 #1
0
    def test_factory_create(self):
        module_name = 'MockModuleName'
        ModuleFactory.register(module_name, MockModule)
        MockModule.__abstractmethods__ = set()

        module_obj = ModuleFactory.create('Some name', module_name)
        assert type(module_obj) == MockModule
예제 #2
0
    async def test_should_pass_with_correct_variables_in_config(self):
        @produce(MockTestInterface)
        @finalize
        class DependingModule(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        @accept(MockTestInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)
        ModuleFactory.register('DependingModule', DependingModule)

        config_file = self.get_config_file(
            'correctly_parametrized_config.yaml')

        correct_parameters = {
            'TYPE_1': 'DependingModule',
            'NAME_2': 'mod2',
            'THRESHOLD': 0.2,
            'BIAS': 0.1,
            'group_name': 'group1'
        }

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory,
                                               correct_parameters)

        assert len(pipeline.modules) == 3
        assert issubclass(pipeline.modules[0].interface, MockTestInterface)
        assert issubclass(pipeline.modules[1].interface, MockTestInterface)
        assert pipeline.modules[2].input_modules == ['mod1', 'mod2']
예제 #3
0
    def test_factory_register_not_submodule(self):
        class NotSubmoduleMock:
            pass

        module_to_register = 'NotSubmoduleMock'
        with pytest.raises(TypeError):
            ModuleFactory.register(module_to_register, NotSubmoduleMock)
예제 #4
0
 def setup_method(self, method):
     ModuleFactory.unregister()
     ModuleFactory.register('MockModuleContext', MockModuleContext)
     ModuleFactory.register('MockModuleData', MockModuleData)
     self.builder = SequentialPipeline()
     self.mod_c = MockModuleContext('mod_c')
     self.mod_d1 = MockModuleData('mod_d1')
     self.mod_d2 = MockModuleData('mod_d2')
예제 #5
0
    def test_factory_register_same_module_twice(self):
        assert len(ModuleFactory.module_references) == 0

        module_to_register = 'SameModuleName'
        ModuleFactory.register(module_to_register, MockModule)
        assert len(ModuleFactory.module_references) == 1

        ModuleFactory.register(module_to_register, MockModule)
        assert len(ModuleFactory.module_references) == 1
예제 #6
0
    def prepare_basic_hooks_test_modules_factory(self):
        @produce(MockTestInterface)
        @finalize
        class DependingModule(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        @accept(MockTestInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)
        ModuleFactory.register('DependingModule', DependingModule)
예제 #7
0
    async def test_should_raise_error_on_incorrect_name_in_config(self):
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        config_file = self.get_config_file(
            'wrong_type_of_pipeline_name_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            with pytest.raises(WrongParameterValueException):
                await ConfigReader.read(config, ModuleFactory)
    async def test_cannot_read_config_with_wrong_single_submodule_interface(
            self):
        @accept(MockCorrectDependingInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        config_file = self.get_config_file(
            'depending_modules_partially_invalid_interface.yaml')
        with open(config_file) as config:
            config = config.read()
            with pytest.raises(Exception):
                pipeline = await ConfigReader.read(config, ModuleFactory)
    async def test_exposing_under_the_same_name(self):
        @expose('a')
        @accept(MockModuleA)
        @finalize
        class MockModuleC(Module.Runtime):
            def run(self, **kwargs):
                pass

        ModuleFactory.register('MockModuleC', MockModuleC)
        builder = SequentialPipeline()
        builder.add_module(MockModuleA('m1'))
        builder.add_module(MockModuleC('m2'))

        with pytest.raises(Exception):
            pipeline = await builder.build()
예제 #10
0
    async def test_should_correctly_assign_pipeline_name_from_config(self):
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        config_file = self.get_config_file(
            'correct_pipeline_name_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory)

        assert len(pipeline.modules) == 1
        assert 'TestPipeline' == pipeline.name
예제 #11
0
    async def test_should_have_not_empty_pipeline_name_when_none_assigned(
            self):
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        config_file = self.get_config_file('no_pipeline_name_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory)

        assert len(pipeline.modules) == 1
        assert pipeline.name is not None
        assert 'Pipeline-' in pipeline.name
    def setup_method(self, method):
        @produce(MockCorrectDependingInterface)
        @finalize
        class ModuleDependingCorrect(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        @produce(MockIncorrectDependingInterface)
        @finalize
        class ModuleDependingIncorrect(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        ModuleFactory.unregister()
        ModuleFactory.register('ModuleDependingCorrect',
                               ModuleDependingCorrect)
        ModuleFactory.register('ModuleDependingIncorrect',
                               ModuleDependingIncorrect)
예제 #13
0
    async def test_should_correctly_assign_numeric_pipeline_name(self):
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        numeric_name = 5

        config_file = self.get_config_file('no_pipeline_name_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config,
                                               ModuleFactory,
                                               name=numeric_name)

        assert len(pipeline.modules) == 1
        assert pipeline.name == numeric_name
예제 #14
0
    async def test_should_correctly_override_assigned_pipeline_name(self):
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)

        overriding_name = 'OverridingName'

        config_file = self.get_config_file(
            'correct_pipeline_name_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config,
                                               ModuleFactory,
                                               name=overriding_name)

        assert len(pipeline.modules) == 1
        assert overriding_name == pipeline.name
    async def test_can_read_config_with_correct_submodule_interfaces(self):
        @accept(MockCorrectDependingInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        ModuleFactory.register('ModuleSample', ModuleSample)
        config_file = self.get_config_file(
            'depending_modules_of_correct_interface.yaml')
        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory)

        assert 3 == len(pipeline.modules)
        assert issubclass(pipeline.modules[0].interface,
                          MockCorrectDependingInterface)
        assert issubclass(pipeline.modules[1].interface,
                          MockCorrectDependingInterface)
        assert pipeline.modules[2].input_modules == ['mod1', 'mod2']
예제 #16
0
    async def test_should_raise_exception_on_duplicated_expose_values(self):
        @produce(MockTestInterface)
        @finalize
        class DependingModule(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        @accept(MockTestInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)
        ModuleFactory.register('DependingModule', DependingModule)

        config_file = self.get_config_file(
            'duplicated_exposed_values_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            with pytest.raises(Exception):
                await ConfigReader.read(config, ModuleFactory)
예제 #17
0
    async def test_should_correctly_access_exposed_results(self):
        @dataclass(frozen=True)
        class DataInterface(Module.Interface):
            data: str

        @produce(DataInterface)
        @expose('should-be-overridden')
        @finalize
        class DependingModule(Module.Runtime):
            def run(self, *args, **kwargs):
                return DataInterface('depending')

        @accept(DataInterface)
        @produce(DataInterface)
        @finalize
        class ModuleSample(Module.Runtime):
            def run(self, *args, **kwargs):
                return DataInterface('parent')

        ModuleFactory.register('ModuleSample', ModuleSample)
        ModuleFactory.register('DependingModule', DependingModule)

        config_file = self.get_config_file(
            'correctly_exposed_modules_for_results_check.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory)

        runtime, _ = await pipeline.run()

        assert 'depending-result' in runtime
        assert 'parent-result' in runtime
        assert 'should-be-overridden' not in runtime
        assert runtime['depending-result'].data == 'depending'
        assert runtime['parent-result'].data == 'parent'
예제 #18
0
    async def build(self, prefix: str = '{CTX}'):
        ModuleFactory.register('ModuleA', ModuleA)
        ModuleFactory.register('ModuleB', ModuleB)
        ModuleFactory.register('ModuleC', ModuleC)

        config_file = self.get_config_file('sample_config_parallel.yaml')
        with open(config_file, 'r') as config:
            config = config.read()
            self.pipeline = await ConfigReader.read(
                config,
                ModuleFactory,
                {'GROUP_M1': 'g1', 'GROUP_M4': 'g3'},
                context=lambda: Context(prefix)
            )
예제 #19
0
    async def build(self, prefix: str = '{CTX}'):
        ModuleFactory.register('ModuleA', ModuleA)
        ModuleFactory.register('ModuleD', ModuleD)
        ModuleFactory.register('ModuleC', ModuleC)

        config_file = self.get_config_file('sample_config_sequential.yaml')
        with open(config_file, 'r') as config:
            config = config.read()
            self.pipeline = await ConfigReader.read(
                config,
                ModuleFactory, {
                    'THRESHOLD_1': 0.2,
                    'THRESHOLD_2': 0.5
                },
                context=lambda: Context(prefix))
예제 #20
0
    async def test_should_overide_declared_expose_values(self):
        @produce(MockTestInterface)
        @expose('declared-depending-result')
        @finalize
        class DependingModule(Module.Runtime):
            def run(self, *args, **kwargs):
                pass

        @accept(MockTestInterface)
        @produce(MockTestInterface)
        @expose('declared-sample-result')
        @finalize
        class ModuleSample(Module.Runtime):
            pass

        @accept(MockTestInterface)
        @expose('should-remain-untouched')
        @finalize
        class ModuleSuccessor(Module.Runtime):
            pass

        ModuleFactory.register('ModuleSample', ModuleSample)
        ModuleFactory.register('DependingModule', DependingModule)
        ModuleFactory.register('ModuleSuccessor', ModuleSuccessor)

        config_file = self.get_config_file(
            'correctly_exposed_modules_in_config.yaml')

        with open(config_file) as config:
            config = config.read()
            pipeline = await ConfigReader.read(config, ModuleFactory)

        modules_exposed_dict = {
            module.name: module.exposed
            for module in pipeline.modules
        }

        assert len(pipeline.modules) == 4
        assert modules_exposed_dict['mod0'] == 'sample-result'
        assert modules_exposed_dict['mod1'] is None
        assert modules_exposed_dict['mod2'] == 'mod2'
        assert modules_exposed_dict['mod4'] == 'should-remain-untouched'
예제 #21
0
 def setup_method(self, method):
     ModuleFactory.unregister()
     ModuleFactory.register('MockModule', MockModule)
     self.pipeline = SequentialPipeline()
     self.context = MockContext()
 def test_cannot_register_interfaces(self):
     with pytest.raises(TypeError):
         ModuleFactory.unregister()
         ModuleFactory.register('MockCorrectDependingInterface',
                                MockCorrectDependingInterface)
예제 #23
0
 def setup_method(self, method):
     ModuleFactory.unregister()
     ModuleFactory.register('MockModuleA', MockModuleA)
     ModuleFactory.register('MockModuleB', MockModuleB)
예제 #24
0
    def test_factory_register(self):
        assert len(ModuleFactory.module_references) == 0

        ModuleFactory.register('MockModule', MockModule)
        assert len(ModuleFactory.module_references) == 1
예제 #25
0
 def test_remove_registered_module(self):
     assert len(ModuleFactory.module_references) == 0
     ModuleFactory.register('MockModule', MockModule)
     assert len(ModuleFactory.module_references) == 1
     ModuleFactory.unregister('MockModule')
     assert len(ModuleFactory.module_references) == 0
예제 #26
0
 def wrapper(module_ref):
     module_typeguard(module_ref)
     ModuleFactory.register(module_type, module_ref)
     return module_ref
예제 #27
0
 def test_nonstring_tag(self):
     with pytest.raises(TypeError):
         ModuleFactory.register(MockModule, MockModule)