From f437939895f1f15dfeaa2bb50cab29efe5f7f612 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Tue, 19 Oct 2021 09:50:07 -0500 Subject: [PATCH] Add tests for get peers db method (#1659) --- tests/db/test_squeak_db.py | 21 +++++++++++++++++++++ tests/utils.py | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/tests/db/test_squeak_db.py b/tests/db/test_squeak_db.py index aff02348..ddbceffa 100644 --- a/tests/db/test_squeak_db.py +++ b/tests/db/test_squeak_db.py @@ -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) diff --git a/tests/utils.py b/tests/utils.py index 0818a420..1fc5646b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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, + )