Skip to content

Instantly share code, notes, and snippets.

@slashdotdash
Created October 18, 2018 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashdotdash/e146cac4f9cbf7236338ea8758a1910c to your computer and use it in GitHub Desktop.
Save slashdotdash/e146cac4f9cbf7236338ea8758a1910c to your computer and use it in GitHub Desktop.
defmodule MyApp.DataCase do
use ExUnit.CaseTemplate
using do
quote do
import Commanded.Assertions.EventAssertions
end
end
setup do
{:ok, _} = Application.ensure_all_started(:my_app)
on_exit(fn ->
:ok = Application.stop(:my_app)
:ok = Application.stop(:commanded)
:ok = Application.stop(:eventstore)
MyApp.Storage.reset!()
end)
:ok
end
end
defmodule MyApp.Storage do
@doc """
Clear the event store and read store databases
"""
def reset! do
reset_eventstore()
reset_readstore()
end
defp reset_eventstore do
config = EventStore.Config.parsed() |> EventStore.Config.default_postgrex_opts()
{:ok, conn} = Postgrex.start_link(config)
EventStore.Storage.Initializer.reset!(conn)
end
defp reset_readstore do
config = Application.get_env(:my_app, MyApp.Repo)
{:ok, conn} = Postgrex.start_link(config)
Postgrex.query!(conn, truncate_readstore_tables(), [])
end
defp truncate_readstore_tables do
"""
TRUNCATE TABLE
foo,
bar,
baz
RESTART IDENTITY
CASCADE;
"""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment