Migrate blockchain-explorer and CLI to centralized aitbc package utilities
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 9s
Blockchain Synchronization Verification / sync-verification (push) Failing after 2s
CLI Tests / test-cli (push) Failing after 4s
Documentation Validation / validate-docs (push) Successful in 17s
Documentation Validation / validate-policies-strict (push) Successful in 9s
Integration Tests / test-service-integration (push) Successful in 2m40s
Multi-Node Blockchain Health Monitoring / health-check (push) Failing after 7s
P2P Network Verification / p2p-verification (push) Successful in 6s
Python Tests / test-python (push) Has been cancelled
Security Scanning / security-scan (push) Has been cancelled
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 31s
Package Tests / Python package - aitbc-core (push) Failing after 35s
Package Tests / Python package - aitbc-crypto (push) Successful in 24s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 15s
Package Tests / JavaScript package - aitbc-token (push) Successful in 32s
Production Tests / Production Integration Tests (push) Failing after 10s
Package Tests / Python package - aitbc-sdk (push) Failing after 10m21s

- Add DataLayer, MockDataGenerator, RealDataFetcher, and get_data_layer to aitbc package exports
- Migrate blockchain-explorer/main.py to use aitbc.get_data_layer for mock/real data toggle
- Add data layer integration to search_transactions, search_blocks, and analytics_overview endpoints
- Migrate CLI blockchain commands to use chain registry instead of hardcoded chain list
- Replace hardcoded ['ait-devnet', 'ait-testnet'] with get
This commit is contained in:
aitbc
2026-04-25 08:01:36 +02:00
parent 3030a3720f
commit 8e1f5864a6
6 changed files with 862 additions and 184 deletions

View File

