Skip to content

Commit

Permalink
Use pytest instead of unittest for environ reset
Browse files Browse the repository at this point in the history
  • Loading branch information
merc1er committed Apr 27, 2024
1 parent 6c0d2bb commit 17b9e06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
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)
38 changes: 12 additions & 26 deletions tests/network/test_services.py
@@ -1,7 +1,5 @@
import os
import time
import copy
import unittest

import pytest
import bitcash
Expand Down Expand Up @@ -168,29 +166,17 @@ 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(unittest.TestCase):
class TestBitcoinDotComAPI:
# 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_bitcoincom(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")
Expand All @@ -199,7 +185,7 @@ def test_get_single_endpoint_for_env_variable_bitcoincom(self):
assert isinstance(endpoints[1], ChaingraphAPI) # default
assert isinstance(endpoints[2], BitcoinDotComAPI) # env

def test_get_single_endpoint_for_env_variable_chaingraph(self):
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")
Expand All @@ -208,7 +194,7 @@ def test_get_single_endpoint_for_env_variable_chaingraph(self):
assert isinstance(endpoints[1], BitcoinDotComAPI) # default
assert endpoints[0].node_like == "%mainnet"

def test_get_multiple_endpoint_for_env_variable_bitcoincom(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 @@ -218,8 +204,7 @@ def test_get_multiple_endpoint_for_env_variable_bitcoincom(self):
assert isinstance(endpoints[2], BitcoinDotComAPI) # env
assert isinstance(endpoints[3], BitcoinDotComAPI) # env

def test_get_multiple_endpoint_for_env_variable_chaingraph(self):
os.environ = self.original_environ
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"
Expand Down Expand Up @@ -317,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 17b9e06

Please sign in to comment.