Esempio n. 1
0
 def test_migrate_params_impl_wrong_output_type(self):
     lm = LoadedModule('x',
                       '1',
                       ParamDType.Dict({'x': ParamDType.String()}),
                       migrate_params_impl=lambda x: x)
     with self.assertRaises(ValueError):
         # should be str
         lm.migrate_params({'x': 2})
Esempio n. 2
0
 def test_migrate_params_impl_missing_output(self):
     lm = LoadedModule('x',
                       '1',
                       ParamDType.Dict({'x': ParamDType.String()}),
                       migrate_params_impl=lambda x: x)
     with self.assertRaises(ValueError):
         # should have 'x' key
         lm.migrate_params({})
    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,
        })
Esempio n. 4
0
    def test_fetch_get_params(self):
        workflow = Workflow.objects.create()
        tab = workflow.tabs.create(position=0)
        wf_module = tab.wf_modules.create(order=0, params={'foo': 'bar'})

        async def fetch(params, **kwargs):
            self.assertEqual(params, {'foo': 'bar'})

        self._test_fetch(fetch, DefaultMigrateParams, wf_module,
                         ParamDType.Dict({'foo': ParamDType.String()}))
Esempio n. 5
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})
Esempio n. 6
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})