Skip to content

Add API test harness with a few example tests (plus multiple refactors, other changes) - #1832

Open
LukasKalbertodt wants to merge 13 commits into
elan-ev:mainfrom
LukasKalbertodt:backend-tests
Open

LukasKalbertodt wants to merge 13 commits into
elan-ev:mainfrom
LukasKalbertodt:backend-tests

Conversation

@LukasKalbertodt

Copy link
Copy Markdown
Member

The main point of this PR is that it adds API tests: they live in the backend, are simply executed by cargo test and make sure the API behaves in a way we expect. Since the API is by far the most interesting thing regarding business logic, that was we can write a lot of useful tests.

The tests directly construct types from Juniper and then call execute to run a query. This skips HTTP stuff and in particular: doesn't start a Tobira process per test (like the UI tests do). That makes these tests a lot faster and resource efficient to run, so it shouldn't be a problem adding a lot of those. They are still fully isolated by using their own temporary DB (like the DB tests that existed before).

To write these tests more easily, a few other things were changed/refactored. One notable change is that the unsafe Transaction hack was removed. None of these changes should influence production behavior, but are good changes even apart from the tests.

Should be reviewed commit by commit.


This does not yet contain a large amount of tests. This is mainly the setup for everything, with a few tests as proof of concept. The actual bulk of the tests will follow in another PR.

For the longest time, we had the problem that the API context couldn't
hold any non-'static structs, but we wanted to use one DB transaction
per API request, and those use the `Transaction<'a>` struct from the
corresponding libraries. We solved this by `mem::transmute` on the
transaction to extend its lifetime to `'static`, which is a not correct
obviously, as it borrows the connection.

Having `unsafe` in our code base always bothered me, and this approach
is a bit brittle and just uhg. Also, for adding API tests, it makes the
whole testing harness more annoying.

So now, we changed our workaround by instead writing the transaction
logic ourselves. It's not that much actually. The only annoying thing
is that in `drop`, we cannot await the "ROLLBACK". The PG library has
an internal method to sidestep this, which is accessible in theory,
but is `#[doc(hidden)]`, so I'm not really happy to use it. But our
workaround for that is fine and maybe that method becomes public at
some point.
- Instead of reading the dev config (with lots of unrelated things),
  it uses the env var TOBIRA_TEST_DB for the PG URL, with a useful
  default (matching the CI and dev DB).
- It does not require multi threaded runtimes anymore mainly by using
  "drop database ... with (force)" and executing this from a new thread.
It will be used more widely in future commits and we need a bigger,
general testing utility module anyway.
This will be needed for upcoming tests
This is useful to make `handle_api` less big and also to use it from
unit tests later.
It's useful to distinguish different parts of it
Useful for upcoming commits
The `api` module should not know about HTTP. Mapping the errors to HTTP
responses is now done in `handle_api`, with the same status codes as
before. This also makes `execute` easier to use in tests.
All previous commits lead up to this, essentially. These are the first
tests that test the API. It is done in-process, i.e. there is not a
separate Tobira started for each test, which makes it quite a bit
faster.

However, these tests still use a real PG DB, with temporary databases
for each test being created automatically to ensure isolation. I'm quite
happy with how it turned out. Though we are not done yet. More helper
functions will be added as we write more tests. The most notable gaps
in this are that neither Meili nor Opencast can be used yet. For Meili,
we can likely also just use a running instance, but need a way to
trigger the index update in a test. Opencast... will be harder. We
probably want to mock it, making the `OcClient` in the context a trait
object. But we'll deal with that some other time.
It was derived, but completely unused. If it were used, we would have
noticed that it serialized as an object. With these adjustments, it is
serialized as string, as one would expect.
I expect these docs to change again soon, but oh well.

This branch was successfully deployed

1 active deployment
deployment-test — bde8018c Deployed Nov 20, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:dev Changes primarily for developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant