1
- import bitcash
2
1
import os
3
- import pytest
4
2
import time
3
+ import copy
4
+ import unittest
5
+
6
+ import pytest
7
+ import bitcash
5
8
from bitcash .exceptions import InvalidEndpointURLProvided
6
9
from bitcash .network .meta import Unspent
7
10
from bitcash .network .services import (
@@ -166,36 +169,46 @@ def test_get_unspent_testnet_failure(self):
166
169
167
170
168
171
@decorate_methods (catch_errors_raise_warnings , NetworkAPI .IGNORED_ERRORS )
169
- class TestBitcoinDotComAPI :
172
+ class TestBitcoinDotComAPI ( unittest . TestCase ) :
170
173
# Mainnet
171
174
# Note: There are 1 second sleeps because the default mainnet API has
172
175
# rate limiting and will return 503 if we query it too quickly.
173
176
177
+ def setUp (self ):
178
+ # Save a copy of the original os.environ.
179
+ # Note that this makes the tests slower, but is necessary on some test cases
180
+ # to avoid side effects.
181
+ # TODO: Refactor this to only be used when necessary.
182
+ self .original_environ = copy .deepcopy (os .environ )
183
+
184
+ def tearDown (self ):
185
+ # Restore the original os.environ after the test.
186
+ os .environ = self .original_environ
187
+
174
188
def test_invalid_endpoint_url_mainnet (self ):
175
189
for url in INVALID_ENDPOINT_URLS :
176
190
with pytest .raises (InvalidEndpointURLProvided ):
177
191
BitcoinDotComAPI (url )
178
192
179
- def test_get_single_endpoint_for_env_variable (self ):
193
+ def test_get_single_endpoint_for_env_variable_bitcoincom (self ):
180
194
os .environ ["BITCOINCOM_API_MAINNET" ] = VALID_ENDPOINT_URLS [0 ]
195
+ os .environ ["CHAINGRAPH_API_MAINNET" ] = "%mainnet"
181
196
endpoints = get_endpoints_for ("mainnet" )
182
197
assert len (endpoints ) == 3
183
198
assert isinstance (endpoints [0 ], ChaingraphAPI ) # default
184
199
assert isinstance (endpoints [1 ], ChaingraphAPI ) # default
185
200
assert isinstance (endpoints [2 ], BitcoinDotComAPI ) # env
186
- os .environ .pop ("BITCOINCOM_API_MAINNET" )
201
+
202
+ def test_get_single_endpoint_for_env_variable_chaingraph (self ):
187
203
os .environ ["CHAINGRAPH_API" ] = VALID_ENDPOINT_URLS [0 ]
188
204
os .environ ["CHAINGRAPH_API_MAINNET" ] = "%mainnet"
189
205
endpoints = get_endpoints_for ("mainnet" )
190
- assert len (endpoints ) == 3
206
+ assert len (endpoints ) == 2
191
207
assert isinstance (endpoints [0 ], ChaingraphAPI ) # env
192
208
assert isinstance (endpoints [1 ], BitcoinDotComAPI ) # default
193
- assert isinstance (endpoints [2 ], BitcoinDotComAPI ) # default
194
209
assert endpoints [0 ].node_like == "%mainnet"
195
- os .environ .pop ("CHAINGRAPH_API" )
196
- os .environ .pop ("CHAINGRAPH_API_MAINNET" )
197
210
198
- def test_get_multiple_endpoint_for_env_variable (self ):
211
+ def test_get_multiple_endpoint_for_env_variable_bitcoincom (self ):
199
212
os .environ ["BITCOINCOM_API_MAINNET_1" ] = VALID_ENDPOINT_URLS [0 ]
200
213
os .environ ["BITCOINCOM_API_MAINNET_2" ] = VALID_ENDPOINT_URLS [1 ]
201
214
endpoints = get_endpoints_for ("mainnet" )
@@ -204,22 +217,19 @@ def test_get_multiple_endpoint_for_env_variable(self):
204
217
assert isinstance (endpoints [1 ], ChaingraphAPI ) # default
205
218
assert isinstance (endpoints [2 ], BitcoinDotComAPI ) # env
206
219
assert isinstance (endpoints [3 ], BitcoinDotComAPI ) # env
207
- os .environ .pop ("BITCOINCOM_API_MAINNET_1" )
208
- os .environ .pop ("BITCOINCOM_API_MAINNET_2" )
209
- os .environ ["CHAINGRAPH_API_1" ] = VALID_ENDPOINT_URLS [0 ]
210
- os .environ ["CHAINGRAPH_API_2" ] = VALID_ENDPOINT_URLS [1 ]
220
+
221
+ def test_get_multiple_endpoint_for_env_variable_chaingraph (self ):
222
+ os .environ = self .original_environ
223
+ os .environ ["CHAINGRAPH_API_1" ] = "https://demo.chaingraph.cash/v1/graphql"
224
+ os .environ ["CHAINGRAPH_API_2" ] = "https://demo.chaingraph.cash/v1/graphql"
211
225
os .environ ["CHAINGRAPH_API_MAINNET_2" ] = "%mainnet"
212
226
endpoints = get_endpoints_for ("mainnet" )
213
- assert len (endpoints ) == 4
227
+ assert len (endpoints ) == 3
214
228
assert isinstance (endpoints [0 ], ChaingraphAPI ) # default
215
229
assert isinstance (endpoints [1 ], ChaingraphAPI ) # default
216
230
assert isinstance (endpoints [2 ], BitcoinDotComAPI ) # env
217
- assert isinstance (endpoints [3 ], BitcoinDotComAPI ) # env
218
231
assert endpoints [0 ].node_like == "%"
219
232
assert endpoints [1 ].node_like == "%mainnet"
220
- os .environ .pop ("CHAINGRAPH_API_1" )
221
- os .environ .pop ("CHAINGRAPH_API_2" )
222
- os .environ .pop ("CHAINGRAPH_API_MAINNET_2" )
223
233
224
234
def test_get_balance_mainnet_return_type (self ):
225
235
time .sleep (1 )
0 commit comments