Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ecto 3.0 warnings #103

Merged
merged 3 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reassigned the keys passed in from cast_attachment() which are atoms.

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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Arc.Ecto.Mixfile do
defp deps do
[
{:arc, "~> 0.11.0"},
{:ecto, "~> 2.1"},
{:ecto, "~> 2.1 or ~> 3.0"},
{:mock, "~> 0.1.1", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev}
]
Expand Down
31 changes: 20 additions & 11 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 TestUser do
use Ecto.Schema
Expand Down Expand Up @@ -53,28 +54,36 @@ defmodule ArcTest.Ecto.Schema 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 cs.valid? == false
assert cs.errors == [avatar: {"is invalid", [type: DummyDefinition.Type, validation: :cast]}]
capture_log(fn ->
cs = TestUser.changeset(%TestUser{}, %{"avatar" => upload})
assert called DummyDefinition.store({upload, %TestUser{}})
assert cs.valid? == false
assert cs.errors == [avatar: {"is invalid", [type: DummyDefinition.Type, validation: :cast]}]
end)
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
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
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
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
Expand All @@ -84,7 +93,7 @@ defmodule ArcTest.Ecto.Schema do
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"})
_changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert called DummyDefinition.store({"/path/to/my/file.png", %TestUser{}})
end
end