Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dist-ssr
build/

db-data/
test-db-data/
book-data.csv

# Playwright
Expand Down
37 changes: 37 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,43 @@ cd backend
pnpm dev # or: pnpm run dev
```

## Testing the API

The API tests in [./tests](./tests/) send GraphQL queries through our schema and
resolvers to a real Postgres database, and check the responses. They use
Apollo Server's `executeOperation`, so they don't start the HTTP server. The
tests set the logged-in user directly, since that is all the resolvers read
from the session.

Run the tests with:

```sh
cd backend
pnpm test
```

The tests run their own Postgres server, separate from the development
database, so you don't need `pnpm db:setup` or the book dataset, only
PostgreSQL installed (see above). Before the tests run,
[tests/globalSetup.ts](./tests/globalSetup.ts):

- creates a temporary database folder named `test-db-data` (gitignored)
- starts Postgres from it on port 5434, so it doesn't collide with the
development database on port 5433
- deletes and creates the database `readr_test`, runs our migrations and inserts
a small set of test books from [tests/fixtures.ts](./tests/fixtures.ts)

When the tests are done, Postgres is stopped and the folder is deleted. As a safety check, the setup
refuses to reset a database whose name doesn't end with `_test`.

The tests cover searching, filtering, sorting and paging through books, getting
one book, the lists of filter options, and adding and removing favorites (also
that users must be logged in, and only see their own favorites).

> [!NOTE]
> The test database is very small, so the tests do not check or verify the performance of our database.
> This is rather done by careful planning of indexes and checking the results of `EXPLAIN` queries to see if indexes and efficient strategies are used.
## GraphQL automatically generated types

Whenever you change the GraphQL schema in the folder [./src/schema](./src/schema/), please run the following command:
Expand Down
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"main": "dist/index.js",
"scripts": {
"typecheck": "tsc",
"test": "vitest run",
"lint": "eslint .",
"codegen": "graphql-codegen",
"format": "prettier --write .",
Expand Down Expand Up @@ -73,6 +74,7 @@
"prettier": "3.9.9",
"tsx": "^4.23.15",
"typescript": "^6.0.3",
"typescript-eslint": "^8.72.1"
"typescript-eslint": "^8.72.1",
"vitest": "^5.1.0"
}
}
Loading