From 550c59ccb17f2f949930f1278d9e2335f80b77b0 Mon Sep 17 00:00:00 2001 From: derekpierre Date: Thu, 11 Apr 2024 12:42:49 -0400 Subject: [PATCH] Add mock_async_hooks fixture for use in tests that mimic the use of async txs. --- .../agents/test_coordinator_agent.py | 7 +++++- tests/fixtures.py | 25 +++++++++++-------- tests/unit/test_coordinator.py | 9 ++++--- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/tests/acceptance/agents/test_coordinator_agent.py b/tests/acceptance/agents/test_coordinator_agent.py index 076f4e3fe..f13deb896 100644 --- a/tests/acceptance/agents/test_coordinator_agent.py +++ b/tests/acceptance/agents/test_coordinator_agent.py @@ -112,7 +112,9 @@ def test_initiate_ritual( @pytest_twisted.inlineCallbacks -def test_post_transcript(agent, transcripts, transacting_powers, testerchain, clock): +def test_post_transcript( + agent, transcripts, transacting_powers, testerchain, clock, mock_async_hooks +): ritual_id = agent.number_of_rituals() - 1 txs = [] @@ -121,6 +123,7 @@ def test_post_transcript(agent, transcripts, transacting_powers, testerchain, cl ritual_id=ritual_id, transcript=transcripts[i], transacting_power=transacting_power, + async_tx_hooks=mock_async_hooks, ) txs.append(async_tx) @@ -159,6 +162,7 @@ def test_post_aggregation( cohort, testerchain, clock, + mock_async_hooks, ): testerchain.tx_machine.start() ritual_id = agent.number_of_rituals() - 1 @@ -173,6 +177,7 @@ def test_post_aggregation( public_key=dkg_public_key, participant_public_key=participant_public_key, transacting_power=transacting_power, + async_tx_hooks=mock_async_hooks, ) txs.append(async_tx) diff --git a/tests/fixtures.py b/tests/fixtures.py index 0054fe458..fabad89ea 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -3,14 +3,12 @@ import json import os import shutil import tempfile -import time from datetime import timedelta from functools import partial from pathlib import Path from typing import Tuple from unittest.mock import PropertyMock -import atxm import maya import pytest from click.testing import CliRunner @@ -22,7 +20,10 @@ from web3 import Web3 import tests from nucypher.blockchain.eth.actors import Operator -from nucypher.blockchain.eth.interfaces import BlockchainInterfaceFactory +from nucypher.blockchain.eth.interfaces import ( + BlockchainInterface, + BlockchainInterfaceFactory, +) from nucypher.blockchain.eth.signers.software import KeystoreSigner from nucypher.characters.lawful import Enrico, Ursula from nucypher.config.characters import ( @@ -783,12 +784,14 @@ def mock_operator_aggregation_delay(module_mocker): ) -@pytest.fixture(scope="session", autouse=True) -def mock_default_tracker_cache(session_mocker): - mock = session_mocker.patch.object( - atxm.state._State, - "_FILEPATH", - new_callable=session_mocker.PropertyMock, +@pytest.fixture +def mock_async_hooks(mocker): + hooks = BlockchainInterface.AsyncTxHooks( + on_broadcast=mocker.Mock(), + on_broadcast_failure=mocker.Mock(), + on_fault=mocker.Mock(), + on_finalized=mocker.Mock(), + on_insufficient_funds=mocker.Mock(), ) - mock.return_value = Path(tempfile.gettempdir()) / f".test-txs-{time.time()}.json" - return mock + + return hooks diff --git a/tests/unit/test_coordinator.py b/tests/unit/test_coordinator.py index ba20da0d4..9c2787a43 100644 --- a/tests/unit/test_coordinator.py +++ b/tests/unit/test_coordinator.py @@ -67,7 +67,7 @@ def test_mock_coordinator_initiation( def test_mock_coordinator_round_1( - nodes_transacting_powers, coordinator, random_transcript + nodes_transacting_powers, coordinator, random_transcript, mock_async_hooks ): ritual = coordinator._rituals[0] assert ( @@ -84,7 +84,8 @@ def test_mock_coordinator_round_1( coordinator.post_transcript( ritual_id=0, transcript=transcript, - transacting_power=nodes_transacting_powers[node_address] + transacting_power=nodes_transacting_powers[node_address], + async_tx_hooks=mock_async_hooks, ) performance = ritual.participants[index] @@ -105,6 +106,7 @@ def test_mock_coordinator_round_2( aggregated_transcript, dkg_public_key, random_transcript, + mock_async_hooks, ): ritual = coordinator._rituals[0] assert ( @@ -123,7 +125,8 @@ def test_mock_coordinator_round_2( aggregated_transcript=aggregated_transcript, public_key=dkg_public_key, participant_public_key=participant_public_key, - transacting_power=nodes_transacting_powers[node_address] + transacting_power=nodes_transacting_powers[node_address], + async_tx_hooks=mock_async_hooks, ) participant_public_keys.append(participant_public_key) if index == len(nodes_transacting_powers) - 1: