-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from dwyl/insert-testing
Insert testing
- Loading branch information
Showing
21 changed files
with
552 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
priv/repo/test_app/migrations/20181015145631_create_users.exs
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
priv/repo/test_app/migrations/20181026080544_create_items.exs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
priv/repo/test_app/migrations/20181211161000_unique_index.exs
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
priv/repo/test_app/migrations/20190301105228_create_comments.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Alog.Repo.Migrations.CreateComments do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:comments, primary_key: false) do | ||
# cid & entry_id need to be removed later as they should be handled in execute_ddl I believe | ||
# timestamps are needed in alog but may or may not be in the schema. | ||
add(:cid, :string, primary_key: true) | ||
add(:entry_id, :string) | ||
add(:deleted, :boolean, default: false) | ||
add(:comment, :string) | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
defmodule AlogTest.AllTest do | ||
use Alog.TestApp.DataCase | ||
|
||
alias Alog.TestApp.{User, Helpers} | ||
|
||
describe "all/0:" do | ||
test "succeeds" do | ||
{:ok, _} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
{:ok, _} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
|
||
assert length(User.all()) == 2 | ||
end | ||
|
||
test "does not include old items" do | ||
{:ok, user} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
{:ok, _} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
{:ok, _} = user |> User.changeset(%{postcode: "W2 3EC"}) |> User.update() | ||
|
||
assert length(User.all()) == 2 | ||
end | ||
|
||
test "all return inserted_at original value" do | ||
{:ok, user} = %User{} |> User.changeset(Helpers.user_3_params()) |> User.insert() | ||
{:ok, user_updated} = user |> User.changeset(%{postcode: "W2 3EC"}) |> User.update() | ||
|
||
[user_all] = User.all() | ||
assert user_all.inserted_at == user.inserted_at | ||
assert user_all.postcode == user_updated.postcode | ||
end | ||
end | ||
# alias Alog.TestApp.{User, Helpers} | ||
# | ||
# describe "all/0:" do | ||
# test "succeeds" do | ||
# {:ok, _} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
# {:ok, _} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
# | ||
# assert length(User.all()) == 2 | ||
# end | ||
# | ||
# test "does not include old items" do | ||
# {:ok, user} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
# {:ok, _} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
# {:ok, _} = user |> User.changeset(%{postcode: "W2 3EC"}) |> User.update() | ||
# | ||
# assert length(User.all()) == 2 | ||
# end | ||
# | ||
# test "all return inserted_at original value" do | ||
# {:ok, user} = %User{} |> User.changeset(Helpers.user_3_params()) |> User.insert() | ||
# {:ok, user_updated} = user |> User.changeset(%{postcode: "W2 3EC"}) |> User.update() | ||
# | ||
# [user_all] = User.all() | ||
# assert user_all.inserted_at == user.inserted_at | ||
# assert user_all.postcode == user_updated.postcode | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,74 @@ | ||
defmodule AlogTest do | ||
use Alog.TestApp.DataCase | ||
doctest Alog | ||
|
||
describe "required fields" do | ||
test "schema without delete field raises error" do | ||
assert_raise RuntimeError, fn -> | ||
defmodule NoDeleteSchema do | ||
use Ecto.Schema | ||
use Alog | ||
|
||
schema "bad_schema" do | ||
field(:entry_id, :string) | ||
timestamps() | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "schema without entry_id field raises error" do | ||
assert_raise RuntimeError, fn -> | ||
defmodule NoEntrySchema do | ||
use Ecto.Schema | ||
use Alog | ||
|
||
schema "bad_schema" do | ||
field(:deleted, :boolean, default: false) | ||
timestamps() | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "schema with deleted field of wrong type raises error" do | ||
assert_raise RuntimeError, fn -> | ||
defmodule BadDeletedSchema do | ||
use Ecto.Schema | ||
use Alog | ||
|
||
schema "bad_schema" do | ||
field(:entry_id, :string) | ||
field(:deleted, :string) | ||
timestamps() | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "both required fields do not raise error" do | ||
assert (fn -> | ||
defmodule GoodSchema do | ||
use Ecto.Schema | ||
use Alog | ||
|
||
schema "bad_schema" do | ||
field(:entry_id, :string) | ||
field(:deleted, :boolean, default: false) | ||
timestamps() | ||
end | ||
end | ||
end).() | ||
end | ||
end | ||
|
||
describe "Not compatible with unique index" do | ||
test "Throws error if unique index exists" do | ||
assert_raise RuntimeError, fn -> | ||
%Alog.TestApp.Unique{} | ||
|> Alog.TestApp.Unique.changeset(%{name: "unique item"}) | ||
|> Alog.TestApp.Unique.insert() | ||
end | ||
end | ||
end | ||
# use Alog.TestApp.DataCase | ||
# doctest Alog | ||
# | ||
# describe "required fields" do | ||
# test "schema without delete field raises error" do | ||
# assert_raise RuntimeError, fn -> | ||
# defmodule NoDeleteSchema do | ||
# use Ecto.Schema | ||
# use Alog | ||
# | ||
# schema "bad_schema" do | ||
# field(:entry_id, :string) | ||
# timestamps() | ||
# end | ||
# end | ||
# end | ||
# end | ||
# | ||
# test "schema without entry_id field raises error" do | ||
# assert_raise RuntimeError, fn -> | ||
# defmodule NoEntrySchema do | ||
# use Ecto.Schema | ||
# use Alog | ||
# | ||
# schema "bad_schema" do | ||
# field(:deleted, :boolean, default: false) | ||
# timestamps() | ||
# end | ||
# end | ||
# end | ||
# end | ||
# | ||
# test "schema with deleted field of wrong type raises error" do | ||
# assert_raise RuntimeError, fn -> | ||
# defmodule BadDeletedSchema do | ||
# use Ecto.Schema | ||
# use Alog | ||
# | ||
# schema "bad_schema" do | ||
# field(:entry_id, :string) | ||
# field(:deleted, :string) | ||
# timestamps() | ||
# end | ||
# end | ||
# end | ||
# end | ||
# | ||
# test "both required fields do not raise error" do | ||
# assert (fn -> | ||
# defmodule GoodSchema do | ||
# use Ecto.Schema | ||
# use Alog | ||
# | ||
# schema "bad_schema" do | ||
# field(:entry_id, :string) | ||
# field(:deleted, :boolean, default: false) | ||
# timestamps() | ||
# end | ||
# end | ||
# end).() | ||
# end | ||
# end | ||
# | ||
# describe "Not compatible with unique index" do | ||
# test "Throws error if unique index exists" do | ||
# assert_raise RuntimeError, fn -> | ||
# %Alog.TestApp.Unique{} | ||
# |> Alog.TestApp.Unique.changeset(%{name: "unique item"}) | ||
# |> Alog.TestApp.Unique.insert() | ||
# end | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
defmodule AlogTest.ConstraintTest do | ||
use Alog.TestApp.DataCase | ||
|
||
alias Alog.TestApp.{User, Helpers} | ||
|
||
describe "apply_constraints/1:" do | ||
test "returns error if not unique on insert" do | ||
{:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
|
||
assert {:error, user_2} = | ||
%User{} | ||
|> User.changeset( | ||
Helpers.user_2_params() | ||
|> Map.merge(%{username: user_1.username}) | ||
) | ||
|> User.insert() | ||
|
||
assert user_2.errors == [username: {"has already been taken", []}] | ||
end | ||
|
||
test "returns error if not unique on update" do | ||
{:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
{:ok, user_2} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
|
||
assert {:error, user_2} = | ||
user_2 | ||
|> User.changeset(%{username: user_1.username}) | ||
|> User.update() | ||
|
||
assert user_2.errors == [username: {"has already been taken", []}] | ||
end | ||
end | ||
# use Alog.TestApp.DataCase | ||
# | ||
# alias Alog.TestApp.{User, Helpers} | ||
# | ||
# describe "apply_constraints/1:" do | ||
# test "returns error if not unique on insert" do | ||
# {:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
# | ||
# assert {:error, user_2} = | ||
# %User{} | ||
# |> User.changeset( | ||
# Helpers.user_2_params() | ||
# |> Map.merge(%{username: user_1.username}) | ||
# ) | ||
# |> User.insert() | ||
# | ||
# assert user_2.errors == [username: {"has already been taken", []}] | ||
# end | ||
# | ||
# test "returns error if not unique on update" do | ||
# {:ok, user_1} = %User{} |> User.changeset(Helpers.user_1_params()) |> User.insert() | ||
# {:ok, user_2} = %User{} |> User.changeset(Helpers.user_2_params()) |> User.insert() | ||
# | ||
# assert {:error, user_2} = | ||
# user_2 | ||
# |> User.changeset(%{username: user_1.username}) | ||
# |> User.update() | ||
# | ||
# assert user_2.errors == [username: {"has already been taken", []}] | ||
# end | ||
# end | ||
end |
Oops, something went wrong.