From 020c44742570b6718074c551731365706585d104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Mon, 6 Nov 2023 22:08:19 +0100 Subject: [PATCH] update examples and readme and doc --- README.md | 12 +++--------- examples/phoenix_project/lib/hello/application.ex | 8 +------- lib/ecto.ex | 9 +-------- test/ecto_errors_test.exs | 4 ++-- test/ecto_mysql_test.exs | 2 ++ test/ecto_postgres_test.exs | 2 ++ 6 files changed, 11 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5fa3175..7c7a5a0 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/examples/phoenix_project/lib/hello/application.ex b/examples/phoenix_project/lib/hello/application.ex index 8daddd2..91ec0cc 100644 --- a/examples/phoenix_project/lib/hello/application.ex +++ b/examples/phoenix_project/lib/hello/application.ex @@ -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 diff --git a/lib/ecto.ex b/lib/ecto.ex index 050805c..9a8cb39 100644 --- a/lib/ecto.ex +++ b/lib/ecto.ex @@ -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". @@ -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". @@ -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") @@ -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) diff --git a/test/ecto_errors_test.exs b/test/ecto_errors_test.exs index dfdcc5d..db8ce85 100644 --- a/test/ecto_errors_test.exs +++ b/test/ecto_errors_test.exs @@ -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 diff --git a/test/ecto_mysql_test.exs b/test/ecto_mysql_test.exs index f17608f..485e2b5 100644 --- a/test/ecto_mysql_test.exs +++ b/test/ecto_mysql_test.exs @@ -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( diff --git a/test/ecto_postgres_test.exs b/test/ecto_postgres_test.exs index 346432e..0aa1212 100644 --- a/test/ecto_postgres_test.exs +++ b/test/ecto_postgres_test.exs @@ -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(