| # Sema Translation API - Tests | |
| This directory contains test scripts for the Sema Translation API. | |
| ## Test Files | |
| ### `test_model_download.py` | |
| Tests the model downloading and loading functionality: | |
| - Downloads models from `sematech/sema-utils` repository | |
| - Tests model loading (SentencePiece, FastText, CTranslate2) | |
| - Validates complete translation pipeline | |
| - Includes cleanup functionality | |
| **Usage:** | |
| ```bash | |
| cd tests | |
| python test_model_download.py | |
| ``` | |
| ### `test_api_client.py` | |
| Tests the API endpoints and functionality: | |
| - Health check endpoints | |
| - Translation with auto-detection | |
| - Translation with specified source language | |
| - Error handling validation | |
| - Performance testing with multiple requests | |
| - Documentation endpoint testing | |
| **Usage:** | |
| ```bash | |
| # Test local development server | |
| cd tests | |
| python test_api_client.py | |
| # Test production server | |
| python test_api_client.py https://sematech-sema-api.hf.space | |
| ``` | |
| ## Running Tests | |
| ### Prerequisites | |
| ```bash | |
| pip install requests huggingface_hub ctranslate2 sentencepiece fasttext-wheel | |
| ``` | |
| ### Local Testing | |
| 1. Start the API server: | |
| ```bash | |
| cd backend/sema-api | |
| uvicorn app.main:app --reload | |
| ``` | |
| 2. Run API tests: | |
| ```bash | |
| cd tests | |
| python test_api_client.py | |
| ``` | |
| ### Production Testing | |
| ```bash | |
| cd tests | |
| python test_api_client.py https://sematech-sema-api.hf.space | |
| ``` | |
| ## Test Coverage | |
| ### Model Tests | |
| - β Model downloading from HuggingFace Hub | |
| - β SentencePiece model loading | |
| - β FastText language detection model loading | |
| - β CTranslate2 translation model loading | |
| - β End-to-end translation pipeline | |
| ### API Tests | |
| - β Health check endpoints (`/` and `/health`) | |
| - β Translation endpoint (`/translate`) | |
| - β Auto language detection | |
| - β Manual source language specification | |
| - β Error handling (empty text, invalid requests) | |
| - β Rate limiting behavior | |
| - β Documentation endpoints (`/docs`, `/openapi.json`) | |
| - β Metrics endpoint (`/metrics`) | |
| ### Performance Tests | |
| - β Multiple concurrent requests | |
| - β Response time measurement | |
| - β Character count validation | |
| - β Request tracking with unique IDs | |
| ## Expected Results | |
| ### Model Download Test | |
| ``` | |
| π Starting Sema Utils Model Test | |
| π§ͺ Testing model download from sematech/sema-utils... | |
| β SentencePiece model downloaded | |
| β Language detection model downloaded | |
| β Translation model downloaded | |
| β All models loaded successfully | |
| π Translation successful! | |
| ``` | |
| ### API Client Test | |
| ``` | |
| π§ͺ Testing Sema Translation API | |
| β Health check passed | |
| β Auto-detection translation successful | |
| β Specified source translation successful | |
| β Empty text error handling works correctly | |
| β Performance test completed | |
| β OpenAPI docs accessible | |
| π All API tests passed! | |
| ``` | |
| ## Troubleshooting | |
| ### Common Issues | |
| **Model Download Fails:** | |
| - Check internet connection | |
| - Verify HuggingFace Hub access | |
| - Ensure sufficient disk space | |
| **API Tests Fail:** | |
| - Verify API server is running | |
| - Check correct URL/port | |
| - Ensure all dependencies installed | |
| **Permission Errors:** | |
| - Check file permissions in test directory | |
| - Ensure write access for model downloads | |
| ### Debug Mode | |
| Add debug prints to test scripts for detailed troubleshooting: | |
| ```python | |
| import logging | |
| logging.basicConfig(level=logging.DEBUG) | |
| ``` | |