Skip to content

Commit

Permalink
fix tests on the latest refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Aug 30, 2024
1 parent e524322 commit a08fead
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
REGISTRY="inakri" # set -a && source .env
REGISTRY="ghcr.io" # set -a && source .env
REPO_OWNER="izelnakri"
PGUSER=postgres
PGPASSWORD=postgres
# PGHOST=localhost
MIX_ENV=test
# CIRCLE_BRANCH=$$(if [ -v CIRCLE_BRANCH ]; then echo master; else git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; fi)
# DOCKER_TAG=paper_trail:master
# CIRCLE_BRANCH=$$(if [ -v CIRCLE_BRANCH ]; then echo main; else git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; fi)
# DOCKER_TAG=paper_trail:main
# DOCKER_TAG=$(echo paper_trail:${CIRCLE_BRANCH} | tr '/' '_')
DOCKER_TAG=$DOCKER_TAG
11 changes: 7 additions & 4 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ defmodule PaperTrail do
@doc """
Explicitly inserts a non-versioned already existing record into the Versions table
"""
def initialise(model, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, version_key: :version]) do
def initialise(
model,
options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, version_key: :version]
) do
case has_version?(model) do
false ->

with {:ok, _} <- make_version_struct(%{event: "insert"}, model, options)
|> PaperTrail.RepoClient.repo().insert() do
with {:ok, _} <-
make_version_struct(%{event: "insert"}, model, options)
|> PaperTrail.RepoClient.repo().insert() do
:ok
end

Expand Down
4 changes: 3 additions & 1 deletion lib/paper_trail/multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ defmodule PaperTrail.Multi do
end

def update(multi, changeset, options \\ @default_transaction_options)
def update(%Ecto.Multi{} = multi, %Ecto.Changeset{changes: changes} = changeset, options) when changes==%{} do

def update(%Ecto.Multi{} = multi, %Ecto.Changeset{changes: changes} = changeset, options)
when changes == %{} do
# when there's no changes to save, rely on ecto's update being a no-op
model_key = options[:model_key] || :model
ecto_options = options[:ecto_options] || []
Expand Down
7 changes: 4 additions & 3 deletions lib/paper_trail/version_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule PaperTrail.VersionQueries do
@spec get_version(model :: module, id :: pos_integer, options :: keyword | []) ::
Version.t() | nil
def get_version(model, id, options) do
last(version_query(model, id, options))
last(version_query(model, id, options))
|> PaperTrail.RepoClient.repo().one
end

Expand Down Expand Up @@ -105,11 +105,12 @@ defmodule PaperTrail.VersionQueries do
end

defp version_query(model, id, options) when is_atom(model) do
model
|> Module.split()
model
|> Module.split()
|> List.last()
|> version_query(id, options)
end

defp version_query(item_type, id, options) when is_binary(item_type) do
with opts <- Enum.into(options, %{}) do
version_query(item_type, id)
Expand Down
2 changes: 1 addition & 1 deletion setup-database.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
set -oe allexport
source .env
source ./.env

echo "PGHOST IS:"
echo $PGHOST
Expand Down
3 changes: 1 addition & 2 deletions test/paper_trail/bang_functions_simple_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ defmodule PaperTrailTest.SimpleModeBangFunctions 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)



MultiTenant.setup_tenant(repo())
:ok
end
Expand Down
3 changes: 1 addition & 2 deletions test/paper_trail/bang_functions_strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ defmodule PaperTrailTest.StrictModeBangFunctions do
Application.put_env(:paper_trail, :strict_mode, true)
Application.put_env(:paper_trail, :repo, PaperTrail.Repo)
Application.put_env(:paper_trail, :originator_type, :integer)



MultiTenant.setup_tenant(repo())
:ok
end
Expand Down
14 changes: 10 additions & 4 deletions test/paper_trail/base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ defmodule PaperTrailTest do
setup_all do
Application.put_env(:paper_trail, :strict_mode, false)
Application.put_env(:paper_trail, :repo, PaperTrail.Repo)



:ok
end

Expand Down Expand Up @@ -688,17 +687,24 @@ defmodule PaperTrailTest do
|> PaperTrail.update(origin: "admin")
end

test "updating a company with current params should not create a version" do
test "updating a company with current params should not create a version" do
{:ok, insert_result} = create_company_with_version()

insert_result_version = PaperTrail.get_version(insert_result[:model])
version_count_before_update = PaperTrail.Version.count()

{:ok, update_result} =
update_company_with_version(
insert_result[:model],
@create_company_params,
[]
)

assert PaperTrail.get_version(insert_result[:model]) == insert_result[:version]
version_count_after_update = PaperTrail.Version.count()
update_result_version = PaperTrail.get_version(update_result[:model])

assert version_count_before_update == version_count_after_update
assert insert_result_version == update_result_version
end

defp create_user do
Expand Down
3 changes: 1 addition & 2 deletions test/paper_trail/strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ defmodule PaperTrailStrictModeTest do
setup_all do
Application.put_env(:paper_trail, :strict_mode, true)
Application.put_env(:paper_trail, :repo, PaperTrail.Repo)



:ok
end

Expand Down
9 changes: 7 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Application.start(:postgrex)

Application.put_env(:paper_trail, :ecto_repos, [PaperTrail.Repo, PaperTrail.UUIDRepo, PaperTrail.UUIDWithCustomNameRepo])
Application.put_env(:paper_trail, :ecto_repos, [
PaperTrail.Repo,
PaperTrail.UUIDRepo,
PaperTrail.UUIDWithCustomNameRepo
])

Application.put_env(:paper_trail, :repo, PaperTrail.Repo)
Application.put_env(:paper_trail, :originator, [name: :user, model: User])
Application.put_env(:paper_trail, :originator, name: :user, model: User)

Mix.Task.run("ecto.drop")
Mix.Task.run("ecto.create")
Expand Down
4 changes: 2 additions & 2 deletions test/uuid/uuid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defmodule PaperTrailTest.UUIDTest do
if(System.get_env("STRING_TEST") == nil, do: Ecto.UUID, else: :string)
)



Code.eval_file("lib/paper_trail.ex")
Code.eval_file("lib/version.ex")

repo().delete_all(Version)
repo().delete_all(Admin)
Expand Down
4 changes: 2 additions & 2 deletions test/uuid/uuid_with_custom_name_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ defmodule PaperTrailTest.UUIDWithCustomNameTest do
if(System.get_env("STRING_TEST") == nil, do: Ecto.UUID, else: :string)
)



Code.eval_file("lib/paper_trail.ex")
Code.eval_file("lib/version.ex")

repo().delete_all(Version)
repo().delete_all(Person)
Expand Down
3 changes: 1 addition & 2 deletions test/version/paper_trail_version_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ defmodule PaperTrailTest.Version 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)



MultiTenant.setup_tenant(repo())
:ok
end
Expand Down
2 changes: 1 addition & 1 deletion test/version/version_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule PaperTrailTest.VersionQueries do
setup_all do
Application.put_env(:paper_trail, :repo, PaperTrail.Repo)
Application.put_env(:paper_trail, :originator_type, :integer)

MultiTenant.setup_tenant(repo())
reset_all_data()

Expand Down

0 comments on commit a08fead

Please sign in to comment.