mgw / tests-prompts /backend-unit-tests.md
alessandro trinca tornidor
test: update playwright e2e tests because of the new /thesaurus-inflated-phrase agnostic response structure
7149fa6

Prompt for regenerating backend unit tests

  • Generate integration tests for the FastAPI backend in my_ghost_writer/app.py, covering all public endpoints in my_ghost_writer/app.py and the business logic within the python files in my_ghost_writer/*.py.
  • For each endpoint and main business logic, ensure there is a corresponding integration test in tests/.
  • Use pytest and unittest for structuring tests. Group tests by endpoint or function using classes. E.g., the my_ghost_writer/text_parsers2.py file will have a test class TestTextParsers2 within the tests/my_ghost_writer/test_text_parsers2.py file or pytest.mark.describe.
  • For endpoint tests, use FastAPI’s TestClient to simulate real HTTP requests and responses.
  • Mock only the MongoDB dependency (e.g., pymongo_operations_rw, mongodb_health_check) using unittest.mock or pytest-mock to isolate database logic and simulate edge cases.
  • Do not mock NLTK or spaCy; use real NLP processing to ensure integration between components.
  • Use helper functions and sample request/response JSON files from tests/events/ for consistent test data.
  • Each endpoint/function should have tests for both successful execution and error conditions (invalid input, exceptions, unavailable MongoDB).
  • Assert correct HTTP status codes, response payloads, and error handling.
  • For NLP logic, test with realistic and edge-case text samples (empty, non-English, malformed).
  • For custom handlers, verify add, delete, and query operations, including inverted index consistency.
  • For exception handlers, ensure correct mapping of exceptions to HTTP responses.
  • Handle setup and teardown: Use fixtures to set up and clean up resources before and after tests, ensuring isolation and reproducibility.
  • Test async endpoints and logic: Use pytest-asyncio or equivalent to properly test asynchronous routes and functions.

Also:

  • Document test data and expected outputs.
  • NLP models may change behavior with library updates: use static input and output test data like the body JSON files in tests/events/.

Testing Pattern, Chain of Thought & Steps:

  1. Test Coverage: Map each main module and endpoint to an integration test file in tests/.
  2. Test Structure: Use clear grouping for maintainability. Each test file should focus on one module or endpoint.
  3. MongoDB Mocking: Only mock MongoDB-related functions to simulate DB availability and edge cases.
  4. Real NLP: Use actual NLTK and spaCy processing to validate integration.
  5. Helpers & Data: Use sample JSON files for requests/responses to keep tests reproducible and easy to update.
  6. Happy Path & Errors: Cover both normal and edge cases for robust error handling.
  7. Assertions: Explicitly check status codes, payloads, and error messages for correctness.
  8. Custom Handlers: Ensure add/delete/query operations are additive and consistent.
  9. Exception Handling: Test that exceptions are correctly mapped to HTTP responses.
  10. Setup/Teardown: Use fixtures for resource management and test isolation.
  11. Async Operations: Use async test tools for endpoints and logic that are asynchronous.