Skip to content

Commit

Permalink
repo configurations on tests are now loaded lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 1, 2020
1 parent 8b8f5e2 commit 0fa068f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 70 deletions.
41 changes: 22 additions & 19 deletions test/paper_trail/bang_functions_simple_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
alias SimplePerson, as: Person
alias PaperTrailTest.MultiTenantHelper, as: MultiTenant

@repo PaperTrail.RepoClient.repo()
@create_company_params %{name: "Acme LLC", is_active: true, city: "Greenwich"}
@update_company_params %{
city: "Hong Kong",
Expand All @@ -24,7 +23,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
Application.put_env(:paper_trail, :originator_type, :integer)
Code.eval_file("lib/paper_trail.ex")
Code.eval_file("lib/version.ex")
MultiTenant.setup_tenant(@repo)
MultiTenant.setup_tenant(repo())
:ok
end

Expand Down Expand Up @@ -71,7 +70,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
meta: nil
}

assert company == first(Company, :id) |> @repo.one
assert company == first(Company, :id) |> repo().one
end

test "PaperTrail.insert!/2 with an error raises Ecto.InvalidChangesetError" do
Expand Down Expand Up @@ -126,7 +125,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
meta: nil
}

assert company == first(Company, :id) |> @repo.one |> serialize
assert company == first(Company, :id) |> repo().one |> serialize
end

test "updating a company with originator[user] creates a correct company version" do
Expand Down Expand Up @@ -175,7 +174,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
meta: nil
}

assert company == first(Company, :id) |> @repo.one |> serialize
assert company == first(Company, :id) |> repo().one |> serialize
end

test "PaperTrail.update!/2 with an error raises Ecto.InvalidChangesetError" do
Expand All @@ -195,7 +194,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
user = create_user()
inserted_company = create_company_with_version()
updated_company = update_company_with_version(inserted_company)
company_before_deletion = first(Company, :id) |> @repo.one |> serialize
company_before_deletion = first(Company, :id) |> repo().one |> serialize
deleted_company = PaperTrail.delete!(updated_company, originator: user)

company_count = Company.count()
Expand Down Expand Up @@ -310,7 +309,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
meta: %{"linkname" => "izelnakri"}
}

assert person == first(Person, :id) |> @repo.one |> serialize
assert person == first(Person, :id) |> repo().one |> serialize
end

test "updating a person creates a person version with correct attributes" do
Expand Down Expand Up @@ -381,7 +380,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
meta: %{"linkname" => "izelnakri"}
}

assert person == first(Person, :id) |> @repo.one |> serialize
assert person == first(Person, :id) |> repo().one |> serialize
end

test "deleting a person creates a person version with correct attributes" do
Expand Down Expand Up @@ -412,7 +411,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
})
|> PaperTrail.update!(origin: "scraper", meta: %{linkname: "izelnakri"})

person_before_deletion = first(Person, :id) |> @repo.one |> serialize
person_before_deletion = first(Person, :id) |> repo().one |> serialize

deleted_person =
PaperTrail.delete!(
Expand Down Expand Up @@ -934,13 +933,13 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
# Functions
defp create_user() do
User.changeset(%User{}, %{token: "fake-token", username: "izelnakri"})
|> @repo.insert!
|> repo().insert!
end

defp create_user(:multitenant) do
User.changeset(%User{}, %{token: "fake-token", username: "izelnakri"})
|> MultiTenant.add_prefix_to_changeset()
|> @repo.insert!
|> repo().insert!
end

defp create_company_with_version(params \\ @create_company_params, options \\ nil) do
Expand Down Expand Up @@ -972,11 +971,11 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
end

defp first_company(:multitenant) do
first(Company, :id) |> MultiTenant.add_prefix_to_query() |> @repo.one()
first(Company, :id) |> MultiTenant.add_prefix_to_query() |> repo().one()
end

defp first_person(:multitenant) do
first(Person, :id) |> MultiTenant.add_prefix_to_query() |> @repo.one()
first(Person, :id) |> MultiTenant.add_prefix_to_query() |> repo().one()
end

defp serialize(model) do
Expand All @@ -985,24 +984,28 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do
end

defp reset_all_data() do
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
repo().delete_all(Person)
repo().delete_all(Company)
repo().delete_all(Version)

Person
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()

Company
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()

Version
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()
end

defp convert_to_string_map(map) do
map |> Jason.encode!() |> Jason.decode!()
end

defp repo() do
PaperTrail.RepoClient.repo()
end
end
39 changes: 21 additions & 18 deletions test/paper_trail/bang_functions_strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
alias StrictPerson, as: Person
alias PaperTrailTest.MultiTenantHelper, as: MultiTenant

@repo PaperTrail.RepoClient.repo()
@create_company_params %{name: "Acme LLC", is_active: true, city: "Greenwich"}
@update_company_params %{
city: "Hong Kong",
Expand All @@ -24,7 +23,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
Application.put_env(:paper_trail, :originator_type, :integer)
Code.eval_file("lib/paper_trail.ex")
Code.eval_file("lib/version.ex")
MultiTenant.setup_tenant(@repo)
MultiTenant.setup_tenant(repo())
:ok
end

Expand Down Expand Up @@ -74,7 +73,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
meta: nil
}

assert company == first(Company, :id) |> @repo.one |> serialize
assert company == first(Company, :id) |> repo().one |> serialize
end

test "creating a company without changeset creates a company version with correct attributes" do
Expand Down Expand Up @@ -168,7 +167,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
meta: nil
}

