Mercurial > repos > tduigou > get_db_info
diff testMock.py @ 0:41ac63b5d221 draft
planemo upload for repository https://github.com/brsynth commit 15dbdd1f0a222a8e1b0fb5c16b36885520a3d005
| author | tduigou |
|---|---|
| date | Thu, 10 Apr 2025 08:45:18 +0000 |
| parents | |
| children | 61158f32e5c3 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testMock.py Thu Apr 10 08:45:18 2025 +0000 @@ -0,0 +1,71 @@ +import psycopg2 +from psycopg2 import sql + +def create_db_and_insert_data(): + """Creates a database, a table, and inserts mock data into the PostgreSQL database.""" + # Connect to the PostgreSQL container (default 'postgres' database for setup) + conn = psycopg2.connect( + dbname='postgres', # Default database + user='postgres', # Default user + password='RK17', # Password from Docker environment + host='localhost', # Running locally on the default Docker network + port='5432' # Default PostgreSQL port + ) + + conn.autocommit = True # Necessary to create a database + cursor = conn.cursor() + + # Check if the test database already exists + cursor.execute("SELECT 1 FROM pg_catalog.pg_database WHERE datname = 'test_fragments_db';") + exists = cursor.fetchone() + + if exists: + print("Database 'test_fragments_db' already exists, dropping it...") + cursor.execute("DROP DATABASE test_fragments_db;") + + # Create the new database for testing + cursor.execute('CREATE DATABASE test_fragments_db;') + print("Database 'test_fragments_db' created.") + + cursor.close() + conn.close() + + # Now connect to the new test database + conn = psycopg2.connect( + dbname='test_fragments_db', + user='postgres', + password='RK17', + host='localhost', + port='5432' + ) + + cursor = conn.cursor() + + # Create the 'sample' table instead of 'fragments' + cursor.execute(''' + CREATE TABLE sample ( + fragment TEXT PRIMARY KEY, + sequence TEXT, + metadata_1 TEXT, + metadata_2 TEXT + ); + ''') + + # Insert mock data + cursor.executemany(''' + INSERT INTO sample (fragment, sequence, metadata_1, metadata_2) VALUES (%s, %s, %s, %s); + ''', [ + ('ACP10001AaCbbBS', 'seq for ACP10001AaCbbBS', 'Metadata1 for ACP10001AaCbbBS', 'Metadata2 for ACP10001AaCbbBS'), + ('CFP10002AaCbbBS', 'seq for CFP10002AaCbbBS', 'Metadata1 for CFP10002AaCbbBS', 'Metadata2 for CFP10002AaCbbBS'), + ('XYZ10003AaCbbBS', 'seq for XYZ10003AaCbbBS', 'Metadata1 for XYZ10003AaCbbBS', 'Metadata2 for XYZ10003AaCbbBS'), + ('QWE10004AaCbbBS', 'seq for QWE10004AaCbbBS', 'Metadata1 for QWE10004AaCbbBS', 'Metadata2 for QWE10004AaCbbBS') + ]) + + conn.commit() + print("Mock data inserted into 'sample' table.") + + cursor.close() + conn.close() + +if __name__ == "__main__": + create_db_and_insert_data()
