コード例 #1
0
    def test_migrate_params_impl(self):
        def migrate_params(params):
            return {"x": params["a"], "y": params["b"]}

        lm = LoadedModule(
            "x",
            "1",
            ParamDType.Dict({"x": ParamDType.Integer(), "y": ParamDType.Integer()}),
            migrate_params_impl=migrate_params,
        )
        result = lm.migrate_params({"a": 1, "b": 2})
        self.assertEqual(result, {"x": 1, "y": 2})
コード例 #2
0
    def test_migrate_params_impl(self):
        def migrate_params(params):
            return {'x': params['a'], 'y': params['b']}

        lm = LoadedModule(
            'x',
            '1',
            ParamDType.Dict({
                'x': ParamDType.Integer(),
                'y': ParamDType.Integer(),
            }),
            migrate_params_impl=migrate_params,
        )
        result = lm.migrate_params({'a': 1, 'b': 2})
        self.assertEqual(result, {'x': 1, 'y': 2})
コード例 #3
0
    def test_change_parameters_deny_invalid_params(self, load_module):
        workflow = Workflow.create_and_init()
        wf_module = workflow.tabs.first().wf_modules.create(
            order=0,
            module_id_name='x',
            last_relevant_delta_id=workflow.last_delta_id,
            params={'x': 1}
        )

        ModuleVersion.create_or_replace_from_spec({
            'id_name': 'x', 'name': 'x', 'category': 'Clean',
            'parameters': [
                {'id_name': 'x', 'type': 'integer'},
            ]
        })
        load_module.return_value = LoadedModule('x', '1', ParamDType.Dict({
            'x': ParamDType.Integer(),
        }), migrate_params_impl=lambda x: x)

        with self.assertRaises(ValueError):
            # Now the user requests to change params, giving an invalid param.
            self.run_with_async_db(ChangeParametersCommand.create(
                workflow=workflow,
                wf_module=wf_module,
                new_values={'x': 'Threeve'}
            ))
コード例 #4
0
    def test_list_dtype(self):
        # Check that ParamSpec's with List type produce correct nested DTypes
        param_spec = ParamSpec.from_dict(
            dict(id_name='p',
                 type='list',
                 child_parameters=[
                     {
                         'id_name': 'intparam',
                         'type': 'integer',
                         'name': 'my number'
                     },
                     {
                         'id_name': 'colparam',
                         'type': 'column',
                         'name': 'my column'
                     },
                 ]))
        self.assertEqual(
            param_spec,
            ParamSpec.List(id_name='p',
                           child_parameters=[
                               ParamSpec.Integer(id_name='intparam',
                                                 name='my number'),
                               ParamSpec.Column(id_name='colparam',
                                                name='my column'),
                           ]))
        dtype = param_spec.dtype
        expected_dtype = DT.List(
            DT.Dict({
                'intparam': DT.Integer(),
                'colparam': DT.Column(),
            }))

        # effectively do a deep compare with repr
        self.assertEqual(repr(dtype), repr(expected_dtype))
コード例 #5
0
    def test_change_parameters_deny_invalid_params(self, load_module):
        workflow = Workflow.create_and_init()
        wf_module = workflow.tabs.first().wf_modules.create(
            order=0,
            slug="step-1",
            module_id_name="x",
            last_relevant_delta_id=workflow.last_delta_id,
            params={"x": 1},
        )

        ModuleVersion.create_or_replace_from_spec({
            "id_name":
            "x",
            "name":
            "x",
            "category":
            "Clean",
            "parameters": [{
                "id_name": "x",
                "type": "integer"
            }],
        })
        load_module.return_value = LoadedModule(
            "x",
            "1",
            ParamDType.Dict({"x": ParamDType.Integer()}),
            migrate_params_impl=lambda x: x,
        )

        with self.assertRaises(ValueError):
            # Now the user requests to change params, giving an invalid param.
            self.run_with_async_db(
                ChangeParametersCommand.create(workflow=workflow,
                                               wf_module=wf_module,
                                               new_values={"x": "Threeve"}))
コード例 #6
0
    def test_change_parameters_across_module_versions(self, load_module):
        workflow = Workflow.create_and_init()

        # Initialize a WfModule that used module 'x' version '1' (which we
        # don't need to write in code -- after all, that version might be long
        # gone when ChangeParametersCommand is called.
        wf_module = workflow.tabs.first().wf_modules.create(
            order=0,
            module_id_name='x',
            last_relevant_delta_id=workflow.last_delta_id,
            params={'version': 'v1', 'x': 1}  # version-'1' params
        )

        # Now install version '2' of module 'x'.
        #
        # Version '2''s migrate_params() could do anything; in this test, it
        # simply changes 'version' from 'v1' to 'v2'
        ModuleVersion.create_or_replace_from_spec({
            'id_name': 'x', 'name': 'x', 'category': 'Clean',
            'parameters': [
                {'id_name': 'version', 'type': 'string'},
                {'id_name': 'x', 'type': 'integer'},
            ]
        }, source_version_hash='2')
        load_module.return_value = LoadedModule(
            'x',
            '2',
            ParamDType.Dict({
                'version': ParamDType.String(),
                'x': ParamDType.Integer(),
            }),
            migrate_params_impl=lambda params: {**params, 'version': 'v2'}
        )

        # Now the user requests to change params.
        #
        # The user was _viewing_ version '2' of module 'x', though
        # `wf_module.params` was at version 1. (Workbench ran
        # `migrate_params()` without saving the result when it
        # presented `params` to the user.) So the changes should apply atop
        # _migrated_ params.
        cmd = self.run_with_async_db(ChangeParametersCommand.create(
            workflow=workflow,
            wf_module=wf_module,
            new_values={'x': 2}
        ))
        self.assertEqual(wf_module.params, {
            'version': 'v2',  # migrate_params() ran
            'x': 2,  # and we applied changes on top of its output
        })

        self.run_with_async_db(cmd.backward())
        self.assertEqual(wf_module.params, {
            'version': 'v1',  # exactly what we had before
            'x': 1,
        })
コード例 #7
0
 def test_migrate_params_default(self):
     lm = LoadedModule(
         'x',
         '1',
         ParamDType.Dict({
             'missing': ParamDType.String(default='x'),
             'wrong_type': ParamDType.Boolean(),
             'ok': ParamDType.Integer(),
         }),
         migrate_params_impl=None,
     )
     result = lm.migrate_params({'wrong_type': 'true', 'ok': 3})
     self.assertEqual(result, {'missing': 'x', 'wrong_type': True, 'ok': 3})
コード例 #8
0
 def test_migrate_params_default(self):
     lm = LoadedModule(
         "x",
         "1",
         ParamDType.Dict(
             {
                 "missing": ParamDType.String(default="x"),
                 "wrong_type": ParamDType.Boolean(),
                 "ok": ParamDType.Integer(),
             }
         ),
         migrate_params_impl=None,
     )
     result = lm.migrate_params({"wrong_type": "true", "ok": 3})
     self.assertEqual(result, {"missing": "x", "wrong_type": True, "ok": 3})