From 1be667dce623f5ef650c1f9975bbff7eb9d5c9df Mon Sep 17 00:00:00 2001 From: Nick Kezhaya Date: Thu, 15 Oct 2020 11:07:58 -0500 Subject: [PATCH] Support real pg configuration --- config/test.exs | 13 +++++++++++++ lib/stripe_mock.ex | 20 ++++++++++++++++++++ lib/stripe_mock/database.ex | 15 ++++++++++++--- lib/stripe_mock/repo.ex | 5 ++++- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/config/test.exs b/config/test.exs index 6d92fbf..c595909 100644 --- a/config/test.exs +++ b/config/test.exs @@ -3,3 +3,16 @@ use Mix.Config config :stripe_mock, StripeMockWeb.Endpoint, server: false config :stripe_mock, StripeMock.Repo, pool: Ecto.Adapters.SQL.Sandbox config :logger, level: :warn + +config :stripe_mock, StripeMock.Database, enabled: false + +config :stripe_mock, StripeMock.Repo, + adapter: Ecto.Adapters.Postgres, + database: "stripe_mock_test", + hostname: System.get_env("POSTGRES_HOST", "localhost"), + port: System.get_env("POSTGRES_PORT", "5432"), + username: "postgres", + password: "postgres", + pool: Ecto.Adapters.SQL.Sandbox, + ownership_timeout: 30_000, + timeout: 30_000 diff --git a/lib/stripe_mock.ex b/lib/stripe_mock.ex index 0ab2671..5ced4eb 100644 --- a/lib/stripe_mock.ex +++ b/lib/stripe_mock.ex @@ -24,4 +24,24 @@ defmodule StripeMock do stores everything in its state. It'd be nice if `ecto_mnesia` was updated for Ecto 3, but as of right now this is the next best option. """ + + @doc """ + Truncates all tables. + """ + def reset() do + StripeMock.Repo.query!(""" + TRUNCATE + cards, + customers, + tokens, + sources, + payment_methods, + payment_intents, + charges, + refunds + CASCADE + """) + + :ok + end end diff --git a/lib/stripe_mock/database.ex b/lib/stripe_mock/database.ex index f517f57..3b98e22 100644 --- a/lib/stripe_mock/database.ex +++ b/lib/stripe_mock/database.ex @@ -5,8 +5,13 @@ defmodule StripeMock.Database do use GenServer - def start_link(arg) do - GenServer.start_link(__MODULE__, arg, name: __MODULE__) + def start_link(_) do + GenServer.start_link(__MODULE__, enabled?(), name: __MODULE__) + end + + def enabled?() do + Application.get_env(:stripe_mock, __MODULE__, []) + |> Keyword.get(:enabled, true) end def ecto_config() do @@ -14,7 +19,7 @@ defmodule StripeMock.Database do end @impl true - def init(_) do + def init(true) do {uri, _} = System.cmd("pg_tmp", ["-t", "-w", "180"]) [[username, host, port, database]] = @@ -33,6 +38,10 @@ defmodule StripeMock.Database do {:ok, config} end + def init(_) do + {:ok, []} + end + @impl true def handle_call(:config, _from, state) do {:reply, state, state} diff --git a/lib/stripe_mock/repo.ex b/lib/stripe_mock/repo.ex index 26193d2..d4961f6 100644 --- a/lib/stripe_mock/repo.ex +++ b/lib/stripe_mock/repo.ex @@ -4,7 +4,10 @@ defmodule StripeMock.Repo do alias StripeMock.Database def init(_type, config) do - {:ok, Keyword.merge(config, Database.ecto_config())} + case Database.enabled?() do + true -> {:ok, Keyword.merge(config, Database.ecto_config())} + _ -> {:ok, config} + end end def fetch(schema, id) do