Add test for get sent offer by squeak and peer (#1693)

This commit is contained in:
Jonathan Zernik 2021-10-23 00:20:10 -05:00 committed by GitHub
parent 76e1bd29cc
commit dc6cef8cff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View file

@ -1205,6 +1205,8 @@ class SqueakDb:
"""
Get a sent offer by squeak hash and peer address host. Only
return sent offer if it's not expired and not paid.
TODO: add where clause for peer address network.
"""
s = (
select([self.sent_offers])

View file

@ -1269,3 +1269,76 @@ def test_get_sent_offer_missing(squeak_db, inserted_sent_offer_id):
)
assert retrieved_sent_offer is None
def test_get_sent_offer_by_squeak_and_peer(
squeak_db,
inserted_sent_offer_id,
sent_offer,
squeak_hash,
peer_address,
creation_date,
expiry,
):
expire_time_s = creation_date + expiry
current_time_s = expire_time_s - 10
fake_current_time_ms = current_time_s * 1000
with mock.patch.object(SqueakDb, 'timestamp_now_ms', new_callable=mock.PropertyMock) as mock_timestamp_ms:
mock_timestamp_ms.return_value = fake_current_time_ms
retrieved_sent_offer = squeak_db.get_sent_offer_by_squeak_hash_and_peer(
squeak_hash,
peer_address,
)
assert retrieved_sent_offer._replace(
sent_offer_id=None,
) == sent_offer
def test_get_sent_offer_by_squeak_and_peer_wrong_squeak_hash(
squeak_db,
inserted_sent_offer_id,
sent_offer,
peer_address,
creation_date,
expiry,
):
expire_time_s = creation_date + expiry
current_time_s = expire_time_s - 10
fake_current_time_ms = current_time_s * 1000
with mock.patch.object(SqueakDb, 'timestamp_now_ms', new_callable=mock.PropertyMock) as mock_timestamp_ms:
mock_timestamp_ms.return_value = fake_current_time_ms
retrieved_sent_offer = squeak_db.get_sent_offer_by_squeak_hash_and_peer(
gen_random_hash(),
peer_address,
)
assert retrieved_sent_offer is None
def test_get_sent_offer_by_squeak_and_peer_wrong_peer_address(
squeak_db,
inserted_sent_offer_id,
sent_offer,
squeak_hash,
peer_address,
creation_date,
expiry,
):
expire_time_s = creation_date + expiry
current_time_s = expire_time_s - 10
fake_current_time_ms = current_time_s * 1000
with mock.patch.object(SqueakDb, 'timestamp_now_ms', new_callable=mock.PropertyMock) as mock_timestamp_ms:
mock_timestamp_ms.return_value = fake_current_time_ms
retrieved_sent_offer = squeak_db.get_sent_offer_by_squeak_hash_and_peer(
squeak_hash,
peer_address._replace(host="wrong_host"),
)
assert retrieved_sent_offer is None