Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rest.Bitcoin.com #143

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions bitcash/network/APIs/BitcoinDotComAPI.py
Original file line number Diff line number Diff line change
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
48 changes: 29 additions & 19 deletions tests/network/test_services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import bitcash
import os
import pytest
import time
import copy
import unittest

merc1er marked this conversation as resolved.
Show resolved Hide resolved
import pytest
import bitcash
from bitcash.exceptions import InvalidEndpointURLProvided
from bitcash.network.meta import Unspent
from bitcash.network.services import (
Expand Down Expand Up @@ -166,36 +169,46 @@ def test_get_unspent_testnet_failure(self):


@decorate_methods(catch_errors_raise_warnings, NetworkAPI.IGNORED_ERRORS)
class TestBitcoinDotComAPI:
class TestBitcoinDotComAPI(unittest.TestCase):
# Mainnet
# Note: There are 1 second sleeps because the default mainnet API has
# rate limiting and will return 503 if we query it too quickly.

def setUp(self):
# Save a copy of the original os.environ.
# Note that this makes the tests slower, but is necessary on some test cases
# to avoid side effects.
# TODO: Refactor this to only be used when necessary.
self.original_environ = copy.deepcopy(os.environ)

def tearDown(self):
# Restore the original os.environ after the test.
os.environ = self.original_environ

def test_invalid_endpoint_url_mainnet(self):
for url in INVALID_ENDPOINT_URLS:
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):
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):
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):
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 +217,19 @@ 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):
os.environ = self.original_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