From 67437e4f0acc0acc0e27c0369ee739fc200e2461 Mon Sep 17 00:00:00 2001 From: Cryp Toon Date: Sat, 3 Feb 2024 23:04:51 +0100 Subject: [PATCH] Fix mysql issues and database unittesting --- bitcoinlib/db.py | 10 +- tests/test_tools.py | 161 ++++++------ tests/test_wallets.py | 592 ++++++++++++++++++++++-------------------- 3 files changed, 394 insertions(+), 369 deletions(-) diff --git a/bitcoinlib/db.py b/bitcoinlib/db.py index b3ebadac..f5d966d7 100644 --- a/bitcoinlib/db.py +++ b/bitcoinlib/db.py @@ -286,7 +286,7 @@ class DbKey(Base): "depth=1 are the masterkeys children.") change = Column(Integer, doc="Change or normal address: Normal=0, Change=1") address_index = Column(BigInteger, doc="Index of address in HD key structure address level") - public = Column(LargeBinary(32), index=True, doc="Bytes representation of public key") + public = Column(LargeBinary(33), index=True, doc="Bytes representation of public key") private = Column(EncryptedBinary(48), doc="Bytes representation of private key") wif = Column(EncryptedString(128), index=True, doc="Public or private WIF (Wallet Import Format) representation") compressed = Column(Boolean, default=True, doc="Is key compressed or not. Default is True") @@ -309,7 +309,7 @@ class DbKey(Base): "Default is False") network_name = Column(String(20), ForeignKey('networks.name'), doc="Name of key network, i.e. bitcoin, litecoin, dash") - latest_txid = Column(LargeBinary(32), doc="TxId of latest transaction downloaded from the blockchain") + latest_txid = Column(LargeBinary(33), doc="TxId of latest transaction downloaded from the blockchain") witness_type = Column(String(20), default='segwit', doc="Key witness type, only specify when using mixed wallets. Can be 'legacy', 'segwit' or " "'p2sh-segwit'. Default is segwit.") @@ -369,7 +369,7 @@ class DbTransaction(Base): __tablename__ = 'transactions' id = Column(Integer, Sequence('transaction_id_seq'), primary_key=True, doc="Unique transaction index for internal usage") - txid = Column(LargeBinary(32), index=True, doc="Bytes representation of transaction ID") + txid = Column(LargeBinary(33), index=True, doc="Bytes representation of transaction ID") wallet_id = Column(Integer, ForeignKey('wallets.id'), index=True, doc="ID of wallet which contains this transaction") account_id = Column(Integer, index=True, doc="ID of account") @@ -443,7 +443,7 @@ class DbTransactionInput(Base): witnesses = Column(LargeBinary, doc="Witnesses (signatures) used in Segwit transaction inputs") witness_type = Column(String(20), default='segwit', doc="Type of transaction, can be legacy, segwit or p2sh-segwit. Default is segwit") - prev_txid = Column(LargeBinary(32), + prev_txid = Column(LargeBinary(33), doc="Transaction hash of previous transaction. Previous unspent outputs (UTXO) is spent " "in this input") output_n = Column(BigInteger, doc="Output_n of previous transaction output that is spent in this input") @@ -487,7 +487,7 @@ class DbTransactionOutput(Base): "'nulldata', 'unknown', 'p2wpkh', 'p2wsh', 'p2tr'. Default is p2pkh") value = Column(BigInteger, default=0, doc="Total transaction output value") spent = Column(Boolean, default=False, doc="Indicated if output is already spent in another transaction") - spending_txid = Column(LargeBinary(32), doc="Transaction hash of input which spends this output") + spending_txid = Column(LargeBinary(33), doc="Transaction hash of input which spends this output") spending_index_n = Column(Integer, doc="Index number of transaction input which spends this output") __table_args__ = (UniqueConstraint('transaction_id', 'output_n', name='constraint_transaction_output_unique'),) diff --git a/tests/test_tools.py b/tests/test_tools.py index 6eb62a22..91855a4b 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -4,6 +4,7 @@ # Unit Tests for Bitcoinlib Tools # © 2018 - 2024 January - 1200 Web Development # + import ast import os import sys @@ -13,7 +14,6 @@ try: import mysql.connector import psycopg2 - from parameterized import parameterized_class from psycopg2 import sql from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT except ImportError: @@ -27,60 +27,61 @@ DATABASE_NAME = 'bitcoinlib_unittest' -def init_sqlite(_): - if os.path.isfile(SQLITE_DATABASE_FILE): - os.remove(SQLITE_DATABASE_FILE) - - -def init_postgresql(_): - con = psycopg2.connect(user='postgres', host='localhost', password='postgres') - con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - cur = con.cursor() - cur.execute(sql.SQL("DROP DATABASE IF EXISTS {}").format( - sql.Identifier(DATABASE_NAME)) - ) - cur.execute(sql.SQL("CREATE DATABASE {}").format( - sql.Identifier(DATABASE_NAME)) - ) - cur.close() - con.close() - - -def init_mysql(_): - con = mysql.connector.connect(user='root', host='localhost') - cur = con.cursor() - cur.execute("DROP DATABASE IF EXISTS {}".format(DATABASE_NAME)) - cur.execute("CREATE DATABASE {}".format(DATABASE_NAME)) - con.commit() - cur.close() - con.close() - - -# db_uris = (('sqlite:///' + SQLITE_DATABASE_FILE, init_sqlite),) -# if UNITTESTS_FULL_DATABASE_TEST: -# db_uris += ( -# ('mysql://root@localhost:3306/' + DATABASE_NAME, init_mysql), -# ('postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME, init_postgresql), -# ) +def database_init(): + + def init_sqlite(): + if os.path.isfile(SQLITE_DATABASE_FILE): + os.remove(SQLITE_DATABASE_FILE) + + + def init_postgresql(): + con = psycopg2.connect(user='postgres', host='localhost', password='postgres') + con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) + cur = con.cursor() + cur.execute(sql.SQL("DROP DATABASE IF EXISTS {}").format( + sql.Identifier(DATABASE_NAME)) + ) + cur.execute(sql.SQL("CREATE DATABASE {}").format( + sql.Identifier(DATABASE_NAME)) + ) + cur.close() + con.close() + + + def init_mysql(): + con = mysql.connector.connect(user='user', host='localhost', password='password') + cur = con.cursor() + cur.execute("DROP DATABASE IF EXISTS {}".format(DATABASE_NAME)) + cur.execute("CREATE DATABASE {}".format(DATABASE_NAME)) + con.commit() + cur.close() + con.close() + + if os.getenv('UNITTEST_DATABASE') == 'mysql': + init_mysql() + return 'mysql://user:password@localhost:3306/' + DATABASE_NAME + elif os.getenv('UNITTEST_DATABASE') == 'postgresql': + init_postgresql() + return 'postgresql://postgres:postgres@localhost:5432/' + DATABASE_NAME + else: + init_sqlite() + return SQLITE_DATABASE_FILE -# @parameterized_class(('DATABASE_URI', 'init_fn'), db_uris) class TestToolsCommandLineWallet(unittest.TestCase): def setUp(self): - # self.init_fn() - init_sqlite(self) self.python_executable = sys.executable self.clw_executable = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../bitcoinlib/tools/clw.py')) - self.DATABASE_URI = SQLITE_DATABASE_FILE + self.database_uri = database_init() def test_tools_clw_create_wallet(self): cmd_wlt_create = '%s %s new -w test --passphrase "emotion camp sponsor curious bacon squeeze bean world ' \ 'actual chicken obscure spray" -r -d %s' % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_delete = "%s %s -w test --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "bc1qdv5tuzrluh4lzhnu59je9n83w4hkqjhgg44d5g" output_wlt_delete = "Wallet test has been removed" @@ -99,9 +100,9 @@ def test_tools_clw_create_multisig_wallet(self): 'MeQHdWDp' ] cmd_wlt_create = "%s %s new -w testms -m 2 2 %s -r -n testnet -d %s -o 0" % \ - (self.python_executable, self.clw_executable, ' '.join(key_list), self.DATABASE_URI) + (self.python_executable, self.clw_executable, ' '.join(key_list), self.database_uri) cmd_wlt_delete = "%s %s -w testms --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "2NBrLTapyFqU4Wo29xG4QeEt8kn38KVWRR" output_wlt_delete = "Wallet testms has been removed" @@ -118,9 +119,9 @@ def test_tools_clw_create_multisig_wallet_one_key(self): '5zNYeiX8' ] cmd_wlt_create = "%s %s new -w testms1 -m 2 2 %s -r -n testnet -d %s -o 0" % \ - (self.python_executable, self.clw_executable, ' '.join(key_list), self.DATABASE_URI) + (self.python_executable, self.clw_executable, ' '.join(key_list), self.database_uri) cmd_wlt_delete = "%s %s -w testms1 --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "if you understood and wrote down your key: Receive address:" output_wlt_delete = "Wallet testms1 has been removed" @@ -133,7 +134,7 @@ def test_tools_clw_create_multisig_wallet_one_key(self): def test_tools_clw_create_multisig_wallet_error(self): cmd_wlt_create = "%s %s new -w testms2 -m 2 a -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "Number of total signatures (second argument) must be a numeric value" process = Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True) poutput = process.communicate(input=b'y') @@ -142,13 +143,13 @@ def test_tools_clw_create_multisig_wallet_error(self): def test_tools_clw_transaction_with_script(self): cmd_wlt_create = '%s %s new -w test2 --passphrase "emotion camp sponsor curious bacon squeeze bean world ' \ 'actual chicken obscure spray" -r -n bitcoinlib_test -d %s' % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_update = "%s %s -w test2 -x -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_transaction = "%s %s -w test2 -d %s -s 21HVXMEdxdgjNzgfERhPwX4okXZ8WijHkvu 0.5 -f 100000 -p" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_delete = "%s %s -w test2 --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "blt1qj0mgwyhxuw9p0ngj5kqnxhlrx8ypecqekm2gr7" output_wlt_transaction = 'Transaction pushed to network' output_wlt_delete = "Wallet test2 has been removed" @@ -171,9 +172,9 @@ def test_tools_clw_transaction_with_script(self): def test_tools_clw_create_litecoin_segwit_wallet(self): cmd_wlt_create = '%s %s new -w ltcsw --passphrase "lounge chief tip frog camera build trouble write end ' \ 'sword order share" -d %s -j segwit -n litecoin -r' % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_delete = "%s %s -w ltcsw --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "ltc1qgc7c2z56rr4lftg0fr8tgh2vknqc3yuydedu6m" output_wlt_delete = "Wallet ltcsw has been removed" @@ -194,9 +195,9 @@ def test_tools_clw_create_multisig_wallet_p2sh_segwit(self): '7FW6wpKW' ] cmd_wlt_create = "%s %s new -w testms-p2sh-segwit -m 2 3 %s -r -j p2sh-segwit -d %s -o 0" % \ - (self.python_executable, self.clw_executable, ' '.join(key_list), self.DATABASE_URI) + (self.python_executable, self.clw_executable, ' '.join(key_list), self.database_uri) cmd_wlt_delete = "%s %s -w testms-p2sh-segwit --wallet-remove -d %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output_wlt_create = "3MtNi5U2cjs3EcPizzjarSz87pU9DTANge" output_wlt_delete = "Wallet testms-p2sh-segwit has been removed" @@ -218,7 +219,7 @@ def test_tools_wallet_create_from_key(self): phrase = ("hover rescue clock ocean strategy post melt banner anxiety phone pink paper enhance more " "copy gate bag brass raise logic stone duck muffin conduct") cmd_wlt_create = "%s %s new -w wlt_from_key -c \"%s\" -d %s -y" % \ - (self.python_executable, self.clw_executable, phrase, self.DATABASE_URI) + (self.python_executable, self.clw_executable, phrase, self.database_uri) output_wlt_create = "bc1qpylcrcyqa5wkwe2stzc6h7q0mhs5skxuas44w2" poutput = Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate() @@ -228,11 +229,11 @@ def test_tools_wallet_send_to_multi(self): send_str = ("-s blt1qzt90vqqjsqspuaegu9fh4e2htaxrgt0l76d9gz 0.1 " "-s blt1qu825hm0a6ajg66j79x4tzkn56qmljjms97c5tp 1") cmd_wlt_create = "%s %s new -w wallet_send_to_multi -d %s -n bitcoinlib_test -yq" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_update = "%s %s -w wallet_send_to_multi -d %s -x" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_send = "%s %s -w wallet_send_to_multi -d %s %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI, send_str) + (self.python_executable, self.clw_executable, self.database_uri, send_str) Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate() Popen(cmd_wlt_update, stdin=PIPE, stdout=PIPE, shell=True).communicate() @@ -243,31 +244,31 @@ def test_tools_wallet_empty(self): pk = ("zprvAWgYBBk7JR8GiejuVoZaVXtWf5zNawFbTH88uKao9qnZxBypJQNvh1tGHZghpfjUfSUiS7G7MmNw3cyakkNcNis3MjD4ic54n" "FY5LQxMszQ") cmd_wlt_create = "%s %s new -w wlt_create_and_empty -c %s -d %s -y" % \ - (self.python_executable, self.clw_executable, pk, self.DATABASE_URI) + (self.python_executable, self.clw_executable, pk, self.database_uri) output_wlt_create = "bc1qqnqkjpnmr5zsxar76wxqcntp28ltly0fz6crdg" poutput = Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate() self.assertIn(output_wlt_create, normalize_string(poutput[0])) cmd_wlt_empty = "%s %s -w wlt_create_and_empty -d %s --wallet-empty" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) poutput = Popen(cmd_wlt_empty, stdin=PIPE, stdout=PIPE, shell=True).communicate() self.assertIn("Removed transactions and emptied wallet", normalize_string(poutput[0])) cmd_wlt_info = "%s %s -w wlt_create_and_empty -d %s -i" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) poutput = Popen(cmd_wlt_info, stdin=PIPE, stdout=PIPE, shell=True).communicate() self.assertIn("- - Transactions Account 0 (0)", normalize_string(poutput[0])) self.assertNotIn(output_wlt_create, normalize_string(poutput[0])) def test_tools_wallet_sweep(self): cmd_wlt_create = "%s %s new -w wlt_sweep -d %s -n bitcoinlib_test -yq" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_update = "%s %s -w wlt_sweep -d %s -x" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_send = "%s %s -w wlt_sweep -d %s --sweep blt1qzt90vqqjsqspuaegu9fh4e2htaxrgt0l76d9gz -p" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_info = "%s %s -w wlt_sweep -d %s -i" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate() Popen(cmd_wlt_update, stdin=PIPE, stdout=PIPE, shell=True).communicate() process = Popen(cmd_wlt_send, stdin=PIPE, stdout=PIPE, shell=True) @@ -290,34 +291,34 @@ def test_tools_wallet_multisig_cosigners(self): pub_key3 = ('BC11mYrL5yBtMgaYxHEUg3anvLX3gcLi8hbtwbjymReCgGiP6hYifVMi96M3ejtvZpZbDvetBfbzgRxmu22ZkqP2i7yhFge' 'mSkHp7BRhoDubrQvs') cmd_wlt_create1 = "%s %s new -w wlt_multisig_2_3_A -m 2 3 %s %s %s -d %s -n bitcoinlib_test -q" % \ - (self.python_executable, self.clw_executable, pk1, pub_key2, pub_key3, self.DATABASE_URI) + (self.python_executable, self.clw_executable, pk1, pub_key2, pub_key3, self.database_uri) Popen(cmd_wlt_create1, stdin=PIPE, stdout=PIPE, shell=True).communicate() cmd_wlt_create2 = "%s %s new -w wlt_multisig_2_3_B -m 2 3 %s %s %s -d %s -n bitcoinlib_test -q" % \ - (self.python_executable, self.clw_executable, pub_key1, pub_key2, pk3, self.DATABASE_URI) + (self.python_executable, self.clw_executable, pub_key1, pub_key2, pk3, self.database_uri) Popen(cmd_wlt_create2, stdin=PIPE, stdout=PIPE, shell=True).communicate() cmd_wlt_receive1 = "%s %s -w wlt_multisig_2_3_A -d %s -r -o 1 -q" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output1 = Popen(cmd_wlt_receive1, stdin=PIPE, stdout=PIPE, shell=True).communicate() cmd_wlt_receive2 = "%s %s -w wlt_multisig_2_3_B -d %s -r -o 1 -q" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output2 = Popen(cmd_wlt_receive2, stdin=PIPE, stdout=PIPE, shell=True).communicate() self.assertEqual(output1[0], output2[0]) address = normalize_string(output1[0].strip(b'\n')) cmd_wlt_update1 = "%s %s -w wlt_multisig_2_3_A -d %s -x -o 1" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) Popen(cmd_wlt_update1, stdin=PIPE, stdout=PIPE, shell=True).communicate() cmd_wlt_update2 = "%s %s -w wlt_multisig_2_3_B -d %s -x -o 1" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) Popen(cmd_wlt_update2, stdin=PIPE, stdout=PIPE, shell=True).communicate() create_tx = "%s %s -w wlt_multisig_2_3_A -d %s -s %s 0.5 -o 1" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI, address) + (self.python_executable, self.clw_executable, self.database_uri, address) output = Popen(create_tx, stdin=PIPE, stdout=PIPE, shell=True).communicate() tx_dict_str = '{' + normalize_string(output[0]).split('{', 1)[1] sign_tx = "%s %s -w wlt_multisig_2_3_B -d %s -o 1 --import-tx \"%s\"" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI, + (self.python_executable, self.clw_executable, self.database_uri, tx_dict_str.replace('\r', '').replace('\n', '')) output = Popen(sign_tx, stdin=PIPE, stdout=PIPE, shell=True).communicate() response = normalize_string(output[0]) @@ -327,7 +328,7 @@ def test_tools_wallet_multisig_cosigners(self): filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'import_test.tx') sign_import_tx_file = "%s %s -w wlt_multisig_2_3_B -d %s -o 1 --import-tx-file %s" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI, filename) + (self.python_executable, self.clw_executable, self.database_uri, filename) output = Popen(sign_import_tx_file, stdin=PIPE, stdout=PIPE, shell=True).communicate() response2 = normalize_string(output[0]) self.assertIn('2e07be62d933f5b257ac066b874df651cd6e6763795c24036904024a2b44180b', response2) @@ -337,12 +338,12 @@ def test_tools_wallet_multisig_cosigners(self): def test_tools_transaction_options(self): cmd_wlt_create = "%s %s new -w test_tools_transaction_options -d %s -n bitcoinlib_test -yq" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_update = "%s %s -w test_tools_transaction_options -d %s -x" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) cmd_wlt_send = ("%s %s -w test_tools_transaction_options -d %s -s blt1qg7du8cs0scxccmfly7x252qurv7kwsy6rm4xr7 0.001 " "--number-of-change-outputs 5") % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) Popen(cmd_wlt_create, stdin=PIPE, stdout=PIPE, shell=True).communicate() Popen(cmd_wlt_update, stdin=PIPE, stdout=PIPE, shell=True).communicate() output = normalize_string(Popen(cmd_wlt_send, stdin=PIPE, stdout=PIPE, shell=True).communicate()[0]) @@ -352,14 +353,14 @@ def test_tools_transaction_options(self): self.assertTrue(tx_dict['verified']) cmd_wlt_update2 = "%s %s -w test_tools_transaction_options -d %s -ix" % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI) + (self.python_executable, self.clw_executable, self.database_uri) output = normalize_string(Popen(cmd_wlt_update2, stdin=PIPE, stdout=PIPE, shell=True).communicate()[0]) output_list = [i for i in output.split('Keys')[1].split(' ') if i != ''] first_key_id = int(output_list[1]) address = output_list[3] cmd_wlt_send2 = ("%s %s -w test_tools_transaction_options -d %s " "-s blt1qdjre3yw9hnt53entkp6tflhg34y4sp999emjnk 0.5 -k %d") % \ - (self.python_executable, self.clw_executable, self.DATABASE_URI, first_key_id) + (self.python_executable, self.clw_executable, self.database_uri, first_key_id) output = normalize_string(Popen(cmd_wlt_send2, stdin=PIPE, stdout=PIPE, shell=True).communicate()[0]) self.assertIn(address, output) self.assertIn("Transaction created", output) diff --git a/tests/test_wallets.py b/tests/test_wallets.py index 41898565..23b447bb 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -43,6 +43,8 @@ DATABASEFILE_UNITTESTS_2 = os.path.join(str(BCL_DATABASE_DIR), 'bitcoinlib.unittest2.sqlite') DATABASE_NAME = 'bitcoinlib_test' DATABASE_NAME_2 = 'bitcoinlib2_test' +# SQLITE_DATABASE_FILE = os.path.join(str(BCL_DATABASE_DIR), '%s.sqlite' % DATABASE_NAME) +# SQLITE_DATABASE_FILE2 = os.path.join(str(BCL_DATABASE_DIR), '%s.sqlite' % DATABASE_NAME_2) db_uris = ( ('sqlite', 'sqlite:///' + DATABASEFILE_UNITTESTS, 'sqlite:///' + DATABASEFILE_UNITTESTS_2),) @@ -134,16 +136,45 @@ def db_remove(cls): con.close() -@parameterized_class(*params) -class TestWalletCreate(TestWalletMixin, unittest.TestCase): +def database_init(dbname=DATABASE_NAME): + + print(os.getenv('UNITTEST_DATABASE')) + if os.getenv('UNITTEST_DATABASE') == 'postgresql': + con = psycopg2.connect(user='postgres', host='localhost', password='postgres') + con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) + cur = con.cursor() + cur.execute(sql.SQL("DROP DATABASE IF EXISTS {}").format( + sql.Identifier(dbname)) + ) + cur.close() + con.close() + return 'postgresql://postgres:postgres@localhost:5432/' + dbname + elif os.getenv('UNITTEST_DATABASE') == 'mysql': + con = mysql.connector.connect(user='user', host='localhost', password='password') + cur = con.cursor() + cur.execute("DROP DATABASE IF EXISTS {}".format(dbname)) + cur.execute("CREATE DATABASE {}".format(dbname)) + con.commit() + cur.close() + con.close() + return 'mysql://user:password@localhost:3306/' + dbname + else: + dburi = os.path.join(str(BCL_DATABASE_DIR), '%s.sqlite' % dbname) + if os.path.isfile(dburi): + os.remove(dburi) + return dburi + + +class TestWalletCreate(unittest.TestCase): wallet = None @classmethod def setUpClass(cls): - cls.db_remove() + # cls.database_uri = database_init() + cls.database_uri = database_init() cls.wallet = Wallet.create( name='test_wallet_create', witness_type='legacy', - db_uri=cls.DATABASE_URI) + db_uri=cls.database_uri) def test_wallet_create(self): self.assertTrue(isinstance(self.wallet, Wallet)) @@ -155,8 +186,8 @@ def test_wallet_info(self): self.assertIn("