Skip to content

Commit

Permalink
Merge pull request #52 from dwyl/insert-testing
Browse files Browse the repository at this point in the history
Insert testing
  • Loading branch information
RobStallion authored Mar 4, 2019
2 parents 44f96fc + 2b1bdf0 commit 5c801b4
Show file tree
Hide file tree
Showing 21 changed files with 552 additions and 673 deletions.
22 changes: 12 additions & 10 deletions lib/alog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ defmodule Alog do
@impl true
defdelegate storage_down(opts), to: EAP

@impl true
defdelegate structure_dump(default, config), to: EAP

@impl true
defdelegate structure_load(default, config), to: EAP

# overrides insert/6 defined in Ecto.Adapters.SQL
def insert(adapter_meta, %{source: "schema_migrations", prefix: prefix}, params, on_conflict, returning, opts) do
insert_logic(adapter_meta, "schema_migrations", prefix, params, on_conflict, returning, opts)
end

def insert(adapter_meta, %{source: source, prefix: prefix}, params, on_conflict, returning, opts) do
# converts params from a keyword list to a map
params_map = Enum.into(params, %{})
Expand All @@ -47,10 +45,7 @@ defmodule Alog do
|> Map.put(:entry_id, entry_id)
|> Enum.into([])

{kind, conflict_params, _} = on_conflict
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
insert_logic(adapter_meta, source, prefix, params, on_conflict, returning, opts)
end

# I think that this step need to also make sure that the data is not an exact copy.
Expand All @@ -76,4 +71,11 @@ defmodule Alog do
|> Map.put_new(:inserted_at, NaiveDateTime.utc_now())
|> Map.put_new(:updated_at, NaiveDateTime.utc_now())
end

defp insert_logic(adapter_meta, source, prefix, params, on_conflict, returning, opts) do
{kind, conflict_params, _} = on_conflict
{fields, values} = :lists.unzip(params)
sql = @conn.insert(prefix, source, fields, [fields], on_conflict, returning)
Ecto.Adapters.SQL.struct(adapter_meta, @conn, sql, :insert, source, [], values ++ conflict_params, kind, returning, opts)
end
end
15 changes: 0 additions & 15 deletions priv/repo/test_app/migrations/20181015145631_create_users.exs

This file was deleted.

28 changes: 0 additions & 28 deletions priv/repo/test_app/migrations/20181026080544_create_items.exs

This file was deleted.

15 changes: 0 additions & 15 deletions priv/repo/test_app/migrations/20181211161000_unique_index.exs

This file was deleted.

16 changes: 16 additions & 0 deletions priv/repo/test_app/migrations/20190301105228_create_comments.exs
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
54 changes: 27 additions & 27 deletions test/all_test.exs
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
144 changes: 72 additions & 72 deletions test/alog_test.exs
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
62 changes: 31 additions & 31 deletions test/constraint_test.exs
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
Loading

0 comments on commit 5c801b4

Please sign in to comment.