Skip to content

Commit

Permalink
added mock file and unit test (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
azurwastaken authored Jul 9, 2024
1 parent da22068 commit 83c0272
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pragma-sdk/pragma_sdk/offchain/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ async def get_expiries_list(self, pair: Pair):
else:
logger.debug(f"Status Code: {status_code}")
logger.debug(f"Response Text: {response}")
raise HTTPError(
f"Unable to GET /v1{base_asset}/{quote_asset}/future_expiries for pair {pair} "
raise PragmaAPIError(
f"Unable to GET future_expiries for pair {pair}"
)
return json_response

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"2024-09-27T08:00:00",
"2024-12-27T08:00:00"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"BTC": {
"num_sources_aggregated": 8,
"pair_id": "BTC/USD",
"price": "0x52bd9a0154d",
"timestamp": 1720274400000,
"decimals": 8
},
"ETH": {
"num_sources_aggregated": 8,
"pair_id": "ETH/USD",
"price": "0x45d8ad9ae4",
"timestamp": 1720274400000,
"decimals": 8
}
}
181 changes: 181 additions & 0 deletions pragma-sdk/tests/unit/api_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pragma_sdk.common.logging import get_stream_logger
from pragma_sdk.offchain.client import PragmaAPIClient
from pragma_sdk.offchain.exceptions import PragmaAPIError
from pragma_sdk.common.types.pair import Pair
from tests.integration.constants import MOCK_DIR, SAMPLE_PAIRS

JEDISWAP_POOL = "0x4e021092841c1b01907f42e7058f97e5a22056e605dce08a22868606ad675e0"
Expand Down Expand Up @@ -42,6 +43,27 @@
},
},
},
"get_future_data": {
"function": "get_future_entry",
"url": "https://api.dev.pragma.build/node/v1/data/",
"mock_file": MOCK_DIR / "responses" / "api_client" / "get_future.json",
"expected_result": {
"BTC": {
"num_sources_aggregated": 8,
"pair_id": "BTC/USD",
"price": "0x52bd9a0154d",
"timestamp": 1720274400000,
"decimals": 8,
},
"ETH": {
"num_sources_aggregated": 8,
"pair_id": "ETH/USD",
"price": "0x45d8ad9ae4",
"timestamp": 1720274400000,
"decimals": 8,
},
},
},
"get_ohlc_data": {
"function": "api_get_ohlc",
"url": "https://api.dev.pragma.build/node/v1/aggregation/candlestick/",
Expand Down Expand Up @@ -130,6 +152,12 @@
},
},
},
"get_expiries_list": {
"function": "get_expiries_list",
"url": "https://api.dev.pragma.build/node/v1/data/",
"mock_file": MOCK_DIR / "responses" / "api_client" / "get_expiries_list.json",
"expected_result": ["2024-09-27T08:00:00", "2024-12-27T08:00:00"],
},
}


Expand Down Expand Up @@ -280,3 +308,156 @@ async def test_async_api_client_ohlc_404_error():
str(exc_info.value)
== f"Failed to get OHLC data for pair {base_asset}/{quote_asset}"
)


@pytest.mark.asyncio
async def test_async_api_client_future():
# we only want to mock the external fetcher APIs and not the RPC
with aioresponses() as mock:
api_client = PragmaAPIClient(
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATE_KEY,
"https://api.dev.pragma.build",
"dummy_key",
)
# Mocking the expected call for assets
for asset in SAMPLE_PAIRS:
base_asset = asset.base_currency.id
quote_asset = asset.quote_currency.id
url = (
API_CLIENT_CONFIGS["get_future_data"]["url"]
+ f"{base_asset}/{quote_asset}"
+ "?entry_type=future"
)
with open(
[
config["mock_file"]
for config in API_CLIENT_CONFIGS.values()
if config["function"] == "get_future_entry"
][0],
"r",
encoding="utf-8",
) as filepath:
mock_data = json.load(filepath)
mock.get(
url,
payload=mock_data[base_asset],
)
result = await api_client.get_future_entry(
f"{asset.base_currency.id}/{asset.quote_currency.id}"
)
expected_result = [
config["expected_result"]
for config in API_CLIENT_CONFIGS.values()
if config["function"] == "get_future_entry"
]

assert result.assert_attributes_equal(expected_result[0][base_asset])


@pytest.mark.asyncio
async def test_async_api_client_future_404_error():
# we only want to mock the external fetcher APIs and not the RPC
with aioresponses() as mock:
api_client = PragmaAPIClient(
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATE_KEY,
"https://api.dev.pragma.build",
"dummy_key",
)
# Mocking the expected call for assets
for asset in SAMPLE_PAIRS:
base_asset = asset.base_currency.id
quote_asset = asset.quote_currency.id
url = (
API_CLIENT_CONFIGS["get_future_data"]["url"]
+ f"{base_asset}/{quote_asset}"
+ "?entry_type=future"
)
mock.get(url, status=404)
# Use pytest.raises to capture the exception
with pytest.raises(PragmaAPIError) as exc_info:
await api_client.get_future_entry(f"{base_asset}/{quote_asset}")

# Assert the error message or other details if needed
assert (
str(exc_info.value)
== f"Unable to GET /v1/data for pair {base_asset}/{quote_asset}"
)


@pytest.mark.asyncio
async def test_async_api_client_expiries_list():
# we only want to mock the external fetcher APIs and not the RPC
with aioresponses() as mock:
api_client = PragmaAPIClient(
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATE_KEY,
"https://api.dev.pragma.build",
"dummy_key",
)
# Mocking the expected call for assets
for asset in SAMPLE_PAIRS:
base_asset = asset.base_currency.id
quote_asset = asset.quote_currency.id
url = (
API_CLIENT_CONFIGS["get_expiries_list"]["url"]
+ f"{base_asset}/{quote_asset}"
+ "/future_expiries"
)
with open(
[
config["mock_file"]
for config in API_CLIENT_CONFIGS.values()
if config["function"] == "get_expiries_list"
][0],
"r",
encoding="utf-8",
) as filepath:
mock_data = json.load(filepath)
mock.get(
url,
payload=mock_data,
)
result = await api_client.get_expiries_list(
Pair.from_tickers(
f"{asset.base_currency.id}", f"{asset.quote_currency.id}"
)
)
expected_result = API_CLIENT_CONFIGS["get_expiries_list"]["expected_result"]
assert expected_result == result


@pytest.mark.asyncio
async def test_async_api_client_expiries_list_404_error():
# we only want to mock the external fetcher APIs and not the RPC
with aioresponses() as mock:
api_client = PragmaAPIClient(
ACCOUNT_ADDRESS,
ACCOUNT_PRIVATE_KEY,
"https://api.dev.pragma.build",
"dummy_key",
)
# Mocking the expected call for assets
for asset in SAMPLE_PAIRS:
base_asset = asset.base_currency.id
quote_asset = asset.quote_currency.id
url = (
API_CLIENT_CONFIGS["get_expiries_list"]["url"]
+ f"{base_asset}/{quote_asset}"
+ "/future_expiries"
)
mock.get(url, status=404)
# Use pytest.raises to capture the exception
with pytest.raises(PragmaAPIError) as exc_info:
await api_client.get_expiries_list(
Pair.from_tickers(
f"{asset.base_currency.id}", f"{asset.quote_currency.id}"
)
)

# Assert the error message or other details if needed
assert (
str(exc_info.value)
== f"Unable to GET future_expiries for pair {base_asset}/{quote_asset}"
)

0 comments on commit 83c0272

Please sign in to comment.