/
orm_spec.rb
481 lines (412 loc) · 16.1 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# 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, {}, [{ :admin_id => 1, :name => "Tobias Fünke" }, { :admin_id => 2, :name => "Lindsay Fünke" }].to_json] }
stub.get("/admin_users/1") { |env| [200, {}, { :admin_id => 1, :name => "Tobias Fünke" }.to_json] }
end
end
spawn_model "Foo::User" do
uses_api api
end
spawn_model "Foo::AdminUser" do
uses_api api
primary_key :admin_id
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"
@admin = Foo::AdminUser.find(1)
@admin.id.should == 1
@admin.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_truthy
@new_user.new_record?.should be_truthy
@new_user.fullname.should == "Tobias Fünke"
@existing_user = Foo::User.find(1)
@existing_user.new?.should be_falsey
@existing_user.new_record?.should be_falsey
end
it 'handles new resource with custom primary key' do
@new_user = Foo::AdminUser.new(:fullname => 'Lindsay Fünke', :id => -1)
@new_user.should be_new
@existing_user = Foo::AdminUser.find(1)
@existing_user.should_not be_new
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.response_errors.should == ["Yes", "Sir"]
end
end
context "mapping data, metadata and error data in string keys 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.response_errors.should == ["Yes", "Sir"]
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 }.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)
@attributes[:friends] = val
end
def friends
@attributes[:friends].map { |friend| "* #{friend}" }.join("\n")
end
end
end
it "handles custom setters" do
@user = User.find(1)
@user.friends.should == "* Maeby\n* GOB\n* Anne"
@user.instance_eval do
@attributes[:friends] = ["Maeby", "GOB", "Anne"]
end
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
@attributes[: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?id[]=1&id[]=2") { |env| [200, {}, [{ :id => 1, :age => 42 }, { :id => 2, :age => 34 }].to_json] }
stub.get("/users?age=42&foo=bar") { |env| [200, {}, [{ :id => 3, :age => 42 }].to_json] }
stub.get("/users?age=42") { |env| [200, {}, [{ :id => 1, :age => 42 }].to_json] }
stub.get("/users?age=40") { |env| [200, {}, [{ :id => 1, :age => 40 }].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 by an array id param of length 2" do
@users = User.find(id: [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 with id parameter as an array' do
@users = User.where(id: [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 with other parameters" do
@users = User.where(:age => 42, :foo => "bar").all
@users.should be_kind_of(Array)
@users.first.id.should == 3
end
it "handles finding with other parameters and scoped" do
@users = User.scoped
@users.where(:age => 42).should be_all { |u| u.age == 42 }
@users.where(:age => 40).should be_all { |u| u.age == 40 }
end
end
context "building resources" do
context "when request_new_object_on_build is not set (default)" do
before do
spawn_model("Foo::User")
end
it "builds a new resource without requesting it" do
Foo::User.should_not_receive(:request)
@new_user = Foo::User.build(:fullname => "Tobias Fünke")
@new_user.new?.should be_truthy
@new_user.fullname.should == "Tobias Fünke"
end
end
context "when request_new_object_on_build is set" 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/new") { |env| ok! :id => nil, :fullname => params(env)[:fullname], :email => "tobias@bluthcompany.com" }
end
end
spawn_model("Foo::User") { request_new_object_on_build true }
end
it "requests a new resource" do
Foo::User.should_receive(:request).once.and_call_original
@new_user = Foo::User.build(:fullname => "Tobias Fünke")
@new_user.new?.should be_truthy
@new_user.fullname.should == "Tobias Fünke"
@new_user.email.should == "tobias@bluthcompany.com"
end
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 => Faraday::Utils.parse_query(env[:body])['fullname'], :email => Faraday::Utils.parse_query(env[:body])['email'] }.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", :email => "tobias@bluth.com")
@user.id.should == 1
@user.fullname.should == "Tobias Fünke"
@user.email.should == "tobias@bluth.com"
end
it "handle resource creation through Model.new + #save" do
@user = Foo::User.new(:fullname => "Tobias Fünke")
@user.save.should be_truthy
@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_truthy
@user.fullname.should == "Tobias Fünke"
end
it "returns false when #save gets errors" do
@company = Foo::Company.new
@company.save.should be_falsey
end
it "raises ResourceInvalid when #save! gets errors" do
@company = Foo::Company.new
expect { @company.save! }.to raise_error Her::Errors::ResourceInvalid, "Remote validation failed: name is required"
end
it "don't overwrite data if response is empty" do
@company = Foo::Company.new(:name => 'Company Inc.')
@company.save.should be_falsey
@company.name.should == "Company Inc."
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_falsey
@user.should be_destroyed
end
it "handle resource deletion through #destroy on an existing resource" do
@user = Foo::User.find(1)
@user.destroy
@user.active.should be_falsey
@user.should be_destroyed
end
end
context 'customizing HTTP methods' do
before do
Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end
end
context 'create' do
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.put('/users') { |env| [200, {}, { :id => 1, :fullname => 'Tobias Fünke' }.to_json] }
end
spawn_model 'Foo::User' do
attributes :fullname, :email
method_for :create, 'PUT'
end
end
context 'for top-level class' do
it 'uses the custom method (PUT) instead of default method (POST)' do
user = Foo::User.new(:fullname => 'Tobias Fünke')
user.should be_new
user.save.should be_truthy
end
end
context 'for children class' do
before do
class User < Foo::User; end
@spawned_models << :User
end
it 'uses the custom method (PUT) instead of default method (POST)' do
user = User.new(:fullname => 'Tobias Fünke')
user.should be_new
user.save.should be_truthy
end
end
end
context 'update' do
before do
Her::API.default_api.connection.adapter :test do |stub|
stub.get('/users/1') { |env| [200, {}, { :id => 1, :fullname => 'Lindsay Fünke' }.to_json] }
stub.post('/users/1') { |env| [200, {}, { :id => 1, :fullname => 'Tobias Fünke' }.to_json] }
end
spawn_model 'Foo::User' do
attributes :fullname, :email
method_for :update, :post
end
end
it 'uses the custom method (POST) instead of default method (PUT)' do
user = Foo::User.find(1)
user.fullname.should eq 'Lindsay Fünke'
user.fullname = 'Toby Fünke'
user.save
user.fullname.should eq 'Tobias Fünke'
end
end
end
end