Esempio n. 1
0
def initialize(introspected_modules, load_xml_events=True):
    """
    Initialize when Application is starting.
    Check for new algorithms or new DataTypes.
    """
    SettingsService().check_db_url(TvbProfile.current.db.DB_URL)

    ## Initialize DB
    is_db_empty = initialize_startup()

    ## Create Projects storage root in case it does not exist.
    initialize_storage()

    ## Populate DB algorithms, by introspection
    event_folders = []
    start_introspection_time = datetime.datetime.now()
    for module in introspected_modules:
        introspector = Introspector(module)
        # Introspection is always done, even if DB was not empty.
        introspector.introspect(True)
        event_path = introspector.get_events_path()
        if event_path:
            event_folders.append(event_path)

    # Now remove or mark as removed any unverified Algo-Group, Algo-Category or Portlet
    to_invalidate, to_remove = dao.get_non_validated_entities(
        start_introspection_time)
    for entity in to_invalidate:
        entity.removed = True
    dao.store_entities(to_invalidate)
    for entity in to_remove:
        dao.remove_entity(entity.__class__, entity.id)

    ## Populate events
    if load_xml_events:
        read_events(event_folders)

    if not TvbProfile.is_first_run():
        ## Create default users.
        if is_db_empty:
            dao.store_entity(
                model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None,
                           None, True, None))
            UserService().create_user(
                username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME,
                password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD,
                email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL,
                role=model.ROLE_ADMINISTRATOR)

        ## In case actions related to latest code-changes are needed, make sure they are executed.
        CodeUpdateManager().run_all_updates()

        ## In case the H5 version changed, run updates on all DataTypes
        if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION:
            thread = threading.Thread(
                target=FilesUpdateManager().run_all_updates)
            thread.start()

        ## Clean tvb-first-time-run temporary folder, as we are no longer at the first run:
        shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True)
    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)
 def setUp(self):
     """
     Reset the database before each test.
     """
     self.reset_database()
     #tvb_test path
     core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     self.old_path = cfg.CURRENT_DIR
     cfg.CURRENT_DIR = os.path.dirname(core_path)
     self.introspector = Introspector("tvb_test")
Esempio n. 4
0
    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join("core", "adapters")]

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)
Esempio n. 5
0
def initialize(introspected_modules, load_xml_events=True):
    """
    Initialize when Application is starting.
    Check for new algorithms or new DataTypes.
    """
    SettingsService().check_db_url(TvbProfile.current.db.DB_URL)
    
    ## Initialize DB
    is_db_empty = initialize_startup()
    
    ## Create Projects storage root in case it does not exist.
    initialize_storage()
    
    ## Populate DB algorithms, by introspection
    event_folders = []
    start_introspection_time = datetime.datetime.now()
    for module in introspected_modules:
        introspector = Introspector(module)
        # Introspection is always done, even if DB was not empty.
        introspector.introspect(True)
        event_path = introspector.get_events_path()
        if event_path:
            event_folders.append(event_path)

    # Now remove or mark as removed any unverified Algo-Group, Algo-Category or Portlet
    to_invalidate, to_remove = dao.get_non_validated_entities(start_introspection_time)
    for entity in to_invalidate:
        entity.removed = True
    dao.store_entities(to_invalidate)
    for entity in to_remove:
        dao.remove_entity(entity.__class__, entity.id)
   
    ## Populate events
    if load_xml_events:
        read_events(event_folders)

    if not TvbProfile.is_first_run():
        ## Create default users.
        if is_db_empty:
            dao.store_entity(model.User(TvbProfile.current.web.admin.SYSTEM_USER_NAME, None, None, True, None))
            UserService().create_user(username=TvbProfile.current.web.admin.ADMINISTRATOR_NAME,
                                      password=TvbProfile.current.web.admin.ADMINISTRATOR_PASSWORD,
                                      email=TvbProfile.current.web.admin.ADMINISTRATOR_EMAIL,
                                      role=model.ROLE_ADMINISTRATOR)
        
        ## In case actions related to latest code-changes are needed, make sure they are executed.
        CodeUpdateManager().run_all_updates()

        ## In case the H5 version changed, run updates on all DataTypes
        if TvbProfile.current.version.DATA_CHECKED_TO_VERSION < TvbProfile.current.version.DATA_VERSION:
            thread = threading.Thread(target=FilesUpdateManager().run_all_updates)
            thread.start()

        ## Clean tvb-first-time-run temporary folder, as we are no longer at the first run:
        shutil.rmtree(TvbProfile.current.FIRST_RUN_STORAGE, True)
class IntrospectorTest(BaseTestCase):
    """
    Test class for the introspection module.
    """
    old_current_dir = TvbProfile.current.web.CURRENT_DIR

    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)

    def tearDown(self):
        """
        Revert changes settings and remove recently imported algorithms
        """
        TvbProfile.current.web.CURRENT_DIR = self.old_current_dir

    def test_introspect(self):
        """
        Test that expected categories and groups are found in DB after introspection.
        We also check algorithms introspected during base_testcase.init_test_env
        """

        all_categories = dao.get_algorithm_categories()
        category_ids = [
            cat.id for cat in all_categories
            if cat.displayname == "AdaptersTest"
        ]
        adapters = dao.get_adapters_from_categories(category_ids)
        self.assertEqual(8, len(adapters), "Introspection failed!")
        nr_adapters_mod2 = 0
        for algorithm in adapters:
            self.assertTrue(
                algorithm.module in [
                    'tvb.tests.framework.adapters.testadapter1',
                    'tvb.tests.framework.adapters.testadapter2',
                    'tvb.tests.framework.adapters.testadapter3',
                    'tvb.tests.framework.adapters.ndimensionarrayadapter',
                    'tvb.tests.framework.adapters.testgroupadapter'
                ], "Unknown Adapter module:" + str(algorithm.module))
            self.assertTrue(
                algorithm.classname in [
                    "TestAdapter1", "TestAdapterDatatypeInput", "TestAdapter2",
                    "TestAdapter22", "TestAdapterHugeMemoryRequired",
                    "TestAdapter3", "TestAdapterHDDRequired",
                    "NDimensionArrayAdapter"
                ], "Unknown Adapter Class:" + str(algorithm.classname))
            if algorithm.module == 'tvb.tests.framework.adapters.testadapter2':
                nr_adapters_mod2 += 1
        self.assertEqual(nr_adapters_mod2, 2)
