/
orm_spec.rb
422 lines (357 loc) · 13.4 KB
/
orm_spec.rb
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# encoding: utf-8
require File.join(File.dirname(__FILE__), "../spec_helper.rb")
describe Her::Model::ORM do
context "mapping data to Ruby objects" do
before do
api = Her::API.new
api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
stub.get("/users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
stub.get("/admin_users") { |env| [200, {}, [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }].to_json] }
end
end
spawn_model "Foo::User" do
uses_api api
end
spawn_model "Foo::AdminUser" do
uses_api api
end
end
it "maps a single resource to a Ruby object" do
@user = Foo::User.find(1)
@user.id.should == 1
@user.name.should == "Tobias Fünke"
end
it "maps a collection of resources to an array of Ruby objects" do
@users = Foo::User.all
@users.length.should == 2
@users.first.name.should == "Tobias Fünke"
@users = Foo::AdminUser.all
@users.length.should == 2
@users.first.name.should == "Tobias Fünke"
end
it "handles new resource" do
@new_user = Foo::User.new(:fullname => "Tobias Fünke")
@new_user.new?.should be_true
@new_user.fullname.should == "Tobias Fünke"
@existing_user = Foo::User.find(1)
@existing_user.new?.should be_false
end
it "handles method missing for getter" do
@new_user = Foo::User.new(:fullname => 'Mayonegg')
lambda { @new_user.unknown_method_for_a_user }.should raise_error(NoMethodError)
expect { @new_user.fullname }.to_not raise_error(NoMethodError)
end
it "handles method missing for setter" do
@new_user = Foo::User.new
expect { @new_user.fullname = "Tobias Fünke" }.to_not raise_error(NoMethodError)
end
it "handles method missing for query" do
@new_user = Foo::User.new
expect { @new_user.fullname? }.to_not raise_error(NoMethodError)
end
it "handles respond_to for getter" do
@new_user = Foo::User.new(:fullname => 'Mayonegg')
@new_user.should_not respond_to(:unknown_method_for_a_user)
@new_user.should respond_to(:fullname)
end
it "handles respond_to for setter" do
@new_user = Foo::User.new
@new_user.should respond_to(:fullname=)
end
it "handles respond_to for query" do
@new_user = Foo::User.new
@new_user.should respond_to(:fullname?)
end
end
context "mapping data, metadata and error data to Ruby objects" do
before do
api = Her::API.new
api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::SecondLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users") { |env| [200, {}, { :data => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }], :metadata => { :total_pages => 10, :next_page => 2 }, :errors => ["Oh", "My", "God"] }.to_json] }
stub.post("/users") { |env| [200, {}, { :data => { :name => "George Michael Bluth" }, :metadata => { :foo => "bar" }, :errors => ["Yes", "Sir"] }.to_json] }
end
end
spawn_model :User do
uses_api api
end
end
it "handles metadata on a collection" do
@users = User.all
@users.metadata[:total_pages].should == 10
end
it "handles error data on a collection" do
@users = User.all
@users.errors.length.should == 3
end
it "handles metadata on a resource" do
@user = User.create(:name => "George Michael Bluth")
@user.metadata[:foo].should == "bar"
end
it "handles error data on a resource" do
@user = User.create(:name => "George Michael Bluth")
@user.errors.should == ["Yes", "Sir"]
@user.should be_invalid
end
end
context "defining custom getters and setters" do
before do
api = Her::API.new
api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :friends => ["Maeby", "GOB", "Anne"] }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :id => 1, :organization => true }.to_json] }
end
end
spawn_model :User do
uses_api api
belongs_to :organization
def friends=(val)
val = val.gsub("\r", "").split("\n").map { |friend| friend.gsub(/^\s*\*\s*/, "") } if val and val.is_a?(String)
@data[:friends] = val
end
def friends
@data[:friends].map { |friend| "* #{friend}" }.join("\n")
end
# Why would anybody want to do this? I don’t know.
def organization=(organization)
@data[:organization] = { :foo => :bar }
end
end
end
it "handles custom setters" do
@user = User.find(1)
@user.friends.should == "* Maeby\n* GOB\n* Anne"
@user.instance_eval do
@data[:friends] = ["Maeby", "GOB", "Anne"]
end
end
it "handles custom setters with relationships" do
@user = User.find(2)
@user.organization.should == { :foo => :bar }
end
it "handles custom getters" do
@user = User.new
@user.friends = "* George\n* Oscar\n* Lucille"
@user.friends.should == "* George\n* Oscar\n* Lucille"
@user.instance_eval do
@data[:friends] = ["George", "Oscar", "Lucille"]
end
end
end
context "finding resources" do
before do
api = Her::API.new
api.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :age => 42 }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :id => 2, :age => 34 }.to_json] }
stub.get("/users?age=42") { |env| [200, {}, [{ :id => 1, :age => 42 }].to_json] }
end
end
spawn_model :User do
uses_api api
end
end
it "handles finding by a single id" do
@user = User.find(1)
@user.id.should == 1
end
it "handles finding by multiple ids" do
@users = User.find(1, 2)
@users.should be_kind_of(Array)
@users.length.should == 2
@users[0].id.should == 1
@users[1].id.should == 2
end
it "handles finding by an array of ids" do
@users = User.find([1, 2])
@users.should be_kind_of(Array)
@users.length.should == 2
@users[0].id.should == 1
@users[1].id.should == 2
end
it "handles finding by an array of ids of length 1" do
@users = User.find([1])
@users.should be_kind_of(Array)
@users.length.should == 1
@users[0].id.should == 1
end
it "handles finding with other parameters" do
@users = User.all(:age => 42)
@users.should be_kind_of(Array)
@users.should be_all { |u| u.age == 42 }
end
end
context "creating resources" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
stub.post("/companies") { |env| [200, {}, { :errors => ["name is required"] }.to_json] }
end
end
spawn_model "Foo::User"
spawn_model "Foo::Company"
end
it "handle one-line resource creation" do
@user = Foo::User.create(:fullname => "Tobias Fünke")
@user.id.should == 1
@user.fullname.should == "Tobias Fünke"
end
it "handle resource creation through Model.new + #save" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save.should be_true
@user.fullname.should == "Tobias Fünke"
end
it "returns false when #save gets errors" do
@company = Foo::Company.new
@company.save.should be_false
end
end
context "updating resources" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
stub.put("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
end
end
spawn_model "Foo::User"
end
it "handle resource data update without saving it" do
@user = Foo::User.find(1)
@user.fullname.should == "Tobias Fünke"
@user.fullname = "Kittie Sanchez"
@user.fullname.should == "Kittie Sanchez"
end
it "handle resource update through the .update class method" do
@user = Foo::User.save_existing(1, { :fullname => "Lindsay Fünke" })
@user.fullname.should == "Lindsay Fünke"
end
it "handle resource update through #save on an existing resource" do
@user = Foo::User.find(1)
@user.fullname = "Lindsay Fünke"
@user.save
@user.fullname.should == "Lindsay Fünke"
end
end
context "deleting resources" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke", :active => true }.to_json] }
stub.delete("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke", :active => false }.to_json] }
end
end
spawn_model "Foo::User"
end
it "handle resource deletion through the .destroy class method" do
@user = Foo::User.destroy_existing(1)
@user.active.should be_false
end
it "handle resource deletion through #destroy on an existing resource" do
@user = Foo::User.find(1)
@user.destroy
@user.active.should be_false
end
end
context "saving resources with overridden to_params" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.post("/users") do |env|
body = {
:id => 1,
:fullname => Faraday::Utils.parse_query(env[:body])['fullname']
}.to_json
[200, {}, body]
end
end
end
spawn_model "Foo::User" do
def to_params
{ :fullname => "Lindsay Fünke" }
end
end
end
it "changes the request parameters for one-line resource creation" do
@user = Foo::User.create(:fullname => "Tobias Fünke")
@user.fullname.should == "Lindsay Fünke"
end
it "changes the request parameters for Model.new + #save" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save
@user.fullname.should == "Lindsay Fünke"
end
end
context "checking resource equality" do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
stub.get("/admins/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
end
end
spawn_model "Foo::User"
spawn_model "Foo::Admin"
end
let(:user) { Foo::User.find(1) }
it "returns true for the exact same object" do
user.should == user
end
it "returns true for the same resource via find" do
user.should == Foo::User.find(1)
end
it "returns true for the same class with identical data" do
user.should == Foo::User.new(:id => 1, :fullname => "Lindsay Fünke")
end
it "returns true for a different resource with the same data" do
user.should == Foo::Admin.find(1)
end
it "returns false for the same class with different data" do
user.should_not == Foo::User.new(:id => 2, :fullname => "Tobias Fünke")
end
it "returns false for a non-resource with the same data" do
fake_user = stub(:data => { :id => 1, :fullname => "Lindsay Fünke" })
user.should_not == fake_user
end
it "delegates eql? to ==" do
other = Object.new
user.expects(:==).with(other).returns(true)
user.eql?(other).should be_true
end
it "treats equal resources as equal for Array#uniq" do
user2 = Foo::User.find(1)
[user, user2].uniq.should == [user]
end
it "treats equal resources as equal for hash keys" do
Foo::User.find(1)
hash = { user => true }
hash[Foo::User.find(1)] = false
hash.size.should == 1
hash.should == { user => false }
end
end
end