Skip to content

Commit

Permalink
Add unittests for mysql database
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Feb 6, 2024
1 parent 5d67936 commit 1545324
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 46 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/unittests-mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Bitcoinlib Tests Ubuntu MySQL
on: [push]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
python: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: config.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
UNITTEST_DATABASE: mysql
run: coverage run --source=bitcoinlib -m unittest -v

- name: Coveralls
uses: AndreMiras/coveralls-python-action@develop
with:
parallel: true
flag-name: Unit Test
debug: true

coveralls_finish:
needs: test
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: AndreMiras/coveralls-python-action@develop
with:
parallel-finished: true
debug: true
2 changes: 1 addition & 1 deletion .github/workflows/unittests-noscrypt.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu - No scrypt
name: Bitcoinlib Tests Ubuntu - No scrypt
on: [push]

jobs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu
name: Bitcoinlib Tests Ubuntu
on: [push]

jobs:
Expand All @@ -7,7 +7,7 @@ jobs:

strategy:
matrix:
python: ["3.8", "3.10", "3.11"]
python: ["3.8", "3.11"]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unittests_windows.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Windows Unittests
name: Bitcoinlib Tests Windows
on: [push]

jobs:
Expand Down
3 changes: 2 additions & 1 deletion bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def __init__(self, db_uri=None, password=None):
def drop_db(self, yes_i_am_sure=False):
if yes_i_am_sure:
self.session.commit()
self.session.close_all()
self.session.close()
session.close_all_sessions()
Base.metadata.drop_all(self.engine)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/db_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DbCache:
"""
Cache Database object. Initialize database and open session when creating database object.
Create new database if is doesn't exist yet
Create new database if it doesn't exist yet
"""
def __init__(self, db_uri=None):
Expand Down
9 changes: 4 additions & 5 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import logging
try:
import mysql.connector
import psycopg2
from psycopg2 import sql
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import psycopg
from psycopg import sql
# from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
except ImportError as e:
print("Could not import all modules. Error: %s" % e)
# from psycopg2cffi import compat # Use for PyPy support
Expand Down Expand Up @@ -818,8 +818,7 @@ def setUpClass(cls):
pass

try:
con = psycopg2.connect(user='postgres', host='localhost', password='postgres')
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
con = psycopg.connect(user='postgres', host='localhost', password='postgres', autocommit=True)
cur = con.cursor()
cur.execute(sql.SQL("CREATE DATABASE {}").format(
sql.Identifier('bitcoinlibcache.unittest'))
Expand Down
81 changes: 46 additions & 35 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import mysql.connector
import psycopg
from psycopg import sql
import testing.postgresql
except ImportError as e:
print("Could not import all modules. Error: %s" % e)
# from psycopg2cffi import compat # Use for PyPy support
Expand All @@ -42,60 +43,70 @@
DATABASE_NAME = 'bitcoinlib_test'
DATABASE_NAME_2 = 'bitcoinlib2_test'

db_uris = (
('sqlite', 'sqlite:///' + DATABASEFILE_UNITTESTS, 'sqlite:///' + DATABASEFILE_UNITTESTS_2),)
# db_uris = (
# ('sqlite', 'sqlite:///' + DATABASEFILE_UNITTESTS, 'sqlite:///' + DATABASEFILE_UNITTESTS_2),)

print("UNITTESTS_FULL_DATABASE_TEST: %s" % UNITTESTS_FULL_DATABASE_TEST)
print("DATABASE USED: %s" % os.getenv('UNITTEST_DATABASE'))

if UNITTESTS_FULL_DATABASE_TEST:
db_uris += (
('mysql', 'mysql://root:root@localhost:3306/' + DATABASE_NAME,
'mysql://root:root@localhost:3306/' + DATABASE_NAME_2),
('postgresql', 'postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME,
'postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME_2),
)


params = (('SCHEMA', 'DATABASE_URI', 'DATABASE_URI_2'), (
db_uris
))
# if UNITTESTS_FULL_DATABASE_TEST:
# db_uris += (
# ('mysql', 'mysql://root:root@localhost:3306/' + DATABASE_NAME,
# 'mysql://root:root@localhost:3306/' + DATABASE_NAME_2),
# ('postgresql', 'postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME,
# 'postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME_2),
# )
#
#
# params = (('SCHEMA', 'DATABASE_URI', 'DATABASE_URI_2'), (
# db_uris
# ))


def database_init(dbname=DATABASE_NAME):
session.close_all_sessions()
if os.getenv('UNITTEST_DATABASE') == 'postgresql':
con = psycopg.connect(user='postgres', host='localhost', password='postgres', autocommit=True)
cur = con.cursor()
try:
# cur.execute(sql.SQL("ALTER DATABASE {} allow_connections = off").format(sql.Identifier(dbname)))
cur.execute(sql.SQL("UPDATE pg_database SET datallowconn = 'false' WHERE datname = '{}'").format(
sql.Identifier(dbname)))
cur.execute(sql.SQL("SELECT pg_terminate_backend(pg_stat_activity.pid)"
"FROM pg_stat_activity WHERE pg_stat_activity.datname = '{}'"
"AND pid <> pg_backend_pid();").format(sql.Identifier(dbname)))
except Exception as e:
print(e)
# con = psycopg.connect(user='postgres', host='localhost', password='postgres', autocommit=True)
# cur = con.cursor()
# try:
# cur.execute(sql.SQL("ALTER DATABASE {} allow_connections = off").format(sql.Identifier(dbname)))
# cur.execute(sql.SQL("UPDATE pg_database SET datallowconn = 'false' WHERE datname = '{}'").format(
# sql.Identifier(dbname)))
# cur.execute(sql.SQL("SELECT pg_terminate_backend(pg_stat_activity.pid)"
# "FROM pg_stat_activity WHERE pg_stat_activity.datname = '{}'"
# "AND pid <> pg_backend_pid();").format(sql.Identifier(dbname)))
# except Exception as e:
# print(e)
# res = cur.execute(sql.SQL("SELECT sum(numbackends) FROM pg_stat_database"))
# print(res)
cur.execute(sql.SQL("DROP DATABASE IF EXISTS {}").format(sql.Identifier(dbname)))
cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(dbname)))
# try:
# drop all tables
# cur.execute(sql.SQL("""
# res = cur.execute(sql.SQL("""
# DO $$ DECLARE
# r RECORD;
# BEGIN
# FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
# EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
# END LOOP;
# END $$;"""))
# print(res)
# try:
# cur.execute(sql.SQL("DROP DATABASE IF EXISTS {}").format(sql.Identifier(dbname)))
# cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(dbname)))
# except:
# pass
# try:
# # drop all tables
# finally:
# cur.close()
# con.close()
con.commit()
cur.close()
con.close()
return 'postgresql://postgres:postgres@localhost:5432/' + dbname
# con = psycopg.connect(user='postgres', host='localhost', password='postgres', autocommit=True)
# cur = con.cursor()
# cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(dbname)))
# con.commit()
# cur.close()
# con.close()
# return 'postgresql://postgres:postgres@localhost:5432/' + dbname
# postgresql = testing.postgresql.Postgresql()
# return postgresql.url()
return 'testing.postgresql'
elif os.getenv('UNITTEST_DATABASE') == 'mysql':
con = mysql.connector.connect(user='user', host='localhost', password='password')
cur = con.cursor()
Expand Down

0 comments on commit 1545324

Please sign in to comment.