Files
Aniworld/docs/TESTING.md
Lukas 31eb0026cf Add queue deduplication to prevent duplicate entries
- In-memory dedup in add_to_queue() using _pending_by_episode dict
- Batch-local dedup via seen_in_batch set (handles duplicates within single call)
- Database unique index on episode_id via __table_args__
- 5-minute cooldown in _auto_download_missing() to prevent rapid re-triggers
- Updated _add_to_pending_queue() and _remove_from_pending_queue() to track episode keys
- Added TestQueueDeduplication with 4 test cases
- Updated DEVELOPMENT.md and TESTING.md with queue dedup docs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-23 21:27:41 +02:00

3.4 KiB

Testing Documentation

Document Purpose

This document describes the testing strategy, guidelines, and practices for the Aniworld project.

What This Document Contains

  • Testing Strategy: Overall approach to quality assurance
  • Test Categories: Unit, integration, API, performance, security tests
  • Test Structure: Organization of test files and directories
  • Writing Tests: Guidelines for writing effective tests
  • Fixtures and Mocking: Shared test utilities and mock patterns
  • Running Tests: Commands and configurations
  • Coverage Requirements: Minimum coverage thresholds
  • CI/CD Integration: How tests run in automation
  • Test Data Management: Managing test fixtures and data
  • Best Practices: Do's and don'ts for testing

What This Document Does NOT Contain

  • Production deployment (see DEPLOYMENT.md)
  • Security audit procedures (see SECURITY.md)
  • Bug tracking and issue management
  • Performance benchmarking results

Target Audience

  • Developers writing tests
  • QA Engineers
  • CI/CD Engineers
  • Code reviewers

Sections to Document

  1. Testing Philosophy
    • Test pyramid approach
    • Quality gates
  2. Test Categories
    • Unit Tests (tests/unit/)
    • Integration Tests (tests/integration/)
    • API Tests (tests/api/)
    • Frontend Tests (tests/frontend/)
    • Performance Tests (tests/performance/)
    • Security Tests (tests/security/)
  3. Test Structure and Naming
    • File naming conventions
    • Test function naming
    • Test class organization
  4. Running Tests
    • pytest commands
    • Running specific tests
    • Verbose output
    • Coverage reports
  5. Fixtures and Conftest
    • Shared fixtures
    • Database fixtures
    • Mock services
  6. Mocking Guidelines
    • What to mock
    • Mock patterns
    • External service mocks

Mocking the Download Queue

Use MockQueueRepository for testing download queue functionality:

from src.server.models.download import DownloadItem, EpisodeIdentifier

class MockQueueRepository:
    def __init__(self):
        self._items: Dict[str, DownloadItem] = {}

    async def save_item(self, item: DownloadItem) -> DownloadItem:
        self._items[item.id] = item
        return item

    async def get_item(self, item_id: str) -> Optional[DownloadItem]:
        return self._items.get(item_id)

    async def get_all_items(self) -> List[DownloadItem]:
        return list(self._items.values())

    async def set_error(self, item_id: str, error: str) -> bool:
        if item_id in self._items:
            self._items[item_id].error = error
            return True
        return False

    async def delete_item(self, item_id: str) -> bool:
        if item_id in self._items:
            del self._items[item_id]
            return True
        return False

    async def clear_all(self) -> int:
        count = len(self._items)
        self._items.clear()
        return count

Key points:

  • The mock uses in-memory storage, no database required
  • All async methods are implemented (even if just pass-through)
  • save_item uses item.id as key (must be set before calling)
  • Suitable for unit tests only (no persistence)
  1. Coverage Requirements
  2. CI/CD Integration
  3. Writing Good Tests
    • Arrange-Act-Assert pattern
    • Test isolation
    • Edge cases
  4. Common Pitfalls to Avoid