Ejemplo n.º 1
0
    def test_modules_names(self):
        """Test that all model files are properly discovered"""

        # get all modules of the model directory using the directory __path__
        modules = list()
        for __, modname, ispkg in pkgutil.iter_modules(
                path=[models.__path__[0]]):
            modules.append(modname)

        # ensure that _list_model_names() gets all modules properly
        self.assertEqual(modules, list_module_names(self.m_path))
Ejemplo n.º 2
0
    def test_create_tables(self):
        m_engine = create_engine('sqlite:///:memory:', echo=True)
        cd.create_database_tables(m_engine)

        # connect to the in memory database and query the number of tables
        m_conn = m_engine.connect()
        m_text = text("SELECT name FROM sqlite_master WHERE type='table'")
        result = m_conn.execute(m_text).fetchall()
        m_conn.close()

        # assert that as many tables as models were created.
        # WARNING: this will break when there are many-to-many relationships
        self.assertEqual(len(list_module_names(self.m_path)), len(result))
Ejemplo n.º 3
0
    def _get_device_module(module):
        device_modules = []

        # discover the path for the module directory and the package
        package_path = module.__path__[0]
        package = module.__name__

        modules = list()
        for module_name in list_module_names(package_path):
            modules.append((module_name, module_name.title().replace('_', '')))

        imported_modules = import_modules(modules,
                                          package,
                                          fetch_attribute=True)
        for module, attribute in imported_modules:
            device_modules.append(getattr(module, attribute))

        return device_modules
Ejemplo n.º 4
0
def _list_tables():
    """Collection of all the sqlalchemy model __table__ objects

    :return: A Collection of ``__table__`` objects from sqlalchemy models.
    """
    tables = list()

    modules = list()
    # create module_name and expected class tuples from list_module_names
    # if the module file is account_types the expected class is AccountTypes.
    for module_name in list_module_names(models.__path__[0]):
        modules.append((module_name, module_name.title().replace('_', '')))

    imported_modules = import_modules(modules,
                                      models.__name__,
                                      fetch_attribute=True)
    for module, attribute in imported_modules:
        # Access the modules class and subsequent __table__
        tables.append(getattr(module, attribute).__table__)
    return tables
Ejemplo n.º 5
0
    def test_module_names_complete(self):
        """Test that all config files are property included in __init__.py"""

        # opts and cfg should not be included in init even though they are
        # modules
        remove = ['opts', 'cfg']

        # get all attributes of the config directory
        path = config.__path__[0]
        names = dir(config)
        modules = list()

        # for every attribute that is a module add it to modules
        for name in names:
            if inspect.ismodule(getattr(config, name, None)):
                modules.append(name)

        # remove the not to be included modules
        for r in remove:
            modules.remove(r)

        # ensure that opts._list_module_names() gets all modules properly
        self.assertEqual(modules, di.list_module_names(path, remove))