Skip to content

Commit

Permalink
update examples and readme and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 6, 2023
1 parent 2df0a40 commit 020c447
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 26 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,7 @@ To start a postgres container when running tests, that also enables testing of a
@impl true
def start(_type, _args) do
if Mix.env() == :test do
{:ok, _container} =
Testcontainers.Ecto.postgres_container(
app: :my_app,
user: "postgres",
password: "postgres",
# to avoid port collision set the same port in config/test.exs
# or comment this out
port: 5433
)
{:ok, _container} = Testcontainers.Ecto.postgres_container(app: :my_app)
end

# .. other setup code
Expand All @@ -112,6 +104,8 @@ To start a postgres container when running tests, that also enables testing of a

This will start a postgres container that will be terminated when the test process ends.

The database config in config/test.exs will be temporaly updated in-memory with the random host port on the container, and other properties like username, password and database. In most cases these will default to "test" unless overridden.

See documentation on [Testcontainers.Ecto](https://hexdocs.pm/testcontainers/Testcontainers.Ecto.html) for more information about the options it can take.

There is an example repo here with a bare bones phoenix application, where the only changes are the use of the ecto function and removing the test alias that interferes with it:
Expand Down
8 changes: 1 addition & 7 deletions examples/phoenix_project/lib/hello/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ defmodule Hello.Application do
@impl true
def start(_type, _args) do
if Mix.env() == :test do
{:ok, _container} =
Testcontainers.Ecto.postgres_container(
app: :hello,
user: "postgres",
password: "postgres",
database: "hello_test#{System.get_env("MIX_TEST_PARTITION")}"
)
{:ok, _container} = Testcontainers.Ecto.postgres_container(app: :hello)
end

# to use mysql, change
Expand Down
9 changes: 1 addition & 8 deletions lib/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule Testcontainers.Ecto do
- `:app` - The current application's atom, necessary for building paths and other application-specific logic. This is a required parameter.
- `:repo` (optional) - The Ecto repository module for database interaction. If not provided, it is inferred from the `:app` option using the default naming convention (e.g., `MyApp.Repo`).
- `:image` (optional) - Specifies the Docker image for the Postgres container. This must be a legitimate Postgres image, with the image name beginning with "postgres". If omitted, the default is "postgres:15".
- `:port` (optional) - Specifies the exposed port for the Postgres container (defaults to 5432). Does not translate to host port, which is dynamically assigned.
- `:user` (optional) - Sets the username for the Postgres instance (defaults to "postgres").
- `:password` (optional) - Determines the password for the Postgres user (defaults to "postgres").
- `:database` (optional) - Specifies the name of the database to be created within the Postgres instance. If not provided, the default behavior is to create a database with the name derived from the application's atom, appended with "_test".
Expand Down Expand Up @@ -114,7 +113,6 @@ defmodule Testcontainers.Ecto do
- `:app` - The current application's atom, necessary for building paths and other application-specific logic. This is a required parameter.
- `:repo` (optional) - The Ecto repository module for database interaction. If not provided, it is inferred from the `:app` option using the default naming convention (e.g., `MyApp.Repo`).
- `:image` (optional) - Specifies the Docker image for the Mysql container. This must be a legitimate Mysql image, with the image name beginning with "mysql". If omitted, the default is "mysql:8".
- `:port` (optional) - Specifies the exposed port for the Mysql container (defaults to 3306). Does not translate to host port, which is dynamically assigned.
- `:user` (optional) - Sets the username for the Mysql instance (defaults to "test").
- `:password` (optional) - Determines the password for the Mysql user (defaults to "test").
- `:database` (optional) - Specifies the name of the database to be created within the Mysql instance. If not provided, the default behavior is to create a database with the name derived from the application's atom, appended with "_test".
Expand Down Expand Up @@ -231,10 +229,6 @@ defmodule Testcontainers.Ecto do
repo
end

if not :erlang.function_exported(repo, :__info__, 1) do
raise ArgumentError, "Repo is invalid: repo=#{inspect(repo)}"
end

user = Keyword.get(options, :user, "test")
password = Keyword.get(options, :password, "test")
database = Keyword.get(options, :database, "#{Atom.to_string(app)}_test")
Expand All @@ -247,12 +241,11 @@ defmodule Testcontainers.Ecto do
end

image = Keyword.get(options, :image, container_module.default_image_with_tag())
container_port = Keyword.get(options, :port, container_module.default_port())

config =
container_module.new()
|> container_module.with_image(image)
|> container_module.with_port(container_port)
|> container_module.with_port(container_module.default_port())
|> container_module.with_user(user)
|> container_module.with_database(database)
|> container_module.with_password(password)
Expand Down
4 changes: 2 additions & 2 deletions test/ecto_errors_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Testcontainers.EctoMysqlTest do
defmodule Testcontainers.EctoErrorsTest do
use ExUnit.Case, async: true

import Testcontainers.Ecto

test "repo cannot be nil" do
assert_raise ArgumentError, "Repo is invalid: repo=Testcontainers.Repo", fn ->
assert_raise FunctionClauseError, "no function clause matching in Keyword.merge/2", fn ->
mysql_container(
app: :testcontainers,
repo: nil
Expand Down
2 changes: 2 additions & 0 deletions test/ecto_mysql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Testcontainers.EctoMysqlTest do

@moduletag timeout: 300_000

require Testcontainers.MysqlRepo

test "can use ecto function" do
{:ok, container} =
mysql_container(
Expand Down
2 changes: 2 additions & 0 deletions test/ecto_postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Testcontainers.EctoPostgresTest do

@moduletag timeout: 300_000

require Testcontainers.PostgresRepo

test "can use ecto function" do
{:ok, container} =
postgres_container(
Expand Down

0 comments on commit 020c447

Please sign in to comment.