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 inmy_ghost_writer/app.pyand the business logic within the python files inmy_ghost_writer/*.py. - For each endpoint and main business logic, ensure there is a corresponding integration test in
tests/. - Use
pytestandunittestfor structuring tests. Group tests by endpoint or function using classes. E.g., themy_ghost_writer/text_parsers2.pyfile will have a test classTestTextParsers2within thetests/my_ghost_writer/test_text_parsers2.pyfile orpytest.mark.describe. - For endpoint tests, use FastAPI’s
TestClientto simulate real HTTP requests and responses. - Mock only the MongoDB dependency (e.g.,
pymongo_operations_rw,mongodb_health_check) usingunittest.mockorpytest-mockto 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-asyncioor 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:
- Test Coverage: Map each main module and endpoint to an integration test file in
tests/. - Test Structure: Use clear grouping for maintainability. Each test file should focus on one module or endpoint.
- MongoDB Mocking: Only mock MongoDB-related functions to simulate DB availability and edge cases.
- Real NLP: Use actual NLTK and spaCy processing to validate integration.
- Helpers & Data: Use sample JSON files for requests/responses to keep tests reproducible and easy to update.
- Happy Path & Errors: Cover both normal and edge cases for robust error handling.
- Assertions: Explicitly check status codes, payloads, and error messages for correctness.
- Custom Handlers: Ensure add/delete/query operations are additive and consistent.
- Exception Handling: Test that exceptions are correctly mapped to HTTP responses.
- Setup/Teardown: Use fixtures for resource management and test isolation.
- Async Operations: Use async test tools for endpoints and logic that are asynchronous.