Training Service - Complete Testing Suite
📁 Test Structure
services/training/tests/
├── conftest.py # Test configuration and fixtures
├── test_api.py # API endpoint tests
├── test_ml.py # ML component tests
├── test_service.py # Service layer tests
├── test_messaging.py # Messaging tests
└── test_integration.py # Integration tests
🧪 Test Coverage
1. API Tests (test_api.py)
- ✅ Health check endpoints (
/health,/health/ready,/health/live) - ✅ Metrics endpoint (
/metrics) - ✅ Training job creation and management
- ✅ Single product training
- ✅ Job status tracking and cancellation
- ✅ Data validation endpoints
- ✅ Error handling and edge cases
- ✅ Authentication integration
Key Test Classes:
TestTrainingAPI- Basic API functionalityTestTrainingJobsAPI- Training job managementTestSingleProductTrainingAPI- Single product workflowsTestErrorHandling- Error scenariosTestAuthenticationIntegration- Security tests
2. ML Component Tests (test_ml.py)
- ✅ Data processor functionality
- ✅ Prophet manager operations
- ✅ ML trainer orchestration
- ✅ Feature engineering validation
- ✅ Model training and validation
Key Test Classes:
TestBakeryDataProcessor- Data preparation and feature engineeringTestBakeryProphetManager- Prophet model managementTestBakeryMLTrainer- ML training orchestrationTestIntegrationML- ML component integration
Key Features Tested:
- Spanish holiday detection
- Temporal feature engineering
- Weather and traffic data integration
- Model validation and metrics
- Data quality checks
3. Service Layer Tests (test_service.py)
- ✅ Training service business logic
- ✅ Database operations
- ✅ External service integration
- ✅ Job lifecycle management
- ✅ Error recovery and resilience
Key Test Classes:
TestTrainingService- Core business logicTestTrainingServiceDataFetching- External API integrationTestTrainingServiceExecution- Training workflow executionTestTrainingServiceEdgeCases- Edge cases and error conditions
4. Messaging Tests (test_messaging.py)
- ✅ Event publishing functionality
- ✅ Message structure validation
- ✅ Error handling in messaging
- ✅ Integration with shared components
Key Test Classes:
TestTrainingMessaging- Basic messaging operationsTestMessagingErrorHandling- Error scenariosTestMessagingIntegration- Shared component integrationTestMessagingPerformance- Performance and reliability
5. Integration Tests (test_integration.py)
- ✅ End-to-end workflow testing
- ✅ Service interaction validation
- ✅ Error handling across boundaries
- ✅ Performance and scalability
- ✅ Security and compliance
Key Test Classes:
TestTrainingWorkflowIntegration- Complete workflowsTestServiceInteractionIntegration- Cross-service communicationTestErrorHandlingIntegration- Error propagationTestPerformanceIntegration- Performance characteristicsTestSecurityIntegration- Security validationTestRecoveryIntegration- Recovery scenariosTestComplianceIntegration- GDPR and audit compliance
🔧 Test Configuration (conftest.py)
Fixtures Provided:
test_engine- Test database enginetest_db_session- Database session for teststest_client- HTTP test clientmock_messaging- Mocked messaging systemmock_data_service- Mocked external data servicesmock_ml_trainer- Mocked ML trainermock_prophet_manager- Mocked Prophet managermock_data_processor- Mocked data processortraining_job_in_db- Sample training job in databasetrained_model_in_db- Sample trained model in database
Helper Functions:
assert_training_job_structure()- Validate job data structureassert_model_structure()- Validate model data structure
🚀 Running Tests
Run All Tests:
cd services/training
pytest tests/ -v
Run Specific Test Categories:
# API tests only
pytest tests/test_api.py -v
# ML component tests
pytest tests/test_ml.py -v
# Service layer tests
pytest tests/test_service.py -v
# Messaging tests
pytest tests/test_messaging.py -v
# Integration tests
pytest tests/test_integration.py -v
Run with Coverage:
pytest tests/ --cov=app --cov-report=html --cov-report=term
Run Performance Tests:
pytest tests/test_integration.py::TestPerformanceIntegration -v
Skip Slow Tests:
pytest tests/ -v -m "not slow"
📊 Test Scenarios Covered
Happy Path Scenarios:
- ✅ Complete training workflow (start → progress → completion)
- ✅ Single product training
- ✅ Data validation and preprocessing
- ✅ Model training and storage
- ✅ Event publishing and messaging
- ✅ Job status tracking and cancellation
Error Scenarios:
- ✅ Database connection failures
- ✅ External service unavailability
- ✅ Invalid input data
- ✅ ML training failures
- ✅ Messaging system failures
- ✅ Authentication and authorization errors
Edge Cases:
- ✅ Concurrent job execution
- ✅ Large datasets
- ✅ Malformed configurations
- ✅ Network timeouts
- ✅ Memory pressure scenarios
- ✅ Rapid successive requests
Security Tests:
- ✅ Tenant isolation
- ✅ Input validation
- ✅ SQL injection protection
- ✅ Authentication enforcement
- ✅ Data access controls
Compliance Tests:
- ✅ Audit trail creation
- ✅ Data retention policies
- ✅ GDPR compliance features
- ✅ Backward compatibility
🎯 Test Quality Metrics
Coverage Goals:
- API Layer: 95%+ coverage
- Service Layer: 90%+ coverage
- ML Components: 85%+ coverage
- Integration: 80%+ coverage
Test Types Distribution:
- Unit Tests: ~60% (isolated component testing)
- Integration Tests: ~30% (service interaction testing)
- End-to-End Tests: ~10% (complete workflow testing)
Performance Benchmarks:
- All unit tests complete in <5 seconds
- Integration tests complete in <30 seconds
- End-to-end tests complete in <60 seconds
🔧 Mocking Strategy
External Dependencies Mocked:
- ✅ Data Service: HTTP calls mocked with realistic responses
- ✅ RabbitMQ: Message publishing mocked for isolation
- ✅ Database: SQLite in-memory for fast testing
- ✅ Prophet Models: Training mocked for speed
- ✅ File System: Model storage mocked
Real Components Tested:
- ✅ FastAPI Application: Real app instance
- ✅ Pydantic Validation: Real validation logic
- ✅ SQLAlchemy ORM: Real database operations
- ✅ Business Logic: Real service layer code
🛡️ Continuous Integration
CI Pipeline Tests:
# Example CI configuration
test_matrix:
- python: "3.11"
database: "postgresql"
- python: "3.11"
database: "sqlite"
test_commands:
- pytest tests/ --cov=app --cov-fail-under=85
- pytest tests/test_integration.py -m "not slow"
- pytest tests/ --maxfail=1 --tb=short
Quality Gates:
- ✅ All tests must pass
- ✅ Coverage must be >85%
- ✅ No critical security issues
- ✅ Performance benchmarks met
📈 Test Maintenance
Regular Updates:
- ✅ Add tests for new features
- ✅ Update mocks when APIs change
- ✅ Review and update test data
- ✅ Maintain realistic test scenarios
Monitoring:
- ✅ Test execution time tracking
- ✅ Flaky test identification
- ✅ Coverage trend monitoring
- ✅ Test failure analysis
This comprehensive test suite ensures the training service is robust, reliable, and ready for production deployment! 🎉