Skip to content

Commit 036af34

Browse files
committed
Fix dialyzer issues and add tests
1 parent 2978771 commit 036af34

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/upload/changeset.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ defmodule Upload.Changeset do
2424
@type error :: binary | Ecto.Changeset.error()
2525
@type validation :: (any -> [error])
2626
@type size :: {number, :byte | :kilobyte | :megabyte | :gigabyte | :terabyte}
27+
@type key_function :: (changeset -> String.t())
2728

28-
@type cast_opts :: [{:invalid_message, binary}]
29+
@type cast_opts :: [{:invalid_message, binary} | {:key_function, key_function}]
2930
@type size_opts :: [{:less_than, size} | {:message, binary}]
3031
@type type_opts :: [{:allow, [binary]} | {:forbid, [binary]} | {:message, binary}]
3132

lib/upload/multi.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ defmodule Upload.Multi do
268268
when is_function(fun, 1) and is_function(transform_fn, 3) do
269269
repo = Upload.Config.repo()
270270

271-
Multi.run(multi, opts[:multi_name] || :create_variants, fn ctx ->
271+
Multi.run(multi, opts[:multi_name] || :create_variants, fn _repo, ctx ->
272272
case fun.(ctx) do
273273
nil ->
274274
{:ok, nil}
@@ -322,7 +322,7 @@ defmodule Upload.Multi do
322322
when is_function(fun, 1) and is_function(transform_fn, 3) do
323323
repo = Upload.Config.repo()
324324

325-
Multi.run(multi, opts[:multi_name] || :create_variants, fn ctx ->
325+
Multi.run(multi, opts[:multi_name] || :create_variants, fn _repo, ctx ->
326326
case fun.(ctx) do
327327
nil ->
328328
{:ok, nil}

test/upload/multi_test.exs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Upload.MultiTest do
22
use Upload.DataCase
3+
use Upload.Testing
34

45
alias Upload.Storage
56
alias Upload.Test.Person
@@ -13,7 +14,7 @@ defmodule Upload.MultiTest do
1314
@upload %Plug.Upload{path: @path, filename: "image.jpg"}
1415

1516
describe "handle_changes" do
16-
test "can validate changes" do
17+
test "will validate changes" do
1718
changeset = Person.changeset(%Person{}, %{avatar: "test/fixtures/test.txt"})
1819

1920
{:error, _, changeset, _} =
@@ -31,6 +32,48 @@ defmodule Upload.MultiTest do
3132
end
3233
end
3334

35+
describe "create_variant" do
36+
test "will create a variant" do
37+
changeset = Person.changeset(%Person{}, %{avatar: @upload})
38+
39+
{:ok, _} =
40+
Ecto.Multi.new()
41+
|> Ecto.Multi.insert(:person, changeset)
42+
|> Upload.Multi.handle_changes(:upload_avatar, :person, changeset, :avatar,
43+
key_function: &key_function/1
44+
)
45+
|> Upload.Multi.create_variant(
46+
fn ctx -> ctx.upload_avatar.avatar end,
47+
:small,
48+
&small_transform/3
49+
)
50+
|> Repo.transaction()
51+
52+
person = Repo.one(Person) |> Repo.preload(avatar: :variants)
53+
54+
variant = List.first(person.avatar.variants)
55+
56+
assert variant.variant == "small"
57+
end
58+
59+
test "handles when there are no changes" do
60+
changeset = Person.changeset(%Person{}, %{})
61+
62+
{:ok, _} =
63+
Ecto.Multi.new()
64+
|> Ecto.Multi.insert(:person, changeset)
65+
|> Upload.Multi.handle_changes(:upload_avatar, :person, changeset, :avatar,
66+
key_function: &key_function/1
67+
)
68+
|> Upload.Multi.create_variant(
69+
fn ctx -> ctx.upload_avatar.avatar end,
70+
:small,
71+
&small_transform/3
72+
)
73+
|> Repo.transaction()
74+
end
75+
end
76+
3477
test "upload/3" do
3578
assert {:ok, person} = insert_person(%{avatar: @upload})
3679
assert person.avatar_id

0 commit comments

Comments
 (0)