90 lines
2.1 KiB
Python
90 lines
2.1 KiB
Python
# Test configuration and fixtures
|
|
import os
|
|
import tempfile
|
|
import pytest
|
|
from flask import Flask
|
|
from src.server.app import create_app
|
|
from src.server.infrastructure.database.connection import get_database
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
"""Create and configure a new app instance for each test."""
|
|
# Create a temporary file to isolate the database for each test
|
|
db_fd, db_path = tempfile.mkstemp()
|
|
|
|
app = create_app({
|
|
'TESTING': True,
|
|
'DATABASE_URL': f'sqlite:///{db_path}',
|
|
'SECRET_KEY': 'test-secret-key',
|
|
'WTF_CSRF_ENABLED': False,
|
|
'LOGIN_DISABLED': True,
|
|
})
|
|
|
|
with app.app_context():
|
|
# Initialize database tables
|
|
from src.server.infrastructure.database import models
|
|
models.db.create_all()
|
|
|
|
yield app
|
|
|
|
# Clean up
|
|
os.close(db_fd)
|
|
os.unlink(db_path)
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""A test client for the app."""
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.fixture
|
|
def runner(app):
|
|
"""A test runner for the app's Click commands."""
|
|
return app.test_cli_runner()
|
|
|
|
|
|
class AuthActions:
|
|
def __init__(self, client):
|
|
self._client = client
|
|
|
|
def login(self, username='test', password='test'):
|
|
return self._client.post(
|
|
'/auth/login',
|
|
data={'username': username, 'password': password}
|
|
)
|
|
|
|
def logout(self):
|
|
return self._client.get('/auth/logout')
|
|
|
|
|
|
@pytest.fixture
|
|
def auth(client):
|
|
return AuthActions(client)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_anime_data():
|
|
"""Sample anime data for testing."""
|
|
return {
|
|
'title': 'Test Anime',
|
|
'description': 'A test anime series',
|
|
'episodes': 12,
|
|
'status': 'completed',
|
|
'year': 2023,
|
|
'genres': ['Action', 'Adventure'],
|
|
'cover_url': 'https://example.com/cover.jpg'
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_download_data():
|
|
"""Sample download data for testing."""
|
|
return {
|
|
'anime_id': 1,
|
|
'episode_number': 1,
|
|
'quality': '1080p',
|
|
'status': 'pending',
|
|
'url': 'https://example.com/download/episode1.mp4'
|
|
} |