Skip to content
Open
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
25 changes: 25 additions & 0 deletions documentation/how-to/test-resources.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,28 @@ Finished in 0.08 seconds (0.00s async, 0.08s sync)
```
%{total: 7, failures: 0, excluded: 0, skipped: 0}
```

## Testing AshPostgres
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm....I do worry about placing Postgres-specific docs here. Could we perhaps instead just say indicate that there are additional requirements when working with AshPostgres and link to the relevant docs in AshPostgres instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok, the original issue asked for references to postgres, but i will follow your instructions. I will make another PR when ready.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah that makes sense, ultimately seeing it written out affected my thinking in terms of placement. Best to keep things colocated with their respective package as opposed to spreading out, difficult to manage otherwise. Thank you!


Previous examples above use Ash.DataLayer.Ets for testing. This will not work if we use
AshPostgres, and will need a different setup. We will use DataCase when we have to work
with AshPostgres, as will be shown in the following tests and examples:


```elixir
# test/my_app/accounts_test.exs
defmodule MyApp.AccountsTest do
use MyApp.DataCase

test "creates a user" do
assert {:ok, user} =
MyApp.Accounts.User
|> Ash.Changeset.for_create(:create, %{name: "James"})
|> Ash.create()

assert user.name == "James"
end
end
```

For additional explanation about AshPostgres and DataCase usage, check (/documentation/development/testing.md)
21 changes: 21 additions & 0 deletions documentation/topics/development/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ config :ash, :disable_async?, true

This ensures that Ash does not spawn tasks when executing your requests, which is necessary for doing transactional tests with `AshPostgres`.

## DataCase usage

When using `AshPostgres`, you should use `DataCase` instead of `ExUnit.Case`. Without DataCase, the database connection will fail, resulting in `DBConnection.OwnershipError`, and most of the doctests will break for the same reason.

```elixir
# test/my_app/accounts_test.exs
defmodule MyApp.AccountsTest do
use MyApp.DataCase

test "creates a user" do
# ...
end
end
```
DataCase is a reusable setup module that automatically imports everything necessary from the Ecto library, so you don't have to write alias and import every test file. Before every test, it checks out the database connection, wrapping everything that the test does inside of a database transaction and rolling it back after the test ends, regardless of its result. Checking the connection is what helps us avoid the `DBConnection.OwnershipError`.

For a practical look at writing tests, see [the how-to guide](/documentation/how-to/test-resources.livemd).

For a further explanation on how the sandbox works:
See [Ash-Postgres guide](https://hexdocs.pm/ash_postgres/readme.html)

## Missed notifications

If you are using Ecto's transactional features to ensure that your tests all run in a transaction, Ash will detect that it had notifications to send (if you have any notifiers set up) but couldn't because it was still in a transaction. The default behavior when notifications are missed is to warn. However, this can get pretty noisy in tests. So we suggest adding the following config to your `config/test.exs`.
Expand Down