class IntrospectorTest(BaseTestCase):
    """
    Test class for the introspection module.
    """
    old_current_dir = TvbProfile.current.web.CURRENT_DIR


    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)
        
        
    def tearDown(self):
        """
        Revert changes settings and remove recently imported algorithms
        """
        TvbProfile.current.web.CURRENT_DIR = self.old_current_dir


    def test_introspect(self):
        """
        Test that expected categories and groups are found in DB after introspection.
        We also check algorithms introspected during base_testcase.init_test_env
        """
        
        all_categories = dao.get_algorithm_categories()
        category_ids = [cat.id for cat in all_categories if cat.displayname == "AdaptersTest"]
        adapters = dao.get_adapters_from_categories(category_ids)
        self.assertEqual(8, len(adapters), "Introspection failed!")
        nr_adapters_mod2 = 0
        for algorithm in adapters:
            self.assertTrue(algorithm.module in ['tvb.tests.framework.adapters.testadapter1',
                                                 'tvb.tests.framework.adapters.testadapter2',
                                                 'tvb.tests.framework.adapters.testadapter3',
                                                 'tvb.tests.framework.adapters.ndimensionarrayadapter',
                                                 'tvb.tests.framework.adapters.testgroupadapter'],
                            "Unknown Adapter module:" + str(algorithm.module))
            self.assertTrue(algorithm.classname in ["TestAdapter1", "TestAdapterDatatypeInput",
                                                    "TestAdapter2", "TestAdapter22", "TestAdapterHugeMemoryRequired",
                                                    "TestAdapter3", "TestAdapterHDDRequired",
                                                    "NDimensionArrayAdapter"
                                                    ],
                            "Unknown Adapter Class:" + str(algorithm.classname))
            if algorithm.module == 'tvb.tests.framework.adapters.testadapter2':
                nr_adapters_mod2 += 1
        self.assertEqual(nr_adapters_mod2, 2)
Esempio n. 8
0
 def setUp(self):
     """
     This method sets up the necessary paths for testing.
     """
     self.folder_path = os.path.dirname(__file__)
     #tvb_test path
     core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     self.old_path = cfg.CURRENT_DIR
     cfg.CURRENT_DIR = os.path.dirname(core_path)
     adapters_init.__xml_folders__ = [os.path.join('core', 'adapters')]
     self.introspector = Introspector("tvb_test")
     self.introspector.introspect(True)
     xml_group_path = os.path.join('core', 'adapters', "test_group.xml")
     algo_group = dao.find_group('tvb_test.adapters.testgroupadapter', 'TestGroupAdapter', xml_group_path)
     self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
     self.xml_group_reader = self.xml_group_adapter.xml_reader
Esempio n. 9
0
def initialize(introspected_modules, load_xml_events=True):
    """
    Initialize when Application is starting.
    Check for new algorithms or new DataTypes.
    """
    SettingsService().check_db_url(cfg.DB_URL)

    ## Initialize DB
    is_db_empty = initialize_startup()

    ## Create Projects storage root in case it does not exist.
    initialize_storage()

    ## Populate DB algorithms, by introspection
    event_folders = []
    start_introspection_time = datetime.datetime.now()
    for module in introspected_modules:
        introspector = Introspector(module)
        # Introspection is always done, even if DB was not empty.
        introspector.introspect(True)
        event_path = introspector.get_events_path()
        if event_path:
            event_folders.append(event_path)
    # Now remove any unverified Algo-Groups, categories or Portlets
    invalid_stored_entities = dao.get_non_validated_entities(
        start_introspection_time)
    for entity in invalid_stored_entities:
        dao.remove_entity(entity.__class__, entity.id)

    ## Populate events
    if load_xml_events:
        eventhandler.read_events(event_folders)

    ## Make sure DB events are linked.
    db_events.attach_db_events()

    ## Create default users.
    if is_db_empty:
        dao.store_entity(
            model.User(cfg.SYSTEM_USER_NAME, None, None, True, None))
        UserService().create_user(username=cfg.ADMINISTRATOR_NAME,
                                  password=cfg.ADMINISTRATOR_PASSWORD,
                                  email=cfg.ADMINISTRATOR_EMAIL,
                                  role=ROLE_ADMINISTRATOR)

    ## In case actions related to latest code-changes are needed, make sure they are executed.
    CodeUpdateManager().update_all()
Esempio n. 10
0
    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)
def initialize(introspected_modules, load_xml_events=True):
    """
    Initialize when Application is starting.
    Check for new algorithms or new DataTypes.
    """
    SettingsService().check_db_url(cfg.DB_URL)
    
    ## Initialize DB
    is_db_empty = initialize_startup()
    
    ## Create Projects storage root in case it does not exist.
    initialize_storage()
    
    ## Populate DB algorithms, by introspection
    event_folders = []
    start_introspection_time = datetime.datetime.now()
    for module in introspected_modules:
        introspector = Introspector(module)
        # Introspection is always done, even if DB was not empty.
        introspector.introspect(True)
        event_path = introspector.get_events_path()
        if event_path:
            event_folders.append(event_path)
    # Now remove any unverified Algo-Groups, categories or Portlets
    invalid_stored_entities = dao.get_non_validated_entities(start_introspection_time)
    for entity in invalid_stored_entities:
        dao.remove_entity(entity.__class__, entity.id)
   
    ## Populate events
    if load_xml_events:
        read_events(event_folders)
    
    ## Make sure DB events are linked.
    db_events.attach_db_events()
    
    ## Create default users.
    if is_db_empty:
        dao.store_entity(model.User(cfg.SYSTEM_USER_NAME, None, None, True, None))
        UserService().create_user(username=cfg.ADMINISTRATOR_NAME, password=cfg.ADMINISTRATOR_PASSWORD,
                                  email=cfg.ADMINISTRATOR_EMAIL, role=model.ROLE_ADMINISTRATOR)
        
    ## In case actions related to latest code-changes are needed, make sure they are executed.
    CodeUpdateManager().update_all()
Esempio n. 12
0
    def setUp(self):
        """
        Set up any additionally needed parameters.
        """
        super(GenshiTestGroup, self).setUp()

        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.old_path = cfg.CURRENT_DIR
        cfg.CURRENT_DIR = os.path.dirname(core_path)

        adapters_init.__xml_folders__ = [os.path.join('interfaces', 'web')]
        self.introspector = Introspector("tvb_test")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('interfaces', 'web', "test_group.xml")
        algo_group = dao.find_group('tvb_test.adapters.testgroupadapter',
                                    'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        input_tree = self.xml_group_adapter.get_input_tree()
        input_tree = ABCAdapter.prepare_param_names(input_tree)
        self.template_specification['inputList'] = input_tree
        self.template_specification[base.KEY_PARAMETERS_CONFIG] = False
        resulted_html = _template2string(self.template_specification)
        self.soup = BeautifulSoup(resulted_html)
    def setUp(self):
        """
        This method sets up the necessary paths for testing.
        """
        self.folder_path = os.path.dirname(__file__)

        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join('core', 'adapters')]

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('core', 'adapters', "test_group.xml")
        algo_group = dao.find_group('tvb.tests.framework.adapters.testgroupadapter', 'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        self.xml_group_reader = self.xml_group_adapter.xml_reader
Esempio n. 14
0
class IntrospectorTest(BaseTestCase):
    """
    Test class for the introspection module.
    """
    old_current_dir = TvbProfile.current.web.CURRENT_DIR
    old_xml_path = adapters_init.__xml_folders__


    def setUp(self):
        """
        Introspect supplementary folder:
        """
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join("core", "adapters")]

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)
        
        
    def tearDown(self):
        """
        Revert changes settings and remove recently imported algorithms
        """
        TvbProfile.current.web.CURRENT_DIR = self.old_current_dir
        adapters_init.__xml_folders__ = self.old_xml_path

        for group in dao.get_generic_entity(model.AlgorithmGroup, "simple", "algorithm_param_name"):
            dao.remove_entity(model.AlgorithmGroup, group.id)


    def test_introspect(self):
        """
        Test that expected categories and groups are found in DB after introspection.
        We also check algorithms introspected during base_testcase.init_test_env
        """
        
        all_categories = dao.get_algorithm_categories()
        category_ids = [cat.id for cat in all_categories if cat.displayname == "AdaptersTest"]
        groups = dao.get_groups_by_categories(category_ids)
        self.assertEqual(12, len(groups), "Introspection failed!")
        nr_adapters_mod2 = 0
        nr_from_xml = 0
        for algorithm in groups:
            self.assertTrue(algorithm.module in ['tvb.tests.framework.adapters.testadapter1',
                                                 'tvb.tests.framework.adapters.testadapter2',
                                                 'tvb.tests.framework.adapters.testadapter3',
                                                 'tvb.tests.framework.adapters.ndimensionarrayadapter',
                                                 'tvb.tests.framework.adapters.testgroupadapter'],
                            "Unknown Adapter module:" + str(algorithm.module))
            self.assertTrue(algorithm.classname in ["TestAdapter1", "TestAdapterDatatypeInput",
                                                    "TestAdapter2", "TestAdapter22", "TestAdapterHugeMemoryRequired",
                                                    "TestAdapter3", "TestAdapterHDDRequired",
                                                    "NDimensionArrayAdapter", "TestGroupAdapter"
                                                    ],
                            "Unknown Adapter Class:" + str(algorithm.classname))
            if algorithm.module == 'tvb.tests.framework.adapters.testadapter2':
                nr_adapters_mod2 += 1
            if algorithm.module == 'tvb.tests.framework.adapters.testgroupadapter':
                nr_from_xml += 1
        self.assertEqual(nr_adapters_mod2, 2)
        self.assertEqual(nr_from_xml, 4)


    def test_xml_introspection(self):
        """
        Check that the new xml specified in setUp was correctly introspected.
        """

        init_parameter = os.path.join("core", "adapters", "test_group.xml")
        group = dao.find_group("tvb.tests.framework.adapters.testgroupadapter", "TestGroupAdapter", init_parameter)
        self.assertTrue(group is not None, "The group was not found")
        self.assertEqual(group.init_parameter, init_parameter, "Wrong init_parameter:" + str(group.init_parameter))
        self.assertEqual(group.displayname, "Simple Python Analyzers", "The display-name of the group is not valid")
        self.assertEqual(group.algorithm_param_name, "simple", "The algorithm_param_name of the group is not valid")
        self.assertEqual(group.classname, "TestGroupAdapter", "The class-name of the group is not valid")
        self.assertEqual(group.module, "tvb.tests.framework.adapters.testgroupadapter", "Group Module invalid")
Esempio n. 15
0
class GenshiTestGroup(GenshiTest):
    """
    For a  group of algorithms interface test that
    various fields are generated correctly.
    """
    def setUp(self):
        """
        Set up any additionally needed parameters.
        """
        super(GenshiTestGroup, self).setUp()

        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.old_path = cfg.CURRENT_DIR
        cfg.CURRENT_DIR = os.path.dirname(core_path)

        adapters_init.__xml_folders__ = [os.path.join('interfaces', 'web')]
        self.introspector = Introspector("tvb_test")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('interfaces', 'web', "test_group.xml")
        algo_group = dao.find_group('tvb_test.adapters.testgroupadapter',
                                    'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        input_tree = self.xml_group_adapter.get_input_tree()
        input_tree = ABCAdapter.prepare_param_names(input_tree)
        self.template_specification['inputList'] = input_tree
        self.template_specification[base.KEY_PARAMETERS_CONFIG] = False
        resulted_html = _template2string(self.template_specification)
        self.soup = BeautifulSoup(resulted_html)

    #        file = open("output.html", 'w')
    #        file.write(self.soup.prettify())
    #        file.close()

    def tearDown(self):
        cfg.CURRENT_DIR = self.old_path
        del adapters_init.__xml_folders__
        self.reset_database()

    def test_algorithm_select_is_first(self):
        """
        Test that the first select input is always the algorithm selection 
        and that an updateDivContent is performed on it.
        """
        first_select = self.soup.find('input', attrs=dict(type="radio"))
        self.assertTrue(first_select['name'] == 'bct')
        self.assertTrue("updateDivContent" in first_select['onchange'])

    def test_sub_algorithms_correct(self):
        """
        Test that the two subalgorithms are correctly generated and that
        only one of them is not disabled.
        """
        fail_message = "The subalgorithms are not correctly generated"
        exp = re.compile('data_bct*')
        sub_algos = self.soup.findAll('div', attrs=dict(id=exp))
        self.assertTrue(len(sub_algos) == 2, fail_message)
        disabled = 0
        for one_entry in sub_algos:
            if one_entry.has_key('disabled'):
                disabled += 1
        self.assertTrue(disabled == 1, fail_message)
Esempio n. 16
0
class GenshiTestSimple(GenshiTest):
    """
    For a simple adapter interface (no group of algorithms) test that
    various fields are generated correctly.
    """
    def setUp(self):
        """
        Set up any additionally needed parameters.
        """
        super(GenshiTestSimple, self).setUp()
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.old_path = cfg.CURRENT_DIR
        cfg.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join('interfaces', 'web')]
        self.introspector = Introspector("tvb_test")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('interfaces', 'web', "test_simple.xml")
        algo_group = dao.find_group('tvb_test.adapters.testgroupadapter',
                                    'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        input_tree = self.xml_group_adapter.get_input_tree()

        input_tree = ABCAdapter.prepare_param_names(input_tree)
        self.template_specification['inputList'] = input_tree
        self.template_specification['draw_hidden_ranges'] = True
        self.template_specification[base.KEY_PARAMETERS_CONFIG] = False
        resulted_html = _template2string(self.template_specification)
        self.soup = BeautifulSoup(resulted_html)
        #file = open("output.html", 'w')
        #file.write(self.soup.prettify())
        #file.close()

    def tearDown(self):
        cfg.CURRENT_DIR = self.old_path
        del adapters_init.__xml_folders__
        self.reset_database()

    def test_sub_algo_inputs(self):
        """
        Check the name of inputs generated for each sub-algorithm is done 
        properly with only one option that is not disabled
        """
        exp = re.compile(
            'group_parameters_option_SIM_model_parameters_option_[^ \t\n\r\f\v]*_model_1$'
        )
        all_inputs = self.soup.findAll('input', attrs=dict(name=exp))
        count_disabled = 0
        for one_entry in all_inputs:
            if one_entry.has_key('disabled'):
                count_disabled += 1
        self.assertEqual(
            len(all_inputs), 5,
            "Some inputs not generated or too many inputs generated")
        self.assertEqual(count_disabled, 4,
                         "Disabling input fields was not done correctly")

    def test_hidden_ranger_fields(self):
        """ 
        Check that the default ranger hidden fields are generated correctly 
        """
        ranger1 = self.soup.findAll('input',
                                    attrs=dict(type="hidden",
                                               id="first_range"))
        ranger2 = self.soup.findAll('input',
                                    attrs=dict(type="hidden",
                                               id="second_range"))
        self.assertEqual(len(ranger1), 1, "First ranger generated wrong")
        self.assertEqual(len(ranger2), 1, "Second ranger generated wrong")

    def test_checkbox_range_component(self):
        """
        Check that a range component is generated for the 'coupling method'
        since the type is select and it does not have any sub-attributes,
        nor are it's options dataTypes. Only two options are declared.
        """
        range_id = self.soup.findAll(
            'table',
            attrs=dict(
                id=
                'data_groupSIMgroup_parameters_option_SIM_coupling_methodrange'
            ))
        select_input = self.soup.findAll(
            'input',
            attrs=dict(name="group_parameters_option_SIM_coupling_method",
                       type="radio"))
        self.assertEqual(2, len(select_input))
        linear_checkbox = self.soup.findAll('input',
                                            attrs=dict(type="checkbox",
                                                       value="Linear"))
        nonlinear_checkbox = self.soup.findAll('input',
                                               attrs=dict(type="checkbox",
                                                          value="NonLinear"))
        hidden_value_field = self.soup.findAll(
            'input',
            attrs=dict(type="hidden",
                       name="group_parameters_option_SIM_coupling_method"))
        fail_message = "Something went wrong with generating the range checkboxes component"
        self.assertTrue(len(range_id) == 1, fail_message)
        self.assertEqual(
            linear_checkbox[0]['id'],
            "data_groupSIMgroup_parameters_option_SIM_coupling_methodrangeLinearcheck",
            fail_message)
        self.assertEqual(
            nonlinear_checkbox[0]['id'],
            "data_groupSIMgroup_parameters_option_SIM_coupling_methodrangeNonLinearcheck",
            fail_message)
        self.assertEqual(
            hidden_value_field[0]['id'],
            "data_groupSIMgroup_parameters_option_SIM_coupling_methodrange_hidden",
            fail_message)

    def test_sub_algorithms(self):
        """
        Check that the correct number of sub-algorithms is created
        and that only one of them is not disable
        """
        fail_message = "Something went wrong with generating the sub-algorithms."
        exp = re.compile('data_group_parameters_option_SIM_model*')
        enabled_algo = self.soup.findAll('div',
                                         attrs=dict(id=exp,
                                                    style="display:block"))
        all_algo_disabled = self.soup.findAll('div',
                                              attrs=dict(id=exp,
                                                         disabled='disabled'))
        self.assertTrue(
            len(enabled_algo) == 1 and len(all_algo_disabled) == 6,
            fail_message)
        self.assertFalse(enabled_algo[0] in all_algo_disabled, fail_message)

    def test_normal_ranger(self):
        """
        Check the normal ranger generation. Only one ranger should be created
        because the minValue/ maxValue is specified only for one field. It should
        also be disabled because it is not as default.
        """
        fail_message = "Something went wrong with generating the ranger."

        exp = re.compile('data_group_parameters_option_SIM_model*')
        ranger_parent = self.soup.findAll('table',
                                          attrs={
                                              'id': exp,
                                              'class': "ranger-div-class"
                                          })
        self.assertTrue(len(ranger_parent) == 1, fail_message)

        span_field = self.soup.findAll(
            'span',
            attrs=dict(
                id="data_group_parameters_option_SIM_modelWilsonCowangroup_"
                "parameters_option_SIM_model_parameters_option_WilsonCowan_"
                "model_0_RANGER_interval_span"))
        self.assertEqual(span_field[0].contents[0], '0.01 - 0.91',
                         fail_message)

        spinner_field = self.soup.findAll(
            'input',
            attrs=dict(id="data_group_parameters_option_SIM_modelWilsonCowan"
                       "group_parameters_option_SIM_model_parameters_option_"
                       "WilsonCowan_model_0_RANGER_stepInput"))
        self.assertEqual(str(spinner_field[0]['value']), '0.01', fail_message)

    def test_multiple_select(self):
        """
        Checks the correct creation of a multiple select component.
        """
        fail_message = "Something went wrong with creating multiple select."
        exp = re.compile('group_parameters_option_SIM_monitors*')
        all_multiple_options = self.soup.findAll('div', attrs=dict(id=exp))
        disabled_options = self.soup.findAll('div',
                                             attrs=dict(id=exp,
                                                        disabled='disabled'))
        self.assertEqual(len(all_multiple_options), 4, fail_message)
        self.assertEqual(len(disabled_options), 2, fail_message)
        exp = re.compile('monitors_parameters*')
        all_multiple_params = self.soup.findAll('input', attrs=dict(name=exp))
        disabled_params = self.soup.findAll('input',
                                            attrs=dict(name=exp,
                                                       disabled='disabled'))
        self.assertEqual(len(all_multiple_params), 4, fail_message)
        self.assertEqual(len(disabled_params), 2, fail_message)
class XML_Reader_Test(TransactionalTestCase):
    """
    This is a test class for the tvb.core.adapters.xml_reader module.
    """
    old_current_dir = TvbProfile.current.web.CURRENT_DIR
    old_xml_path = adapters_init.__xml_folders__


    def setUp(self):
        """
        This method sets up the necessary paths for testing.
        """
        self.folder_path = os.path.dirname(__file__)

        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join('core', 'adapters')]

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('core', 'adapters', "test_group.xml")
        algo_group = dao.find_group('tvb.tests.framework.adapters.testgroupadapter', 'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        self.xml_group_reader = self.xml_group_adapter.xml_reader


    def tearDown(self):
        """
        Revert changes settings and remove recently imported algorithms
        """
        TvbProfile.current.web.CURRENT_DIR = self.old_current_dir
        adapters_init.__xml_folders__ = self.old_xml_path

        for group in dao.get_generic_entity(model.AlgorithmGroup, "simple", "algorithm_param_name"):
            dao.remove_entity(model.AlgorithmGroup, group.id)


    def test_algorithm_group_attributes(self):
        """
        Tests the attributes of an algorithm group
        """
        self.assertEqual(self.xml_group_adapter.xml_reader.root_name, "simple")
        self.assertEqual(self.xml_group_adapter.xml_reader.get_type(),
                         "tvb.tests.framework.adapters.testgroupadapter.TestGroupAdapter")
        self.assertEqual(self.xml_group_adapter.xml_reader.get_ui_name(), "Simple Python Analyzers")
        self.assertTrue("externals/BCT" in self.xml_group_adapter.xml_reader.get_additional_path())


    def test_code_for_algorithm(self):
        """
        Tests the code that has to be executed for an algorithm
        """
        self.assertEqual(self.xml_group_adapter.get_call_code("CC"),
                         "cross_correlation(data1, data2, chan1, chan2, mode)")


    def test_import_for_algorithm(self):
        """
        Tests the code that has to be imported for an algorithm
        """
        self.assertEqual(self.xml_group_adapter.get_import_code("CC"), "tvb.analyzers.simple_analyzers")


    def test_inputs_for_algorithm(self):
        """
        Tests the inputs for an algorithm
        """
        inputs = self.xml_group_adapter.xml_reader.get_inputs("CC")
        self.check_inputs(inputs)


    def test_outputs_for_algorithm(self):
        """
        Tests the outputs for an algorithm
        """
        outputs = self.xml_group_adapter.xml_reader.get_outputs("CC")
        self.check_outputs(outputs)


    def test_get_interface(self):
        """
        Tests the interface for an algorithm group
        """
        interface = self.xml_group_adapter.get_input_tree()
        self.assertEqual(len(interface), 1, "Didnt parse correctly.")
        tree_root = interface[0]
        self.assertEqual(tree_root[xml_reader.ATT_NAME], "simple")
        self.assertEqual(tree_root[xml_reader.ATT_LABEL], "Analysis Algorithm:")
        self.assertTrue(tree_root[xml_reader.ATT_REQUIRED])
        self.assertEqual(tree_root[xml_reader.ATT_TYPE], xml_reader.TYPE_SELECT)
        options = tree_root[xml_reader.ELEM_OPTIONS]
        self.assertEqual(len(options), 1)
        self.assertEqual(options[0][xml_reader.ATT_VALUE], "CC")
        self.assertEqual(options[0][xml_reader.ATT_NAME], "Cross Correlation")
        attributes = options[0][xml_reader.ATT_ATTRIBUTES]
        self.assertEqual(len(attributes), 3, "Didnt parse correctly.")
        self.check_inputs(attributes)
        outputs = options[0][xml_reader.ELEM_OUTPUTS]
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        self.check_outputs(outputs)


    def test_get_algorithms_dictionary(self):
        """
        Tests the list of algorithms for an algorithm group
        """
        algorithms = self.xml_group_adapter.xml_reader.get_algorithms_dictionary()
        self.assertEqual(len(algorithms), 1, "Didn't parse correctly.")
        algorithm = algorithms["CC"]
        self.assertEqual(algorithm[xml_reader.ATT_IDENTIFIER], "CC")
        self.assertEqual(algorithm[xml_reader.ATT_CODE], "cross_correlation(data1, data2, chan1, chan2, mode)")
        self.assertEqual(algorithm[xml_reader.ATT_IMPORT], "tvb.analyzers.simple_analyzers")
        self.assertEqual(algorithm[xml_reader.ATT_NAME], "Cross Correlation")
        #check inputs
        inputs = algorithms["CC"][xml_reader.INPUTS_KEY]
        new_inputs = []
        for inp in inputs.keys():
            new_inputs.append(inputs[inp])
        self.check_inputs(new_inputs)
        #check outputs
        outputs = algorithms["CC"][xml_reader.OUTPUTS_KEY]
        self.check_outputs(outputs)


    def test_get_all_outputs(self):
        """
        Tests the all outputs of the algorithms from an algorithm group
        """
        outputs = self.xml_group_reader.get_all_outputs()
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        self.check_outputs(outputs)


    def test_no_algorithm_node(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains any
        algorithm node.
        """
        try:
            xml_group_path = os.path.join(self.folder_path, "no_algorithm_node.xml")
            xml_reader.XMLGroupReader.get_instance(xml_group_path)
            self.fail("Test should fail. The xml doesn't contain algorithms")
        except XmlParserException:
            #OK, do nothing
            pass


    def test_no_code_node(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains all
        the required nodes. The missing node is code from one of the algorithms.
        """
        xml_group_path = os.path.join(self.folder_path, "no_code_node.xml")
        self.assertRaises(XmlParserException, xml_reader.XMLGroupReader, xml_group_path)
        #The call should fail. 
        #One of the algorithms doesn't contain required attribute 'code'


    def test_missing_required_attribute(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains all
        the required attributes. The missing attribute is 'name' from one of the algorithms.
        """
        try:
            xml_group_path = os.path.join(self.folder_path, "missing_required_attribute.xml")
            xml_reader.XMLGroupReader.get_instance(xml_group_path)
            self.fail("Test should fail. One of the algorithms doesn't have required 'name' att")
        except XmlParserException:
            #OK, do nothing
            pass


    def test_invalid_schema_url(self):
        """
        Test that when XML schema can not be found (e.g. due to no internet connection, 
        or server down) the XML is still read.
        """
        xml_group_path = os.path.join(self.folder_path, "test_invalid_schema_url.xml")
        self.xml_group_reader = xml_reader.XMLGroupReader.get_instance(xml_group_path)
        self.test_inputs_for_algorithm()
        self.test_outputs_for_algorithm()


    def check_inputs(self, inputs_list):
        """
        The given list of inputs is expected to be for the "CC" algorithm.
        """
        inputs_dict = {}
        for input_ in inputs_list:
            inputs_dict[input_[xml_reader.ATT_NAME]] = input_

        expected_conditions = {'fields': ['datatype_class._nr_dimensions'],
                               'values': ['2'], 'operations': ['==']}
        self._check_input(inputs_dict, "data1", True, "tvb.datatypes.arrays.MappedArray", "First dataset:",
                          "First set of signals", "data", "default_data", None, expected_conditions)

        self._check_input(inputs_dict, "chan1", False, "int", "First channel index:", None, None, None, None, None)

        expected_options = {'valid': 'Valid', 'same': 'Same', 'full': 'Full'}
        self._check_input(inputs_dict, "mode", True, "select", "Mode:", "Flag that indicates the size of the output",
                          None, "full", expected_options, None)


    def _check_input(self, all_inputs, input_name, is_required, expected_type, expected_label, expected_description,
                     expected_field, expected_default, expected_options, expected_conditions):
        """Validate one input"""

        input_ = all_inputs[input_name]

        self.assertEqual(input_[xml_reader.ATT_NAME], input_name)
        self.assertEqual(input_[xml_reader.ATT_LABEL], expected_label)
        self.assertEqual(input_[xml_reader.ATT_TYPE], expected_type)

        if is_required:
            self.assertTrue(input_[xml_reader.ATT_REQUIRED])
        else:
            self.assertFalse(input_[xml_reader.ATT_REQUIRED])

        if expected_description is None:
            self.assertFalse(xml_reader.ATT_DESCRIPTION in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_DESCRIPTION], expected_description)

        if expected_field is None:
            self.assertFalse(xml_reader.ATT_FIELD in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_FIELD], expected_field)

        if expected_default is None:
            self.assertFalse(xml_reader.ATT_DEFAULT in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_DEFAULT], expected_default)

        if expected_options is None:
            self.assertFalse(xml_reader.ELEM_OPTIONS in input_.keys())
        else:
            options = input_[xml_reader.ELEM_OPTIONS]
            for opt in options:
                self.assertTrue(opt[xml_reader.ATT_VALUE] in expected_options)
                self.assertEquals(opt[xml_reader.ATT_NAME], expected_options[opt[xml_reader.ATT_VALUE]])

        if expected_conditions is None:
            self.assertFalse(xml_reader.ELEM_CONDITIONS in input_.keys())
        else:
            ops_filter = input_[xml_reader.ELEM_CONDITIONS]
            for key_attr in expected_conditions.keys():
                actual_values = getattr(ops_filter, key_attr)
                self.assertEqual(len(actual_values), len(expected_conditions[key_attr]))
                for att in actual_values:
                    self.assertTrue(att in expected_conditions[key_attr])


    def check_outputs(self, outputs):
        """
        The given list of outputs is expected to be for the "CC" algorithm.
        """
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        #first output
        self.assertEqual(outputs[0][xml_reader.ATT_TYPE], "tvb.datatypes.arrays.MappedArray")
        fields = outputs[0][xml_reader.ELEM_FIELD]
        self.assertEqual(len(fields), 3, "Expected 3 fields.")
        fields_dict = {}
        for field_ in fields:
            fields_dict[field_[xml_reader.ATT_NAME]] = field_
        self.assertEqual(fields_dict["data"][xml_reader.ATT_REFERENCE], "$0#")
        self.assertEqual(fields_dict["data_name"][xml_reader.ATT_VALUE], "Covariance matrix")
        self.assertEqual(fields_dict["label_x"][xml_reader.ATT_VALUE], "Nodes")
        #second output
        self.assertEqual(outputs[1][xml_reader.ATT_TYPE], "tvb.datatypes.arrays.MappedArray")
        fields = outputs[1][xml_reader.ELEM_FIELD]
        self.assertEqual(len(fields), 2)
        fields_dict = {}
        for field_ in fields:
            fields_dict[field_[xml_reader.ATT_NAME]] = field_
        self.assertEqual(fields_dict["data"][xml_reader.ATT_REFERENCE], "$0#")
        self.assertEqual(fields_dict["data_name"][xml_reader.ATT_VALUE], "Cross correlation")
Esempio n. 18
0
class XML_Reader_Test(TransactionalTestCase):
    """
    This is a test class for the tvb.core.adapters.xml_reader module.
    """
    old_current_dir = TvbProfile.current.web.CURRENT_DIR
    old_xml_path = adapters_init.__xml_folders__

    def setUp(self):
        """
        This method sets up the necessary paths for testing.
        """
        self.folder_path = os.path.dirname(__file__)

        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TvbProfile.current.web.CURRENT_DIR = os.path.dirname(core_path)
        adapters_init.__xml_folders__ = [os.path.join('core', 'adapters')]

        self.introspector = Introspector("tvb.tests.framework")
        self.introspector.introspect(True)

        xml_group_path = os.path.join('core', 'adapters', "test_group.xml")
        algo_group = dao.find_group(
            'tvb.tests.framework.adapters.testgroupadapter',
            'TestGroupAdapter', xml_group_path)
        self.xml_group_adapter = ABCAdapter.build_adapter(algo_group)
        self.xml_group_reader = self.xml_group_adapter.xml_reader

    def tearDown(self):
        """
        Revert changes settings and remove recently imported algorithms
        """
        TvbProfile.current.web.CURRENT_DIR = self.old_current_dir
        adapters_init.__xml_folders__ = self.old_xml_path

        for group in dao.get_generic_entity(model.AlgorithmGroup, "simple",
                                            "algorithm_param_name"):
            dao.remove_entity(model.AlgorithmGroup, group.id)

    def test_algorithm_group_attributes(self):
        """
        Tests the attributes of an algorithm group
        """
        self.assertEqual(self.xml_group_adapter.xml_reader.root_name, "simple")
        self.assertEqual(
            self.xml_group_adapter.xml_reader.get_type(),
            "tvb.tests.framework.adapters.testgroupadapter.TestGroupAdapter")
        self.assertEqual(self.xml_group_adapter.xml_reader.get_ui_name(),
                         "Simple Python Analyzers")
        self.assertTrue("externals/BCT" in self.xml_group_adapter.xml_reader.
                        get_additional_path())

    def test_code_for_algorithm(self):
        """
        Tests the code that has to be executed for an algorithm
        """
        self.assertEqual(
            self.xml_group_adapter.get_call_code("CC"),
            "cross_correlation(data1, data2, chan1, chan2, mode)")

    def test_import_for_algorithm(self):
        """
        Tests the code that has to be imported for an algorithm
        """
        self.assertEqual(self.xml_group_adapter.get_import_code("CC"),
                         "tvb.analyzers.simple_analyzers")

    def test_inputs_for_algorithm(self):
        """
        Tests the inputs for an algorithm
        """
        inputs = self.xml_group_adapter.xml_reader.get_inputs("CC")
        self.check_inputs(inputs)

    def test_outputs_for_algorithm(self):
        """
        Tests the outputs for an algorithm
        """
        outputs = self.xml_group_adapter.xml_reader.get_outputs("CC")
        self.check_outputs(outputs)

    def test_get_interface(self):
        """
        Tests the interface for an algorithm group
        """
        interface = self.xml_group_adapter.get_input_tree()
        self.assertEqual(len(interface), 1, "Didnt parse correctly.")
        tree_root = interface[0]
        self.assertEqual(tree_root[xml_reader.ATT_NAME], "simple")
        self.assertEqual(tree_root[xml_reader.ATT_LABEL],
                         "Analysis Algorithm:")
        self.assertTrue(tree_root[xml_reader.ATT_REQUIRED])
        self.assertEqual(tree_root[xml_reader.ATT_TYPE],
                         xml_reader.TYPE_SELECT)
        options = tree_root[xml_reader.ELEM_OPTIONS]
        self.assertEqual(len(options), 1)
        self.assertEqual(options[0][xml_reader.ATT_VALUE], "CC")
        self.assertEqual(options[0][xml_reader.ATT_NAME], "Cross Correlation")
        attributes = options[0][xml_reader.ATT_ATTRIBUTES]
        self.assertEqual(len(attributes), 3, "Didnt parse correctly.")
        self.check_inputs(attributes)
        outputs = options[0][xml_reader.ELEM_OUTPUTS]
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        self.check_outputs(outputs)

    def test_get_algorithms_dictionary(self):
        """
        Tests the list of algorithms for an algorithm group
        """
        algorithms = self.xml_group_adapter.xml_reader.get_algorithms_dictionary(
        )
        self.assertEqual(len(algorithms), 1, "Didn't parse correctly.")
        algorithm = algorithms["CC"]
        self.assertEqual(algorithm[xml_reader.ATT_IDENTIFIER], "CC")
        self.assertEqual(
            algorithm[xml_reader.ATT_CODE],
            "cross_correlation(data1, data2, chan1, chan2, mode)")
        self.assertEqual(algorithm[xml_reader.ATT_IMPORT],
                         "tvb.analyzers.simple_analyzers")
        self.assertEqual(algorithm[xml_reader.ATT_NAME], "Cross Correlation")
        #check inputs
        inputs = algorithms["CC"][xml_reader.INPUTS_KEY]
        new_inputs = []
        for inp in inputs.keys():
            new_inputs.append(inputs[inp])
        self.check_inputs(new_inputs)
        #check outputs
        outputs = algorithms["CC"][xml_reader.OUTPUTS_KEY]
        self.check_outputs(outputs)

    def test_get_all_outputs(self):
        """
        Tests the all outputs of the algorithms from an algorithm group
        """
        outputs = self.xml_group_reader.get_all_outputs()
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        self.check_outputs(outputs)

    def test_no_algorithm_node(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains any
        algorithm node.
        """
        try:
            xml_group_path = os.path.join(self.folder_path,
                                          "no_algorithm_node.xml")
            xml_reader.XMLGroupReader.get_instance(xml_group_path)
            self.fail("Test should fail. The xml doesn't contain algorithms")
        except XmlParserException:
            #OK, do nothing
            pass

    def test_no_code_node(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains all
        the required nodes. The missing node is code from one of the algorithms.
        """
        xml_group_path = os.path.join(self.folder_path, "no_code_node.xml")
        self.assertRaises(XmlParserException, xml_reader.XMLGroupReader,
                          xml_group_path)
        #The call should fail.
        #One of the algorithms doesn't contain required attribute 'code'

    def test_missing_required_attribute(self):
        """
        This test trys to load an invalid xml. The xml doesn't contains all
        the required attributes. The missing attribute is 'name' from one of the algorithms.
        """
        try:
            xml_group_path = os.path.join(self.folder_path,
                                          "missing_required_attribute.xml")
            xml_reader.XMLGroupReader.get_instance(xml_group_path)
            self.fail(
                "Test should fail. One of the algorithms doesn't have required 'name' att"
            )
        except XmlParserException:
            #OK, do nothing
            pass

    def test_invalid_schema_url(self):
        """
        Test that when XML schema can not be found (e.g. due to no internet connection, 
        or server down) the XML is still read.
        """
        xml_group_path = os.path.join(self.folder_path,
                                      "test_invalid_schema_url.xml")
        self.xml_group_reader = xml_reader.XMLGroupReader.get_instance(
            xml_group_path)
        self.test_inputs_for_algorithm()
        self.test_outputs_for_algorithm()

    def check_inputs(self, inputs_list):
        """
        The given list of inputs is expected to be for the "CC" algorithm.
        """
        inputs_dict = {}
        for input_ in inputs_list:
            inputs_dict[input_[xml_reader.ATT_NAME]] = input_

        expected_conditions = {
            'fields': ['datatype_class._nr_dimensions'],
            'values': ['2'],
            'operations': ['==']
        }
        self._check_input(inputs_dict, "data1", True,
                          "tvb.datatypes.arrays.MappedArray", "First dataset:",
                          "First set of signals", "data", "default_data", None,
                          expected_conditions)

        self._check_input(inputs_dict, "chan1", False, "int",
                          "First channel index:", None, None, None, None, None)

        expected_options = {'valid': 'Valid', 'same': 'Same', 'full': 'Full'}
        self._check_input(inputs_dict, "mode", True, "select", "Mode:",
                          "Flag that indicates the size of the output", None,
                          "full", expected_options, None)

    def _check_input(self, all_inputs, input_name, is_required, expected_type,
                     expected_label, expected_description, expected_field,
                     expected_default, expected_options, expected_conditions):
        """Validate one input"""

        input_ = all_inputs[input_name]

        self.assertEqual(input_[xml_reader.ATT_NAME], input_name)
        self.assertEqual(input_[xml_reader.ATT_LABEL], expected_label)
        self.assertEqual(input_[xml_reader.ATT_TYPE], expected_type)

        if is_required:
            self.assertTrue(input_[xml_reader.ATT_REQUIRED])
        else:
            self.assertFalse(input_[xml_reader.ATT_REQUIRED])

        if expected_description is None:
            self.assertFalse(xml_reader.ATT_DESCRIPTION in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_DESCRIPTION],
                             expected_description)

        if expected_field is None:
            self.assertFalse(xml_reader.ATT_FIELD in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_FIELD], expected_field)

        if expected_default is None:
            self.assertFalse(xml_reader.ATT_DEFAULT in input_.keys())
        else:
            self.assertEqual(input_[xml_reader.ATT_DEFAULT], expected_default)

        if expected_options is None:
            self.assertFalse(xml_reader.ELEM_OPTIONS in input_.keys())
        else:
            options = input_[xml_reader.ELEM_OPTIONS]
            for opt in options:
                self.assertTrue(opt[xml_reader.ATT_VALUE] in expected_options)
                self.assertEquals(opt[xml_reader.ATT_NAME],
                                  expected_options[opt[xml_reader.ATT_VALUE]])

        if expected_conditions is None:
            self.assertFalse(xml_reader.ELEM_CONDITIONS in input_.keys())
        else:
            ops_filter = input_[xml_reader.ELEM_CONDITIONS]
            for key_attr in expected_conditions.keys():
                actual_values = getattr(ops_filter, key_attr)
                self.assertEqual(len(actual_values),
                                 len(expected_conditions[key_attr]))
                for att in actual_values:
                    self.assertTrue(att in expected_conditions[key_attr])

    def check_outputs(self, outputs):
        """
        The given list of outputs is expected to be for the "CC" algorithm.
        """
        self.assertEqual(len(outputs), 2, "Didnt parse correctly.")
        #first output
        self.assertEqual(outputs[0][xml_reader.ATT_TYPE],
                         "tvb.datatypes.arrays.MappedArray")
        fields = outputs[0][xml_reader.ELEM_FIELD]
        self.assertEqual(len(fields), 3, "Expected 3 fields.")
        fields_dict = {}
        for field_ in fields:
            fields_dict[field_[xml_reader.ATT_NAME]] = field_
        self.assertEqual(fields_dict["data"][xml_reader.ATT_REFERENCE], "$0#")
        self.assertEqual(fields_dict["data_name"][xml_reader.ATT_VALUE],
                         "Covariance matrix")
        self.assertEqual(fields_dict["label_x"][xml_reader.ATT_VALUE], "Nodes")
        #second output
        self.assertEqual(outputs[1][xml_reader.ATT_TYPE],
                         "tvb.datatypes.arrays.MappedArray")
        fields = outputs[1][xml_reader.ELEM_FIELD]
        self.assertEqual(len(fields), 2)
        fields_dict = {}
        for field_ in fields:
            fields_dict[field_[xml_reader.ATT_NAME]] = field_
        self.assertEqual(fields_dict["data"][xml_reader.ATT_REFERENCE], "$0#")
        self.assertEqual(fields_dict["data_name"][xml_reader.ATT_VALUE],
                         "Cross correlation")
class IntrospectorTest(BaseTestCase):
    """
    Test class for the introspector module.
    """
    def setUp(self):
        """
        Reset the database before each test.
        """
        self.reset_database()
        #tvb_test path
        core_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.old_path = cfg.CURRENT_DIR
        cfg.CURRENT_DIR = os.path.dirname(core_path)
        self.introspector = Introspector("tvb_test")

    def tearDown(self):
        """
        Reset the database when test is done.
        """
        cfg.CURRENT_DIR = self.old_path
        self.reset_database()

    def test_introspect(self):
        """
        Test the actual introspect module on a test structure created in:
        tvb_test.dummy_adapters and tvb_test.dummy_datatypes
        """
        self.introspector.introspect(True)
        initialize_storage()

        all_categories = dao.get_algorithm_categories()
        category_ids = [
            cat.id for cat in all_categories
            if cat.displayname == "AdaptersTest"
        ]
        groups = dao.get_groups_by_categories(category_ids)
        self.assertEqual(len(groups), 11, "Introspection failed!")
        nr_adapters_mod2 = 0
        for algorithm in groups:
            self.assertTrue(
                algorithm.module in [
                    'tvb_test.adapters.testadapter1',
                    'tvb_test.adapters.testadapter2',
                    'tvb_test.adapters.testadapter3',
                    'tvb_test.adapters.ndimensionarrayadapter',
                    "tvb.adapters.analyzers.group_python_adapter",
                    "tvb_test.adapters.testgroupadapter"
                ], "Unknown Adapter:" + str(algorithm.module))
            self.assertTrue(
                algorithm.classname in [
                    "TestAdapter1", "TestAdapterDatatypeInput", "TestAdapter2",
                    "TestAdapter22", "TestAdapter3", "NDimensionArrayAdapter",
                    "PythonAdapter", "TestAdapterHDDRequired",
                    "TestAdapterHugeMemoryRequired",
                    "TestAdapterNoMemoryImplemented", "TestGroupAdapter"
                ], "Unknown Adapter Class:" + str(algorithm.classname))
            if algorithm.module == 'tvb_test.adapters.testadapter2':
                nr_adapters_mod2 = nr_adapters_mod2 + 1
        self.assertEqual(
            nr_adapters_mod2, 2,
            "Two adapters should have been loaded from module tvb_test.adapters2!"
        )

    def test_xml_introspection(self):
        """
        Check the XML introspection. The folders which are introspected
        are defined in the variable __xml_folders__ from tvb_test/adapters/_init.py
        """
        self.introspector.introspect(True)
        initialize_storage()
        init_parameter = os.path.join("core", "adapters", "test_group.xml")
        group = dao.find_group("tvb_test.adapters.testgroupadapter",
                               "TestGroupAdapter", init_parameter)
        self.assertTrue(group is not None, "The group was not found")
        self.assertEqual(group.init_parameter, init_parameter,
                         "Wrong init_parameter:" + str(group.init_parameter))
        self.assertEqual(group.displayname, "Simple Python Analyzers",
                         "The display-name of the group is not valid")
        self.assertEqual(group.algorithm_param_name, "simple",
                         "The algorithm_param_name of the group is not valid")
        self.assertEqual(group.classname, "TestGroupAdapter",
                         "The class-name of the group is not valid")
        self.assertEqual(group.module, "tvb_test.adapters.testgroupadapter",
                         "Group Module invalid")