コード例 #1
0
def test_execute_with_tuple_params(mock_db_model: Type[BaseModel], db_manager: DBManager):
    db_manager.safe_create_table(mock_db_model)
    table_name = mock_db_model.table_name()
    params = ('text', None, getrandbits(10), random())
    connection = db_manager.connection

    execute(connection, query_str=f"INSERT INTO {table_name}(column_2, column_3, column_4, column_5)"
                                  f" VALUES (?, ?, ?, ?)", params=params, commit=True)
    row = db_manager.last_insert_rowid()
    row_data = execute(connection, f"SELECT * FROM {table_name} WHERE rowid = ?;", params=(row,),
                       commit=True)

    assert row_data.fetchone()[1:] == params  # skip rowid column
コード例 #2
0
def test_insert_scan_start(db_manager):
    db_manager.safe_create_table(ScanModel)

    start = datetime.now()
    query = insert_scan_start()
    execute(db_manager.connection, query, (start, ))

    res = execute(
        db_manager.connection,
        f"SELECT * FROM {ScanModel.table_name()} WHERE scan_start_time = ?",
        (start, ),
        commit=True).fetchone()
    assert res is not None
コード例 #3
0
def test_execute_with_dict_params(mock_db_model: Type[BaseModel], db_manager: DBManager):
    db_manager.safe_create_table(mock_db_model)
    table_name = mock_db_model.table_name()
    params = {'val_2': 'some text', 'val_3': None, 'val_4': getrandbits(10), 'val_5': random()}

    execute(db_manager.connection, query_str=f"INSERT INTO {table_name}(column_2, column_3, column_4, column_5)"
                                             f" VALUES (:val_2, :val_3,"
                                             f" :val_4, :val_5)",
            params=params, commit=True)

    row = db_manager.last_insert_rowid()
    row_data = execute(db_manager.connection, f"SELECT * FROM {table_name} WHERE rowid=:rowid;",
                       params={'rowid': row}, commit=True)

    assert row_data.fetchone()[1:] == tuple(value for key, value in params.items())  # skip rowid column
コード例 #4
0
def test_save_scan_results(scanner, random_dir_and_files, db_manager):
    num_of_files, dir_ = random_dir_and_files
    res = Scanner.scan(dir_)

    # Hack to work on the same db path
    old_path = DBManager._DEFAULT_DB_PATH
    DBManager._DEFAULT_DB_PATH = db_manager.db_path

    # init tables
    db_manager.create_table(FileRecordModel)
    db_manager.create_table(ScanModel)

    scan_id = scanner.save_scan_results(res)
    query = f"SELECT * FROM {FileRecordModel.table_name()} WHERE file_path=? and scan_id=?"
    DBManager._DEFAULT_DB_PATH = old_path  # Restore previous db path

    assert execute(db_manager.connection, query, (res[0].file_path, scan_id)).fetchone() is not None
コード例 #5
0
    def save_scan_results(cls, file_records: List[FileRecord], scan_id: Optional[int] = None):
        """ Saves scan results to local db for later use, returns scan_id """
        manager = DBManager()
        connection_ = manager.connection
        if not scan_id:
            execute(connection_, insert_scan_start(), (datetime.now(),))
            scan_id = manager.last_insert_rowid()

        execute(connection_, update_scan_completion_time_query(), (datetime.now(), scan_id), commit=True)

        for file_record in file_records:
            query, params = save_file_record_to_db(file_record, scan_id)
            execute(connection_, query, params)
        connection_.commit()

        return scan_id
コード例 #6
0
 def last_insert_rowid(self) -> int:
     """ returns the ROWID of the last row insert from the database connection"""
     data = execute(self.connection,
                    ModelQueries.last_insert_rowid()).fetchone()[0]
     return data
コード例 #7
0
 def table_exists(self, db_model: Type[BaseModel]) -> bool:
     if execute(self.connection,
                ModelQueries.table_exists(db_model)).fetchone() is None:
         return False
     return True
コード例 #8
0
 def create_table(self, db_model: Type[BaseModel]) -> None:
     execute(self.connection,
             query_str=ModelQueries.create_table_query(db_model),
             commit=True)