Skip to content

Commit

Permalink
generate fixture db only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 20, 2025
1 parent b250dda commit cc2a724
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
6 changes: 3 additions & 3 deletions counterparty-core/counterpartycore/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .mocks.bitcoind import bitcoind_mock
from .mocks.ledgerdb import ledger_db
from .mocks.bitcoind import bitcoind_mock, monkeymodule
from .mocks.ledgerdb import build_dbs, ledger_db

__all__ = ["bitcoind_mock", "ledger_db"]
__all__ = ["bitcoind_mock", "ledger_db", "build_dbs", "monkeymodule"]
12 changes: 12 additions & 0 deletions counterparty-core/counterpartycore/pytest/fixtures_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ def test_ledger(ledger_db):

assert len(transactions) == tx_count
assert len(burns) == burn_count


def test_ledger2(ledger_db):
cursor = ledger_db.cursor()
burns = cursor.execute("SELECT * FROM burns").fetchall()
transactions = cursor.execute("SELECT * FROM transactions").fetchall()

tx_count = len([tx for tx in ledgerdb.UNITTEST_FIXTURE if tx[0] != "mine_empty_blocks"])
burn_count = len([tx for tx in ledgerdb.UNITTEST_FIXTURE if tx[0] == "burn"])

assert len(transactions) == tx_count
assert len(burns) == burn_count
29 changes: 20 additions & 9 deletions counterparty-core/counterpartycore/pytest/mocks/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,27 @@ def search_pubkey(source, tx_hashes):
return DEFAULT_PARAMS["pubkey"][source]


@pytest.fixture(scope="function")
def bitcoind_mock(monkeypatch):
@pytest.fixture(scope="session")
def monkeymodule():
from _pytest.monkeypatch import MonkeyPatch

mpatch = MonkeyPatch()
yield mpatch
mpatch.undo()


@pytest.fixture(scope="session")
def bitcoind_mock(monkeymodule):
bitcoind_module = "counterpartycore.lib.backend.bitcoind"
gettxinfo_module = "counterpartycore.lib.parser.gettxinfo"
backend_module = "counterpartycore.lib.backend"
monkeypatch.setattr(f"{bitcoind_module}.list_unspent", list_unspent)
monkeypatch.setattr(f"{bitcoind_module}.satoshis_per_vbyte", satoshis_per_vbyte)
monkeypatch.setattr(f"{bitcoind_module}.get_vin_info", get_vin_info)
monkeypatch.setattr(f"{bitcoind_module}.get_utxo_address_and_value", get_utxo_address_and_value)
monkeypatch.setattr(f"{gettxinfo_module}.is_valid_der", is_valid_der)
monkeypatch.setattr(f"{backend_module}.search_pubkey", search_pubkey)
monkeypatch.setattr("counterpartycore.lib.messages.bet.date_passed", lambda x: False)
monkeymodule.setattr(f"{bitcoind_module}.list_unspent", list_unspent)
monkeymodule.setattr(f"{bitcoind_module}.satoshis_per_vbyte", satoshis_per_vbyte)
monkeymodule.setattr(f"{bitcoind_module}.get_vin_info", get_vin_info)
monkeymodule.setattr(
f"{bitcoind_module}.get_utxo_address_and_value", get_utxo_address_and_value
)
monkeymodule.setattr(f"{gettxinfo_module}.is_valid_der", is_valid_der)
monkeymodule.setattr(f"{backend_module}.search_pubkey", search_pubkey)
monkeymodule.setattr("counterpartycore.lib.messages.bet.date_passed", lambda x: False)
return sys.modules[__name__]
30 changes: 25 additions & 5 deletions counterparty-core/counterpartycore/pytest/mocks/ledgerdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from bitcoinutils.transactions import Transaction
from counterpartycore.lib import config
from counterpartycore.lib.api import composer
from counterpartycore.lib.api import composer, dbbuilder
from counterpartycore.lib.cli import server
from counterpartycore.lib.cli.main import arg_parser
from counterpartycore.lib.ledger.currentstate import CurrentState
Expand All @@ -29,8 +29,9 @@ def enable_all_protocol_changes():
os.remove(regtest_protocole_file)


@pytest.fixture(scope="function")
def ledger_db(bitcoind_mock):
@pytest.fixture(scope="session", autouse=True)
def build_dbs(bitcoind_mock):
print("Building databases...")
# prepare empty directory
if os.path.exists(DATA_DIR):
shutil.rmtree(DATA_DIR)
Expand All @@ -45,6 +46,7 @@ def ledger_db(bitcoind_mock):
DATA_DIR,
]
)
print("args", args)
server.initialise_log_and_config(args)

# initialise database
Expand All @@ -58,7 +60,7 @@ def ledger_db(bitcoind_mock):
bitcoind_mock.mine_block(db, [])

for tx_params in UNITTEST_FIXTURE:
print("tx_params", tx_params)
# print("tx_params", tx_params)
if isinstance(tx_params[1], tuple):
return db

Expand Down Expand Up @@ -87,4 +89,22 @@ def ledger_db(bitcoind_mock):
# re-enable all protocol changes
enable_all_protocol_changes()

return db
dbbuilder.build_state_db()


@pytest.fixture(scope="function")
def ledger_db(build_dbs):
tmpdir = os.path.join(DATA_DIR, "tmp")
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
os.makedirs(tmpdir)

database_path = os.path.join(tmpdir, "counterparty.regtest.db")
shutil.copyfile(config.DATABASE, database_path)

db = database.get_db_connection(database_path, read_only=False)

yield db

db.close()
shutil.rmtree(tmpdir)

0 comments on commit cc2a724

Please sign in to comment.