From 5df86eee938c9f0ce4459a5db7d8ea8c1efe5208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Mon, 30 Nov 2020 17:12:37 +0100 Subject: [PATCH 1/2] Run Formatter --- lib/paper_trail.ex | 26 +++++++++++++++++-- lib/paper_trail/multi.ex | 19 +++++++++++--- mix.exs | 1 - .../bang_functions_simple_mode_test.exs | 11 +++++--- .../bang_functions_strict_mode_test.exs | 12 ++++++--- test/paper_trail/base_test.exs | 16 +++++++++--- test/paper_trail/strict_mode_test.exs | 16 +++++++++--- test/support/simple_models.exs | 1 - 8 files changed, 79 insertions(+), 23 deletions(-) diff --git a/lib/paper_trail.ex b/lib/paper_trail.ex index 02250b93..1fea2558 100644 --- a/lib/paper_trail.ex +++ b/lib/paper_trail.ex @@ -22,7 +22,18 @@ defmodule PaperTrail do @doc """ Inserts a record to the database with a related version insertion in one transaction """ - def insert(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: []]) do + def insert( + changeset, + options \\ [ + origin: nil, + meta: nil, + originator: nil, + prefix: nil, + model_key: :model, + version_key: :version, + ecto_options: [] + ] + ) do PaperTrail.Multi.new() |> PaperTrail.Multi.insert(changeset, options) |> PaperTrail.Multi.commit() @@ -31,7 +42,18 @@ defmodule PaperTrail do @doc """ Same as insert/2 but returns only the model struct or raises if the changeset is invalid. """ - def insert!(changeset, options \\ [origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: []]) do + def insert!( + changeset, + options \\ [ + origin: nil, + meta: nil, + originator: nil, + prefix: nil, + model_key: :model, + version_key: :version, + ecto_options: [] + ] + ) do repo = RepoClient.repo() ecto_options = options[:ecto_options] || [] diff --git a/lib/paper_trail/multi.ex b/lib/paper_trail/multi.ex index 33fa7160..4b7d44b1 100644 --- a/lib/paper_trail/multi.ex +++ b/lib/paper_trail/multi.ex @@ -22,9 +22,19 @@ defmodule PaperTrail.Multi do defdelegate get_item_type(data), to: Serializer defdelegate get_model_id(model), to: Serializer - def insert(%Ecto.Multi{} = multi, changeset, options \\ [ - origin: nil, meta: nil, originator: nil, prefix: nil, model_key: :model, version_key: :version, ecto_options: [] - ]) do + def insert( + %Ecto.Multi{} = multi, + changeset, + options \\ [ + origin: nil, + meta: nil, + originator: nil, + prefix: nil, + model_key: :model, + version_key: :version, + ecto_options: [] + ] + ) do model_key = options[:model_key] || :model version_key = options[:version_key] || :version ecto_options = options[:ecto_options] || [] @@ -56,7 +66,8 @@ defmodule PaperTrail.Multi do repo.insert(updated_changeset, ecto_options) end) - |> Ecto.Multi.run(version_key, fn repo, %{initial_version: initial_version, model: model} -> + |> Ecto.Multi.run(version_key, fn repo, + %{initial_version: initial_version, model: model} -> target_version = make_version_struct(%{event: "insert"}, model, options) |> serialize() Version.changeset(initial_version, target_version) |> repo.update diff --git a/mix.exs b/mix.exs index 6042c9cc..1c47b885 100644 --- a/mix.exs +++ b/mix.exs @@ -29,7 +29,6 @@ defmodule PaperTrail.Mixfile do [ {:ecto, ">= 3.4.6"}, {:ecto_sql, ">= 3.4.5"}, - {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:jason, ">= 1.2.0", only: [:dev, :test]}, {:postgrex, ">= 0.0.0", only: [:dev, :test]} diff --git a/test/paper_trail/bang_functions_simple_mode_test.exs b/test/paper_trail/bang_functions_simple_mode_test.exs index bd115a35..cde4779d 100644 --- a/test/paper_trail/bang_functions_simple_mode_test.exs +++ b/test/paper_trail/bang_functions_simple_mode_test.exs @@ -87,12 +87,17 @@ defmodule PaperTrailTest.SimpleModeBangFunctions do test "PaperTrail.insert!/2 passes ecto options through (e.g. upsert options)" do user = create_user() - _result = create_company_with_version(@create_company_params, [originator: user]) + _result = create_company_with_version(@create_company_params, originator: user) new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona") - ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] - result = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options]) + ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] + + result = + create_company_with_version(new_create_company_params, + originator: user, + ecto_options: ecto_options + ) assert Company.count() == 1 assert Version.count() == 2 diff --git a/test/paper_trail/bang_functions_strict_mode_test.exs b/test/paper_trail/bang_functions_strict_mode_test.exs index 324034bd..1d3af3a9 100644 --- a/test/paper_trail/bang_functions_strict_mode_test.exs +++ b/test/paper_trail/bang_functions_strict_mode_test.exs @@ -124,12 +124,17 @@ defmodule PaperTrailTest.StrictModeBangFunctions do test "PaperTrail.insert!/2 passes ecto options through (e.g. upsert options)" do user = create_user() - _result = create_company_with_version(@create_company_params, [originator: user]) + _result = create_company_with_version(@create_company_params, originator: user) new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona") - ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] - result = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options]) + ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] + + result = + create_company_with_version(new_create_company_params, + originator: user, + ecto_options: ecto_options + ) assert Company.count() == 1 assert Version.count() == 2 @@ -1046,5 +1051,4 @@ defmodule PaperTrailTest.StrictModeBangFunctions do defp convert_to_string_map(map) do map |> Jason.encode!() |> Jason.decode!() end - end diff --git a/test/paper_trail/base_test.exs b/test/paper_trail/base_test.exs index 7f03fc9e..bebb065b 100644 --- a/test/paper_trail/base_test.exs +++ b/test/paper_trail/base_test.exs @@ -99,17 +99,25 @@ defmodule PaperTrailTest do test "PaperTrail.insert/2 passes ecto options through (e.g. upsert options)" do user = create_user() - {:ok, _result} = create_company_with_version(@create_company_params, [originator: user]) + {:ok, _result} = create_company_with_version(@create_company_params, originator: user) new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona") - ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] - {:ok, result} = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options]) + ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] + + {:ok, result} = + create_company_with_version(new_create_company_params, + originator: user, + ecto_options: ecto_options + ) assert Company.count() == 1 assert Version.count() == 2 - assert Map.take(serialize(result[:model]), [:name, :city]) == %{name: "Acme LLC", city: "Barcelona"} + assert Map.take(serialize(result[:model]), [:name, :city]) == %{ + name: "Acme LLC", + city: "Barcelona" + } end test "updating a company with originator creates a correct company version" do diff --git a/test/paper_trail/strict_mode_test.exs b/test/paper_trail/strict_mode_test.exs index 8a7dae62..30299d6c 100644 --- a/test/paper_trail/strict_mode_test.exs +++ b/test/paper_trail/strict_mode_test.exs @@ -131,17 +131,25 @@ defmodule PaperTrailStrictModeTest do test "PaperTrail.insert/2 passes ecto options through (e.g. upsert options)" do user = create_user() - {:ok, _result} = create_company_with_version(@create_company_params, [originator: user]) + {:ok, _result} = create_company_with_version(@create_company_params, originator: user) new_create_company_params = @create_company_params |> Map.replace!(:city, "Barcelona") - ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] - {:ok, result} = create_company_with_version(new_create_company_params, [originator: user, ecto_options: ecto_options]) + ecto_options = [on_conflict: {:replace_all_except, ~w{name}a}, conflict_target: :name] + + {:ok, result} = + create_company_with_version(new_create_company_params, + originator: user, + ecto_options: ecto_options + ) assert Company.count() == 1 assert Version.count() == 2 - assert Map.take(serialize(result[:model]), [:name, :city]) == %{name: "Acme LLC", city: "Barcelona"} + assert Map.take(serialize(result[:model]), [:name, :city]) == %{ + name: "Acme LLC", + city: "Barcelona" + } end test "updating a company creates a company version with correct item_changes" do diff --git a/test/support/simple_models.exs b/test/support/simple_models.exs index 2bed06c8..8b1bf402 100644 --- a/test/support/simple_models.exs +++ b/test/support/simple_models.exs @@ -1,4 +1,3 @@ - defmodule LocationType do use Ecto.Type From d544ba197338ce7a7f7b42c920aa73e048922ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Thu, 15 Oct 2020 12:29:59 +0100 Subject: [PATCH 2/2] Enable capture_log to make test output readable --- test/test_helper.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.exs b/test/test_helper.exs index 16fb8ccf..65f68312 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -16,4 +16,4 @@ Code.require_file("test/support/uuid_with_custom_name_models.exs") ExUnit.configure(seed: 0) -ExUnit.start() +ExUnit.start(capture_log: true)