Skip to content

Commit

Permalink
awesome update test case added
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jul 15, 2016
1 parent 2973269 commit bcd30f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"db_connection": {:hex, :db_connection, "1.0.0-rc.3", "d9ceb670fe300271140af46d357b669983cd16bc0d01206d7d3222dde56cf038", [:mix], [{:sbroker, "~> 1.0.0-beta.3", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"ecto": {:hex, :ecto, "2.0.2", "b02331c1f20bbe944dbd33c8ecd8f1ccffecc02e344c4471a891baf3a25f5406", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.11.2", [hex: :postgrex, optional: true]}, {:db_connection, "~> 1.0-rc.2", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}]},
"paper_trail": {:hex, :paper_trail, "0.0.5", "720e15788bd6c5c40323ae54ded2fa80453e7e8668bc8f8044e01af2c3e9079d", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"paper_trail": {:hex, :paper_trail, "0.0.6", "93cd76de83a837a32c7a60181da2dc2db625a596aeed55f990ae511bb45eb496", [:mix], [{:poison, "2.1.0", [hex: :poison, optional: false]}, {:ecto, "~> 2.0.2", [hex: :ecto, optional: false]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: false]}]},
"poison": {:hex, :poison, "2.1.0", "f583218ced822675e484648fa26c933d621373f01c6c76bd00005d7bd4b82e27", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}}
47 changes: 46 additions & 1 deletion example/test/company_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,53 @@ defmodule CompanyTest do
}
end

test "updating a company creates a company version with correct attributes" do
test "updating a company creates a company version with correct item_changes" do
old_company = first(Company, :id) |> Repo.one
new_company = Company.changeset(old_company, %{
city: "Hong Kong",
website: "http://www.acme.com",
facebook: "acme.llc"
})

new_company |> inspect |> IO.puts

{:ok, result} = PaperTrail.update(new_company)

company_count = Repo.all(
from company in Company,
select: count(company.id)
)

company = result[:model] |> Map.drop([:__meta__, :__struct__, :inserted_at, :updated_at, :id])

version_count = Repo.all(
from version in PaperTrail.Version,
select: count(version.id)
)

version = result[:version] |> Map.drop([:__meta__, :__struct__, :inserted_at])

assert company_count == [1]
assert version_count == [2]

assert company == %{
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]) == %{
event: "update",
item_type: "Company",
item_id: Repo.one(first(Company, :id)).id,
item_changes: %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"},
meta: nil
}
end

test "deleting a company creates a company version with correct attributes" do
Expand Down
1 change: 1 addition & 0 deletions example/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ExUnit.configure seed: 0
ExUnit.start
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule PaperTrail.Mixfile do

defp description do
"""
Track and record all the changes in your database with PaperTrail.
Track and record all the changes in your database. Revert back to anytime in history.
"""
end

Expand Down

0 comments on commit bcd30f7

Please sign in to comment.