Skip to content

Conversation

johahor
Copy link
Member

@johahor johahor commented Nov 6, 2026 •

Summary

Adds tests for our GraphQL API. The tests send GraphQL queries through our schema and resolvers to a real Postgres database and check the responses, using Apollo Server's executeOperation (no HTTP server). The logged-in user is set directly in the context, since that's all the resolvers read from the session.

How it works

pnpm test in backend runs its own Postgres, separate from the development database:

  1. Creates a temporary database folder test-db-data (gitignored) and starts Postgres from it on port 5434, so it doesn't collide with the development database on 5433.
  2. Creates readr_test, runs our migrations and inserts 7 test books and 2 test users from tests/fixtures.ts.
  3. Runs the tests, then stops Postgres and deletes the folder.

You only need PostgreSQL installed, not pnpm db:setup or the book dataset. As a safety check, the setup refuses to reset a database whose name doesn't end with _test.

What is tested

  • Books: search by title or author (part of the text, case-insensitive, no matches), the default order (most ratings first), filtering by genre ("any of"), language, format and page range, paging with totalCount counting all matches, sorting by rating across all matches (ties by number of ratings, unrated books last), book(id) with genres and null for a missing book, and the filter option lists.
  • Favorites: rejected when not logged in, adding and removing (no duplicates), users only see their own favorites, a missing book is rejected, and the favorites list (newest first) and favorite genres.

To check that the tests catch real bugs, I broke the sorting, counting and title search in searchBooks one at a time before merging main. The right test failed each time.

Other changes

  • backend/README.md: new "Testing the API" section.
  • docs/infrastructure.md: Vitest added under backend.
  • frontend/README.md: the test section was outdated after test: rewrite tests with Apollo MockedProvider #85 and is updated.
  • Merged main and updated the tests to the current API: query instead of title, and RELEVANCE (most ratings first) as the default sort.

How to test

cd backend
pnpm install   # Vitest is new in the backend
pnpm test

AI usage

The tests and this description were generated with AI (Claude) and reviewed before opening the PR.

Closes #102

@johahor johahor added the test Writing/editing tests label Nov 6, 2026
@johahor johahor self-assigned this Nov 6, 2026
@johahor
Copy link
Member Author

johahor commented Nov 6, 2026

One decision I'd like some input on is the database the API is tested against. This implementation creates a small local test database for each run, and never touches the production database. I couldn't find any guidelines in the project description on exactly what to use, so I'll assume this is fine.

I think the E2E tests should run against a local setup as well, rather than the production database on the VM. Otherwise they would create test users and favorites in the real data, and depend on data that can change. We can probably reuse this test database setup for that.

@johahor johahor marked this pull request as ready for review November 12, 2026 22:43
@johahor johahor requested a review from a team as a code owner November 12, 2026 22:43
@hestro
Copy link
Member

hestro commented Nov 14, 2026

One decision I'd like some input on is the database the API is tested against. This implementation creates a small local test database for each run, and never touches the production database. I couldn't find any guidelines in the project description on exactly what to use, so I'll assume this is fine.

I think the E2E tests should run against a local setup as well, rather than the production database on the VM. Otherwise they would create test users and favorites in the real data, and depend on data that can change. We can probably reuse this test database setup for that.

I think this is a good choice, and it sounds like you have some good ideas for some of the other tests as well

hestro
hestro previously approved these changes Nov 14, 2026
Copy link
Member

@hestro hestro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have the API covered by tests! All tests pass

@martakam
Copy link
Member

Should we have a separate issue to get these tests to run in CI, or would you like to do this here?

@johahor
Copy link
Member Author

johahor commented Nov 16, 2026

Separate. I can fix that.

martakam
martakam previously approved these changes Nov 16, 2026
@johahor johahor merged commit 4aeb8a0 into main Nov 16, 2026
7 checks passed
@martakam martakam deleted the test/api-tests branch November 16, 2026 20:05
Sign in to join this conversation on GitHub.
Labels
test Writing/editing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test: add API tests
3 participants