-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathquery_spec.cr
More file actions
253 lines (201 loc) · 6.18 KB
/
query_spec.cr
File metadata and controls
253 lines (201 loc) · 6.18 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
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
require "../pg_spec"
require "../../repository_spec"
require "../models"
describe "Repository(Postgres)#query" do
repo = repo(:postgresql)
# This is John
#
user = uninitialized User
describe "insert" do
context "with a simple model" do
user = User.new(
name: "John",
active: true,
favorite_numbers: [3, 17]
)
user = repo.query(user.insert.returning("*")).first
it "returns instance" do
user.should be_a(User)
user.favorite_numbers.should eq [3, 17]
end
end
end
# And this is John's referrer, Jake
#
referrer = repo.query(User.insert(name: "Jake").returning(User)).first
describe "update" do
context "with attributes" do
previous_uuid = user.uuid
# We're updating John's balance and activity status
#
query = User.query
.update
.set(active: false)
.set(balance: 100.0_f32, updated_at: nil)
.set(favorite_numbers: [11])
.where(uuid: user.uuid.not_nil!)
.returning(:uuid, :balance)
user = repo.query(query).first
it "preloads attributes" do
user.uuid.should eq previous_uuid
user.balance.should eq 100.0
end
end
context "with direct references" do
# We're setting John's referrer to Jake
#
query = User.update.set(referrer: referrer).where(uuid: user.uuid.not_nil!).returning(User)
user = repo.query(query).first
it "preloads references" do
user.referrer.not_nil!.uuid.should be_a(UUID)
end
end
context "in a transaction" do
db = repo.db
query = User
.update
.set(favorite_numbers: [17])
.where(uuid: user.uuid.not_nil!)
.returning(:favorite_numbers)
repo.db.transaction do |tx|
repo.db = tx
repo.query(query)
ensure
repo.db = db
end
it "updates the user" do
query = User.query
.select(:favorite_numbers, :uuid)
.where(uuid: user.uuid.not_nil!)
updated_user = repo.query(query).first
updated_user.favorite_numbers.should eq([17])
end
context "raising an error" do
error_was_raised = false
query = User
.update
.set(favorite_numbers: [13])
.where(uuid: user.uuid.not_nil!)
.returning(:favorite_numbers)
begin
repo.db.transaction do |tx|
repo.db = tx
repo.query(query)
raise "error"
ensure
repo.db = db
end
rescue
error_was_raised = true
end
it "raises the error" do
error_was_raised.should be_truthy
end
it "rolls back the transaction" do
query = User.query
.select(:favorite_numbers, :uuid)
.where(uuid: user.uuid.not_nil!)
updated_user = repo.query(query).first
updated_user.favorite_numbers.should_not eq([13])
end
end
end
end
describe "where" do
user = repo.query(User.where(uuid: user.uuid.not_nil!).and_where(balance: 100.0_f32)).first
it "returns a User instance" do
user.name.should eq "John"
end
context "with direct non-enumerable join" do
query = User.query
.select(:name, :uuid)
.where(uuid: user.uuid.not_nil!)
.join referrer: true do |q|
q.select("referrer.*")
end
user = repo.query(query).first
it "returns a User instance" do
user.name.should eq "John"
end
it "preloads direct references" do
user.referrer.not_nil!.name.should eq "Jake"
end
end
end
tag = repo.query(Tag.insert(content: "foo").returning("*")).first
post = uninitialized Post
describe "insert" do
context "with complex model" do
post = repo.query(Post.query
.insert(author: user, tags: [tag], content: "Blah-blah", cover: "foo".to_slice, meta: Post::Meta.new({"foo" => "bar"}))
.returning(Post)
).first
it "returns model instance" do
post.should be_a(Post)
post.meta!.meta.should eq({"foo" => "bar"})
post.cover!.should eq("foo".to_slice)
end
it "preloads direct non-enumerable references" do
post.author.not_nil!.uuid.should eq user.uuid
post.author.not_nil!.name.should be_nil
end
it "preloads direct enumerable references" do
post.tags.not_nil!.size.should eq 1
post.tags.not_nil!.first.id.should eq tag.id
post.tags.not_nil!.first.content.should be_nil
end
end
context "multiple instances" do
posts = [
Post.new(author: user, tags: [tag], content: "Foo"),
Post.new(author: user, content: "Bar"),
]
posts = repo.query(posts.insert.returning(Post))
posts.first.content!.should eq "Foo"
end
end
new_user = repo.query(User.insert(name: "James").returning(User)).first
describe "update" do
context "with complex reference updates" do
changeset = post.changeset
changeset.update(tags: [] of Tag, editor: new_user)
changeset.update(created_at: Time.now)
post = repo.query(post.update(changeset).returning("*")).first
it "returns model instance" do
post.should be_a(Post)
end
it "preloads direct non-enumerable references" do
post.editor.not_nil!.uuid.should eq new_user.uuid
end
it "preloads direct enumerable references" do
post.tags.not_nil!.size.should eq 0
end
end
end
describe "where" do
context "with foreign non-enumerable join" do
post = repo.query(Post.query
.where(id: post.id.not_nil!)
.and("cardinality(tag_ids) = ?", 0)
.join author: true do |q|
q.select("author.*")
end
.join editor: true do |q|
q.select(:uuid)
end
).first
it "returns model instance" do
post.should be_a(Post)
end
it "preloads references" do
post.author.not_nil!.uuid.should eq user.uuid
post.editor.not_nil!.uuid.should eq new_user.uuid
end
end
end
describe "#delete" do
it do
repo.query(post.delete.returning(:id)).first.id!.should eq post.id
end
end
end