From 154532490d0ae36f5fca63cf7bebfd8f800f69c8 Mon Sep 17 00:00:00 2001 From: Cryp Toon Date: Tue, 6 Feb 2024 09:25:25 +0100 Subject: [PATCH] Add unittests for mysql database --- .github/workflows/unittests-mysql.yaml | 44 ++++++++++++ .github/workflows/unittests-noscrypt.yaml | 2 +- .github/workflows/unittests.yaml | 4 +- .github/workflows/unittests_windows.yaml | 2 +- bitcoinlib/db.py | 3 +- bitcoinlib/db_cache.py | 2 +- tests/test_services.py | 9 ++- tests/test_wallets.py | 81 +++++++++++++---------- 8 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/unittests-mysql.yaml diff --git a/.github/workflows/unittests-mysql.yaml b/.github/workflows/unittests-mysql.yaml new file mode 100644 index 00000000..ca6b2afa --- /dev/null +++ b/.github/workflows/unittests-mysql.yaml @@ -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 diff --git a/.github/workflows/unittests-noscrypt.yaml b/.github/workflows/unittests-noscrypt.yaml index 9dae687c..26ae6aaa 100644 --- a/.github/workflows/unittests-noscrypt.yaml +++ b/.github/workflows/unittests-noscrypt.yaml @@ -1,4 +1,4 @@ -name: Bitcoinlib Unittests Coveralls Ubuntu - No scrypt +name: Bitcoinlib Tests Ubuntu - No scrypt on: [push] jobs: diff --git a/.github/workflows/unittests.yaml b/.github/workflows/unittests.yaml index 28ae94bc..8b5c1293 100644 --- a/.github/workflows/unittests.yaml +++ b/.github/workflows/unittests.yaml @@ -1,4 +1,4 @@ -name: Bitcoinlib Unittests Coveralls Ubuntu +name: Bitcoinlib Tests Ubuntu on: [push] jobs: @@ -7,7 +7,7 @@ jobs: strategy: matrix: - python: ["3.8", "3.10", "3.11"] + python: ["3.8", "3.11"] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/unittests_windows.yaml b/.github/workflows/unittests_windows.yaml index 560fc4e6..183c6451 100644 --- a/.github/workflows/unittests_windows.yaml +++ b/.github/workflows/unittests_windows.yaml @@ -1,4 +1,4 @@ -name: Bitcoinlib Windows Unittests +name: Bitcoinlib Tests Windows on: [push] jobs: diff --git a/bitcoinlib/db.py b/bitcoinlib/db.py index f8e9b2c2..b82cbeab 100644 --- a/bitcoinlib/db.py +++ b/bitcoinlib/db.py @@ -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 diff --git a/bitcoinlib/db_cache.py b/bitcoinlib/db_cache.py index 18b99fd1..98d9b7f3 100644 --- a/bitcoinlib/db_cache.py +++ b/bitcoinlib/db_cache.py @@ -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): diff --git a/tests/test_services.py b/tests/test_services.py index fdd2a558..c29ce3ba 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -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 @@ -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')) diff --git a/tests/test_wallets.py b/tests/test_wallets.py index a9890897..0fe0014f 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -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 @@ -42,46 +43,42 @@ 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 @@ -89,13 +86,27 @@ def database_init(dbname=DATABASE_NAME): # 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()