diff --git a/test/common/unit/test_tools.py b/test/common/unit/test_tools.py index eebdab9..b4d2ad4 100644 --- a/test/common/unit/test_tools.py +++ b/test/common/unit/test_tools.py @@ -1,7 +1,26 @@ -from common.tools import check_sha256_hex_format +import os +import pytest +import logging +from copy import deepcopy + +from pisa import conf_fields + +from common.constants import LOCATOR_LEN_BYTES +from common.tools import ( + check_sha256_hex_format, + check_locator_format, + compute_locator, + setup_data_folder, + check_conf_fields, + extend_paths, + setup_logging, +) from test.common.unit.conftest import get_random_value_hex +conf_fields_copy = deepcopy(conf_fields) + + def test_check_sha256_hex_format(): # Only 32-byte hex encoded strings should pass the test wrong_inputs = [None, str(), 213, 46.67, dict(), "A" * 63, "C" * 65, bytes(), get_random_value_hex(31)] @@ -10,3 +29,98 @@ def test_check_sha256_hex_format(): for v in range(100): assert check_sha256_hex_format(get_random_value_hex(32)) is True + + +def test_check_locator_format(): + # Check that only LOCATOR_LEN_BYTES long string pass the test + + wrong_inputs = [ + None, + str(), + 213, + 46.67, + dict(), + "A" * (2 * LOCATOR_LEN_BYTES - 1), + "C" * (2 * LOCATOR_LEN_BYTES + 1), + bytes(), + get_random_value_hex(LOCATOR_LEN_BYTES - 1), + ] + for wtype in wrong_inputs: + assert check_sha256_hex_format(wtype) is False + + for _ in range(100): + assert check_locator_format(get_random_value_hex(LOCATOR_LEN_BYTES)) is True + + +def test_compute_locator(): + # The best way of checking that compute locator is correct is by using check_locator_format + for _ in range(100): + assert check_locator_format(compute_locator(get_random_value_hex(LOCATOR_LEN_BYTES))) is True + + # String of length smaller than LOCATOR_LEN_BYTES bytes must fail + for i in range(1, LOCATOR_LEN_BYTES): + assert check_locator_format(compute_locator(get_random_value_hex(i))) is False + + +def test_setup_data_folder(): + # This method should create a folder if it does not exist, and do nothing otherwise + test_folder = "test_folder" + assert not os.path.isdir(test_folder) + + setup_data_folder(test_folder) + + assert os.path.isdir(test_folder) + + os.rmdir(test_folder) + + +def test_check_conf_fields(): + # The test should work with a valid config_fields (obtained from a valid conf.py) + assert type(check_conf_fields(conf_fields_copy)) == dict + + +def test_bad_check_conf_fields(): + # Create a messed up version of the file that should throw an error. + conf_fields_copy["BTC_RPC_USER"] = 0000 + conf_fields_copy["BTC_RPC_PASSWD"] = "password" + conf_fields_copy["BTC_RPC_HOST"] = 000 + + # We should get a ValueError here. + with pytest.raises(Exception): + check_conf_fields(conf_fields_copy) + + +def test_extend_paths(): + # Test that only items with the path flag are extended + config_fields = { + "foo": {"value": "foofoo"}, + "var": {"value": "varvar", "path": True}, + "foovar": {"value": "foovarfoovar"}, + } + base_path = "base_path/" + extended_config_field = extend_paths(base_path, config_fields) + + for k, field in extended_config_field.items(): + if field.get("path") is True: + assert base_path in field.get("value") + else: + assert base_path not in field.get("value") + + +def test_setup_logging(): + # Check that setup_logging creates two new logs for every prefix + prefix = "foo" + log_file = "var.log" + + f_log_suffix = "_file_log" + c_log_suffix = "_console_log" + + assert len(logging.getLogger(prefix + f_log_suffix).handlers) is 0 + assert len(logging.getLogger(prefix + c_log_suffix).handlers) is 0 + + setup_logging(log_file, prefix) + + assert len(logging.getLogger(prefix + f_log_suffix).handlers) is 1 + assert len(logging.getLogger(prefix + c_log_suffix).handlers) is 1 + + os.remove(log_file) diff --git a/test/pisa/unit/test_pisad.py b/test/pisa/unit/test_pisad.py deleted file mode 100644 index 30db71e..0000000 --- a/test/pisa/unit/test_pisad.py +++ /dev/null @@ -1,51 +0,0 @@ -import importlib -import os -import pytest -from shutil import copyfile - -from pisa.pisad import load_config - -test_conf_file_path = os.getcwd() + "/test/pisa/unit/test_conf.py" - - -def test_load_config(): - # Copy the sample-conf.py file to use as a test config file. - copyfile(os.getcwd() + "/pisa/sample_conf.py", test_conf_file_path) - - import test.pisa.unit.test_conf as conf - - # If the file has all the correct fields and data, it should return a dict. - conf_dict = load_config(conf) - assert type(conf_dict) == dict - - # Delete the file. - os.remove(test_conf_file_path) - - -def test_bad_load_config(): - # Create a messed up version of the file that should throw an error. - with open(test_conf_file_path, "w") as f: - f.write('# bitcoind\nBTC_RPC_USER = 0000\nBTC_RPC_PASSWD = "password"\nBTC_RPC_HOST = 000') - - import test.pisa.unit.test_conf as conf - - importlib.reload(conf) - - with pytest.raises(Exception): - conf_dict = load_config(conf) - - os.remove(test_conf_file_path) - - -def test_empty_load_config(): - # Create an empty version of the file that should throw an error. - open(test_conf_file_path, "a") - - import test.pisa.unit.test_conf as conf - - importlib.reload(conf) - - with pytest.raises(Exception): - conf_dict = load_config(conf) - - os.remove(test_conf_file_path)