Add sys import to test files and remove obsolete integration tests
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 9s
Blockchain Synchronization Verification / sync-verification (push) Failing after 1s
CLI Tests / test-cli (push) Failing after 3s
Documentation Validation / validate-docs (push) Successful in 6s
Documentation Validation / validate-policies-strict (push) Successful in 2s
Integration Tests / test-service-integration (push) Successful in 40s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 1s
P2P Network Verification / p2p-verification (push) Successful in 2s
Production Tests / Production Integration Tests (push) Successful in 21s
Python Tests / test-python (push) Successful in 13s
Security Scanning / security-scan (push) Failing after 46s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Successful in 17s
Smart Contract Tests / lint-solidity (push) Successful in 10s

- Add sys import to 29 test files across agent-coordinator, blockchain-event-bridge, blockchain-node, and coordinator-api
- Remove apps/blockchain-event-bridge/tests/test_integration.py (obsolete bridge integration tests)
- Remove apps/coordinator-api/tests/test_integration.py (obsolete API integration tests)
- Implement GPU registration in marketplace_gpu.py with GPURegistry model persistence
This commit is contained in:
aitbc
2026-04-23 16:43:17 +02:00
parent b8b1454573
commit e60cc3226c
134 changed files with 14321 additions and 1873 deletions

View File

@@ -0,0 +1 @@
"""Exchange integration service tests"""

View File

@@ -0,0 +1,256 @@
"""Edge case and error handling tests for exchange integration service"""
import pytest
import sys
import sys
from pathlib import Path
from unittest.mock import Mock, patch
from fastapi.testclient import TestClient
# Mock aiohttp before importing
sys.modules['aiohttp'] = Mock()
from main import app, ExchangeRegistration, TradingPair, OrderRequest, exchanges, trading_pairs, orders
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
exchanges.clear()
trading_pairs.clear()
orders.clear()
yield
exchanges.clear()
trading_pairs.clear()
orders.clear()
@pytest.mark.unit
def test_exchange_registration_empty_name():
"""Test ExchangeRegistration with empty name"""
registration = ExchangeRegistration(
name="",
api_key="test_key_123"
)
assert registration.name == ""
@pytest.mark.unit
def test_exchange_registration_empty_api_key():
"""Test ExchangeRegistration with empty API key"""
registration = ExchangeRegistration(
name="TestExchange",
api_key=""
)
assert registration.api_key == ""
@pytest.mark.unit
def test_trading_pair_zero_min_order_size():
"""Test TradingPair with zero min order size"""
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.0,
price_precision=8,
quantity_precision=6
)
assert pair.min_order_size == 0.0
@pytest.mark.unit
def test_trading_pair_negative_min_order_size():
"""Test TradingPair with negative min order size"""
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=-0.001,
price_precision=8,
quantity_precision=6
)
assert pair.min_order_size == -0.001
@pytest.mark.unit
def test_order_request_zero_quantity():
"""Test OrderRequest with zero quantity"""
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="limit",
quantity=0.0,
price=0.00001
)
assert order.quantity == 0.0
@pytest.mark.unit
def test_order_request_negative_quantity():
"""Test OrderRequest with negative quantity"""
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="limit",
quantity=-100.0,
price=0.00001
)
assert order.quantity == -100.0
@pytest.mark.integration
def test_order_request_invalid_side():
"""Test OrderRequest with invalid side"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Create order with invalid side (API doesn't validate, but test the behavior)
order = OrderRequest(
symbol="AITBC/BTC",
side="invalid",
type="limit",
quantity=100.0,
price=0.00001
)
# This will be accepted by the API as it doesn't validate the side
response = client.post("/api/v1/orders", json=order.model_dump())
assert response.status_code == 200
@pytest.mark.integration
def test_order_request_invalid_type():
"""Test OrderRequest with invalid type"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Create order with invalid type (API doesn't validate, but test the behavior)
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="invalid",
quantity=100.0,
price=0.00001
)
# This will be accepted by the API as it doesn't validate the type
response = client.post("/api/v1/orders", json=order.model_dump())
assert response.status_code == 200
@pytest.mark.integration
def test_connect_already_connected_exchange():
"""Test connecting to already connected exchange"""
client = TestClient(app)
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123"
)
# Register exchange
client.post("/api/v1/exchanges/register", json=registration.model_dump())
# Connect first time
client.post("/api/v1/exchanges/testexchange/connect")
# Connect second time should return already_connected
response = client.post("/api/v1/exchanges/testexchange/connect")
assert response.status_code == 200
data = response.json()
assert data["status"] == "already_connected"
@pytest.mark.integration
def test_update_market_price_missing_fields():
"""Test updating market price with missing fields"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC-BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
create_response = client.post("/api/v1/pairs/create", json=pair.model_dump())
assert create_response.status_code == 200
# Update with missing price
price_data = {"volume": 50000.0}
response = client.post("/api/v1/market-data/aitbc-btc/price", json=price_data)
assert response.status_code == 200
data = response.json()
# Should use None for missing price
assert data["current_price"] is None
@pytest.mark.integration
def test_update_market_price_zero_price():
"""Test updating market price with zero price"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC-BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
create_response = client.post("/api/v1/pairs/create", json=pair.model_dump())
assert create_response.status_code == 200
# Update with zero price
price_data = {"price": 0.0}
response = client.post("/api/v1/market-data/aitbc-btc/price", json=price_data)
assert response.status_code == 200
data = response.json()
assert data["current_price"] == 0.0
@pytest.mark.integration
def test_update_market_price_negative_price():
"""Test updating market price with negative price"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC-BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
create_response = client.post("/api/v1/pairs/create", json=pair.model_dump())
assert create_response.status_code == 200
# Update with negative price
price_data = {"price": -0.00001}
response = client.post("/api/v1/market-data/aitbc-btc/price", json=price_data)
assert response.status_code == 200
data = response.json()
assert data["current_price"] == -0.00001

View File

@@ -0,0 +1,378 @@
"""Integration tests for exchange integration service"""
import pytest
import sys
import sys
from pathlib import Path
from unittest.mock import Mock, patch
from fastapi.testclient import TestClient
# Mock aiohttp before importing
sys.modules['aiohttp'] = Mock()
from main import app, ExchangeRegistration, TradingPair, OrderRequest, exchanges, trading_pairs, orders
@pytest.fixture(autouse=True)
def reset_state():
"""Reset global state before each test"""
exchanges.clear()
trading_pairs.clear()
orders.clear()
yield
exchanges.clear()
trading_pairs.clear()
orders.clear()
@pytest.mark.integration
def test_root_endpoint():
"""Test root endpoint"""
client = TestClient(app)
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert data["service"] == "AITBC Exchange Integration"
assert data["status"] == "running"
@pytest.mark.integration
def test_health_check_endpoint():
"""Test health check endpoint"""
client = TestClient(app)
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert "exchanges_connected" in data
assert "active_pairs" in data
assert "total_orders" in data
@pytest.mark.integration
def test_register_exchange():
"""Test exchange registration"""
client = TestClient(app)
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123",
sandbox=True
)
response = client.post("/api/v1/exchanges/register", json=registration.model_dump())
assert response.status_code == 200
data = response.json()
assert data["exchange_id"] == "testexchange"
assert data["status"] == "registered"
assert data["name"] == "TestExchange"
@pytest.mark.integration
def test_register_duplicate_exchange():
"""Test registering duplicate exchange"""
client = TestClient(app)
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123"
)
# First registration
client.post("/api/v1/exchanges/register", json=registration.model_dump())
# Second registration should fail
response = client.post("/api/v1/exchanges/register", json=registration.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_connect_exchange():
"""Test connecting to exchange"""
client = TestClient(app)
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123"
)
# Register exchange first
client.post("/api/v1/exchanges/register", json=registration.model_dump())
# Connect to exchange
response = client.post("/api/v1/exchanges/testexchange/connect")
assert response.status_code == 200
data = response.json()
assert data["exchange_id"] == "testexchange"
assert data["status"] == "connected"
@pytest.mark.integration
def test_connect_nonexistent_exchange():
"""Test connecting to nonexistent exchange"""
client = TestClient(app)
response = client.post("/api/v1/exchanges/nonexistent/connect")
assert response.status_code == 404
@pytest.mark.integration
def test_create_trading_pair():
"""Test creating trading pair"""
client = TestClient(app)
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
response = client.post("/api/v1/pairs/create", json=pair.model_dump())
assert response.status_code == 200
data = response.json()
assert data["pair_id"] == "aitbc/btc"
assert data["symbol"] == "AITBC/BTC"
assert data["status"] == "created"
@pytest.mark.integration
def test_create_duplicate_trading_pair():
"""Test creating duplicate trading pair"""
client = TestClient(app)
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
# First creation
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Second creation should fail
response = client.post("/api/v1/pairs/create", json=pair.model_dump())
assert response.status_code == 400
@pytest.mark.integration
def test_list_trading_pairs():
"""Test listing trading pairs"""
client = TestClient(app)
response = client.get("/api/v1/pairs")
assert response.status_code == 200
data = response.json()
assert "pairs" in data
assert "total_pairs" in data
@pytest.mark.integration
def test_get_trading_pair():
"""Test getting specific trading pair"""
client = TestClient(app)
pair = TradingPair(
symbol="AITBC-BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
# Create pair first
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Get pair with lowercase symbol as pair_id
response = client.get("/api/v1/pairs/aitbc-btc")
assert response.status_code == 200
data = response.json()
assert data["symbol"] == "AITBC-BTC"
@pytest.mark.integration
def test_get_nonexistent_trading_pair():
"""Test getting nonexistent trading pair"""
client = TestClient(app)
response = client.get("/api/v1/pairs/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_create_order():
"""Test creating order"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Create order
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="limit",
quantity=100.0,
price=0.00001
)
response = client.post("/api/v1/orders", json=order.model_dump())
assert response.status_code == 200
data = response.json()
assert data["symbol"] == "AITBC/BTC"
assert data["side"] == "buy"
assert data["status"] == "filled"
assert data["filled_quantity"] == 100.0
@pytest.mark.integration
def test_create_order_nonexistent_pair():
"""Test creating order for nonexistent pair"""
client = TestClient(app)
order = OrderRequest(
symbol="NONEXISTENT/BTC",
side="buy",
type="limit",
quantity=100.0,
price=0.00001
)
response = client.post("/api/v1/orders", json=order.model_dump())
assert response.status_code == 404
@pytest.mark.integration
def test_list_orders():
"""Test listing orders"""
client = TestClient(app)
response = client.get("/api/v1/orders")
assert response.status_code == 200
data = response.json()
assert "orders" in data
assert "total_orders" in data
@pytest.mark.integration
def test_get_order():
"""Test getting specific order"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Create order
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="limit",
quantity=100.0,
price=0.00001
)
create_response = client.post("/api/v1/orders", json=order.model_dump())
order_id = create_response.json()["order_id"]
# Get order
response = client.get(f"/api/v1/orders/{order_id}")
assert response.status_code == 200
data = response.json()
assert data["order_id"] == order_id
@pytest.mark.integration
def test_get_nonexistent_order():
"""Test getting nonexistent order"""
client = TestClient(app)
response = client.get("/api/v1/orders/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_list_exchanges():
"""Test listing exchanges"""
client = TestClient(app)
response = client.get("/api/v1/exchanges")
assert response.status_code == 200
data = response.json()
assert "exchanges" in data
assert "total_exchanges" in data
@pytest.mark.integration
def test_get_exchange():
"""Test getting specific exchange"""
client = TestClient(app)
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123"
)
# Register exchange first
client.post("/api/v1/exchanges/register", json=registration.model_dump())
# Get exchange
response = client.get("/api/v1/exchanges/testexchange")
assert response.status_code == 200
data = response.json()
assert data["exchange_id"] == "testexchange"
@pytest.mark.integration
def test_get_nonexistent_exchange():
"""Test getting nonexistent exchange"""
client = TestClient(app)
response = client.get("/api/v1/exchanges/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_update_market_price():
"""Test updating market price"""
client = TestClient(app)
# Create trading pair first
pair = TradingPair(
symbol="AITBC-BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
client.post("/api/v1/pairs/create", json=pair.model_dump())
# Update price
price_data = {"price": 0.000015, "volume": 50000.0}
response = client.post("/api/v1/market-data/aitbc-btc/price", json=price_data)
assert response.status_code == 200
data = response.json()
assert data["current_price"] == 0.000015
@pytest.mark.integration
def test_update_price_nonexistent_pair():
"""Test updating price for nonexistent pair"""
client = TestClient(app)
price_data = {"price": 0.000015}
response = client.post("/api/v1/market-data/nonexistent/price", json=price_data)
assert response.status_code == 404
@pytest.mark.integration
def test_get_market_data():
"""Test getting market data"""
client = TestClient(app)
response = client.get("/api/v1/market-data")
assert response.status_code == 200
data = response.json()
assert "market_data" in data
assert "total_pairs" in data

View File

@@ -0,0 +1,101 @@
"""Unit tests for exchange integration service"""
import pytest
import sys
import sys
from pathlib import Path
from unittest.mock import Mock, patch
# Mock aiohttp before importing
sys.modules['aiohttp'] = Mock()
from main import app, ExchangeRegistration, TradingPair, OrderRequest
@pytest.mark.unit
def test_app_initialization():
"""Test that the FastAPI app initializes correctly"""
assert app is not None
assert app.title == "AITBC Exchange Integration Service"
assert app.version == "1.0.0"
@pytest.mark.unit
def test_exchange_registration_model():
"""Test ExchangeRegistration model"""
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123",
sandbox=True,
description="Test exchange"
)
assert registration.name == "TestExchange"
assert registration.api_key == "test_key_123"
assert registration.sandbox is True
assert registration.description == "Test exchange"
@pytest.mark.unit
def test_exchange_registration_defaults():
"""Test ExchangeRegistration default values"""
registration = ExchangeRegistration(
name="TestExchange",
api_key="test_key_123"
)
assert registration.name == "TestExchange"
assert registration.api_key == "test_key_123"
assert registration.sandbox is True
assert registration.description is None
@pytest.mark.unit
def test_trading_pair_model():
"""Test TradingPair model"""
pair = TradingPair(
symbol="AITBC/BTC",
base_asset="AITBC",
quote_asset="BTC",
min_order_size=0.001,
price_precision=8,
quantity_precision=6
)
assert pair.symbol == "AITBC/BTC"
assert pair.base_asset == "AITBC"
assert pair.quote_asset == "BTC"
assert pair.min_order_size == 0.001
assert pair.price_precision == 8
assert pair.quantity_precision == 6
@pytest.mark.unit
def test_order_request_model():
"""Test OrderRequest model"""
order = OrderRequest(
symbol="AITBC/BTC",
side="buy",
type="limit",
quantity=100.0,
price=0.00001
)
assert order.symbol == "AITBC/BTC"
assert order.side == "buy"
assert order.type == "limit"
assert order.quantity == 100.0
assert order.price == 0.00001
@pytest.mark.unit
def test_order_request_market_order():
"""Test OrderRequest for market order"""
order = OrderRequest(
symbol="AITBC/BTC",
side="sell",
type="market",
quantity=50.0
)
assert order.symbol == "AITBC/BTC"
assert order.side == "sell"
assert order.type == "market"
assert order.quantity == 50.0
assert order.price is None