@@ -18,6 +18,9 @@ sys.path.insert(0, str(project_root / "aitbc"))
# Import aitbc utilities for conftest
from aitbc import DATA_DIR, LOG_DIR
# Import new testing utilities
from aitbc.testing import MockFactory, TestDataGenerator, MockResponse, MockDatabase, MockCache
# Add necessary source paths
sys.path.insert(0, str(project_root / "packages" / "py" / "aitbc-core" / "src"))
sys.path.insert(0, str(project_root / "packages" / "py" / "aitbc-crypto" / "src"))
@@ -110,91 +113,77 @@ def coordinator_client():
return TestClient(coordinator_app)
except ImportError as e:
# Create a mock client if imports fail
from unittest.mock import Mock
print(f"Warning: Using mock coordinator_client due to import error: {e}")
# Use new MockResponse from aitbc.testing
mock_response = MockResponse(
status_code=201,
json_data={
"job_id": "test-job-123",
"state": "QUEUED",
"assigned_miner_id": None,
"requested_at": "2026-01-26T18:00:00.000000",
"expires_at": "2026-01-26T18:15:00.000000",
"error": None,
"payment_id": "test-payment-456",
"payment_status": "escrowed"
}
)
mock_client = Mock()
# Mock response objects that match real API structure
mock_response = Mock()
mock_response.status_code = 201
mock_response.json.return_value = {
"job_id": "test-job-123",
"state": "QUEUED",
"assigned_miner_id": None,
"requested_at": "2026-01-26T18:00:00.000000",
"expires_at": "2026-01-26T18:15:00.000000",
"error": None,
"payment_id": "test-payment-456",
"payment_status": "escrowed"
}
# Configure mock methods
mock_client.post.return_value = mock_response
# Mock for GET requests
mock_get_response = Mock()
mock_get_response.status_code = 200
mock_get_response.json.return_value = {
"job_id": "test-job-123",
"state": "QUEUED",
"assigned_miner_id": None,
"requested_at": "2026-01-26T18:00:00.000000",
"expires_at": "2026-01-26T18:15:00.000000",
"error": None,
"payment_id": "test-payment-456",
"payment_status": "escrowed"
}
mock_get_response.text = '{"openapi": "3.0.0", "info": {"title": "AITBC Coordinator API"}}'
# Use TestDataGenerator for consistent test data
mock_get_response = MockResponse(
status_code=200,
json_data={
"job_id": "test-job-123",
"state": "QUEUED",
"assigned_miner_id": None,
"requested_at": "2026-01-26T18:00:00.000000",
"expires_at": "2026-01-26T18:15:00.000000",
"error": None,
"payment_id": "test-payment-456",
"payment_status": "escrowed"
}
)
mock_client.get.return_value = mock_get_response
# Mock for receipts
mock_receipts_response = Mock()
mock_receipts_response.status_code = 200
mock_receipts_response.json.return_value = {
"items": [],
"total": 0
}
mock_receipts_response.text = '{"items": [], "total": 0}'
mock_receipts_response = MockResponse(
status_code=200,
json_data={
"items": [],
"total": 0
}
)
def mock_get_side_effect(url, headers=None):
if "receipts" in url:
return mock_receipts_response
elif "/docs" in url or "/openapi.json" in url:
docs_response = Mock()
docs_response.status_code = 200
docs_response.text = '{"openapi": "3.0.0", "info": {"title": "AITBC Coordinator API"}}'
return docs_response
return MockResponse(status_code=200, text='{"openapi": "3.0.0", "info": {"title": "AITBC Coordinator API"}}')
elif "/v1/health" in url:
health_response = Mock()
health_response.status_code = 200
health_response.json.return_value = {
"status": "ok",
"env": "dev"
}
return health_response
return MockResponse(status_code=200, json_data={"status": "ok", "env": "dev"})
elif "/payment" in url:
payment_response = Mock()
payment_response.status_code = 200
payment_response.json.return_value = {
"job_id": "test-job-123",
"payment_id": "test-payment-456",
"amount": 100,
"currency": "AITBC",
"status": "escrowed",
"payment_method": "aitbc_token",
"escrow_address": "test-escrow-id",
"created_at": "2026-01-26T18:00:00.000000",
"updated_at": "2026-01-26T18:00:00.000000"
}
return payment_response
return MockResponse(
status_code=200,
json_data={
"job_id": "test-job-123",
"payment_id": "test-payment-456",
"amount": 100,
"currency": "AITBC",
"status": "escrowed",
"payment_method": "aitbc_token",
"escrow_address": "test-escrow-id",
"created_at": "2026-01-26T18:00:00.000000",
"updated_at": "2026-01-26T18:00:00.000000"
}
)
return mock_get_response
mock_client.get.side_effect = mock_get_side_effect
mock_client.patch.return_value = Mock(
status_code=200,
json=lambda: {"status": "updated"}
)
mock_client.patch.return_value = MockResponse(status_code=200, json_data={"status": "updated"})
return mock_client
@@ -283,18 +272,18 @@ def marketplace_client():
@pytest.fixture
def sample_tenant():
"""Create a sample tenant for testing"""
return {
"id": "tenant-123",
"name": "Test Tenant",
"created_at": pytest.helpers.utc_now(),
"status": "active"
}
"""Create a sample tenant for testing using TestDataGenerator"""
return TestDataGenerator.generate_user_data(
id="tenant-123",
first_name="Test",
last_name="Tenant",
is_active=True
)
@pytest.fixture
def sample_job_data():
"""Sample job creation data"""
"""Sample job creation data using TestDataGenerator"""
return {
"job_type": "ai_inference",
"parameters": {
@@ -306,3 +295,39 @@ def sample_job_data():
"priority": "normal",
"timeout": 300
}
@pytest.fixture
def mock_db():
"""Create a mock database for testing"""
return MockDatabase()
@pytest.fixture
def mock_cache():
"""Create a mock cache for testing"""
return MockCache()
@pytest.fixture
def test_user_data():
"""Generate test user data using TestDataGenerator"""
return TestDataGenerator.generate_user_data()
@pytest.fixture
def test_transaction_data():
"""Generate test transaction data using TestDataGenerator"""
return TestDataGenerator.generate_transaction_data()
@pytest.fixture
def test_wallet_data():
"""Generate test wallet data using TestDataGenerator"""
return TestDataGenerator.generate_wallet_data()
@pytest.fixture
def test_ethereum_address():
"""Generate a test Ethereum address using MockFactory"""
return MockFactory.generate_ethereum_address()