mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-23 20:21:03 +02:00
6.1 KiB
6.1 KiB
TrustGraph Python API Integration Tests
This directory contains integration tests for the TrustGraph Python API refactor.
Overview
The integration tests verify the following components:
- Basic API instantiation and client creation
- REST API functionality (synchronous and asynchronous)
- WebSocket API functionality (synchronous and asynchronous)
- Bulk operations for data import/export
- Metrics endpoints
- Streaming types and data structures
Prerequisites
-
Python dependencies:
pip install pytest pytest-asyncio -
Running TrustGraph Gateway API:
- The tests require a running TrustGraph Gateway API server
- Default URL:
http://localhost:8088/ - Configure via environment variables (see below)
Running the Tests
Quick Start (No Gateway Required)
To run tests in "skip mode" (validates structure without requiring a running server):
export SKIP_INTEGRATION_TESTS=true
pytest tests/test_api_integration.py -v
Full Integration Tests (Requires Running Gateway)
-
Start your TrustGraph Gateway API server
-
Configure environment variables:
export TRUSTGRAPH_URL=http://localhost:8088/ export TRUSTGRAPH_TOKEN=your-auth-token-here # Optional export TRUSTGRAPH_TEST_FLOW=test-flow # Optional export SKIP_INTEGRATION_TESTS=false # Enable tests -
Run the tests:
pytest tests/test_api_integration.py -v
Running Specific Test Classes
# Test only REST API
pytest tests/test_api_integration.py::TestRESTAPI -v
# Test only WebSocket API
pytest tests/test_api_integration.py::TestWebSocketAPI -v
# Test only async functionality
pytest tests/test_api_integration.py::TestAsyncRESTAPI -v
pytest tests/test_api_integration.py::TestAsyncWebSocketAPI -v
# Test only bulk operations
pytest tests/test_api_integration.py::TestBulkOperations -v
# Test only metrics
pytest tests/test_api_integration.py::TestMetrics -v
Running with Coverage
pytest tests/test_api_integration.py --cov=trustgraph.api --cov-report=html
Environment Variables
| Variable | Description | Default |
|---|---|---|
TRUSTGRAPH_URL |
Gateway API URL | http://localhost:8088/ |
TRUSTGRAPH_TOKEN |
Authentication token | None |
TRUSTGRAPH_TEST_FLOW |
Flow ID for testing | test-flow |
SKIP_INTEGRATION_TESTS |
Skip tests requiring live server | false |
Test Categories
1. Basic Connection Tests (TestBasicConnection)
- API instantiation with/without token
- Context manager support
- Lazy initialization of clients
2. REST API Tests (TestRESTAPI)
- Flow listing
- Flow class listing
- Flow instance creation
- Method existence verification
3. Async REST API Tests (TestAsyncRESTAPI)
- Async flow operations
- Async context manager
- Async/await patterns
4. WebSocket API Tests (TestWebSocketAPI)
- WebSocket client creation
- Flow instance creation
- Method availability
- URL conversion (HTTP → WS)
5. Async WebSocket API Tests (TestAsyncWebSocketAPI)
- Async WebSocket operations
- Streaming support verification
- Method signatures
6. Bulk Operations Tests (TestBulkOperations)
- Bulk client instantiation
- Import/export methods
- Async bulk operations
- Iterator-based data transfer
7. Metrics Tests (TestMetrics)
- Metrics endpoint access
- Prometheus format validation
- Async metrics retrieval
8. Streaming Types Tests (TestStreamingTypes)
AgentThoughtchunk creationAgentObservationchunk creationAgentAnswerchunk creationRAGChunkcreation
9. Triple Type Tests (TestTripleType)
- Triple data structure validation
Test Structure
tests/
├── __init__.py # Test package marker
├── README.md # This file
└── test_api_integration.py # Main integration test suite
Adding New Tests
To add new integration tests:
-
Create a new test class in
test_api_integration.py:class TestNewFeature: """Test new feature functionality""" @skip_if_no_gateway def test_new_feature(self): api = Api(url=GATEWAY_URL, token=AUTH_TOKEN) # Your test code here assert result is not None -
Add the
@skip_if_no_gatewaydecorator to tests requiring a live server -
Use pytest.mark.asyncio for async tests:
@skip_if_no_gateway @pytest.mark.asyncio async def test_async_feature(self): api = Api(url=GATEWAY_URL, token=AUTH_TOKEN) result = await api.async_flow().list() assert isinstance(result, list)
Continuous Integration
For CI/CD pipelines:
# Example GitHub Actions workflow
- name: Run Integration Tests
env:
TRUSTGRAPH_URL: http://localhost:8088/
SKIP_INTEGRATION_TESTS: false
run: |
# Start Gateway API in background
./start-gateway.sh &
sleep 10
# Run tests
pytest tests/test_api_integration.py -v
# Stop Gateway API
./stop-gateway.sh
Troubleshooting
Tests are skipped
- Check that
SKIP_INTEGRATION_TESTSis set tofalse - Verify the Gateway API is running at the configured URL
Connection errors
- Verify
TRUSTGRAPH_URLis correct - Check that the Gateway API is accessible
- Ensure firewall rules allow connections
Authentication errors
- Verify
TRUSTGRAPH_TOKENis set correctly - Check token validity with the Gateway API
Timeout errors
- Increase timeout in Api instantiation
- Check Gateway API performance
- Verify network connectivity
Future Enhancements
Planned test additions:
- End-to-end streaming tests (requires live LLM)
- Bulk operation performance tests
- Error handling and edge cases
- WebSocket reconnection scenarios
- Token refresh mechanisms
- Multi-flow coordination tests
Contributing
When adding new API features:
- Add corresponding integration tests
- Document environment variables if needed
- Update this README with new test categories
- Ensure tests can run in skip mode for CI
License
Same as TrustGraph project license.