From dd927036a5cd325021b1a5b5ee0e7816a6b37c37 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Tue, 12 Oct 2021 19:00:44 -0700 Subject: [PATCH] Add block_info fixture to global conftest (#1584) --- tests/bitcoin/test_bitcoin_core_client.py | 42 -------------------- tests/conftest.py | 48 +++++++++++++++++++---- tests/core/test_squeak_core.py | 12 +++--- tests/core/test_squeaks.py | 4 +- 4 files changed, 48 insertions(+), 58 deletions(-) diff --git a/tests/bitcoin/test_bitcoin_core_client.py b/tests/bitcoin/test_bitcoin_core_client.py index edb1514b..f6157fb1 100644 --- a/tests/bitcoin/test_bitcoin_core_client.py +++ b/tests/bitcoin/test_bitcoin_core_client.py @@ -23,7 +23,6 @@ import json import mock import pytest -from bitcoin.core import CBlockHeader from requests import HTTPError from requests.exceptions import ConnectionError from requests.exceptions import RequestException @@ -66,47 +65,6 @@ def bitcoin_core_client(bitcoin_host, bitcoin_port, bitcoin_user, bitcoin_pass): ) -@pytest.fixture -def block_count(): - yield 555 - - -@pytest.fixture -def block_hash_str(): - # Block hash of block at height 555 - yield '00000000edade40797e3c4bf27edeb65733d1884beaa8c502a89d50a54111e1c' - - -@pytest.fixture -def block_hash(block_hash_str): - yield bytes.fromhex(block_hash_str) - - -@pytest.fixture -def block_header_str(): - # Block header of block at height 555 - yield '0100000079c30d2c23727a1e9f5feda4e7feb8ea0bda2ab98e23e7f6a9cf594f00000000b0de897e42fa7a3b5c3a6bfb8e797acf4ffbc16169394b03ad93296524ed633dcfef6e49ffff001d36d19a6c' - - -@pytest.fixture -def block_header_bytes(block_header_str): - yield bytes.fromhex(block_header_str) - - -@pytest.fixture -def block_header(block_header_bytes): - yield CBlockHeader.deserialize(block_header_bytes) - - -@pytest.fixture -def block_info(block_count, block_hash, block_header): - yield BlockInfo( - block_height=block_count, - block_hash=block_hash, - block_header=block_header, - ) - - class MockResponse: def json(self): diff --git a/tests/conftest.py b/tests/conftest.py index 4703fda2..aefc5be6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import pytest -from bitcoin.core import CoreMainParams +from bitcoin.core import CBlockHeader from squeak.core.signing import CSigningKey from squeak.core.signing import CSqueakAddress @@ -49,11 +49,43 @@ def address_str(address): @pytest.fixture -def genesis_block_info(): +def block_count(): + yield 555 + + +@pytest.fixture +def block_hash_str(): + # Block hash of block at height 555 + yield '00000000edade40797e3c4bf27edeb65733d1884beaa8c502a89d50a54111e1c' + + +@pytest.fixture +def block_hash(block_hash_str): + yield bytes.fromhex(block_hash_str) + + +@pytest.fixture +def block_header_str(): + # Block header of block at height 555 + yield '0100000079c30d2c23727a1e9f5feda4e7feb8ea0bda2ab98e23e7f6a9cf594f00000000b0de897e42fa7a3b5c3a6bfb8e797acf4ffbc16169394b03ad93296524ed633dcfef6e49ffff001d36d19a6c' + + +@pytest.fixture +def block_header_bytes(block_header_str): + yield bytes.fromhex(block_header_str) + + +@pytest.fixture +def block_header(block_header_bytes): + yield CBlockHeader.deserialize(block_header_bytes) + + +@pytest.fixture +def block_info(block_count, block_hash, block_header): yield BlockInfo( - block_height=0, - block_hash=CoreMainParams.GENESIS_BLOCK.GetHash(), - block_header=CoreMainParams.GENESIS_BLOCK.get_header().serialize(), + block_height=block_count, + block_hash=block_hash, + block_header=block_header, ) @@ -63,12 +95,12 @@ def squeak_content(): @pytest.fixture -def squeak_and_secret_key(signing_key, squeak_content, genesis_block_info): +def squeak_and_secret_key(signing_key, squeak_content, block_info): yield make_squeak_with_block( signing_key, squeak_content, - genesis_block_info.block_height, - genesis_block_info.block_hash, + block_info.block_height, + block_info.block_hash, ) diff --git a/tests/core/test_squeak_core.py b/tests/core/test_squeak_core.py index 7b0cbe73..12be8cc6 100644 --- a/tests/core/test_squeak_core.py +++ b/tests/core/test_squeak_core.py @@ -217,8 +217,8 @@ class MockLightningClient(LightningClient): @pytest.fixture -def bitcoin_client(genesis_block_info): - return MockBitcoinClient(genesis_block_info) +def bitcoin_client(block_info): + return MockBitcoinClient(block_info) @pytest.fixture @@ -312,11 +312,11 @@ def test_make_squeak_with_contact_profile( def test_get_block_header( squeak_core, squeak, - genesis_block_info, + block_info, ): block_header = squeak_core.get_block_header(squeak) - assert block_header == genesis_block_info.block_header + assert block_header == block_info.block_header def test_get_block_header_invalid_block_hash( @@ -332,10 +332,10 @@ def test_check_squeak(squeak_core, squeak): squeak_core.check_squeak(squeak) -def test_get_best_block_height(squeak_core, genesis_block_info): +def test_get_best_block_height(squeak_core, block_info): best_block_height = squeak_core.get_best_block_height() - assert best_block_height == genesis_block_info.block_height + assert best_block_height == block_info.block_height def test_create_offer(squeak_core, squeak, secret_key, peer_address, price_msat): diff --git a/tests/core/test_squeaks.py b/tests/core/test_squeaks.py index 4cfb2c84..a98e8d38 100644 --- a/tests/core/test_squeaks.py +++ b/tests/core/test_squeaks.py @@ -24,8 +24,8 @@ from squeaknode.core.squeaks import get_decrypted_content from squeaknode.core.squeaks import get_payment_point_of_secret_key -def test_make_squeak(squeak): - assert squeak.nBlockHeight == 0 +def test_make_squeak(squeak, block_count): + assert squeak.nBlockHeight == block_count def test_check_squeak(squeak):