Skip to content

Commit c42cf09

Browse files
committed
clean up between tests
1 parent 4ecfb6c commit c42cf09

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ config :coffee_shop, CoffeeShop.Repo,
66
password: "postgres",
77
database: "coffee_shop_test",
88
hostname: "localhost",
9-
pool: Ecto.Adapters.SQL.Sandbox
9+
pool_size: 1
1010

1111
# We don't run a server during test. If one is required,
1212
# you can enable the server option below.

test/support/conn_case.ex

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ defmodule CoffeeShopWeb.ConnCase do
66
Such tests rely on `Phoenix.ConnTest` and also
77
import other functionality to make it easier
88
to build common data structures and query the data layer.
9-
10-
Finally, if the test case interacts with the database,
11-
we enable the SQL sandbox, so changes done to the database
12-
are reverted at the end of every test. If you are using
13-
PostgreSQL, you can even run database tests asynchronously
14-
by setting `use CoffeeShopWeb.ConnCase, async: true`, although
15-
this option is not recommended for other databases.
169
"""
1710

1811
use ExUnit.CaseTemplate
@@ -28,12 +21,8 @@ defmodule CoffeeShopWeb.ConnCase do
2821
end
2922
end
3023

31-
setup tags do
32-
:ok = Ecto.Adapters.SQL.Sandbox.checkout(CoffeeShop.Repo)
33-
34-
unless tags[:async] do
35-
Ecto.Adapters.SQL.Sandbox.mode(CoffeeShop.Repo, {:shared, self()})
36-
end
24+
setup _tags do
25+
CoffeeShop.Storage.reset!()
3726

3827
{:ok, conn: Phoenix.ConnTest.build_conn()}
3928
end

test/support/storage.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule CoffeeShop.Storage do
2+
@doc """
3+
Reset the event store and read store databases.
4+
"""
5+
def reset! do
6+
:ok = Application.stop(:coffee_shop)
7+
8+
reset_eventstore!()
9+
10+
# reset_readstore!() # This will come in handy once we add a readstore
11+
12+
{:ok, _} = Application.ensure_all_started(:coffee_shop)
13+
end
14+
15+
defp reset_eventstore! do
16+
{:ok, conn} =
17+
CoffeeShop.EventStore.config()
18+
|> EventStore.Config.default_postgrex_opts()
19+
|> Postgrex.start_link()
20+
21+
EventStore.Storage.Initializer.reset!(conn)
22+
end
23+
24+
defp reset_readstore! do
25+
readstore_config = Application.get_env(:coffee_shop, CoffeeShop.Repo)
26+
27+
{:ok, conn} = Postgrex.start_link(readstore_config)
28+
29+
Postgrex.query!(conn, truncate_readstore_tables(), [])
30+
end
31+
32+
defp truncate_readstore_tables do
33+
"""
34+
TRUNCATE TABLE
35+
projection_versions
36+
RESTART IDENTITY;
37+
"""
38+
end
39+
end

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
ExUnit.configure(exclude: [:pending])
12
ExUnit.start()
2-
Ecto.Adapters.SQL.Sandbox.mode(CoffeeShop.Repo, :manual)

0 commit comments

Comments
 (0)