Last active
May 18, 2024 04:23
-
-
Save Wigny/30c368ecbb9176a997672e951ac7b2dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mix.install([ | |
{:ecto_sql, "~> 3.11"}, | |
{:myxql, "~> 0.6.4"}, | |
{:testcontainers, "~> 1.8"} | |
]) | |
alias Testcontainers.MySqlContainer | |
{:ok, _} = Testcontainers.start_link() | |
{:ok, container} = Testcontainers.start_container(MySqlContainer.with_image(MySqlContainer.new(), "mysql:8.4")) | |
Application.put_env(:example, Repo, MySqlContainer.connection_parameters(container)) | |
ExUnit.start() | |
ExUnit.run() | |
defmodule Repo do | |
use Ecto.Repo, | |
otp_app: :example, | |
adapter: Ecto.Adapters.MyXQL | |
end | |
defmodule Migration0 do | |
use Ecto.Migration | |
def change do | |
create table("users") do | |
add(:roles, :json) | |
end | |
end | |
end | |
defmodule User do | |
use Ecto.Schema | |
schema "users" do | |
field(:roles, {:array, Ecto.Enum}, values: ~w[admin customer]a) | |
end | |
end | |
defmodule Test do | |
use ExUnit.Case, async: true | |
import Ecto.Query | |
setup :start_repo | |
test "value in array" do | |
user = user_fixture() | |
assert user == Repo.one(where(User, [u], :admin in u.roles)) | |
end | |
defp user_fixture do | |
Repo.insert!(%User{roles: [:admin]}) | |
end | |
defp start_repo(_context) do | |
_ = Repo.__adapter__().storage_down(Repo.config()) | |
:ok = Repo.__adapter__().storage_up(Repo.config()) | |
{:ok, _} = Supervisor.start_link([Repo], strategy: :one_for_one) | |
Ecto.Migrator.run(Repo, [{0, Migration0}], :up, all: true, log_migrations_sql: :debug) | |
:ok | |
end | |
end |
Author
Wigny
commented
May 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment