From af0e9c81b5971be4963431622cec4be9ca980b1f Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 22 Oct 2019 16:38:28 +0100 Subject: [PATCH] Adds some missing tests --- .coveragerc | 1 + pisa/api.py | 2 +- pisa/responder.py | 1 + test/unit/test_api.py | 11 +++++++++++ test/unit/test_block_processor.py | 15 +++++++++++++++ test/unit/test_carrier.py | 1 - test/unit/test_tools.py | 11 ++++++++++- 7 files changed, 39 insertions(+), 3 deletions(-) diff --git a/.coveragerc b/.coveragerc index 74c5a28..53a6100 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,6 +1,7 @@ [run] omit = pisa/pisad.py + pisa/logger.py pisa/sample_conf.py pisa/time_traveler.py pisa/utils/auth_proxy.py \ No newline at end of file diff --git a/pisa/api.py b/pisa/api.py index af2e491..1398d98 100644 --- a/pisa/api.py +++ b/pisa/api.py @@ -84,7 +84,7 @@ def get_appointment(): response.append(job_data) if not response: - response.append({"locator": locator, "status": "not found"}) + response.append({"locator": locator, "status": "not_found"}) response = jsonify(response) diff --git a/pisa/responder.py b/pisa/responder.py index 058b658..2309b03 100644 --- a/pisa/responder.py +++ b/pisa/responder.py @@ -121,6 +121,7 @@ class Responder: Cleaner.delete_completed_jobs(self.jobs, self.tx_job_map, completed_jobs, height) self.rebroadcast(txs_to_rebroadcast) + # NOTCOVERED else: logger.warning("Reorg found", local_prev_block_hash=prev_block_hash, remote_prev_block_hash=block.get('previousblockhash')) diff --git a/test/unit/test_api.py b/test/unit/test_api.py index ba052e5..63b2967 100644 --- a/test/unit/test_api.py +++ b/test/unit/test_api.py @@ -1,6 +1,7 @@ import json import pytest import requests +from os import urandom from hashlib import sha256 from binascii import unhexlify @@ -99,6 +100,16 @@ def test_request_appointment(new_appointment): assert (all([status == "being_watched" for status in appointment_status])) +def test_request_random_appointment(): + r = requests.get(url=PISA_API + "/get_appointment?locator=" + urandom(32).hex()) + assert (r.status_code == 200) + + received_appointments = json.loads(r.content) + appointment_status = [appointment.pop("status") for appointment in received_appointments] + + assert (all([status == "not_found" for status in appointment_status])) + + def test_add_appointment_multiple_times(new_appointment, n=MULTIPLE_APPOINTMENTS): # Multiple appointments with the same locator should be valid # DISCUSS: #34-store-identical-appointments diff --git a/test/unit/test_block_processor.py b/test/unit/test_block_processor.py index a0ec37f..5183842 100644 --- a/test/unit/test_block_processor.py +++ b/test/unit/test_block_processor.py @@ -43,6 +43,12 @@ def test_get_block(best_block_hash): assert block.get('hash') == best_block_hash and 'height' in block and 'previousblockhash' in block and 'tx' in block +def test_get_random_block(): + block = BlockProcessor.get_block(urandom(32).hex()) + + assert block is None + + def test_get_block_count(): block_count = BlockProcessor.get_block_count() assert isinstance(block_count, int) and block_count >= 0 @@ -55,6 +61,15 @@ def test_potential_matches(txids, locator_uuid_map): assert locator_uuid_map.keys() == potential_matches.keys() +def test_potential_matches_random(locator_uuid_map): + txids = [urandom(32).hex() for _ in range(len(locator_uuid_map))] + + potential_matches = BlockProcessor.get_potential_matches(txids, locator_uuid_map) + + # None of the ids should match + assert len(potential_matches) == 0 + + def test_potential_matches_random_data(locator_uuid_map): # The likelihood of finding a potential match with random data should be negligible txids = [urandom(32).hex() for _ in range(TEST_SET_SIZE)] diff --git a/test/unit/test_carrier.py b/test/unit/test_carrier.py index 6671283..165d417 100644 --- a/test/unit/test_carrier.py +++ b/test/unit/test_carrier.py @@ -25,7 +25,6 @@ def carrier(): def test_send_transaction(run_bitcoind, carrier): - # We are mocking bitcoind and in our simulator txid == tx tx = TX.create_dummy_transaction() txid = sha256d(tx) diff --git a/test/unit/test_tools.py b/test/unit/test_tools.py index 251663e..f9bdca2 100644 --- a/test/unit/test_tools.py +++ b/test/unit/test_tools.py @@ -1,6 +1,6 @@ from pisa import logging from pisa.tools import check_txid_format -from pisa.tools import can_connect_to_bitcoind, in_correct_network +from pisa.tools import can_connect_to_bitcoind, in_correct_network, bitcoin_cli logging.getLogger().disabled = True @@ -22,6 +22,15 @@ def test_can_connect_to_bitcoind(): # assert can_connect_to_bitcoind() is False +def test_bitcoin_cli(): + try: + bitcoin_cli().help() + assert True + + except Exception: + assert False + + def test_check_txid_format(): assert(check_txid_format(None) is False) assert(check_txid_format("") is False)