Skip to content

Commit

Permalink
refactor: move caveat to rollback database config
Browse files Browse the repository at this point in the history
  • Loading branch information
hampuslavin committed Oct 15, 2024
1 parent 92c3b2b commit 2435dac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
22 changes: 21 additions & 1 deletion docs/06-concepts/18-testing/02-the-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ enum RollbackDatabase {
}
```

There are two main reasons to change the default setting:
There are a few reasons to change the default setting:

1. **Scenario tests**: when consecutive `test` cases depend on each other. While generally considered an anti-pattern, it can be useful when the set up for the test group is very expensive. In this case `rollbackDatabase` can be set to `RollbackDatabase.afterAll` to ensure that the database state persists between `test` cases. At the end of the `withServerpod` scope, all database changes will be rolled back.

Expand Down Expand Up @@ -203,6 +203,26 @@ dart test integration_test --concurrency=1

For the other cases this is not an issue, as each `withServerpod` has its own transaction and will therefore be isolated.

3. **Database exceptions that are quelled**: There is a specific edge case where the test tools behavior deviates from production behavior. See example below:

```dart
var transactionFuture = session.db.transaction((tx) async {
var data = UniqueData(number: 1, email: '[email protected]');
try {
await UniqueData.db.insertRow(session, data, transaction: tx);
await UniqueData.db.insertRow(session, data, transaction: tx);
} on DatabaseException catch (_) {
// Ignore the database exception
}
});
// ATTENTION: This will throw an exception in production
// but not in the test tools.
await transactionFuture;
```

In production, the transaction call will throw if any database exception happened during its execution, _even_ if the exception was first caught inside the transaction. However, in the test tools this will not throw an exception due to how the nested transactions are emulated. Quelling exceptions like this is not best practise, but if the code under test does this setting `rollbackDatabase` to `RollbackDatabse.disabled` will ensure the code behaves like in production.

### `runMode`

The run mode that Serverpod should be running in. Defaults to `test`.
Expand Down
35 changes: 0 additions & 35 deletions docs/06-concepts/18-testing/05-caveates-and-edge-cases.md

This file was deleted.

0 comments on commit 2435dac

Please sign in to comment.