# Testing Guide for verify_sonarr_api Function This guide explains how to test the `verify_sonarr_api` function from the plexsonarr.py script. ## Test Files Overview ### 1. test_verify_api.py - Simple Interactive Test A user-friendly test script that provides: - โœ… Mocked success tests - โŒ Mocked failure tests - ๐Ÿงช HTTP error simulation - ๐ŸŒ Real API testing (optional) **Usage:** ```bash python test_verify_api.py ``` ### 2. test_plexsonarr.py - Comprehensive pytest Suite Full test suite using pytest framework that covers: - Successful API verification - HTTP errors (401, 404, 500) - Connection errors - Timeout scenarios - Invalid JSON responses **Usage:** ```bash pytest test_plexsonarr.py -v ``` ## Quick Start ### Install Dependencies ```bash pip install -r requirements.txt ``` ### Run Simple Tests ```bash python test_verify_api.py ``` ### Run Comprehensive Tests ```bash pytest test_plexsonarr.py -v ``` ### Run All Tests ```bash pytest -v ``` ## Test Scenarios Covered ### โœ… Success Cases - Valid API response (200 OK) - Correct API key and URL - Successful connection to Sonarr ### โŒ Error Cases - **401 Unauthorized**: Invalid API key - **404 Not Found**: Incorrect endpoint or server - **500 Server Error**: Sonarr internal error - **Connection Error**: Network issues - **Timeout**: Slow response from server ### ๐Ÿ”ง Edge Cases - Invalid JSON response - Malformed responses - Network connectivity issues ## Configuration The tests use the same configuration as your main script: ```python SONARR_API_KEY = "2537de37fded4874ae83da9cf3c14f34" SONARR_SERVER_URL = "http://192.168.50.111:8989" ``` ## Understanding Test Output ### Successful Test Run ``` ๐Ÿš€ Starting verify_sonarr_api tests... โœ… Mock success test passed! โœ… Mock failure test passed! โœ… Mock HTTP error test passed! โœ… Real API test passed! Your Sonarr API is working. ``` ### Failed Real API Test ``` โŒ Failed to verify Sonarr API: HTTPSConnectionPool(host='192.168.50.111', port=8989) ๐Ÿ’ก Tip: Check your SONARR_SERVER_URL and SONARR_API_KEY configuration ``` ## Pytest Output Explanation ```bash pytest test_plexsonarr.py -v ``` Each test case shows: - **PASSED**: Test completed successfully - **FAILED**: Test found an issue - **Coverage**: Which scenarios were tested ## Troubleshooting ### Common Issues 1. **Import Errors** ``` ModuleNotFoundError: No module named 'pytest' ``` **Solution**: Install dependencies with `pip install -r requirements.txt` 2. **Connection Refused** ``` ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) ``` **Solution**: Check if Sonarr is running and accessible 3. **API Key Invalid** ``` 401 Unauthorized ``` **Solution**: Verify your SONARR_API_KEY in the configuration ### Debugging Tips 1. **Check Sonarr Status** ```bash curl http://192.168.50.111:8989/api/v3/system/status?apikey=YOUR_API_KEY ``` 2. **Test Network Connectivity** ```bash ping 192.168.50.111 telnet 192.168.50.111 8989 ``` 3. **Verify API Key** - Open Sonarr web interface - Go to Settings โ†’ General - Check the API Key in Security section ## Test Development ### Adding New Tests To add a new test case to `test_plexsonarr.py`: ```python def test_verify_sonarr_api_new_scenario(self): """Test description""" with requests_mock.Mocker() as m: expected_url = f"{plexsonarr.SONARR_SERVER_URL}/api/v3/system/status?apikey={plexsonarr.SONARR_API_KEY}" m.get(expected_url, status_code=YOUR_STATUS_CODE) # Your test logic here with pytest.raises(ExpectedException): plexsonarr.verify_sonarr_api() ``` ### Test Best Practices 1. **Mock External Calls**: Always mock HTTP requests in unit tests 2. **Test Edge Cases**: Include error scenarios and unusual responses 3. **Clear Test Names**: Use descriptive test function names 4. **Isolated Tests**: Each test should be independent 5. **Verify Behavior**: Check both success and failure paths ## Continuous Integration Add to your CI pipeline: ```yaml # .github/workflows/test.yml name: Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.11 - name: Install dependencies run: pip install -r requirements.txt - name: Run tests run: pytest test_plexsonarr.py -v ``` ## Performance Testing For load testing the API: ```bash # Install httpie for testing pip install httpie # Test API endpoint directly http GET "http://192.168.50.111:8989/api/v3/system/status?apikey=YOUR_API_KEY" ``` ## Summary The testing setup provides: - ๐Ÿ” **Comprehensive Coverage**: All success/failure scenarios - ๐Ÿš€ **Easy Execution**: Simple commands to run tests - ๐Ÿ“Š **Clear Reporting**: Detailed output and error messages - ๐Ÿ› ๏ธ **Development Ready**: Easy to extend with new test cases - ๐ŸŒ **Real-world Testing**: Option to test against actual Sonarr API This ensures your `verify_sonarr_api` function works correctly in all scenarios!