Skip to content

Commit 9dce464

Browse files
committed
clean up testing
1 parent 43c9b54 commit 9dce464

11 files changed

+163
-185
lines changed

tests/test_async_client.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import pytest
88

99
from project_x_py import (
10-
AsyncProjectX,
10+
ProjectX,
1111
ProjectXConfig,
1212
ProjectXConnectionError,
13+
RateLimiter,
1314
)
14-
from project_x_py.async_client import AsyncRateLimiter
1515

1616

1717
@pytest.fixture
@@ -24,7 +24,7 @@ def mock_env_vars(monkeypatch):
2424
@pytest.mark.asyncio
2525
async def test_async_client_creation():
2626
"""Test async client can be created."""
27-
client = AsyncProjectX(
27+
client = ProjectX(
2828
username="test_user",
2929
api_key="test_key",
3030
)
@@ -37,7 +37,7 @@ async def test_async_client_creation():
3737
@pytest.mark.asyncio
3838
async def test_async_client_from_env(mock_env_vars):
3939
"""Test creating async client from environment variables."""
40-
async with AsyncProjectX.from_env() as client:
40+
async with ProjectX.from_env() as client:
4141
assert client.username == "test_username"
4242
assert client.api_key == "test_api_key"
4343
assert client._client is not None # Client should be initialized
@@ -46,7 +46,7 @@ async def test_async_client_from_env(mock_env_vars):
4646
@pytest.mark.asyncio
4747
async def test_async_context_manager():
4848
"""Test async client works as context manager."""
49-
client = AsyncProjectX(username="test", api_key="key")
49+
client = ProjectX(username="test", api_key="key")
5050

5151
# Client should not be initialized yet
5252
assert client._client is None
@@ -63,7 +63,7 @@ async def test_async_context_manager():
6363
@pytest.mark.asyncio
6464
async def test_http2_support():
6565
"""Test that HTTP/2 is enabled."""
66-
client = AsyncProjectX(username="test", api_key="key")
66+
client = ProjectX(username="test", api_key="key")
6767

6868
async with client:
6969
# Check HTTP/2 is enabled
@@ -73,7 +73,7 @@ async def test_http2_support():
7373
@pytest.mark.asyncio
7474
async def test_authentication_flow():
7575
"""Test authentication flow with mocked responses."""
76-
client = AsyncProjectX(username="test", api_key="key")
76+
client = ProjectX(username="test", api_key="key")
7777

7878
# Mock responses
7979
mock_login_response = {
@@ -113,7 +113,7 @@ async def test_authentication_flow():
113113
@pytest.mark.asyncio
114114
async def test_concurrent_requests():
115115
"""Test that async client can handle concurrent requests."""
116-
client = AsyncProjectX(username="test", api_key="key")
116+
client = ProjectX(username="test", api_key="key")
117117

118118
# Mock responses
119119
positions_response = [
@@ -175,7 +175,7 @@ async def mock_make_request(method, endpoint, **kwargs):
175175
@pytest.mark.asyncio
176176
async def test_cache_functionality():
177177
"""Test that caching works for instruments."""
178-
client = AsyncProjectX(username="test", api_key="key")
178+
client = ProjectX(username="test", api_key="key")
179179

180180
instrument_response = [
181181
{
@@ -222,7 +222,7 @@ async def mock_make_request(method, endpoint, **kwargs):
222222
@pytest.mark.asyncio
223223
async def test_error_handling():
224224
"""Test error handling and retries."""
225-
client = AsyncProjectX(username="test", api_key="key")
225+
client = ProjectX(username="test", api_key="key")
226226

227227
async with client:
228228
# Test connection error with retries
@@ -240,7 +240,7 @@ async def test_error_handling():
240240
@pytest.mark.asyncio
241241
async def test_health_status():
242242
"""Test health status reporting."""
243-
client = AsyncProjectX(username="test", api_key="key")
243+
client = ProjectX(username="test", api_key="key")
244244
client.account_info = MagicMock()
245245
client.account_info.name = "Test Account"
246246

@@ -269,7 +269,7 @@ async def test_health_status():
269269
@pytest.mark.asyncio
270270
async def test_list_accounts():
271271
"""Test listing accounts."""
272-
client = AsyncProjectX(username="test", api_key="key")
272+
client = ProjectX(username="test", api_key="key")
273273

274274
mock_accounts = [
275275
{
@@ -307,7 +307,7 @@ async def test_list_accounts():
307307
@pytest.mark.asyncio
308308
async def test_search_instruments():
309309
"""Test searching for instruments."""
310-
client = AsyncProjectX(username="test", api_key="key")
310+
client = ProjectX(username="test", api_key="key")
311311

312312
mock_instruments = [
313313
{
@@ -349,7 +349,7 @@ async def test_search_instruments():
349349
@pytest.mark.asyncio
350350
async def test_get_bars():
351351
"""Test getting market data bars."""
352-
client = AsyncProjectX(username="test", api_key="key")
352+
client = ProjectX(username="test", api_key="key")
353353
client.account_info = MagicMock()
354354
client.account_info.id = 123
355355

@@ -399,7 +399,7 @@ async def test_get_bars():
399399
@pytest.mark.asyncio
400400
async def test_search_trades():
401401
"""Test searching trade history."""
402-
client = AsyncProjectX(username="test", api_key="key")
402+
client = ProjectX(username="test", api_key="key")
403403
client.account_info = MagicMock()
404404
client.account_info.id = 123
405405

@@ -452,9 +452,10 @@ async def test_rate_limiting():
452452
"""Test rate limiting functionality."""
453453
import time
454454

455-
client = AsyncProjectX(username="test", api_key="key")
455+
client = ProjectX(username="test", api_key="key")
456456
# Set aggressive rate limit for testing
457-
client.rate_limiter = AsyncRateLimiter(max_requests=2, window_seconds=1)
457+
rate_limiter = RateLimiter(requests_per_minute=2)
458+
client.rate_limiter = rate_limiter
458459

459460
async with client:
460461
with patch.object(

tests/test_async_comprehensive.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import pytest
1212

1313
from project_x_py import (
14-
AsyncProjectX,
1514
ProjectX,
1615
ProjectXAuthenticationError,
1716
ProjectXConfig,
@@ -24,7 +23,7 @@ class TestAsyncProjectXClient:
2423
@pytest.mark.asyncio
2524
async def test_async_init_with_credentials(self):
2625
"""Test async client initialization with explicit credentials."""
27-
client = AsyncProjectX(username="test_user", api_key="test_key")
26+
client = ProjectX(username="test_user", api_key="test_key")
2827

2928
assert client.username == "test_user"
3029
assert client.api_key == "test_key"
@@ -36,7 +35,7 @@ async def test_async_init_with_config(self):
3635
"""Test async client initialization with custom configuration."""
3736
config = ProjectXConfig(timeout_seconds=60, retry_attempts=5)
3837

39-
client = AsyncProjectX(username="test_user", api_key="test_key", config=config)
38+
client = ProjectX(username="test_user", api_key="test_key", config=config)
4039

4140
assert client.config.timeout_seconds == 60
4241
assert client.config.retry_attempts == 5
@@ -45,8 +44,8 @@ async def test_async_init_with_config(self):
4544
async def test_async_init_missing_credentials(self):
4645
"""Test async client initialization with missing credentials."""
4746
# AsyncProjectX doesn't validate credentials at init time
48-
client1 = AsyncProjectX(username="", api_key="test_key")
49-
client2 = AsyncProjectX(username="test_user", api_key="")
47+
client1 = ProjectX(username="", api_key="test_key")
48+
client2 = ProjectX(username="test_user", api_key="")
5049

5150
# Validation happens during authentication
5251
assert client1.username == ""
@@ -69,9 +68,7 @@ async def test_async_authenticate_success(self):
6968
mock_response.raise_for_status.return_value = None
7069
mock_client.post.return_value = mock_response
7170

72-
async with AsyncProjectX(
73-
username="test_user", api_key="test_key"
74-
) as client:
71+
async with ProjectX(username="test_user", api_key="test_key") as client:
7572
await client.authenticate()
7673

7774
assert client.session_token == "test_jwt_token"
@@ -98,9 +95,7 @@ async def test_async_authenticate_failure(self):
9895
)
9996
mock_client.post.return_value = mock_response
10097

101-
async with AsyncProjectX(
102-
username="test_user", api_key="test_key"
103-
) as client:
98+
async with ProjectX(username="test_user", api_key="test_key") as client:
10499
with pytest.raises(ProjectXAuthenticationError):
105100
await client.authenticate()
106101

@@ -167,9 +162,7 @@ async def mock_response_func(url, **kwargs):
167162
mock_client.post = mock_response_func
168163
mock_client.get = mock_response_func
169164

170-
async with AsyncProjectX(
171-
username="test_user", api_key="test_key"
172-
) as client:
165+
async with ProjectX(username="test_user", api_key="test_key") as client:
173166
await client.authenticate()
174167

175168
# Test concurrent operations
@@ -203,9 +196,7 @@ async def post(self, *args, **kwargs):
203196
return response
204197

205198
with patch("httpx.AsyncClient", MockAsyncClient):
206-
async with AsyncProjectX(
207-
username="test_user", api_key="test_key"
208-
) as client:
199+
async with ProjectX(username="test_user", api_key="test_key") as client:
209200
pass
210201

211202
assert cleanup_called
@@ -240,9 +231,7 @@ async def mock_get(url, **kwargs):
240231

241232
mock_client.get = mock_get
242233

243-
async with AsyncProjectX(
244-
username="test_user", api_key="test_key"
245-
) as client:
234+
async with ProjectX(username="test_user", api_key="test_key") as client:
246235
await client.authenticate()
247236

248237
# Use gather with return_exceptions=True
@@ -315,7 +304,7 @@ async def mock_async_client():
315304
mock_client.post.return_value = auth_response
316305
mock_client.get.return_value = account_response
317306

318-
client = AsyncProjectX(username="test_user", api_key="test_key")
307+
client = ProjectX(username="test_user", api_key="test_key")
319308
# Simulate authentication
320309
client.session_token = "test_token"
321310
client.account_info = Mock(id="12345", name="Test Account")
@@ -335,9 +324,9 @@ async def test_authenticated_async_client_operations(self, mock_async_client):
335324
@pytest.mark.asyncio
336325
async def test_async_rate_limiting(self):
337326
"""Test async rate limiting functionality."""
338-
from project_x_py.utils import AsyncRateLimiter
327+
from project_x_py import RateLimiter
339328

340-
rate_limiter = AsyncRateLimiter(requests_per_minute=120) # 2 per second
329+
rate_limiter = RateLimiter(requests_per_minute=120) # 2 per second
341330

342331
request_count = 0
343332

@@ -369,7 +358,7 @@ def test_config_compatibility(self):
369358
assert sync_client.config.timeout_seconds == 45
370359

371360
# Test with async client
372-
async_client = AsyncProjectX(username="test", api_key="test", config=config)
361+
async_client = ProjectX(username="test", api_key="test", config=config)
373362
assert async_client.config.timeout_seconds == 45
374363

375364
@pytest.mark.asyncio

tests/test_async_integration.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import pytest
1414

1515
from project_x_py import (
16-
AsyncProjectX,
17-
create_async_order_manager,
18-
create_async_position_manager,
19-
create_async_trading_suite,
16+
ProjectX,
17+
create_order_manager,
18+
create_position_manager,
19+
create_trading_suite,
2020
)
2121
from project_x_py.models import Account, Instrument
2222

@@ -81,7 +81,7 @@ async def delayed_response(delay=0.1):
8181
delayed_response(),
8282
]
8383

84-
async with AsyncProjectX("test_user", "test_key") as client:
84+
async with ProjectX("test_user", "test_key") as client:
8585
client.account_info = mock_account
8686

8787
# Sequential calls
@@ -114,15 +114,15 @@ async def delayed_response(delay=0.1):
114114
@pytest.mark.asyncio
115115
async def test_trading_suite_integration():
116116
"""Test complete trading suite with all components integrated."""
117-
with patch("project_x_py.AsyncProjectX") as mock_client_class:
117+
with patch("project_x_py.ProjectX") as mock_client_class:
118118
# Create mock client
119-
mock_client = AsyncMock(spec=AsyncProjectX)
120-
mock_client.jwt_token = "test_jwt"
119+
mock_client = AsyncMock(spec=ProjectX)
120+
mock_client.session_token = "test_jwt"
121121
mock_client.account_info = MagicMock(id="12345")
122122
mock_client_class.return_value = mock_client
123123

124124
# Create trading suite
125-
suite = await create_async_trading_suite(
125+
suite = await create_trading_suite(
126126
instrument="MGC",
127127
project_x=mock_client,
128128
jwt_token="test_jwt",
@@ -150,13 +150,13 @@ async def test_trading_suite_integration():
150150
@pytest.mark.asyncio
151151
async def test_concurrent_order_placement():
152152
"""Test placing multiple orders concurrently."""
153-
with patch("project_x_py.AsyncProjectX") as mock_client_class:
154-
mock_client = AsyncMock(spec=AsyncProjectX)
153+
with patch("project_x_py.ProjectX") as mock_client_class:
154+
mock_client = AsyncMock(spec=ProjectX)
155155
mock_client.place_order = AsyncMock(
156156
side_effect=[MagicMock(success=True, orderId=f"ORD{i}") for i in range(5)]
157157
)
158158

159-
order_manager = create_async_order_manager(mock_client)
159+
order_manager = create_order_manager(mock_client)
160160
await order_manager.initialize()
161161

162162
# Place 5 orders concurrently
@@ -193,13 +193,13 @@ async def mock_add_callback(event_type, callback):
193193
realtime_client.add_callback = mock_add_callback
194194

195195
# Create managers with shared realtime client
196-
with patch("project_x_py.AsyncProjectX") as mock_client_class:
196+
with patch("project_x_py.ProjectX") as mock_client_class:
197197
mock_client = AsyncMock()
198198

199-
order_manager = create_async_order_manager(mock_client, realtime_client)
199+
order_manager = create_order_manager(mock_client, realtime_client)
200200
await order_manager.initialize()
201201

202-
position_manager = create_async_position_manager(mock_client, realtime_client)
202+
position_manager = create_position_manager(mock_client, realtime_client)
203203
await position_manager.initialize()
204204

205205
# Verify callbacks are registered
@@ -211,7 +211,7 @@ async def mock_add_callback(event_type, callback):
211211
@pytest.mark.asyncio
212212
async def test_concurrent_data_analysis():
213213
"""Test analyzing multiple timeframes concurrently."""
214-
with patch("project_x_py.AsyncProjectX") as mock_client_class:
214+
with patch("project_x_py.ProjectX") as mock_client_class:
215215
mock_client = AsyncMock()
216216

217217
# Mock data retrieval with different delays
@@ -249,7 +249,7 @@ async def get_data(symbol, days, interval):
249249
@pytest.mark.asyncio
250250
async def test_error_handling_in_concurrent_operations():
251251
"""Test that errors in concurrent operations are handled properly."""
252-
with patch("project_x_py.AsyncProjectX") as mock_client_class:
252+
with patch("project_x_py.ProjectX") as mock_client_class:
253253
mock_client = AsyncMock()
254254

255255
# Mix successful and failing operations
@@ -330,16 +330,15 @@ async def main_logic():
330330
@pytest.mark.asyncio
331331
async def test_rate_limiting_with_concurrent_requests():
332332
"""Test that rate limiting works correctly with concurrent requests."""
333-
from project_x_py.utils import AsyncRateLimiter
333+
from project_x_py import RateLimiter
334334

335-
rate_limiter = AsyncRateLimiter(requests_per_minute=60) # 1 per second
335+
rate_limiter = RateLimiter(requests_per_minute=60) # 1 per second
336336

337337
request_times = []
338338

339339
async def make_request(i):
340-
async with rate_limiter:
341-
request_times.append(time.time())
342-
await asyncio.sleep(0.01) # Simulate work
340+
request_times.append(time.time())
341+
await asyncio.sleep(0.01) # Simulate work
343342

344343
# Try to make 5 requests concurrently
345344
start_time = time.time()

0 commit comments

Comments
 (0)