def test_default_init(self):
     """
     A plugin with no context passed in should have sane defaults.
     """
     vis_dir = galaxy_mock.MockDir({
         'config': {
             'vis1.xml': ''
         },
         'static': {},
         'templates': {},
     })
     plugin = self.plugin_class(galaxy_mock.MockApp(), vis_dir.root_path,
                                'myvis', {})
     self.assertEqual(plugin.name, 'myvis')
     self.assertEqual(plugin.path, vis_dir.root_path)
     self.assertEqual(plugin.config, {})
     self.assertEqual(plugin.base_url, 'myvis')
     # template
     self.assertTrue(plugin.serves_templates)
     self.assertEqual(plugin.template_path,
                      vis_dir.root_path + '/templates')
     self.assertEqual(plugin.template_lookup.__class__.__name__,
                      'TemplateLookup')
     # resource parser
     self.assertIsInstance(plugin.resource_parser,
                           resource_parser.ResourceParser)
示例#2
0
 def _new_trans(self, allowed_origin_hostnames=None):
     app = galaxy_mock.MockApp()
     app.config = CORSParsingMockConfig(
         allowed_origin_hostnames=allowed_origin_hostnames)
     webapp = galaxy_mock.MockWebapp(app.security)
     environ = galaxy_mock.buildMockEnviron()
     trans = StubGalaxyWebTransaction(environ, app, webapp)
     return trans
 def test_init_without_static_or_templates(self):
     """
     A plugin that has neither template or static directory should serve neither.
     """
     vis_dir = galaxy_mock.MockDir({'config': {'vis1.xml': ''}})
     plugin = self.plugin_class(galaxy_mock.MockApp(), vis_dir.root_path,
                                'myvis', dict())
     self.assertFalse(plugin.serves_templates)
    def test_build_config(self):
        """
        """
        plugin_config: dict = dict()
        plugin = self.plugin_class(galaxy_mock.MockApp(), '', 'myvis',
                                   plugin_config)
        config = plugin._build_config({})
        self.assertIsInstance(config, vis_utils.OpenObject)
        self.assertEqual(config.__dict__, {})

        # existing should flow through
        plugin_config = dict()
        plugin = self.plugin_class(galaxy_mock.MockApp(), '', 'myvis',
                                   plugin_config)
        existing_config = dict(wat=1)
        config = plugin._build_config(existing_config)
        self.assertEqual(config.wat, 1)

        # unlisted/non-param kwargs should NOT overwrite existing
        plugin_config = dict()
        plugin = self.plugin_class(galaxy_mock.MockApp(), '', 'myvis',
                                   plugin_config)
        existing_config = dict(wat=1)
        config = plugin._build_config(existing_config, wat=2)
        self.assertEqual(config.wat, 1)

        # listed/param kwargs *should* overwrite existing
        plugin_config = dict(params=dict(wat={
            'csv': False,
            'required': False,
            'type': 'int',
            'var_name_in_template': 'wot'
        }, ))
        plugin = self.plugin_class(galaxy_mock.MockApp(), '', 'myvis',
                                   plugin_config)
        existing_config = dict(wat=1)
        # send as string like a query would - should be parsed
        config = plugin._build_config(existing_config, wat='2')
        self.assertEqual(config.wat, 2)
    def test_build_render_vars_default(self):
        """
        Render vars passed to render should default properly.
        """
        # well, that's kind of a lot of typing to say nothing new
        config = dict(name='Cat Fancy Magazine\'s Genomic Visualization')
        plugin = self.plugin_class(galaxy_mock.MockApp(), '', 'myvis', config)

        render_vars = plugin._build_render_vars(config)
        self.assertEqual(render_vars['visualization_name'], plugin.name)
        self.assertEqual(render_vars['visualization_display_name'],
                         plugin.config['name'])
        self.assertEqual(render_vars['title'], None)
        self.assertEqual(render_vars['saved_visualization'], None)
        self.assertEqual(render_vars['visualization_id'], None)
        self.assertEqual(render_vars['query'], {})
        self.assertIsInstance(render_vars['config'], vis_utils.OpenObject)
        self.assertEqual(render_vars['config'].__dict__, {})
    def test_render(self):
        """
        """
        # use the python in a template to test for variables that should be there
        # TODO: gotta be a better way
        testing_template = clean_multiline_string("""\
        <%
            found_all = True
            should_have = [
                title, visualization_name, visualization_display_name,
                visualization_id, saved_visualization,
                query, config,
                embedded,
                vars
            ]
            for var in should_have:
                try:
                    var = str( var )
                except NameError as name_err:
                    found_all = False
                    break
        %>
        ${ found_all }
        """)

        mock_app_dir = galaxy_mock.MockDir({
            'cache': {},
            'template.mako': testing_template
        })
        mock_app = galaxy_mock.MockApp(root=mock_app_dir.root_path)
        plugin = self.plugin_class(mock_app, '', 'myvis',
                                   {"name": "Vlad News Bears"})

        # somewhat easier to set this up by hand
        plugin.config['entry_point'] = {'file': 'template.mako'}
        plugin.template_path = mock_app_dir.root_path
        plugin.template_lookup = plugin._build_template_lookup(
            mock_app_dir.root_path)

        response = plugin.render(trans=galaxy_mock.MockTrans(app=mock_app))
        self.assertIsInstance(response, str)
        self.assertEqual(response.strip(), "True")
    def test_script_entry(self):
        """"""
        script_entry_config = clean_multiline_string("""\
        <?xml version="1.0" encoding="UTF-8"?>
        <visualization name="js-test">
            <data_sources>
                <data_source>
                    <model_class>HistoryDatasetAssociation</model_class>
                </data_source>
            </data_sources>
            <entry_point entry_point_type="script" data-main="one" src="bler"></entry_point>
        </visualization>
        """)

        mock_app_dir = galaxy_mock.MockDir({
            'plugins': {
                'jstest': {
                    'config': {
                        'jstest.xml': script_entry_config
                    },
                    'static': {}
                },
            }
        })
        mock_app = galaxy_mock.MockApp(root=mock_app_dir.root_path)
        plugin_mgr = VisualizationsRegistry(mock_app,
            directories_setting='plugins',
            template_cache_dir=template_cache_dir)
        script_entry = plugin_mgr.plugins['jstest']

        self.assertIsInstance(script_entry, plugin.ScriptVisualizationPlugin)
        self.assertEqual(script_entry.name, 'jstest')
        self.assertTrue(script_entry.serves_templates)

        trans = galaxy_mock.MockTrans()
        script_entry._set_up_template_plugin(mock_app_dir.root_path, [addtional_templates_dir])
        response = script_entry._render({}, trans=trans, embedded=True)
        self.assertTrue('src="bler"' in response)
        self.assertTrue('type="text/javascript"' in response)
        self.assertTrue('data-main="one"' in response)
        mock_app_dir.remove()
 def test_init_with_context(self):
     """
     A plugin with context passed in should use those in it's set up.
     """
     vis_dir = galaxy_mock.MockDir({
         'config': {
             'vis1.xml': ''
         },
         'static': {},
         'templates': {},
     })
     context = dict(base_url='u/wot/m8',
                    template_cache_dir='template_cache',
                    additional_template_paths=['one'])
     plugin = self.plugin_class(galaxy_mock.MockApp(),
                                vis_dir.root_path,
                                'myvis', {},
                                context=context)
     self.assertEqual(plugin.base_url, 'u/wot/m8/myvis')
     self.assertEqual(plugin.template_lookup.__class__.__name__,
                      'TemplateLookup')
    def test_plugin_load_from_repo(self):
        """should attempt load if criteria met"""
        mock_app = galaxy_mock.MockApp(root=glx_dir)
        plugin_mgr = VisualizationsRegistry(mock_app,
            directories_setting=vis_reg_path,
            template_cache_dir=None)

        expected_plugins_path = os.path.join(glx_dir, vis_reg_path)
        self.assertEqual(plugin_mgr.base_url, 'visualizations')
        self.assertEqual(plugin_mgr.directories, [expected_plugins_path])

        scatterplot = plugin_mgr.plugins['scatterplot']
        self.assertEqual(scatterplot.name, 'scatterplot')
        self.assertEqual(scatterplot.path, os.path.join(expected_plugins_path, 'scatterplot'))
        self.assertEqual(scatterplot.base_url, '/'.join((plugin_mgr.base_url, scatterplot.name)))
        self.assertTrue(scatterplot.serves_templates)
        self.assertEqual(scatterplot.template_path, os.path.join(scatterplot.path, 'templates'))
        self.assertEqual(scatterplot.template_lookup.__class__.__name__, 'TemplateLookup')

        trackster = plugin_mgr.plugins['trackster']
        self.assertEqual(trackster.name, 'trackster')
        self.assertEqual(trackster.path, os.path.join(expected_plugins_path, 'trackster'))
        self.assertEqual(trackster.base_url, '/'.join((plugin_mgr.base_url, trackster.name)))
        self.assertFalse(trackster.serves_templates)
    def test_plugin_load(self):
        """"""
        mock_app_dir = galaxy_mock.MockDir({
            'plugins': {
                'vis1': {
                    'config': {
                        'vis1.xml': config1
                    },
                    'static': {},
                    'templates': {},
                },
                'vis2': {
                    'config': {
                        'vis2.xml': config1
                    }
                },
                'not_a_vis1': {
                    'config': {
                        'vis1.xml': 'blerbler'
                    },
                },
                # empty
                'not_a_vis2': {},
                'not_a_vis3': 'blerbler',
                # bad config
                'not_a_vis4': {
                    'config': {
                        'not_a_vis4.xml': 'blerbler'
                    }
                },
                'not_a_vis5': {
                    # no config
                    'static': {},
                    'templates': {},
                },
            }
        })
        mock_app = galaxy_mock.MockApp(root=mock_app_dir.root_path)
        plugin_mgr = VisualizationsRegistry(mock_app,
            directories_setting='plugins',
            template_cache_dir=template_cache_dir)

        expected_plugins_path = os.path.join(mock_app_dir.root_path, 'plugins')
        expected_plugin_names = ['vis1', 'vis2']

        self.assertEqual(plugin_mgr.base_url, 'visualizations')
        self.assertEqual(plugin_mgr.directories, [expected_plugins_path])
        self.assertEqual(sorted(plugin_mgr.plugins.keys()), expected_plugin_names)

        vis1 = plugin_mgr.plugins['vis1']
        self.assertEqual(vis1.name, 'vis1')
        self.assertEqual(vis1.path, os.path.join(expected_plugins_path, 'vis1'))
        self.assertEqual(vis1.base_url, '/'.join((plugin_mgr.base_url, vis1.name)))
        self.assertTrue(vis1.serves_templates)
        self.assertEqual(vis1.template_path, os.path.join(vis1.path, 'templates'))
        self.assertEqual(vis1.template_lookup.__class__.__name__, 'TemplateLookup')

        vis1_as_dict = vis1.to_dict()
        assert vis1_as_dict["specs"]
        specs = vis1_as_dict["specs"]
        assert "exports" in specs
        exports = specs["exports"]
        assert len(exports) == 3
        assert "png" in exports
        assert "svg" in exports
        assert "pdf" in exports

        vis2 = plugin_mgr.plugins['vis2']
        self.assertEqual(vis2.name, 'vis2')
        self.assertEqual(vis2.path, os.path.join(expected_plugins_path, 'vis2'))
        self.assertEqual(vis2.base_url, '/'.join((plugin_mgr.base_url, vis2.name)))
        self.assertFalse(vis2.serves_templates)

        mock_app_dir.remove()
        template_cache_dir
    def test_interactive_environ_plugin_load(self):
        """
        """
        jupyter_config = clean_multiline_string("""\
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE interactive_environment SYSTEM "../../interactive_environments.dtd">
        <interactive_environment name="Jupyter">
            <data_sources>
                <data_source>
                    <model_class>HistoryDatasetAssociation</model_class>
                    <test type="isinstance" test_attr="datatype" result_type="datatype">tabular.Tabular</test>
                    <test type="isinstance" test_attr="datatype" result_type="datatype">data.Text</test>
                    <to_param param_attr="id">dataset_id</to_param>
                </data_source>
            </data_sources>
            <params>
                <param type="dataset" var_name_in_template="hda" required="true">dataset_id</param>
            </params>
            <template>jupyter.mako</template>
        </interactive_environment>
        """)

        templates: Dict[str, str] = {}
        mock_app_dir_config = {
            'plugins': {
                'jupyter': {
                    'config': {
                        'jupyter.xml': jupyter_config
                    },
                    'templates': templates,
                },
            },
        }

        # going to use a fake template here to simplify testing
        jupyter_template = "${ ie_request }-${ get_api_key() }"
        templates['jupyter.mako'] = jupyter_template
        # so that we don't create a cached version of that fake template in the real mako caches
        #   we'll set up a cache in the temp dir
        mock_app_dir_config['caches'] = {}
        # and make sure the vis reg uses that
        mock_app_dir = galaxy_mock.MockDir(mock_app_dir_config)
        mock_app = galaxy_mock.MockApp(root=mock_app_dir.root_path)
        plugin_mgr = VisualizationsRegistry(mock_app,
            directories_setting='plugins',
            template_cache_dir=os.path.join(mock_app_dir.root_path, 'caches'))

        # ...then start testing
        expected_plugins_path = os.path.join(mock_app_dir.root_path, 'plugins')
        expected_plugin_names = ['jupyter']

        self.assertEqual(plugin_mgr.base_url, 'visualizations')
        self.assertEqual(plugin_mgr.directories, [expected_plugins_path])
        self.assertEqual(sorted(plugin_mgr.plugins.keys()), expected_plugin_names)

        jupyter = plugin_mgr.plugins['jupyter']
        config = jupyter.config

        self.assertEqual(jupyter.name, 'jupyter')
        self.assertEqual(config.get('plugin_type'), 'interactive_environment')

        # get_api_key needs a user, fill_template a trans
        user = model.User(email="*****@*****.**", password="******")
        trans = galaxy_mock.MockTrans(user=user)
        # use a mock request factory - this will be written into the filled template to show it was used
        jupyter.INTENV_REQUEST_FACTORY = lambda t, p: 'mock'

        # should return the (new) api key for the above user (see the template above)
        response = jupyter._render({}, trans=trans)
        response.strip()
        self.assertIsInstance(response, str)
        self.assertTrue('-' in response)
        ie_request, api_key = response.split('-')

        self.assertEqual(ie_request, 'mock')

        match = re.match(r'[a-f0-9]{32}', api_key)
        assert match
        self.assertEqual(match.span(), (0, 32))

        mock_app_dir.remove()
示例#12
0
def create_base_test(connection, amqp_type, amqp_connection=None):
    app = galaxy_mock.MockApp(database_connection=connection)
    app.config.database_connection = connection
    app.config.amqp_internal_connection = amqp_connection or "sqlalchemy+%s" % app.config.database_connection
    app.amqp_type = amqp_type
    return app