Skip to content

Commit

Permalink
Support real pg configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 15, 2020
1 parent d73dbcf commit 1be667d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
13 changes: 13 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions lib/stripe_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 12 additions & 3 deletions lib/stripe_mock/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ 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
GenServer.call(__MODULE__, :config)
end

@impl true
def init(_) do
def init(true) do
{uri, _} = System.cmd("pg_tmp", ["-t", "-w", "180"])

[[username, host, port, database]] =
Expand All @@ -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}
Expand Down
5 changes: 4 additions & 1 deletion lib/stripe_mock/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1be667d

Please sign in to comment.