Skip to content

Commit

Permalink
feat: add doc for all exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
hampuslavin committed Oct 8, 2024
1 parent d2bc770 commit a399dce
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/06-concepts/18-testing/01-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ A few things to note from the above example:

- The test tools should be imported from the generated test tools file and not the `serverpod_test` package.
- The `withServerpod` callback takes two parameters: `sessionBuilder` and `endpoints`.
- `sessionBuilder` is used to build a `session` object that represents the state of the world for your endpoints and is used to set up scenarios.
- `sessionBuilder` is used to build a `session` object that represents the state of the world during an endpoint call and is used to set up scenarios.
- `endpoints` contains all your Serverpod endpoints and lets you call them.

:::info
Expand Down
86 changes: 83 additions & 3 deletions docs/06-concepts/18-testing/02-the-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Using `sessionBuilder` to set up a test scenario

The `withServerpod` helper provides a `session` object that helps with setting up different scenarios for tests. It looks like the following:
The `withServerpod` helper provides a `sessionBuilder` object that helps with setting up different scenarios for tests. It looks like the following:

```dart
/// A test specific builder to create a [Session] that for instance can be used to call database methods.
Expand All @@ -24,10 +24,25 @@ abstract class TestSessionBuilder {

To create a new state, simply call `copyWith` with the new properties and use the new session builder in the endpoint call.

Below follows examples of some common scenarios.

### Setting authenticated state

To control the authenticated state of the session, the `AuthenticationOverride` class can be used:

```dart
/// An override for the authentication state in a test session.
abstract class AuthenticationOverride {
/// Sets the session to be authenticated with the provided userId and scope.
static AuthenticationOverride authenticationInfo(
int userId, Set<Scope> scopes,
{String? authId});
/// Sets the session to be unauthenticated. This is the default.
static AuthenticationOverride unauthenticated();
}
```

Pass it to the `sessionBuilder.copyWith` to simulate different scenarios. Below follows an example for each case:

```dart
withServerpod('Given AuthenticatedExample endpoint',
(sessionBuilder, endpoints) {
Expand Down Expand Up @@ -205,3 +220,68 @@ Wether session logging should be enabled. Defaults to `false`.
### `applyMigrations`

Wether pending migrations should be applied when starting Serverpod. Defaults to `true`.

## Test exceptions

The following exceptions are exported from the generated test tools file and can be thrown in various scenarios, see below.

### `ServerpodUnauthenticatedException`

```dart
/// The user was not authenticated.
class ServerpodUnauthenticatedException implements Exception {
ServerpodUnauthenticatedException();
}
```

### `ServerpodInsufficientAccessException`

```dart
/// The authentication key provided did not have sufficient access.
class ServerpodInsufficientAccessException implements Exception {
ServerpodInsufficientAccessException();
}
```

### `InvalidConfigurationException`

```dart
/// Thrown when an invalid configuration state is found.
class InvalidConfigurationException implements Exception {
final String message;
InvalidConfigurationException(this.message);
}
```

### `ConnectionClosedException`

```dart
/// Thrown if a stream connection is closed with an error.
/// For example, if the user authentication was revoked.
class ConnectionClosedException implements Exception {
const ConnectionClosedException();
}
```

## Test helpers

### `flushEventQueue`

Test helper to flush the event queue.
Useful for waiting for async events to complete before continuing the test.

```dart
Future<void> flushEventQueue();
```

For example, if depending on a generator function to execute up to its `yield`, then the
event queue can be flushed to ensure the generator has executed up to that point:

```dart
var stream = endpoints.someEndoint.generatorFunction(session);
await flushEventQueue();
```

See also [this complete example](advanced-examples#multiple-users-with-stream).
4 changes: 2 additions & 2 deletions docs/06-concepts/18-testing/03-advanced-examples.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Advanced examples

## Test business logic that depends on `session`
## Test business logic that depends on `Session`

It is common to break out business logic into modules and keep it separate from the endpoints. If such a module depends on a `Session` object (e.g to interact with the database), then the `withServerpod` helper can still be used and the second `endpoint` argument can simply be ignored:

Expand Down Expand Up @@ -34,7 +34,7 @@ withServerpod('Given decreasing product quantity when quantity is zero', (
});
```

## Multiple users interacting with a shared stream
## Multiple users interacting with a shared stream {#multiple-users-with-stream}

For cases where there are multiple users reading from or writing to a stream, such as real-time communication, it can be helpful to validate this behavior in tests.

Expand Down

0 comments on commit a399dce

Please sign in to comment.