Files
bDS2/test/bds/repo/bootstrap_test.exs
2026-04-24 18:22:25 +02:00

71 lines
1.8 KiB
Elixir

defmodule BDS.Repo.BootstrapTest do
use ExUnit.Case, async: false
alias BDS.Projects.Project
defmodule TempRepo do
use Ecto.Repo,
otp_app: :bds,
adapter: Ecto.Adapters.SQLite3
end
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(BDS.Repo)
__MODULE__.RepoConfigBackup.put_env()
on_exit(fn ->
__MODULE__.RepoConfigBackup.restore_env()
end)
:ok
end
test "ensure_schema creates persistence tables in a blank sqlite database" do
temp_db = Path.join(System.tmp_dir!(), "bds-bootstrap-#{System.unique_integer([:positive])}.db")
Application.put_env(:bds, TempRepo,
database: temp_db,
pool_size: 1,
stacktrace: true,
show_sensitive_data_on_connection_error: true
)
start_supervised!(TempRepo)
on_exit(fn ->
File.rm_rf(temp_db)
end)
assert :ok = BDS.RepoBootstrap.ensure_schema(repo: TempRepo)
tables =
Ecto.Adapters.SQL.query!(TempRepo, "SELECT name FROM sqlite_master WHERE type = 'table'", []).rows
|> Enum.map(&hd/1)
assert "projects" in tables
assert "posts" in tables
assert "templates" in tables
end
test "ensure_ready seeds the default project in the app repo" do
BDS.Repo.delete_all(Project)
assert :ok = BDS.RepoBootstrap.ensure_ready(migrate?: false)
assert %Project{id: "default", name: "My Blog", is_active: true} = BDS.Projects.get_active_project()
end
defmodule RepoConfigBackup do
def put_env do
Process.put({__MODULE__, :temp_repo_config}, Application.get_env(:bds, TempRepo))
end
def restore_env do
case Process.get({__MODULE__, :temp_repo_config}) do
nil -> Application.delete_env(:bds, TempRepo)
config -> Application.put_env(:bds, TempRepo, config)
end
end
end
end