Skip to content

Commit

Permalink
tests updated for originator params feature
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 16, 2017
1 parent 9d9ba81 commit 7e243a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ user = create_user()
# all these set originator_id's for the version records
PaperTrail.insert(changeset, originator: user)
{:ok, result} = PaperTrail.update(edit_changeset, originator: user)
# or you can use :user instead of :originator if this is your config:
# or you can use :user in the params instead of :originator if this is your config:
# paper_trail originator: [name: :user, model: YourApplication.User]
{:ok, result} = PaperTrail.update(edit_changeset, user: user)
result[:version] |> Repo.preload(:user) |> Map.get(:user) # we can access the user who made the change from the version thanks to originator relationships!
Expand Down Expand Up @@ -278,7 +278,7 @@ edited_company = Company.changeset(company, %{name: "Acme LLC"})
# or even with an originator:
user = create_user()
deleted_company = Company.changeset(edited_company, %{})
|> PaperTrail.delete(origin: "worker:github", originator: user.id, meta: %{slug: "acme-llc", important: true})
|> PaperTrail.delete(origin: "worker:github", originator: user, meta: %{slug: "acme-llc", important: true})
```

## Suggestions
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PaperTrail.Mixfile do
def project do
[
app: :paper_trail,
version: "0.6.3",
version: "0.7.0",
elixir: "~> 1.4",
description: description(),
build_embedded: Mix.env == :prod,
Expand Down
4 changes: 2 additions & 2 deletions test/paper_trail_strict_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule PaperTrailStrictModeTest do

test "creating a company creates a company version with correct attributes" do
user = create_user()
{:ok, result} = create_company_with_version(@create_company_params, originator: user)
{:ok, result} = create_company_with_version(@create_company_params, user: user)

company_count = Company.count()
version_count = Version.count()
Expand Down Expand Up @@ -138,7 +138,7 @@ defmodule PaperTrailStrictModeTest do
{:ok, insert_company_result} = create_company_with_version()
{:ok, update_company_result} = update_company_with_version(insert_company_result[:model])
company_before_deletion = first(Company, :id) |> @repo.one |> serialize
{:ok, result} = PaperTrail.delete(update_company_result[:model], originator: user)
{:ok, result} = PaperTrail.delete(update_company_result[:model], user: user)

company_count = Company.count()
version_count = Version.count()
Expand Down
42 changes: 40 additions & 2 deletions test/paper_trail_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# test one with user:, one with originator
defmodule PaperTrailTest do
use ExUnit.Case

Expand Down Expand Up @@ -73,7 +72,46 @@ defmodule PaperTrailTest do
assert result == ecto_result
end

test "updating a company creates a company version with correct item_changes" do
test "updating a company with originator creates a correct company version" do
user = create_user()
{:ok, insert_result} = create_company_with_version()
{:ok, result} = update_company_with_version(
insert_result[:model], @update_company_params, user: user
)

company_count = Company.count()
version_count = Version.count()

company = result[:model] |> serialize
version = result[:version] |> serialize

assert Map.keys(result) == [:model, :version]
assert company_count == 1
assert version_count == 2
assert Map.drop(company, [:id, :inserted_at, :updated_at]) == %{
name: "Acme LLC",
is_active: true,
city: "Hong Kong",
website: "http://www.acme.com",
address: nil,
facebook: "acme.llc",
twitter: nil,
founded_in: nil
}
assert Map.drop(version, [:id, :inserted_at]) == %{
event: "update",
item_type: "SimpleCompany",
item_id: company.id,
item_changes: %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"},
originator_id: user.id,
origin: nil,
meta: nil
}
assert company == first(Company, :id) |> @repo.one |> serialize
end


test "updating a company with originator[user] creates a correct company version" do
user = create_user()
{:ok, insert_result} = create_company_with_version()
{:ok, result} = update_company_with_version(
Expand Down

0 comments on commit 7e243a0

Please sign in to comment.