Skip to content

Commit

Permalink
format test/*.exs test/mix/*
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski committed Jul 23, 2023
1 parent e25a7ce commit 5922071
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 121 deletions.
78 changes: 48 additions & 30 deletions test/mix/tasks/ecto.dump_load_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ defmodule Mix.Tasks.Ecto.DumpLoadTest do
@behaviour Ecto.Adapter.Structure

defmacro __before_compile__(_), do: :ok
def dumpers(_, _), do: raise "not implemented"
def loaders(_, _), do: raise "not implemented"
def checkout(_, _, _), do: raise "not implemented"
def checked_out?(_), do: raise "not implemented"
def dumpers(_, _), do: raise("not implemented")
def loaders(_, _), do: raise("not implemented")
def checkout(_, _, _), do: raise("not implemented")
def checked_out?(_), do: raise("not implemented")
def ensure_all_started(_, _), do: {:ok, []}

def init(_opts) do
child_spec = Supervisor.child_spec({Task, fn -> :timer.sleep(:infinity) end}, [])
{:ok, child_spec, %{}}
end

def structure_dump(_, _), do: Process.get(:structure_dump) || raise "no structure_dump"
def structure_load(_, _), do: Process.get(:structure_load) || raise "no structure_load"
def dump_cmd(_, _, _), do: Process.get(:dump_cmd) || raise "no dump_cmd"
def structure_dump(_, _), do: Process.get(:structure_dump) || raise("no structure_dump")
def structure_load(_, _), do: Process.get(:structure_load) || raise("no structure_load")
def dump_cmd(_, _, _), do: Process.get(:dump_cmd) || raise("no dump_cmd")
end

defmodule NoStructureAdapter do
@behaviour Ecto.Adapter
defmacro __before_compile__(_), do: :ok
def dumpers(_, _), do: raise "not implemented"
def loaders(_, _), do: raise "not implemented"
def init(_), do: raise "not implemented"
def checkout(_, _, _), do: raise "not implemented"
def checked_out?(_), do: raise "not implemented"
def ensure_all_started(_, _), do: raise "not implemented"
def dumpers(_, _), do: raise("not implemented")
def loaders(_, _), do: raise("not implemented")
def init(_), do: raise("not implemented")
def checkout(_, _, _), do: raise("not implemented")
def checked_out?(_), do: raise("not implemented")
def ensure_all_started(_, _), do: raise("not implemented")
end

# Mocked repos
Expand All @@ -60,38 +60,47 @@ defmodule Mix.Tasks.Ecto.DumpLoadTest do

test "runs the adapter structure_dump" do
Process.put(:structure_dump, {:ok, "foo"})
Dump.run ["-r", to_string(Repo)]
Dump.run(["-r", to_string(Repo)])
assert_received {:mix_shell, :info, [msg]}
assert msg =~ "The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been dumped to foo"
end

test "runs the adapter structure_dump for migration_repo" do
Application.put_env(:ecto_sql, Repo, [migration_repo: MigrationRepo])
Application.put_env(:ecto_sql, Repo, migration_repo: MigrationRepo)

Process.put(:structure_dump, {:ok, "foo"})
Dump.run ["-r", to_string(Repo)]
Dump.run(["-r", to_string(Repo)])

assert_received {:mix_shell, :info, ["The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been dumped to foo" <> _]}
assert_received {:mix_shell, :info,
[
"The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been dumped to foo" <>
_
]}

assert_received {:mix_shell, :info, ["The structure for Mix.Tasks.Ecto.DumpLoadTest.MigrationRepo has been dumped to foo" <> _]}
assert_received {:mix_shell, :info,
[
"The structure for Mix.Tasks.Ecto.DumpLoadTest.MigrationRepo has been dumped to foo" <>
_
]}
end

test "runs the adapter structure_dump with --quiet" do
Process.put(:structure_dump, {:ok, "foo"})
Dump.run ["-r", to_string(Repo), "--quiet"]
Dump.run(["-r", to_string(Repo), "--quiet"])
refute_received {:mix_shell, :info, [_]}
end

test "raises an error when structure_dump gives an unknown feedback" do
Process.put(:structure_dump, {:error, :confused})

assert_raise Mix.Error, fn ->
Dump.run ["-r", to_string(Repo)]
Dump.run(["-r", to_string(Repo)])
end
end

test "raises an error on structure_dump when the adapter doesn't define a storage" do
assert_raise Mix.Error, ~r/to implement Ecto.Adapter.Structure/, fn ->
Dump.run ["-r", to_string(NoStructureRepo)]
Dump.run(["-r", to_string(NoStructureRepo)])
end
end

Expand All @@ -101,49 +110,58 @@ defmodule Mix.Tasks.Ecto.DumpLoadTest do
table_exists? = fn _, _ -> false end

Process.put(:structure_load, {:ok, "foo"})
Load.run ["-r", to_string(Repo)], table_exists?
Load.run(["-r", to_string(Repo)], table_exists?)

assert_received {:mix_shell, :info, [msg]}
assert msg =~ "The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been loaded from foo"
end

test "runs the adapter structure_load for migration_repo" do
Application.put_env(:ecto_sql, Repo, [migration_repo: MigrationRepo])
Application.put_env(:ecto_sql, Repo, migration_repo: MigrationRepo)

table_exists? = fn _, _ -> false end

Process.put(:structure_load, {:ok, "foo"})
Load.run ["-r", to_string(Repo)], table_exists?
Load.run(["-r", to_string(Repo)], table_exists?)

assert_received {:mix_shell, :info, ["The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been loaded from foo" <> _]}
assert_received {:mix_shell, :info,
[
"The structure for Mix.Tasks.Ecto.DumpLoadTest.Repo has been loaded from foo" <>
_
]}

assert_received {:mix_shell, :info, ["The structure for Mix.Tasks.Ecto.DumpLoadTest.MigrationRepo has been loaded from foo" <> _]}
assert_received {:mix_shell, :info,
[
"The structure for Mix.Tasks.Ecto.DumpLoadTest.MigrationRepo has been loaded from foo" <>
_
]}
end

test "runs the adapter structure_load with --quiet" do
table_exists? = fn _, _ -> false end
Process.put(:structure_load, {:ok, "foo"})
Load.run ["-r", to_string(Repo), "--quiet"], table_exists?
Load.run(["-r", to_string(Repo), "--quiet"], table_exists?)
refute_received {:mix_shell, :info, [_]}
end

test "skips when the database is loaded with --skip-if-loaded" do
table_exists? = fn _, _ -> true end
assert :ok == Load.run ["-r", to_string(Repo), "--skip-if-loaded"], table_exists?
assert :ok == Load.run(["-r", to_string(Repo), "--skip-if-loaded"], table_exists?)
end

test "raises an error when structure_load gives an unknown feedback" do
table_exists? = fn _, _ -> false end

Process.put(:structure_load, {:error, :confused})

assert_raise Mix.Error, fn ->
Load.run ["-r", to_string(Repo)], table_exists?
Load.run(["-r", to_string(Repo)], table_exists?)
end
end

test "raises an error on structure_load when the adapter doesn't define a storage" do
assert_raise Mix.Error, ~r/to implement Ecto.Adapter.Structure/, fn ->
Load.run ["-r", to_string(NoStructureRepo)]
Load.run(["-r", to_string(NoStructureRepo)])
end
end
end
29 changes: 17 additions & 12 deletions test/mix/tasks/ecto.gen.migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,54 @@ defmodule Mix.Tasks.Ecto.Gen.MigrationTest do
end

test "generates a new migration" do
[path] = run ["-r", to_string(Repo), "my_migration"]
[path] = run(["-r", to_string(Repo), "my_migration"])
assert Path.dirname(path) == @migrations_path
assert Path.basename(path) =~ ~r/^\d{14}_my_migration\.exs$/
assert_file path, fn file ->

assert_file(path, fn file ->
assert file =~ "defmodule Mix.Tasks.Ecto.Gen.MigrationTest.Repo.Migrations.MyMigration do"
assert file =~ "use Ecto.Migration"
assert file =~ "def change do"
end
end)
end

test "generates a new migration with Custom Migration Module" do
Application.put_env(:ecto_sql, :migration_module, MyCustomApp.MigrationModule)
[path] = run ["-r", to_string(Repo), "my_custom_migration"]
[path] = run(["-r", to_string(Repo), "my_custom_migration"])
Application.delete_env(:ecto_sql, :migration_module)
assert Path.dirname(path) == @migrations_path
assert Path.basename(path) =~ ~r/^\d{14}_my_custom_migration\.exs$/
assert_file path, fn file ->
assert file =~ "defmodule Mix.Tasks.Ecto.Gen.MigrationTest.Repo.Migrations.MyCustomMigration do"

assert_file(path, fn file ->
assert file =~
"defmodule Mix.Tasks.Ecto.Gen.MigrationTest.Repo.Migrations.MyCustomMigration do"

assert file =~ "use MyCustomApp.MigrationModule"
assert file =~ "def change do"
end
end)
end

test "underscores the filename when generating a migration" do
run ["-r", to_string(Repo), "MyMigration"]
run(["-r", to_string(Repo), "MyMigration"])
assert [name] = File.ls!(@migrations_path)
assert name =~ ~r/^\d{14}_my_migration\.exs$/
end

test "custom migrations_path" do
dir = Path.join([unquote(tmp_path), "custom_migrations"])
[path] = run ["-r", to_string(Repo), "--migrations-path", dir, "custom_path"]
[path] = run(["-r", to_string(Repo), "--migrations-path", dir, "custom_path"])
assert Path.dirname(path) == dir
end

test "raises when existing migration exists" do
run ["-r", to_string(Repo), "my_migration"]
run(["-r", to_string(Repo), "my_migration"])

assert_raise Mix.Error, ~r"migration can't be created", fn ->
run ["-r", to_string(Repo), "my_migration"]
run(["-r", to_string(Repo), "my_migration"])
end
end

test "raises when missing file" do
assert_raise Mix.Error, fn -> run ["-r", to_string(Repo)] end
assert_raise Mix.Error, fn -> run(["-r", to_string(Repo)]) end
end
end
52 changes: 33 additions & 19 deletions test/mix/tasks/ecto.migrate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
defmodule Repo do
def start_link(_) do
Process.put(:started, true)
Task.start_link fn ->

Task.start_link(fn ->
Process.flag(:trap_exit, true)

receive do
{:EXIT, _, :normal} -> :ok
end
end
end)
end

def stop do
Expand Down Expand Up @@ -56,73 +58,82 @@ defmodule Mix.Tasks.Ecto.MigrateTest do

test "runs the migrator with app_repo config" do
Application.put_env(:ecto_sql, :ecto_repos, [Repo])
run [], fn _, _, _, _ ->

run([], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
end)

assert Process.get(:migrated)
assert Process.get(:started)
after
Application.delete_env(:ecto, :ecto_repos)
end

test "runs the migrator after starting repo" do
run ["-r", to_string(Repo)], fn _, _, _, _ ->
run(["-r", to_string(Repo)], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
end)

assert Process.get(:migrated)
assert Process.get(:started)
end

test "runs the migrator with the already started repo" do
run ["-r", to_string(StartedRepo)], fn _, _, _, _ ->
run(["-r", to_string(StartedRepo)], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
end)

assert Process.get(:migrated)
assert Process.get(:already_started)
end

test "runs the migrator with two repos" do
run ["-r", to_string(Repo), "-r", to_string(StartedRepo)], fn _, _, _, _ ->
run(["-r", to_string(Repo), "-r", to_string(StartedRepo)], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
end)

assert Process.get(:migrated)
assert Process.get(:started)
assert Process.get(:already_started)
end

test "runs the migrator yielding the repository and migrations path" do
run ["-r", to_string(Repo), "--quiet", "--prefix", "foo"], fn repo, [path], direction, opts ->
run(["-r", to_string(Repo), "--quiet", "--prefix", "foo"], fn repo, [path], direction, opts ->
assert repo == Repo
refute path =~ ~r/_build/
assert direction == :up
assert opts[:all] == true
assert opts[:log] == false
assert opts[:prefix] == "foo"
[]
end
end)

assert Process.get(:started)
end

test "runs the migrator with --step" do
run ["-r", to_string(Repo), "-n", "1"], fn repo, [path], direction, opts ->
run(["-r", to_string(Repo), "-n", "1"], fn repo, [path], direction, opts ->
assert repo == Repo
refute path =~ ~r/_build/
assert direction == :up
assert opts == [repo: "Elixir.Mix.Tasks.Ecto.MigrateTest.Repo", step: 1]
[]
end
end)

assert Process.get(:started)
end

test "raises when migrations path does not exist" do
File.rm_rf!(@migrations_path)

assert_raise Mix.Error, fn ->
run ["-r", to_string(Repo)], fn _, _, _, _ -> [] end
run(["-r", to_string(Repo)], fn _, _, _, _ -> [] end)
end

assert !Process.get(:started)
end

Expand All @@ -132,18 +143,21 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
File.mkdir_p!(path1)
File.mkdir_p!(path2)

run ["-r", to_string(Repo), "--migrations-path", path1, "--migrations-path", path2],
fn Repo, [^path1, ^path2], _, _ -> [] end
run(
["-r", to_string(Repo), "--migrations-path", path1, "--migrations-path", path2],
fn Repo, [^path1, ^path2], _, _ -> [] end
)
end

test "runs the migrator with --to_exclusive" do
run ["-r", to_string(Repo), "--to-exclusive", "12345"], fn repo, [path], direction, opts ->
run(["-r", to_string(Repo), "--to-exclusive", "12345"], fn repo, [path], direction, opts ->
assert repo == Repo
refute path =~ ~r/_build/
assert direction == :up
assert opts == [repo: "Elixir.Mix.Tasks.Ecto.MigrateTest.Repo", to_exclusive: 12345]
[]
end
end)

assert Process.get(:started)
end
end
Loading

0 comments on commit 5922071

Please sign in to comment.