Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified backend/app/core/__pycache__/config.cpython-313.pyc
Binary file not shown.
Binary file modified backend/app/core/__pycache__/orchestrator.cpython-313.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
165 changes: 151 additions & 14 deletions backend/app/services/agents/tests/test_onchain_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ async def test_fetch_onchain_metrics_http_error_raises_onchainagenthttperror(moc
create_mock_response(404) # All attempts fail
]

with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_onchain_metrics(url="http://test.com/onchain")
assert excinfo.value.status_code == 404
assert mock_client_instance.get.call_count == 3 # Retries should still happen
with patch.object(fetch_onchain_metrics.retry, 'wait', new=wait_fixed(0.01)), \
patch.object(fetch_onchain_metrics.retry, 'stop', new=stop_after_attempt(3)):
with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_onchain_metrics(url="http://test.com/onchain")
assert excinfo.value.status_code == 404
assert mock_client_instance.get.call_count == 3 # Retries should still happen

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
Expand All @@ -172,9 +174,11 @@ async def test_fetch_onchain_metrics_unexpected_error_raises_onchainagentexcepti
Exception("Unexpected error")
]

with pytest.raises(OnchainAgentException):
await fetch_onchain_metrics(url="http://test.com/onchain")
assert mock_client_instance.get.call_count == 3 # Retries should still happen
with patch.object(fetch_onchain_metrics.retry, 'wait', new=wait_fixed(0.01)), \
patch.object(fetch_onchain_metrics.retry, 'stop', new=stop_after_attempt(3)):
with pytest.raises(OnchainAgentException):
await fetch_onchain_metrics(url="http://test.com/onchain")
assert mock_client_instance.get.call_count == 3 # Retries should still happen

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
Expand All @@ -187,10 +191,12 @@ async def test_fetch_tokenomics_http_error_raises_onchainagenthttperror(mock_asy
create_mock_response(403)
]

with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_tokenomics(url="http://test.com/tokenomics")
assert excinfo.value.status_code == 403
assert mock_client_instance.get.call_count == 3 # Retries should still happen
with patch.object(fetch_tokenomics.retry, 'wait', new=wait_fixed(0.01)), \
patch.object(fetch_tokenomics.retry, 'stop', new=stop_after_attempt(3)):
with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_tokenomics(url="http://test.com/tokenomics")
assert excinfo.value.status_code == 403
assert mock_client_instance.get.call_count == 3 # Retries should still happen

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
Expand All @@ -203,6 +209,137 @@ async def test_fetch_tokenomics_unexpected_error_raises_onchainagentexception(mo
Exception("Another unexpected error")
]

with pytest.raises(OnchainAgentException):
await fetch_tokenomics(url="http://test.com/tokenomics")
assert mock_client_instance.get.call_count == 3 # Retries should still happen
with patch.object(fetch_tokenomics.retry, 'wait', new=wait_fixed(0.01)), \
patch.object(fetch_tokenomics.retry, 'stop', new=stop_after_attempt(3)):
with pytest.raises(OnchainAgentException):
await fetch_tokenomics(url="http://test.com/tokenomics")
assert mock_client_instance.get.call_count == 3 # Retries should still happen

# --- New tests for successful fetching and schema validation ---

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_onchain_metrics_success_and_schema(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

expected_metrics = {
"total_transactions": 1000,
"active_users": 500,
"average_transaction_value": 150.75,
"timestamp": "2023-10-27T10:00:00Z"
}
mock_client_instance.get.return_value = create_mock_response(200, expected_metrics)

result = await fetch_onchain_metrics(url="http://test.com/onchain")
assert result == expected_metrics
assert "total_transactions" in result
assert "active_users" in result
assert "average_transaction_value" in result
assert "timestamp" in result
assert isinstance(result["total_transactions"], int)
assert isinstance(result["active_users"], int)
assert isinstance(result["average_transaction_value"], float)
assert isinstance(result["timestamp"], str)

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_tokenomics_success_and_schema(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

expected_tokenomics = {
"total_supply": "1000000000",
"circulating_supply": "800000000",
"market_cap_usd": "1500000000.50",
"token_price_usd": "1.50",
"last_updated": "2023-10-27T10:00:00Z"
}
mock_client_instance.get.return_value = create_mock_response(200, expected_tokenomics)

result = await fetch_tokenomics(url="http://test.com/tokenomics")
assert result == expected_tokenomics
assert "total_supply" in result
assert "circulating_supply" in result
assert "market_cap_usd" in result
assert "token_price_usd" in result
assert "last_updated" in result
assert isinstance(result["total_supply"], str)
assert isinstance(result["circulating_supply"], str)
assert isinstance(result["market_cap_usd"], str)
assert isinstance(result["token_price_usd"], str)
assert isinstance(result["last_updated"], str)

# --- New tests for handling missing fields ---

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_onchain_metrics_missing_fields(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

# Simulate a response with some missing fields
incomplete_metrics = {
"total_transactions": 1234,
"timestamp": "2023-10-27T11:00:00Z"
}
mock_client_instance.get.return_value = create_mock_response(200, incomplete_metrics)

result = await fetch_onchain_metrics(url="http://test.com/onchain")
assert result == incomplete_metrics
# The agent should return whatever it gets, schema validation is typically done downstream
assert "total_transactions" in result
assert "active_users" not in result
assert "average_transaction_value" not in result
assert "timestamp" in result

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_tokenomics_missing_fields(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

# Simulate a response with some missing fields
incomplete_tokenomics = {
"total_supply": "999999999",
"token_price_usd": "2.10"
}
mock_client_instance.get.return_value = create_mock_response(200, incomplete_tokenomics)

result = await fetch_tokenomics(url="http://test.com/tokenomics")
assert result == incomplete_tokenomics
assert "total_supply" in result
assert "circulating_supply" not in result
assert "market_cap_usd" not in result
assert "token_price_usd" in result
assert "last_updated" not in result

# --- New tests for invalid token IDs (simulated via API response) ---

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_onchain_metrics_invalid_token_id(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

# Simulate an API response indicating an invalid token ID (e.g., 400 Bad Request)
error_response_data = {"error": "Invalid token ID provided"}
mock_client_instance.get.return_value = create_mock_response(400, error_response_data)

with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_onchain_metrics(url="http://test.com/onchain", params={"token_id": "invalid"})
assert excinfo.value.status_code == 400

@pytest.mark.asyncio
@patch('httpx.AsyncClient')
async def test_fetch_tokenomics_invalid_token_id(mock_async_client):
mock_client_instance = AsyncMock()
mock_async_client.return_value.__aenter__.return_value = mock_client_instance

# Simulate an API response indicating an invalid token ID (e.g., 404 Not Found)
error_response_data = {"message": "Token not found"}
mock_client_instance.get.return_value = create_mock_response(404, error_response_data)

with pytest.raises(OnchainAgentHTTPError) as excinfo:
await fetch_tokenomics(url="http://test.com/tokenomics", params={"token_id": "nonexistent"})
assert excinfo.value.status_code == 404
Loading