88"""Module that checks the general Futures Base API class."""
99
1010from asyncio import run
11+ from typing import Self
1112from unittest import IsolatedAsyncioTestCase
1213
1314import pytest
2122
2223
2324@pytest .mark .futures
24- def test_KrakenFuturesBaseAPI_without_exception () -> None :
25- """
26- Checks first if the expected error will be raised and than
27- creates a new KrakenFuturesBaseAPI instance that do not raise
28- the custom Kraken exceptions. This new instance than executes
29- the same request and the returned response gets evaluated.
30- """
31- with pytest .raises (KrakenRequiredArgumentMissingError ):
32- FuturesClient (
33- key = "fake" ,
34- secret = "fake" ,
35- ).request (method = "POST" , uri = "/derivatives/api/v3/sendorder" , auth = True )
36-
37- result : dict = (
38- FuturesClient (key = "fake" , secret = "fake" , use_custom_exceptions = False ) # type: ignore[union-attr]
39- .request (method = "POST" , uri = "/derivatives/api/v3/sendorder" , auth = True )
40- .json ()
41- )
42-
43- assert result .get ("result" ) == "error"
44- assert result .get ("error" ) == "requiredArgumentMissing"
25+ class TestFuturesBaseAPI :
26+ """Test class for Futures Base API functionality."""
4527
28+ def test_KrakenFuturesBaseAPI_without_exception (self ) -> None :
29+ """
30+ Checks first if the expected error will be raised and then
31+ creates a new KrakenFuturesBaseAPI instance that does not raise
32+ the custom Kraken exceptions. This new instance then executes
33+ the same request and the returned response gets evaluated.
34+ """
35+ with pytest .raises (KrakenRequiredArgumentMissingError ):
36+ FuturesClient (
37+ key = "fake" ,
38+ secret = "fake" ,
39+ ).request (method = "POST" , uri = "/derivatives/api/v3/sendorder" , auth = True )
40+
41+ result : dict = (
42+ FuturesClient (key = "fake" , secret = "fake" , use_custom_exceptions = False ) # type: ignore[union-attr]
43+ .request (method = "POST" , uri = "/derivatives/api/v3/sendorder" , auth = True )
44+ .json ()
45+ )
4646
47- @pytest .mark .futures
48- @pytest .mark .futures_auth
49- def test_futures_rest_contextmanager (
50- futures_market : Market ,
51- futures_auth_funding : Funding ,
52- futures_demo_trade : Trade ,
53- futures_auth_user : User ,
54- ) -> None :
55- """
56- Checks if the clients can be used as context manager.
57- """
58- with futures_market as market :
59- assert isinstance (market .get_tick_types (), list )
47+ assert result .get ("result" ) == "error"
48+ assert result .get ("error" ) == "requiredArgumentMissing"
49+
50+ @pytest .mark .futures_auth
51+ def test_futures_rest_contextmanager (
52+ self ,
53+ futures_market : Market ,
54+ futures_auth_funding : Funding ,
55+ futures_demo_trade : Trade ,
56+ futures_auth_user : User ,
57+ ) -> None :
58+ """
59+ Checks if the clients can be used as context manager.
60+ """
61+ with futures_market as market :
62+ assert isinstance (market .get_tick_types (), list )
6063
61- with futures_auth_funding as funding :
62- assert is_success (funding .get_historical_funding_rates (symbol = "PF_SOLUSD" ))
64+ with futures_auth_funding as funding :
65+ assert is_success (funding .get_historical_funding_rates (symbol = "PF_SOLUSD" ))
6366
64- with futures_auth_user as user :
65- assert is_success (user .get_wallets ())
67+ with futures_auth_user as user :
68+ assert is_success (user .get_wallets ())
6669
67- with futures_demo_trade as trade :
68- assert is_success (trade .get_fills ())
70+ with futures_demo_trade as trade :
71+ assert is_success (trade .get_fills ())
6972
7073
7174# ==============================================================================
7275# Futures async client
7376
7477
7578@pytest .mark .futures
76- def test_futures_async_rest_contextmanager () -> None :
77- """
78- Checks if the clients can be used as context manager.
79- """
80-
81- async def check () -> None :
82- async with FuturesAsyncClient () as client :
83- assert isinstance (
84- await client .request (
85- "GET" ,
86- "/api/charts/v1/spot/PI_XBTUSD/1h" ,
87- auth = False ,
88- post_params = {"from" : "1668989233" , "to" : "1668999233" },
89- ),
90- dict ,
91- )
92-
93- run (check ())
79+ class TestFuturesBaseAPIAsync :
80+ """Test class for Futures Base API async functionality."""
9481
82+ def test_futures_async_rest_contextmanager (self : Self ) -> None :
83+ """
84+ Checks if the clients can be used as context manager.
85+ """
9586
96- @pytest .mark .futures
97- @pytest .mark .futures_auth
98- def test_futures_rest_async_client_post (
99- futures_api_key : str ,
100- futures_secret_key : str ,
101- ) -> None :
102- """
103- Check the instantiation as well as a simple request using the async client.
104- """
105-
106- async def check () -> None :
107- client = FuturesAsyncClient (futures_api_key , futures_secret_key )
108- try :
109- assert isinstance (
110- await client .request (
111- "POST" ,
112- "/derivatives/api/v3/orders/status" ,
113- post_params = {
114- "orderIds" : [
115- "bcaaefce-27a3-44b4-b13a-19df21e3f087" ,
116- "685d5a1a-23eb-450c-bf17-1e4ab5c6fe8a" ,
117- ],
118- },
119- ),
120- dict ,
121- )
122- finally :
123- await client .close ()
124-
125- run (check ())
87+ async def check () -> None :
88+ async with FuturesAsyncClient () as client :
89+ assert isinstance (
90+ await client .request (
91+ "GET" ,
92+ "/api/charts/v1/spot/PI_XBTUSD/1h" ,
93+ auth = False ,
94+ post_params = {"from" : "1668989233" , "to" : "1668999233" },
95+ ),
96+ dict ,
97+ )
98+
99+ run (check ())
100+
101+ @pytest .mark .futures_auth
102+ def test_futures_rest_async_client_post (
103+ self : Self ,
104+ futures_api_key : str ,
105+ futures_secret_key : str ,
106+ ) -> None :
107+ """
108+ Check the instantiation as well as a simple request using the async client.
109+ """
110+
111+ async def check () -> None :
112+ client = FuturesAsyncClient (futures_api_key , futures_secret_key )
113+ try :
114+ assert isinstance (
115+ await client .request (
116+ "POST" ,
117+ "/derivatives/api/v3/orders/status" ,
118+ post_params = {
119+ "orderIds" : [
120+ "bcaaefce-27a3-44b4-b13a-19df21e3f087" ,
121+ "685d5a1a-23eb-450c-bf17-1e4ab5c6fe8a" ,
122+ ],
123+ },
124+ ),
125+ dict ,
126+ )
127+ finally :
128+ await client .close ()
129+
130+ run (check ())
126131
127132
133+ @pytest .mark .futures
134+ @pytest .mark .futures_market
135+ @pytest .mark .futures_market
128136class TestProxyPyEmbedded (TestCase , IsolatedAsyncioTestCase ):
129- def get_proxy_str (self ) -> str :
137+ def get_proxy_str (self : Self ) -> str :
130138 return f"http://127.0.0.1:{ self .PROXY .flags .port } "
131139
132- @pytest .mark .futures
133- @pytest .mark .futures_market
134140 def test_futures_rest_proxies (self ) -> None :
135141 """
136142 Checks if the clients can be used with a proxy.
@@ -147,9 +153,7 @@ def test_futures_rest_proxies(self) -> None:
147153 )
148154
149155 @pytest .mark .asyncio
150- @pytest .mark .futures
151- @pytest .mark .futures_market
152- async def test_futures_rest_proxies_async (self ) -> None :
156+ async def test_futures_rest_proxies_async (self : Self ) -> None :
153157 """
154158 Checks if the async clients can be used with a proxy.
155159 """
0 commit comments