Add Plex Sonarr integration script and testing framework
- Implemented a Python script for integrating Plex Media Server with Sonarr to manage TV show seasons based on viewing habits. - Added features for automated season management, exclusion list support, and detailed logging. - Created a requirements.txt file to include necessary packages: plexapi, requests, pytest, pytest-mock, and requests-mock. - Developed unit tests for the API verification function using pytest and requests_mock. - Included a standalone script to unmonitor excluded shows in Sonarr. - Added a test script for manual API verification with mock and real API tests.
This commit is contained in:
+219
@@ -0,0 +1,219 @@
|
||||
# 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!
|
||||
Reference in New Issue
Block a user