Skip to content

Commit

Permalink
Backport/Merge pull request stavro#103 from dbernheisel/db-ecto-3
Browse files Browse the repository at this point in the history
Fix Ecto 3.0 warnings
 Conflicts:
	mix.exs
	test/schema_test.exs
  • Loading branch information
stavro authored and novaugust committed Apr 1, 2019
1 parent f49207a commit a711eb7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lib/arc_ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Arc.Ecto.Schema do
end

# Cast supports both atom and string keys, ensure we're matching on both.
allowed = Enum.map(allowed, fn key ->
allowed_param_keys = Enum.map(allowed, fn key ->
case key do
key when is_binary(key) -> key
key when is_atom(key) -> Atom.to_string(key)
Expand All @@ -31,7 +31,7 @@ defmodule Arc.Ecto.Schema do
%{} ->
params
|> Arc.Ecto.Schema.convert_params_to_binary
|> Map.take(allowed)
|> Map.take(allowed_param_keys)
|> Enum.reduce([], fn
# Don't wrap nil casts in the scope object
{field, nil}, fields -> [{field, nil} | fields]
Expand Down
30 changes: 17 additions & 13 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ defmodule Arc.Ecto.Mixfile do
@version "0.8.0"

def project do
[app: :arc_ecto,
version: @version,
elixir: "~> 1.0",
deps: deps(),
[
app: :arc_ecto,
version: @version,
elixir: "~> 1.0",
deps: deps(),

# Hex
description: description(),
package: package()]
# Hex
description: description(),
package: package()
]
end

# Configuration for the OTP application
Expand All @@ -28,10 +30,12 @@ defmodule Arc.Ecto.Mixfile do
end

defp package do
[maintainers: ["Sean Stavropoulos"],
licenses: ["Apache 2.0"],
links: %{"GitHub" => "https://github.com/stavro/arc_ecto"},
files: ~w(mix.exs README.md lib)]
[
maintainers: ["Sean Stavropoulos"],
licenses: ["Apache 2.0"],
links: %{"GitHub" => "https://github.com/stavro/arc_ecto"},
files: ~w(mix.exs README.md lib)
]
end

# Dependencies can be Hex packages:
Expand All @@ -45,8 +49,8 @@ defmodule Arc.Ecto.Mixfile do
# Type `mix help deps` for more examples and options
defp deps do
[
{:arc, "~> 0.8.0"},
{:ecto, "~> 2.1"},
{:arc, "~> 0.8.0"},
{:ecto, "~> 2.1 or ~> 3.0"},
{:mock, "~> 0.1.1", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev}
]
Expand Down
87 changes: 64 additions & 23 deletions test/schema_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule ArcTest.Ecto.Schema do
use ExUnit.Case, async: false
import Mock
import ExUnit.CaptureLog

defmodule DummyDefinition do
use Arc.Definition
Expand All @@ -13,8 +14,8 @@ defmodule ArcTest.Ecto.Schema do
use Arc.Ecto.Schema

schema "users" do
field :first_name, :string
field :avatar, DummyDefinition.Type
field(:first_name, :string)
field(:avatar, DummyDefinition.Type)
end

def changeset(user, params \\ :invalid) do
Expand Down Expand Up @@ -53,52 +54,92 @@ defmodule ArcTest.Ecto.Schema do

test "supports :invalid changeset" do
cs = TestUser.changeset(%TestUser{})
assert cs.valid? == false
assert cs.changes == %{}
assert cs.errors == [avatar: {"can't be blank", [validation: :required]}]
assert cs.valid? == false
assert cs.changes == %{}
assert cs.errors == [avatar: {"can't be blank", [validation: :required]}]
end

test_with_mock "cascades storage success into a valid change", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:ok, "file.png"} end] do
test_with_mock "cascades storage success into a valid change", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:ok, "file.png"}
end do
upload = build_upload("/path/to/my/file.png")
cs = TestUser.changeset(%TestUser{}, %{"avatar" => upload})
assert cs.valid?
%{file_name: "file.png", updated_at: _} = cs.changes.avatar
end

test_with_mock "cascades storage error into an error", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:error, :invalid_file} end] do
test_with_mock "cascades storage error into an error", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:error, :invalid_file}
end do
upload = build_upload("/path/to/my/file.png")
cs = TestUser.changeset(%TestUser{}, %{"avatar" => upload})
assert called DummyDefinition.store({upload, %TestUser{}})
assert called(DummyDefinition.store({upload, %TestUser{}}))
assert cs.valid? == false
assert cs.errors == [avatar: {"is invalid", [type: ArcTest.Ecto.Schema.DummyDefinition.Type, validation: :cast]}]

assert cs.errors == [
avatar:
{"is invalid", [type: ArcTest.Ecto.Schema.DummyDefinition.Type, validation: :cast]}
]
end

test_with_mock "converts changeset into schema", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:error, :invalid_file} end] do
test_with_mock "converts changeset into schema", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:error, :invalid_file}
end do
upload = build_upload("/path/to/my/file.png")
TestUser.changeset(%TestUser{}, %{"avatar" => upload})
assert called DummyDefinition.store({upload, %TestUser{}})

capture_log(fn ->
TestUser.changeset(%TestUser{}, %{"avatar" => upload})
assert called(DummyDefinition.store({upload, %TestUser{}}))
end)
end

test_with_mock "applies changes to schema", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:error, :invalid_file} end] do
test_with_mock "applies changes to schema", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:error, :invalid_file}
end do
upload = build_upload("/path/to/my/file.png")
TestUser.changeset(%TestUser{}, %{"avatar" => upload, "first_name" => "test"})
assert called DummyDefinition.store({upload, %TestUser{first_name: "test"}})

capture_log(fn ->
TestUser.changeset(%TestUser{}, %{"avatar" => upload, "first_name" => "test"})
assert called(DummyDefinition.store({upload, %TestUser{first_name: "test"}}))
end)
end

test_with_mock "converts atom keys", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:error, :invalid_file} end] do
test_with_mock "converts atom keys", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:error, :invalid_file}
end do
upload = build_upload("/path/to/my/file.png")
TestUser.changeset(%TestUser{}, %{avatar: upload})
assert called DummyDefinition.store({upload, %TestUser{}})

capture_log(fn ->
TestUser.changeset(%TestUser{}, %{avatar: upload})
assert called(DummyDefinition.store({upload, %TestUser{}}))
end)
end

test_with_mock "casting nil attachments", DummyDefinition, [store: fn({%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"}, %TestUser{}}) -> {:ok, "file.png"} end] do
changeset = TestUser.changeset(%TestUser{}, %{"avatar" => build_upload("/path/to/my/file.png")})
test_with_mock "casting nil attachments", DummyDefinition,
store: fn {%{__struct__: Plug.Upload, path: "/path/to/my/file.png", filename: "file.png"},
%TestUser{}} ->
{:ok, "file.png"}
end do
changeset =
TestUser.changeset(%TestUser{}, %{"avatar" => build_upload("/path/to/my/file.png")})

changeset = TestUser.changeset2(changeset, %{"avatar" => nil})
assert nil == Ecto.Changeset.get_field(changeset, :avatar)
end

test_with_mock "allow_paths => true", DummyDefinition, [store: fn({"/path/to/my/file.png", %TestUser{}}) -> {:ok, "file.png"} end] do
changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}})
test_with_mock "allow_paths => true", DummyDefinition,
store: fn {"/path/to/my/file.png", %TestUser{}} -> {:ok, "file.png"} end do
_changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert called(DummyDefinition.store({"/path/to/my/file.png", %TestUser{}}))
end
end

0 comments on commit a711eb7

Please sign in to comment.