Skip to content

Commit

Permalink
better structure for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 18, 2017
1 parent 9b01c35 commit cf30124
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
22 changes: 15 additions & 7 deletions lib/paper_trail.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# document hashbangs in docs, README, write tests for both normal mode and strict mode
# document bang function in README, write tests for both normal mode and strict mode
defmodule PaperTrail do
import Ecto.Changeset

Expand Down Expand Up @@ -100,9 +100,11 @@ defmodule PaperTrail do
end
end

# add doc
@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]) do
{:ok, _model} = @repo.transaction(fn ->
@repo.transaction(fn ->
case @client.strict_mode() do
true ->
version_id = get_sequence_id("versions") + 1
Expand Down Expand Up @@ -179,8 +181,11 @@ defmodule PaperTrail do
end
end

@doc """
Same as update/2 but returns only the model struct or raises if the changeset is invalid.
"""
def update!(changeset, options \\ [origin: nil, meta: nil, originator: nil]) do
{:ok, _model} = @repo.transaction(fn ->
@repo.transaction(fn ->
case @client.strict_mode() do
true ->
version_data = changeset.data |> Map.merge(%{
Expand All @@ -198,7 +203,7 @@ defmodule PaperTrail do
_ ->
model = @repo.update!(changeset)
version_struct = make_version_struct(%{event: "update"}, changeset, options)
@repo.insert(version_struct)
@repo.insert!(version_struct)
end
model
end) |> elem(1)
Expand All @@ -222,11 +227,14 @@ defmodule PaperTrail do
end
end

@doc """
Same as delete/2 but returns only the model struct or raises if the changeset is invalid.
"""
def delete!(struct, options \\ [origin: nil, meta: nil, originator: nil]) do
{:ok, _model} = @repo.transaction(fn ->
@repo.transaction(fn ->
model = @repo.delete!(struct)
version_struct = make_version_struct(%{event: "delete"}, struct, options)
@repo.insert(version_struct)
@repo.insert!(version_struct)
end) |> elem(1)
end

Expand Down
35 changes: 35 additions & 0 deletions test/paper_trail/bang_functions_simple_mode_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule PaperTrailTest.SimpleModeBangFunctions do
use ExUnit.Case

import Ecto.Query

alias PaperTrail.Version
alias SimpleCompany, as: Company
alias SimplePerson, as: Person

@repo PaperTrail.RepoClient.repo
@create_company_params %{name: "Acme LLC", is_active: true, city: "Greenwich"}
@update_company_params %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"}

doctest PaperTrail

setup_all do
Application.put_env(:paper_trail, :strict_mode, false)
:ok
end

setup do
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
on_exit fn ->
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
end
:ok
end



end
34 changes: 34 additions & 0 deletions test/paper_trail/bang_functions_strict_mode_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule PaperTrailTest.StrictModeBangFunctions do
use ExUnit.Case

import Ecto.Query

alias PaperTrail.Version
alias StrictCompany, as: Company
alias StrictPerson, as: Person

@repo PaperTrail.RepoClient.repo
@create_company_params %{name: "Acme LLC", is_active: true, city: "Greenwich"}
@update_company_params %{city: "Hong Kong", website: "http://www.acme.com", facebook: "acme.llc"}

doctest PaperTrail

setup_all do
Application.put_env(:paper_trail, :strict_mode, true)
:ok
end

setup do
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
on_exit fn ->
@repo.delete_all(Person)
@repo.delete_all(Company)
@repo.delete_all(Version)
end
:ok
end


end
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# TODO: strict_mode(check the current_version_id changes on next versions), Test error cases, producer_id, PaperTrail.insert_all

Mix.Task.run "ecto.create", ~w(-r PaperTrail.Repo)
Mix.Task.run "ecto.migrate", ~w(-r PaperTrail.Repo)

PaperTrail.Repo.start_link

Code.require_file("test/helpers/simple_model_definitions.exs")
Code.require_file("test/helpers/strict_model_definitions.exs")
Code.require_file("test/support/simple_models.exs")
Code.require_file("test/support/strict_models.exs")

ExUnit.configure seed: 0

Expand Down

0 comments on commit cf30124

Please sign in to comment.