assert company == first(Company, :id) |> @repo.one |> serialize
assert company == first(Company, :id) |> repo().one |> serialize
end

test "PaperTrail.update!/2 with an error raises Ecto.InvalidChangesetError" do
Expand All @@ -190,7 +189,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
inserted_company_version = PaperTrail.get_version(inserted_company)
updated_company = update_company_with_version(inserted_company)
updated_company_version = PaperTrail.get_version(updated_company)
company_before_deletion = first(Company, :id) |> @repo.one |> serialize
company_before_deletion = first(Company, :id) |> repo().one |> serialize
deleted_company = PaperTrail.delete!(updated_company, user: user)

company_count = Company.count()
Expand Down Expand Up @@ -308,7 +307,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
meta: %{"linkname" => "izelnakri"}
}

assert person == first(Person, :id) |> @repo.one |> serialize
assert person == first(Person, :id) |> repo().one |> serialize
end

test "updating a person creates a person version with correct attributes" do
Expand Down Expand Up @@ -385,7 +384,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
meta: %{"linkname" => "izelnakri"}
}

assert person == first(Person, :id) |> @repo.one |> serialize
assert person == first(Person, :id) |> repo().one |> serialize
end

test "deleting a person creates a person version with correct attributes" do
Expand Down Expand Up @@ -418,7 +417,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
|> PaperTrail.update!(origin: "scraper", meta: %{linkname: "izelnakri"})

updated_person_version = PaperTrail.get_version(updated_person) |> serialize
person_before_deletion = first(Person, :id) |> @repo.one
person_before_deletion = first(Person, :id) |> repo().one

deleted_person =
PaperTrail.delete!(
Expand Down Expand Up @@ -961,13 +960,13 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
# Functions
defp create_user() do
User.changeset(%User{}, %{token: "fake-token", username: "izelnakri"})
|> @repo.insert!
|> repo().insert!
end

defp create_user(:multitenant) do
User.changeset(%User{}, %{token: "fake-token", username: "izelnakri"})
|> MultiTenant.add_prefix_to_changeset()
|> @repo.insert!
|> repo().insert!
end

defp create_company_with_version(params \\ @create_company_params, options \\ nil) do
Expand Down Expand Up @@ -999,11 +998,11 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
end

defp first_company(:multitenant) do
first(Company, :id) |> MultiTenant.add_prefix_to_query() |> @repo.one()
first(Company, :id) |> MultiTenant.add_prefix_to_query() |> repo().one()
end

defp first_person(:multitenant) do
first(Person, :id) |> MultiTenant.add_prefix_to_query() |> @repo.one()
first(Person, :id) |> MultiTenant.add_prefix_to_query() |> repo().one()
end

defp serialize(model) do
Expand All @@ -1012,24 +1011,28 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
end

defp reset_all_data() do
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
repo().delete_all(Person)
repo().delete_all(Company)
repo().delete_all(Version)

Person
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()

Company
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()

Version
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()
end

defp convert_to_string_map(map) do
map |> Jason.encode!() |> Jason.decode!()
end

defp repo() do
PaperTrail.RepoClient.repo()
end
end
18 changes: 10 additions & 8 deletions test/paper_trail/paper_trail_version_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,29 @@ defmodule PaperTrailTest.Version do
}
@invalid_attrs %{}

@repo PaperTrail.RepoClient.repo()

setup_all do
Application.put_env(:paper_trail, :strict_mode, false)
Application.put_env(:paper_trail, :repo, PaperTrail.Repo)
Application.put_env(:paper_trail, :originator_type, :integer)
Code.eval_file("lib/paper_trail.ex")
Code.eval_file("lib/version.ex")
MultiTenant.setup_tenant(@repo)
MultiTenant.setup_tenant(repo())
:ok
end

setup do
@repo.delete_all(Version)
repo().delete_all(Version)

Version
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()

on_exit(fn ->
@repo.delete_all(Version)
repo().delete_all(Version)

Version
|> MultiTenant.add_prefix_to_query()
|> @repo.delete_all()
|> repo().delete_all()
end)

:ok
Expand Down Expand Up @@ -142,7 +140,7 @@ defmodule PaperTrailTest.Version do
end

def add_three_versions(prefix \\ nil) do
@repo.insert_all(
repo().insert_all(
Version,
[
@valid_attrs,
Expand Down Expand Up @@ -175,4 +173,8 @@ defmodule PaperTrailTest.Version do
relationships = resource.__struct__.__schema__(:associations)
Map.drop(resource, [:__meta__, :__struct__] ++ relationships)
end

defp repo() do
PaperTrail.RepoClient.repo()
end
end
Loading

0 comments on commit 0fa068f

Please sign in to comment.