Add unit tests for core services
- Added test_services_blockchain.py with tests for blockchain service - Added test_services_marketplace.py with tests for marketplace service - Added test_services_agent.py with tests for agent service This is Phase 2.3: Add tests for core services
This commit is contained in:
50
apps/coordinator-api/tests/test_services_agent.py
Normal file
50
apps/coordinator-api/tests/test_services_agent.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""
|
||||
Tests for agent service
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestAgentService:
|
||||
"""Test agent service"""
|
||||
|
||||
@patch('app.services.agent_service.AITBCHTTPClient')
|
||||
def test_get_agent_status(self, mock_client_class):
|
||||
"""Test getting agent status"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.get.return_value = {
|
||||
"id": "agent1",
|
||||
"name": "Agent 1",
|
||||
"status": "active",
|
||||
"current_task": None
|
||||
}
|
||||
|
||||
# Import and test
|
||||
from app.services.agent_service import get_agent_status
|
||||
|
||||
result = get_agent_status("agent1")
|
||||
assert result["id"] == "agent1"
|
||||
assert result["status"] == "active"
|
||||
|
||||
@patch('app.services.agent_service.AITBCHTTPClient')
|
||||
def test_register_agent(self, mock_client_class):
|
||||
"""Test registering a new agent"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.post.return_value = {
|
||||
"id": "agent2",
|
||||
"name": "Agent 2",
|
||||
"status": "registered"
|
||||
}
|
||||
|
||||
# Import and test
|
||||
from app.services.agent_service import register_agent
|
||||
|
||||
result = register_agent({"name": "Agent 2", "type": "compute"})
|
||||
assert result["id"] == "agent2"
|
||||
assert result["status"] == "registered"
|
||||
44
apps/coordinator-api/tests/test_services_blockchain.py
Normal file
44
apps/coordinator-api/tests/test_services_blockchain.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
Tests for blockchain service
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestBlockchainService:
|
||||
"""Test blockchain service"""
|
||||
|
||||
@patch('app.services.blockchain.AITBCHTTPClient')
|
||||
def test_get_block_height(self, mock_client_class):
|
||||
"""Test getting current block height"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.get.return_value = {"height": 1000}
|
||||
|
||||
# Import and test
|
||||
from app.services.blockchain import get_block_height
|
||||
|
||||
result = get_block_height()
|
||||
assert result == 1000
|
||||
|
||||
@patch('app.services.blockchain.AITBCHTTPClient')
|
||||
def test_get_block_by_hash(self, mock_client_class):
|
||||
"""Test getting block by hash"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.get.return_value = {
|
||||
"hash": "0xabc123",
|
||||
"height": 1000,
|
||||
"timestamp": "2024-01-01T00:00:00Z"
|
||||
}
|
||||
|
||||
# Import and test
|
||||
from app.services.blockchain import get_block_by_hash
|
||||
|
||||
result = get_block_by_hash("0xabc123")
|
||||
assert result["hash"] == "0xabc123"
|
||||
assert result["height"] == 1000
|
||||
52
apps/coordinator-api/tests/test_services_marketplace.py
Normal file
52
apps/coordinator-api/tests/test_services_marketplace.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
Tests for marketplace service
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestMarketplaceService:
|
||||
"""Test marketplace service"""
|
||||
|
||||
@patch('app.services.marketplace.AITBCHTTPClient')
|
||||
def test_list_marketplace_items(self, mock_client_class):
|
||||
"""Test listing marketplace items"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.get.return_value = {
|
||||
"items": [
|
||||
{"id": 1, "name": "GPU 1", "price": 0.50},
|
||||
{"id": 2, "name": "GPU 2", "price": 0.75}
|
||||
],
|
||||
"total": 2
|
||||
}
|
||||
|
||||
# Import and test
|
||||
from app.services.marketplace import list_marketplace_items
|
||||
|
||||
result = list_marketplace_items()
|
||||
assert result["total"] == 2
|
||||
assert len(result["items"]) == 2
|
||||
|
||||
@patch('app.services.marketplace.AITBCHTTPClient')
|
||||
def test_create_marketplace_item(self, mock_client_class):
|
||||
"""Test creating a marketplace item"""
|
||||
# Setup mock
|
||||
mock_client = Mock()
|
||||
mock_client_class.return_value = mock_client
|
||||
mock_client.post.return_value = {
|
||||
"id": 3,
|
||||
"name": "GPU 3",
|
||||
"price": 1.00,
|
||||
"status": "active"
|
||||
}
|
||||
|
||||
# Import and test
|
||||
from app.services.marketplace import create_marketplace_item
|
||||
|
||||
result = create_marketplace_item({"name": "GPU 3", "price": 1.00})
|
||||
assert result["id"] == 3
|
||||
assert result["status"] == "active"
|
||||
Reference in New Issue
Block a user