Add tests for get peers db method (#1659)

This commit is contained in:
Jonathan Zernik 2021-10-19 09:50:07 -05:00 committed by GitHub
parent 17ad673a33
commit f437939895
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View file

@ -31,6 +31,7 @@ from tests.utils import gen_contact_profile
from tests.utils import gen_random_hash
from tests.utils import gen_signing_key
from tests.utils import gen_signing_profile
from tests.utils import gen_squeak_peer
from tests.utils import gen_squeak_with_block_header
@ -265,6 +266,17 @@ def followed_contact_profile_ids(squeak_db, inserted_contact_profile_ids):
yield inserted_contact_profile_ids
@pytest.fixture
def inserted_squeak_peer_ids(squeak_db):
ret = []
for i in range(100):
peer_name = "peer_{}".format(i)
peer = gen_squeak_peer(peer_name)
peer_id = squeak_db.insert_peer(peer)
ret.append(peer_id)
yield ret
def test_init_with_retries(squeak_db):
with mock.patch.object(squeak_db, 'init', autospec=True) as mock_init, \
mock.patch('squeaknode.db.squeak_db.time.sleep', autospec=True) as mock_sleep:
@ -991,3 +1003,12 @@ def test_get_profile_by_name_none(
profile = squeak_db.get_profile_by_name(other_name)
assert profile is None
def test_get_squeak_peers(
squeak_db,
inserted_squeak_peer_ids,
):
peers = squeak_db.get_peers()
assert len(inserted_squeak_peer_ids) == len(peers)

View file

@ -21,12 +21,16 @@
# SOFTWARE.
import hashlib
import os
import random
import uuid
from bitcoin.core import CBlockHeader
from squeak.core.signing import CSigningKey
from squeak.core.signing import CSqueakAddress
from squeaknode.core.peer_address import Network
from squeaknode.core.peer_address import PeerAddress
from squeaknode.core.peers import create_saved_peer
from squeaknode.core.profiles import create_contact_profile
from squeaknode.core.profiles import create_signing_profile
from squeaknode.core.squeaks import HASH_LENGTH
@ -104,3 +108,17 @@ def gen_contact_profile(profile_name, address):
profile_name,
address,
)
def gen_squeak_peer(peer_name):
host = "random_host_{}".format(uuid.uuid1())
port = random.randint(1, 10000)
peer_address = PeerAddress(
network=Network.IPV4,
host=host,
port=port,
)
return create_saved_peer(
peer_name,
peer_address,
)