-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathschema_test.exs
More file actions
190 lines (162 loc) · 6.46 KB
/
schema_test.exs
File metadata and controls
190 lines (162 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
defmodule WaffleTest.Ecto.Schema do
use ExUnit.Case, async: false
import Mock
import ExUnit.CaptureLog
defmodule TestUser do
use Ecto.Schema
import Ecto.Changeset
use Waffle.Ecto.Schema
schema "users" do
field(:first_name, :string)
field(:avatar, DummyDefinition.Type)
end
def changeset(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
|> cast_attachments(params, ~w(avatar)a)
|> validate_required(:avatar)
end
def path_changeset(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
|> cast_attachments(params, ~w(avatar)a, allow_paths: true)
|> validate_required(:avatar)
end
def url_changeset(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
|> cast_attachments(params, ~w(avatar)a, allow_urls: true)
|> validate_required(:avatar)
end
def changeset2(user, params \\ :invalid) do
user
|> cast(params, ~w(first_name)a)
|> cast_attachments(params, ~w(avatar)a)
end
end
def build_upload(path) do
%{__struct__: Plug.Upload, path: path, filename: Path.basename(path)}
end
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]}]
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
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
upload = build_upload("/path/to/my/file.png")
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")
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")
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")
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")})
changeset = TestUser.changeset2(changeset, %{"avatar" => nil})
assert nil == Ecto.Changeset.get_field(changeset, :avatar)
end
test_with_mock "casting empty attachments", DummyDefinition,
store: fn {"", %TestUser{}} -> {:ok, ""} end do
changeset = TestUser.path_changeset(%TestUser{}, %{"avatar" => ""})
assert nil == Ecto.Changeset.get_field(changeset, :avatar)
assert not called(DummyDefinition.store({"", %TestUser{}}))
end
test_with_mock "allow_paths => true", DummyDefinition,
store: fn {"/path/to/my/file.png", %TestUser{}} -> {:ok, "file.png"} end do
TestUser.path_changeset(%TestUser{}, %{"avatar" => " /path/to/my/file.png "})
assert called(DummyDefinition.store({"/path/to/my/file.png", %TestUser{}}))
end
test_with_mock "allow_urls => true", DummyDefinition,
store: fn {"http://external.url/file.png", %TestUser{}} ->
{:ok, "file.png"}
end do
TestUser.url_changeset(%TestUser{}, %{"avatar" => " http://external.url/file.png "})
assert called(DummyDefinition.store({"http://external.url/file.png", %TestUser{}}))
end
test_with_mock "allow_urls => true with an invalid URL", DummyDefinition,
store: fn {"/path/to/my/file.png", %TestUser{}} ->
{:ok, "file.png"}
end do
TestUser.url_changeset(%TestUser{}, %{"avatar" => "/path/to/my/file.png"})
assert not called(DummyDefinition.store({"/path/to/my/file.png", %TestUser{}}))
end
test_with_mock "casting binary data struct attachments", DummyDefinition,
store: fn {%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}} ->
{:ok, "file.png"}
end do
TestUser.changeset(%TestUser{}, %{
"avatar" => %{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}
})
assert called(
DummyDefinition.store(
{%{filename: "/path/to/my/file.png", binary: <<1, 2, 3>>}, %TestUser{}}
)
)
end
test_with_mock "casting base64-encoded data struct attachments", DummyDefinition,
store: fn {%{filename: _, binary: _}, %TestUser{}} ->
{:ok, "file.png"}
end do
cs = TestUser.changeset(%TestUser{}, %{
"avatar" => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
})
assert cs.valid?
assert %{avatar: %{file_name: "file.png", updated_at: _}} = cs.changes
end
end