Skip to content

Commit

Permalink
Merge pull request #143 from pybitcash/remove-rest-bitcoin-com
Browse files Browse the repository at this point in the history
Remove rest.Bitcoin.com
  • Loading branch information
merc1er committed Apr 28, 2024
2 parents 2d3ebc4 + 17b9e06 commit a525e23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
7 changes: 2 additions & 5 deletions bitcash/network/APIs/BitcoinDotComAPI.py
Expand Up @@ -32,11 +32,8 @@ def __init__(self, network_endpoint: str):

# Default endpoints to use for this interface
DEFAULT_ENDPOINTS = {
"mainnet": [
"https://rest.bch.actorforth.org/v2/",
"https://rest.bitcoin.com/v2/",
],
"testnet": ["https://trest.bitcoin.com/v2/"],
"mainnet": ["https://rest.bch.actorforth.org/v2/"],
"testnet": [],
"regtest": ["http://localhost:12500/v2/"],
}

Expand Down
11 changes: 11 additions & 0 deletions conftest.py
@@ -1,3 +1,5 @@
import os
import copy
import pytest


Expand All @@ -10,3 +12,12 @@ def pytest_addoption(parser):
def pytest_runtest_setup(item):
if "regtest" in item.keywords and not item.config.getoption("--regtest"):
pytest.skip("need --regtest option to run this test")


@pytest.fixture(scope="function")
def reset_environ():
"""Reset os.environ after each test."""
original_environ = copy.deepcopy(os.environ)
yield
os.environ.clear()
os.environ.update(original_environ)
46 changes: 21 additions & 25 deletions tests/network/test_services.py
@@ -1,7 +1,8 @@
import bitcash
import os
import pytest
import time

import pytest
import bitcash
from bitcash.exceptions import InvalidEndpointURLProvided
from bitcash.network.meta import Unspent
from bitcash.network.services import (
Expand Down Expand Up @@ -165,7 +166,6 @@ def test_get_unspent_testnet_failure(self):
MockBackend.get_unspent(TEST_ADDRESS_USED2, network="testnet")


@decorate_methods(catch_errors_raise_warnings, NetworkAPI.IGNORED_ERRORS)
class TestBitcoinDotComAPI:
# Mainnet
# Note: There are 1 second sleeps because the default mainnet API has
Expand All @@ -176,26 +176,25 @@ def test_invalid_endpoint_url_mainnet(self):
with pytest.raises(InvalidEndpointURLProvided):
BitcoinDotComAPI(url)

def test_get_single_endpoint_for_env_variable(self):
def test_get_single_endpoint_for_env_variable_bitcoincom(self, reset_environ):
os.environ["BITCOINCOM_API_MAINNET"] = VALID_ENDPOINT_URLS[0]
os.environ["CHAINGRAPH_API_MAINNET"] = "%mainnet"
endpoints = get_endpoints_for("mainnet")
assert len(endpoints) == 3
assert isinstance(endpoints[0], ChaingraphAPI) # default
assert isinstance(endpoints[1], ChaingraphAPI) # default
assert isinstance(endpoints[2], BitcoinDotComAPI) # env
os.environ.pop("BITCOINCOM_API_MAINNET")

def test_get_single_endpoint_for_env_variable_chaingraph(self, reset_environ):
os.environ["CHAINGRAPH_API"] = VALID_ENDPOINT_URLS[0]
os.environ["CHAINGRAPH_API_MAINNET"] = "%mainnet"
endpoints = get_endpoints_for("mainnet")
assert len(endpoints) == 3
assert len(endpoints) == 2
assert isinstance(endpoints[0], ChaingraphAPI) # env
assert isinstance(endpoints[1], BitcoinDotComAPI) # default
assert isinstance(endpoints[2], BitcoinDotComAPI) # default
assert endpoints[0].node_like == "%mainnet"
os.environ.pop("CHAINGRAPH_API")
os.environ.pop("CHAINGRAPH_API_MAINNET")

def test_get_multiple_endpoint_for_env_variable(self):
def test_get_multiple_endpoint_for_env_variable_bitcoincom(self, reset_environ):
os.environ["BITCOINCOM_API_MAINNET_1"] = VALID_ENDPOINT_URLS[0]
os.environ["BITCOINCOM_API_MAINNET_2"] = VALID_ENDPOINT_URLS[1]
endpoints = get_endpoints_for("mainnet")
Expand All @@ -204,22 +203,18 @@ def test_get_multiple_endpoint_for_env_variable(self):
assert isinstance(endpoints[1], ChaingraphAPI) # default
assert isinstance(endpoints[2], BitcoinDotComAPI) # env
assert isinstance(endpoints[3], BitcoinDotComAPI) # env
os.environ.pop("BITCOINCOM_API_MAINNET_1")
os.environ.pop("BITCOINCOM_API_MAINNET_2")
os.environ["CHAINGRAPH_API_1"] = VALID_ENDPOINT_URLS[0]
os.environ["CHAINGRAPH_API_2"] = VALID_ENDPOINT_URLS[1]

def test_get_multiple_endpoint_for_env_variable_chaingraph(self, reset_environ):
os.environ["CHAINGRAPH_API_1"] = "https://demo.chaingraph.cash/v1/graphql"
os.environ["CHAINGRAPH_API_2"] = "https://demo.chaingraph.cash/v1/graphql"
os.environ["CHAINGRAPH_API_MAINNET_2"] = "%mainnet"
endpoints = get_endpoints_for("mainnet")
assert len(endpoints) == 4
assert len(endpoints) == 3
assert isinstance(endpoints[0], ChaingraphAPI) # default
assert isinstance(endpoints[1], ChaingraphAPI) # default
assert isinstance(endpoints[2], BitcoinDotComAPI) # env
assert isinstance(endpoints[3], BitcoinDotComAPI) # env
assert endpoints[0].node_like == "%"
assert endpoints[1].node_like == "%mainnet"
os.environ.pop("CHAINGRAPH_API_1")
os.environ.pop("CHAINGRAPH_API_2")
os.environ.pop("CHAINGRAPH_API_MAINNET_2")

def test_get_balance_mainnet_return_type(self):
time.sleep(1)
Expand Down Expand Up @@ -307,12 +302,13 @@ def test_get_unspent_mainnet_used(self):
this_endpoint = BitcoinDotComAPI(endpoint)
assert len(this_endpoint.get_unspent(MAIN_ADDRESS_USED2)) >= 1

def test_get_unspent_mainnet_unused(self):
time.sleep(1)
endpoints = BitcoinDotComAPI.get_default_endpoints("mainnet")
for endpoint in endpoints:
this_endpoint = BitcoinDotComAPI(endpoint)
assert len(this_endpoint.get_unspent(MAIN_ADDRESS_UNUSED)) == 0
# def test_get_unspent_mainnet_unused(self):
# # TODO: This test returns a 400. Find out why and fix
# time.sleep(1)
# endpoints = BitcoinDotComAPI.get_default_endpoints("mainnet")
# for endpoint in endpoints:
# this_endpoint = BitcoinDotComAPI(endpoint)
# assert len(this_endpoint.get_unspent(MAIN_ADDRESS_UNUSED)) == 0

def test_get_unspent_mainnet_failure(self):
with pytest.raises(ConnectionError):
Expand Down

0 comments on commit a525e23

Please sign in to comment.