Simplify unit tests for interests (#1579)

This commit is contained in:
Jonathan Zernik 2021-10-12 15:11:38 -07:00 committed by GitHub
parent bae8063c28
commit 3dcc2681d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 35 deletions

View file

@ -40,7 +40,12 @@ def signing_key():
@pytest.fixture
def address(signing_key):
verifying_key = signing_key.get_verifying_key()
yield str(CSqueakAddress.from_verifying_key(verifying_key))
yield CSqueakAddress.from_verifying_key(verifying_key)
@pytest.fixture
def address_str(address):
yield str(address)
@pytest.fixture

View file

@ -23,16 +23,12 @@ from squeak.net import CInterested
from squeaknode.core.interests import get_differential_squeaks
from squeaknode.core.interests import squeak_matches_interest
from tests.utils import address_from_signing_key
from tests.utils import gen_random_hash
from tests.utils import gen_signing_key
from tests.utils import gen_squeak
from tests.utils import gen_squeak_addresses
def test_squeak_matches_interest():
signing_key = gen_signing_key()
address = address_from_signing_key(signing_key)
def test_squeak_matches_interest(signing_key, address):
squeak = gen_squeak(signing_key, 5678)
interest = CInterested(
addresses=(address,),
@ -43,9 +39,7 @@ def test_squeak_matches_interest():
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_empty_addresses():
signing_key = gen_signing_key()
address_from_signing_key(signing_key)
def test_squeak_matches_interest_empty_addresses(signing_key, address):
squeak = gen_squeak(signing_key, 5678)
interest = CInterested(
nMinBlockHeight=5000,
@ -55,9 +49,7 @@ def test_squeak_matches_interest_empty_addresses():
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_above_block_range():
signing_key = gen_signing_key()
address = address_from_signing_key(signing_key)
def test_squeak_matches_interest_above_block_range(signing_key, address):
squeak = gen_squeak(signing_key, 5678)
interest = CInterested(
addresses=(address,),
@ -68,9 +60,7 @@ def test_squeak_matches_interest_above_block_range():
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_below_block_range():
signing_key = gen_signing_key()
address = address_from_signing_key(signing_key)
def test_squeak_matches_interest_below_block_range(signing_key, address):
squeak = gen_squeak(signing_key, 5678)
interest = CInterested(
addresses=(address,),
@ -81,9 +71,7 @@ def test_squeak_matches_interest_below_block_range():
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_address_no_match():
signing_key = gen_signing_key()
address_from_signing_key(signing_key)
def test_squeak_matches_interest_address_no_match(signing_key, address):
squeak = gen_squeak(signing_key, 5678)
other_addresses = tuple(gen_squeak_addresses(3))
interest = CInterested(
@ -95,9 +83,7 @@ def test_squeak_matches_interest_address_no_match():
assert not squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_is_reply():
signing_key = gen_signing_key()
address_from_signing_key(signing_key)
def test_squeak_matches_interest_is_reply(signing_key):
replyto_hash = gen_random_hash()
squeak = gen_squeak(signing_key, 5678, replyto_hash=replyto_hash)
interest = CInterested(
@ -107,9 +93,7 @@ def test_squeak_matches_interest_is_reply():
assert squeak_matches_interest(squeak, interest)
def test_squeak_matches_interest_is_not_reply():
signing_key = gen_signing_key()
address_from_signing_key(signing_key)
def test_squeak_matches_interest_is_not_reply(signing_key):
replyto_hash = gen_random_hash()
squeak = gen_squeak(signing_key, 5678, replyto_hash=replyto_hash)
other_replyto_hash = gen_random_hash()

View file

@ -36,7 +36,7 @@ def private_key(signing_key):
@pytest.fixture
def invalid_address():
def invalid_address_str():
yield "abcdefg"
@ -52,22 +52,22 @@ def test_create_signing_profile_empty_name():
assert "Profile name cannot be empty." in str(excinfo.value)
def test_import_signing_profile(profile_name, private_key, address):
def test_import_signing_profile(profile_name, private_key, address_str):
profile = create_signing_profile(profile_name, private_key)
assert profile.profile_name == profile_name
assert profile.private_key == private_key.encode()
assert profile.address == address
assert profile.address == address_str
def test_create_contact_profile(profile_name, address):
profile = create_contact_profile(profile_name, address)
def test_create_contact_profile(profile_name, address_str):
profile = create_contact_profile(profile_name, address_str)
assert profile.profile_name == profile_name
assert profile.address == address
assert profile.address == address_str
def test_create_contact_profile_invalid_address(profile_name, invalid_address):
def test_create_contact_profile_invalid_address(profile_name, invalid_address_str):
with pytest.raises(Exception) as excinfo:
create_contact_profile(profile_name, invalid_address)
create_contact_profile(profile_name, invalid_address_str)
assert "Invalid squeak address" in str(excinfo.value)

View file

@ -210,16 +210,16 @@ def test_get_missing_squeak(squeak_db, squeak):
assert retrieved_squeak is None
def test_get_squeak_entry(squeak_db, squeak, block_header, address, inserted_squeak_hash):
def test_get_squeak_entry(squeak_db, squeak, block_header, address_str, inserted_squeak_hash):
retrieved_squeak_entry = squeak_db.get_squeak_entry(inserted_squeak_hash)
assert retrieved_squeak_entry.squeak_hash == inserted_squeak_hash
assert retrieved_squeak_entry.address == address
assert retrieved_squeak_entry.address == address_str
assert retrieved_squeak_entry.content is None
assert retrieved_squeak_entry.block_time == block_header.nTime
def test_get_missing_squeak_entry(squeak_db, squeak, address):
def test_get_missing_squeak_entry(squeak_db, squeak):
squeak_hash = get_hash(squeak)
retrieved_squeak_entry = squeak_db.get_squeak_entry(squeak_hash)