Exemplo n.º 1
0
class SqliteTableExistence(object):
    """
    Validation for the table of sqlite
    """

    def __init__(self, dbname, tblname, returns_bool=False):
        """
        Args:
            dbname: database name
            tblname: table name
            returns_bool: return bool or not
        """
        self.__sqlite_adptr = SqliteAdapter()
        self.__dbname = dbname
        self.__tblname = tblname
        self.__returns_bool = returns_bool
        self._logger = LisboaLog.get_logger(__name__)

    def __call__(self):
        try:
            self.__sqlite_adptr.connect(self.__dbname)
            cur = self.__sqlite_adptr.fetch(
                'SELECT name FROM sqlite_master WHERE type="table" AND name="%s"'
                % self.__tblname
            )
            result = cur.fetchall()
            if self.__returns_bool is True:
                return True if result else False

            if not result and self.__returns_bool is False:
                raise SqliteInvalid("Sqlite table %s not found" % self.__tblname)

        finally:
            self.__sqlite_adptr.close()
Exemplo n.º 2
0
 def _insert_test_data(self, obj):
     adapter = SqliteAdapter()
     adapter.connect(self._DB_NAME)
     try:
         adapter.drop_table(self._TBL_NAME)
         adapter.create_table(self._TBL_NAME, ["No", "TEXT"])
         adapter.execute_many_insert(self._TBL_NAME, ["No", "TEXT"], obj)
         adapter.commit()
     finally:
         adapter.close()
Exemplo n.º 3
0
class BaseSqlite(BaseStep):
    """
    Base class of all the sqlite classes
    """
    def __init__(self):
        super().__init__()
        self._sqlite_adptr = SqliteAdapter()
        self._dbname = None
        self._columns = []
        self._vacuum = False

    def dbname(self, dbname):
        self._dbname = dbname

    def columns(self, columns):
        self._columns = columns

    def vacuum(self, vacuum):
        self._vacuum = vacuum

    def execute(self, *args):
        # essential parameters check
        param_valid = EssentialParameters(self.__class__.__name__,
                                          [self._dbname])
        param_valid()

    def _dict_factory(self, cursor, row):
        d = {}
        for i, col in enumerate(cursor.description):
            d[col[0]] = row[i]
        return d

    def _close_database(self):
        """
        Disconnect sqlite database (execute vacuume if necessary)
        """
        self._sqlite_adptr.close()
        if self._vacuum is True:
            try:
                self._sqlite_adptr.connect(self._dbname)
                self._sqlite_adptr.execute("VACUUM")
            finally:
                self._sqlite_adptr.close()
Exemplo n.º 4
0
    def execute(self, *args):
        # essential parameters check
        valid = EssentialParameters(
            self.__class__.__name__,
            [
                self._order,
                self._src_dir,
                self._src_pattern,
                self._dest_dir,
            ],
        )
        valid()

        files = super().get_target_files(self._src_dir, self._src_pattern)
        self._logger.info("Files found %s" % files)

        ymd_hms = datetime.now().strftime("%Y%m%d%H%M%S%f")
        dbname = ".%s_%s.db" % (ymd_hms, StringUtil().random_str(8))
        tblname = "temp_table"

        sqlite = SqliteAdapter()
        sqlite.connect(dbname)
        try:
            for file in files:
                _, filename = os.path.split(file)
                dest_file = os.path.join(self._dest_dir, filename)

                sqlite.import_table(file, tblname, encoding=self._encoding)
                sqlite.export_table(
                    tblname,
                    dest_file,
                    encoding=self._encoding,
                    order=self._order,
                    no_duplicate=self._no_duplicate,
                )
            sqlite.close()
        finally:
            os.remove(dbname)
Exemplo n.º 5
0
class TestSqliteCreation(object):
    def setup_method(self, method):
        self._db_dir = os.path.join(env.BASE_DIR, "db")
        self._adptr = SqliteAdapter()

    def test_connect_ok(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)
        exists_db_file = os.path.exists(db_file)
        shutil.rmtree(self._db_dir)
        assert exists_db_file is True

    def test_close_ok(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)
        self._adptr.close()
        exists_db_file = os.path.exists(db_file)
        shutil.rmtree(self._db_dir)
        assert exists_db_file is True

    def test_fetch_ok(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)
        self._adptr.execute("create table spam_table (id, name, age);")
        cursor1 = self._adptr.fetch("select * from spam_table")
        cursor2 = self._adptr.fetch("select * from spam_table")
        os.path.exists(db_file)
        shutil.rmtree(self._db_dir)
        assert isinstance(type(cursor1), type(cursor2)) is False

    def test_execute_many_insert_ok(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)
        self._adptr.execute("create table spam_table (id, name, age);")

        def test_insert():
            dummy_data = [{"id": 1, "name": "spam1", "age": 24}]
            self._adptr.execute_many_insert("spam_table",
                                            ["id", "name", "age"], dummy_data)

        test_insert()
        self._adptr.commit()
        cursor = self._adptr.fetch("select * from spam_table;")
        shutil.rmtree(self._db_dir)
        for c in cursor:
            assert c == (1, "spam1", 24)

    def test_execute_many_insert_ng_no_tblname(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)
        self._adptr.execute("create table spam_table (id, name, age);")

        def test_insert():
            dummy_data = [{"id": 1, "name": "spam1", "age": 24}]
            self._adptr.execute_many_insert(None, ["id", "name", "age"],
                                            dummy_data)

        with pytest.raises(ValueError) as execinfo:
            test_insert()
        shutil.rmtree(self._db_dir)
        assert str(execinfo.value) == "Parameters are missing"

    def test_create_table(self):
        # create spam.db
        os.makedirs(self._db_dir)
        db_file = os.path.join(self._db_dir, "spam.db")
        self._adptr.connect(db_file)

        columns = ["id", "na\"me", "a'ge"]
        self._adptr.create_table("spam_table", columns)
        result = self._adptr.get_column_names("spam_table")

        shutil.rmtree(self._db_dir)
        assert columns == result