-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathrepo_test.exs
273 lines (230 loc) · 7.03 KB
/
repo_test.exs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
defmodule Northwind.RepoTest do
use ExUnit.Case
alias Northwind.{Importer, Model, Repo}
import Ecto.Query
setup do
repo_id = __MODULE__
repo_start = {Northwind.Repo, :start_link, []}
{:ok, _} = start_supervised(%{id: repo_id, start: repo_start})
:ok = Importer.perform()
end
test "List All" do
Repo.all(Model.Employee)
end
test "Insert / Delete Employee" do
changes = %{first_name: "Evadne", employee_id: 1024}
changeset = Model.Employee.changeset(changes)
{:ok, employee} = Repo.insert(changeset)
Repo.delete(employee)
end
test "Insert Employees" do
changes = [%{first_name: "Fred", employee_id: 100}, %{first_name: "Steven", employee_id: 200}]
nil = Repo.get(Model.Employee, 100)
Repo.insert_all(Model.Employee, changes)
%{first_name: "Fred"} = Repo.get(Model.Employee, 100)
end
test "List all Employees Again" do
Repo.all(Model.Employee)
end
test "Select with Bound ID" do
Repo.get(Model.Employee, 2)
end
test "Where" do
Model.Employee
|> where([x], x.title == "Vice President Sales" and x.first_name == "Andrew")
|> Repo.all()
end
test "Where In None" do
employee_ids = []
Model.Employee
|> where([x], x.employee_id in ^employee_ids)
|> select([x], x.employee_id)
|> Repo.all()
|> Enum.sort()
|> (&assert(&1 == employee_ids)).()
end
test "Where In One" do
employee_ids = [3]
Model.Employee
|> where([x], x.employee_id in ^employee_ids)
|> select([x], x.employee_id)
|> Repo.all()
|> Enum.sort()
|> (&assert(&1 == employee_ids)).()
end
test "Where In Multiple" do
employee_ids = [3, 5, 7]
Model.Employee
|> where([x], x.employee_id in ^employee_ids)
|> select([x], x.employee_id)
|> Repo.all()
|> Enum.sort()
|> (&assert(&1 == employee_ids)).()
end
test "Where In Nested" do
employee_ids = [3, 5, 7]
employee_first_names = ["Janet"]
Model.Employee
|> where([x], x.employee_id in ^employee_ids)
|> where([x], x.first_name in ^employee_first_names)
|> select([x], x.employee_id)
|> Repo.all()
|> Enum.sort()
|> (&assert(&1 == [3])).()
end
test "Select Where" do
Model.Employee
|> where([x], x.title == "Vice President Sales" and x.first_name == "Andrew")
|> select([x], x.last_name)
|> Repo.all()
end
test "Select Where Is Nil" do
query = Model.Employee |> where([x], is_nil(x.title))
assert [] = Repo.all(query)
changes = %{first_name: "Ghost", employee_id: 4096}
changeset = Model.Employee.changeset(changes)
{:ok, %{employee_id: employee_id} = employee} = Repo.insert(changeset)
assert [%{employee_id: ^employee_id}] = Repo.all(query)
Repo.delete(employee)
end
test "Select / Update" do
Model.Employee
|> where([x], x.title == "Vice President Sales")
|> Repo.all()
|> List.first()
|> Model.Employee.changeset(%{title: "SVP Sales"})
|> Repo.update()
end
test "Assoc Traversal" do
Model.Employee
|> Repo.get(5)
|> Ecto.assoc(:reports)
|> Repo.all()
|> List.first()
|> Ecto.assoc(:manager)
|> Repo.one()
|> Ecto.assoc(:reports)
|> Repo.all()
end
test "Promote to Customer" do
Model.Employee
|> where([x], x.title == "Vice President Sales" and x.first_name == "Andrew")
|> Repo.one()
|> Model.Employee.changeset(%{title: "Customer"})
|> Repo.update()
end
test "Stream Employees" do
Model.Employee
|> Repo.stream()
|> Enum.to_list()
end
test "Order / Shipper / Orders Preloading" do
Model.Order
|> Repo.all()
|> Repo.preload(shipper: :orders)
end
test "Order / Shipper + Employee Preloading" do
Model.Order
|> Repo.all()
|> Repo.preload([[shipper: :orders], :employee, :customer], in_parallel: true)
end
test "Order / Shipper / Orders Preloading before all()" do
Model.Order
|> preload([_], shipper: :orders)
|> Repo.all()
end
test "Order By Desc company_name, Asc phone" do
sorted_etso =
Model.Shipper
|> order_by([x], desc: x.company_name, asc: x.phone)
|> Repo.all()
sorted_code =
Model.Shipper
|> Repo.all()
|> Enum.sort_by(& &1.company_name, :desc)
|> Enum.sort_by(& &1.phone)
assert sorted_etso == sorted_code
end
test "Delete All" do
assert Repo.delete_all(Model.Employee)
assert [] == Repo.all(Model.Employee)
end
test "Delete Where" do
query = Model.Employee |> where([e], e.employee_id in [1, 5])
assert [a, b] = Repo.all(query)
assert {2, nil} = Repo.delete_all(query)
assert [] == Repo.all(query)
refute [] == Repo.all(Model.Employee)
end
test "Delete Where Select" do
query = Model.Employee |> where([e], e.employee_id in [1, 5])
assert [a, b] = Repo.all(query)
assert {2, list} = Repo.delete_all(query |> select([e], {e, e.employee_id}))
assert is_list(list)
assert Enum.any?(list, &(elem(&1, 1) == 1))
assert Enum.any?(list, &(elem(&1, 1) == 5))
assert [] = Repo.all(query)
refute [] == Repo.all(Model.Employee)
end
describe "With JSON Extract Paths" do
test "using literal value" do
Model.Employee
|> where([e], e.metadata["twitter"] == "@andrew_fuller")
|> Repo.one!()
end
test "using brackets" do
Model.Employee
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> Repo.one!()
end
test "with variable pinning" do
field = "passport"
Model.Employee
|> where([e], e.metadata["documents"][^field] == "verified")
|> Repo.one!()
Model.Employee
|> select([e], json_extract_path(e.metadata, ["documents", "passport"]))
|> Repo.all()
|> Enum.any?(&(&1 == "verified"))
|> assert()
end
test "with arrays" do
Model.Employee
|> select([e], json_extract_path(e.metadata, ["photos", 0, "url"]))
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> Repo.one!()
|> (&(&1 == "https://example.com/a")).()
|> assert()
Model.Employee
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> select([e], e.metadata["photos"][0]["url"])
|> Repo.one!()
|> (&(&1 == "https://example.com/a")).()
|> assert()
Model.Employee
|> select([e], e.metadata["photos"][1]["url"])
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> Repo.one!()
|> (&(&1 == "https://example.com/b")).()
|> assert()
end
test "with where/in" do
Model.Employee
|> where([e], e.metadata["documents"]["passport"] in ~w(verified))
|> select([e], e.metadata["photos"][1]["url"])
|> Repo.one!()
|> (&(&1 == "https://example.com/b")).()
|> assert()
end
test "in deletion" do
Model.Employee
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> Repo.delete_all()
assert_raise Ecto.NoResultsError, fn ->
Model.Employee
|> where([e], e.metadata["documents"]["passport"] == "verified")
|> Repo.one!()
end
end
end
end