dupe 1.0.1 → 1.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dupe/active_resource_extensions.rb +7 -9
- data/lib/dupe/dupe.rb +13 -7
- data/lib/dupe/hash_pruner.rb +6 -2
- data/lib/dupe/mock.rb +10 -7
- data/spec/lib_specs/active_resource_extensions_spec.rb +320 -133
- data/spec/lib_specs/dupe_spec.rb +273 -186
- data/spec/lib_specs/mock_definitions_spec.rb +101 -40
- data/spec/lib_specs/mock_spec.rb +300 -124
- data/spec/lib_specs/network_spec.rb +32 -8
- metadata +38 -62
data/spec/lib_specs/dupe_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe Dupe do
|
|
4
4
|
before do
|
5
5
|
Dupe.reset
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
describe "#sequences" do
|
9
9
|
it "should be empty on initialization" do
|
10
10
|
Dupe.sequences.should == {}
|
@@ -44,7 +44,7 @@ describe Dupe do
|
|
44
44
|
Dupe.next(:email).should == "email-2@address.com"
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
describe "reset" do
|
49
49
|
it "should call reset_models, reset_database, and reset_network" do
|
50
50
|
Dupe.should_receive(:reset_models).once
|
@@ -66,7 +66,7 @@ describe Dupe do
|
|
66
66
|
Dupe.sequences.should == {}
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
describe "reset_models" do
|
71
71
|
it "should reset @models to an empty hash" do
|
72
72
|
Dupe.models.length.should == 0
|
@@ -79,7 +79,7 @@ describe Dupe do
|
|
79
79
|
Dupe.models.should == {}
|
80
80
|
end
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
describe "reset_database" do
|
84
84
|
it "should clear out the database" do
|
85
85
|
Dupe.define :book
|
@@ -89,7 +89,7 @@ describe Dupe do
|
|
89
89
|
Dupe.database.tables.should be_empty
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
describe "reset_network" do
|
94
94
|
it "should clear out the network" do
|
95
95
|
Dupe.create :book
|
@@ -99,156 +99,13 @@ describe Dupe do
|
|
99
99
|
Dupe.network.mocks.values.inject(false) {|b,v| b || !v.empty?}.should == false
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
103
|
-
describe "define" do
|
104
|
-
|
105
|
-
# Dupe.define :model_name
|
106
|
-
it "should accept a single symbol parameter" do
|
107
|
-
proc {
|
108
|
-
Dupe.define :book
|
109
|
-
}.should_not raise_error
|
110
|
-
end
|
111
|
-
|
112
|
-
# Dupe.define :model_name do |attrs|
|
113
|
-
# attrs.attr1 'Default Value'
|
114
|
-
# attrs.attr2 do |value|
|
115
|
-
# 'transformed value'
|
116
|
-
# end
|
117
|
-
# end
|
118
|
-
it "should accept a symbol plus a block (that accepts a single parameter)" do
|
119
|
-
proc {
|
120
|
-
Dupe.define :book do
|
121
|
-
end
|
122
|
-
}.should raise_error(
|
123
|
-
ArgumentError,
|
124
|
-
"Unknown Dupe.define parameter format. Please consult the API" +
|
125
|
-
" for information on how to use Dupe.define"
|
126
|
-
)
|
127
|
-
|
128
|
-
proc {
|
129
|
-
Dupe.define :book do |attrs|
|
130
|
-
attrs.author 'Anon'
|
131
|
-
attrs.title 'Untitled'
|
132
|
-
end
|
133
|
-
}.should_not raise_error
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should create a model and a schema with the desired definition" do
|
137
|
-
Dupe.define :book do |attrs|
|
138
|
-
attrs.author 'Anon'
|
139
|
-
attrs.genre do
|
140
|
-
'Unknown' + rand(2).to_s
|
141
|
-
end
|
142
|
-
attrs.title 'Untitled' do |value|
|
143
|
-
"transformed #{value}"
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
Dupe.models.length.should == 1
|
148
|
-
Dupe.models[:book].schema.attribute_templates[:author].name.should == :author
|
149
|
-
Dupe.models[:book].schema.attribute_templates[:author].default.should == 'Anon'
|
150
|
-
Dupe.models[:book].schema.attribute_templates[:title].name.should == :title
|
151
|
-
Dupe.models[:book].schema.attribute_templates[:title].default.should == 'Untitled'
|
152
|
-
Dupe.models[:book].schema.attribute_templates[:title].transformer.should be_kind_of(Proc)
|
153
|
-
Dupe.models[:book].schema.attribute_templates[:title].transformer.call('value').should == 'transformed value'
|
154
|
-
Dupe.models[:book].schema.attribute_templates[:genre].name.should == :genre
|
155
|
-
Dupe.models[:book].schema.attribute_templates[:genre].transformer.should be_nil
|
156
|
-
Dupe.models[:book].schema.attribute_templates[:genre].default.should be_kind_of(Proc)
|
157
|
-
Dupe.models[:book].schema.attribute_templates[:genre].default.call.should match(/^Unknown\d$/)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "should add a table to the database" do
|
161
|
-
Dupe.database.tables.length.should == 0
|
162
|
-
Dupe.database.tables[:book].should be_nil
|
163
|
-
Dupe.define :book
|
164
|
-
Dupe.database.tables.length.should == 1
|
165
|
-
Dupe.database.tables[:book].should_not be_nil
|
166
|
-
Dupe.database.tables[:book].should == []
|
167
|
-
end
|
168
|
-
|
169
|
-
it "should add find(:all) and find(<id>) mocks to the network" do
|
170
|
-
Dupe.network.mocks[:get].should be_empty
|
171
|
-
Dupe.create :book
|
172
|
-
Dupe.network.mocks[:get].should_not be_empty
|
173
|
-
Dupe.network.mocks[:get].length.should == 2
|
174
|
-
|
175
|
-
find_all_mock = Dupe.network.mocks[:get].first
|
176
|
-
find_all_mock.class.should == Dupe::Network::GetMock
|
177
|
-
find_all_mock.url_pattern.should == %r{^/books\.xml$}
|
178
|
-
find_all_mock.mocked_response('/books.xml').should == Dupe.find(:books).to_xml(:root => 'books')
|
179
|
-
|
180
|
-
find_one_mock = Dupe.network.mocks[:get].last
|
181
|
-
find_one_mock.class.should == Dupe::Network::GetMock
|
182
|
-
find_one_mock.url_pattern.should == %r{^/books/(\d+)\.xml$}
|
183
|
-
find_one_mock.mocked_response('/books/1.xml').should == Dupe.find(:book).to_xml(:root => 'book')
|
184
|
-
end
|
185
102
|
|
186
|
-
it "should add a POST (create resource) mock to the network" do
|
187
|
-
Dupe.network.mocks[:post].should be_empty
|
188
|
-
Dupe.define :book
|
189
|
-
Dupe.network.mocks[:post].should_not be_empty
|
190
|
-
Dupe.network.mocks[:post].length.should == 1
|
191
|
-
|
192
|
-
post_mock = Dupe.network.mocks[:post].first
|
193
|
-
post_mock.class.should == Dupe::Network::PostMock
|
194
|
-
post_mock.url_pattern.should == %r{^/books\.xml$}
|
195
|
-
resp, url = post_mock.mocked_response('/books.xml', {:title => "Rooby"})
|
196
|
-
Hash.from_xml(resp).should == Hash.from_xml(Dupe.find(:book).to_xml(:root => 'book'))
|
197
|
-
url.should == "/books/1.xml"
|
198
|
-
end
|
199
|
-
|
200
|
-
it "should add a PUT (update resource) mock to the network" do
|
201
|
-
Dupe.network.mocks[:put].should be_empty
|
202
|
-
book = Dupe.create :book, :title => 'Rooby!'
|
203
|
-
Dupe.network.mocks[:put].should_not be_empty
|
204
|
-
Dupe.network.mocks[:put].length.should == 1
|
205
|
-
|
206
|
-
put_mock = Dupe.network.mocks[:put].first
|
207
|
-
put_mock.class.should == Dupe::Network::PutMock
|
208
|
-
put_mock.url_pattern.should == %r{^/books/(\d+)\.xml$}
|
209
|
-
resp, url = put_mock.mocked_response('/books/1.xml', {:title => "Rails!"})
|
210
|
-
resp.should == nil
|
211
|
-
url.should == "/books/1.xml"
|
212
|
-
book.title.should == "Rails!"
|
213
|
-
end
|
214
|
-
|
215
|
-
it "should add a DELETE mock to the network" do
|
216
|
-
Dupe.network.mocks[:delete].should be_empty
|
217
|
-
book = Dupe.create :book, :title => 'Rooby!'
|
218
|
-
Dupe.network.mocks[:delete].should_not be_empty
|
219
|
-
Dupe.network.mocks[:delete].length.should == 1
|
220
|
-
|
221
|
-
delete_mock = Dupe.network.mocks[:delete].first
|
222
|
-
delete_mock.class.should == Dupe::Network::DeleteMock
|
223
|
-
delete_mock.url_pattern.should == %r{^/books/(\d+)\.xml$}
|
224
|
-
end
|
225
|
-
|
226
|
-
|
227
|
-
it "should honor ActiveResource site prefix's for the find(:all) and find(<id>) mocks" do
|
228
|
-
Dupe.network.mocks[:get].should be_empty
|
229
|
-
class Author < ActiveResource::Base; self.site='http://somewhere.com/book_services'; end
|
230
|
-
Dupe.create :author
|
231
|
-
Dupe.network.mocks[:get].should_not be_empty
|
232
|
-
Dupe.network.mocks[:get].length.should == 2
|
233
|
-
|
234
|
-
find_all_mock = Dupe.network.mocks[:get].first
|
235
|
-
find_all_mock.class.should == Dupe::Network::GetMock
|
236
|
-
find_all_mock.url_pattern.should == %r{^/book_services/authors\.xml$}
|
237
|
-
find_all_mock.mocked_response('/book_services/authors.xml').should == Dupe.find(:authors).to_xml(:root => 'authors')
|
238
|
-
|
239
|
-
find_one_mock = Dupe.network.mocks[:get].last
|
240
|
-
find_one_mock.class.should == Dupe::Network::GetMock
|
241
|
-
find_one_mock.url_pattern.should == %r{^/book_services/authors/(\d+)\.xml$}
|
242
|
-
find_one_mock.mocked_response('/book_services/authors/1.xml').should == Dupe.find(:author).to_xml(:root => 'author')
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
103
|
describe "create" do
|
247
104
|
it "should require a model name parameter" do
|
248
105
|
proc {Dupe.create}.should raise_error(ArgumentError)
|
249
106
|
proc {Dupe.create :book}.should_not raise_error(ArgumentError)
|
250
107
|
end
|
251
|
-
|
108
|
+
|
252
109
|
it "should create a model if one doesn't already exist" do
|
253
110
|
Dupe.models.should be_empty
|
254
111
|
Dupe.create :book
|
@@ -256,7 +113,7 @@ describe Dupe do
|
|
256
113
|
Dupe.models[:book].should_not be_nil
|
257
114
|
Dupe.models[:book].name.should == :book
|
258
115
|
end
|
259
|
-
|
116
|
+
|
260
117
|
it "should be smart enough to singularize the model name before lookup/create" do
|
261
118
|
Dupe.define :book
|
262
119
|
Dupe.models.should_not be_empty
|
@@ -272,7 +129,7 @@ describe Dupe do
|
|
272
129
|
Dupe.models[:author].name.should == :author
|
273
130
|
Dupe.models[:authors].should be_nil
|
274
131
|
end
|
275
|
-
|
132
|
+
|
276
133
|
it "should create (and return) a database record if passed a single hash" do
|
277
134
|
Dupe.define :book
|
278
135
|
Dupe.database.tables[:book].should be_empty
|
@@ -281,7 +138,7 @@ describe Dupe do
|
|
281
138
|
Dupe.database.tables[:book].length.should == 1
|
282
139
|
Dupe.database.tables[:book].first.should == @book
|
283
140
|
end
|
284
|
-
|
141
|
+
|
285
142
|
it "should create several records if passed an array of hashes (and return an array of the records created)" do
|
286
143
|
Dupe.define :book
|
287
144
|
Dupe.database.tables[:book].should be_empty
|
@@ -291,13 +148,13 @@ describe Dupe do
|
|
291
148
|
Dupe.database.tables[:book].first.should == @books.first
|
292
149
|
Dupe.database.tables[:book].last.should == @books.last
|
293
150
|
end
|
294
|
-
|
151
|
+
|
295
152
|
it "should symbolize hash keys to keep from duplicating column names" do
|
296
153
|
b = Dupe.create :book, 'title' => 'War And Peace', :title => 'War And Peace'
|
297
154
|
b.title.should == 'War And Peace'
|
298
155
|
b[:title].should == 'War And Peace'
|
299
156
|
b['title'].should == nil
|
300
|
-
|
157
|
+
|
301
158
|
bs = Dupe.create :books, [{:test => 2, 'test' => 2}, {:test => 4, 'test' => 4}]
|
302
159
|
bs.first.test.should == 2
|
303
160
|
bs.first[:test].should == 2
|
@@ -328,18 +185,18 @@ describe Dupe do
|
|
328
185
|
b.isbn.should == 1
|
329
186
|
end
|
330
187
|
end
|
331
|
-
|
188
|
+
|
332
189
|
describe "create resources that have an after_create callback" do
|
333
190
|
before do
|
334
191
|
Dupe.define :book do |book|
|
335
192
|
book.uniquify :title, :author, :genre
|
336
|
-
|
193
|
+
|
337
194
|
book.after_create do |b|
|
338
195
|
b.label = b.title.downcase.gsub(/\ +/, '-') unless b.label
|
339
196
|
end
|
340
197
|
end
|
341
198
|
end
|
342
|
-
|
199
|
+
|
343
200
|
it "should create a label based on the title if a label is passed in during the create call" do
|
344
201
|
b = Dupe.create :book, :title => 'Testing Testing'
|
345
202
|
b.label.should == 'testing-testing'
|
@@ -347,21 +204,21 @@ describe Dupe do
|
|
347
204
|
b.label.should == 'testbook'
|
348
205
|
end
|
349
206
|
end
|
350
|
-
|
207
|
+
|
351
208
|
describe "find" do
|
352
209
|
before do
|
353
210
|
Dupe.define :book
|
354
211
|
@book = Dupe.create :book
|
355
212
|
end
|
356
|
-
|
213
|
+
|
357
214
|
it "should require a model name parameter" do
|
358
215
|
proc { Dupe.find }.should raise_error(ArgumentError)
|
359
216
|
end
|
360
|
-
|
217
|
+
|
361
218
|
it "should require the model to exist" do
|
362
219
|
proc { Dupe.find :unknown_models }.should raise_error(Dupe::Database::TableDoesNotExistError)
|
363
220
|
end
|
364
|
-
|
221
|
+
|
365
222
|
it "should return an array if you ask for a plural model (e.g., Dupe.find :books)" do
|
366
223
|
result = Dupe.find :books
|
367
224
|
result.should be_kind_of(Array)
|
@@ -369,27 +226,27 @@ describe Dupe do
|
|
369
226
|
result.length.should == 1
|
370
227
|
result.first.should == @book
|
371
228
|
end
|
372
|
-
|
229
|
+
|
373
230
|
it "should return a single record (or nil) if you ask for a singular model (e.g., Dupe.find :book)" do
|
374
231
|
result = Dupe.find :book
|
375
232
|
result.should be_kind_of(Dupe::Database::Record)
|
376
233
|
result.should == @book
|
377
|
-
|
234
|
+
|
378
235
|
result = Dupe.find(:book) {|b| false}
|
379
236
|
result.should be_nil
|
380
237
|
end
|
381
238
|
end
|
382
|
-
|
239
|
+
|
383
240
|
describe "debug" do
|
384
241
|
it "should default to false" do
|
385
242
|
Dupe.debug.should == false
|
386
243
|
end
|
387
|
-
|
244
|
+
|
388
245
|
it "should allow you to set it to true" do
|
389
246
|
Dupe.debug = true
|
390
247
|
Dupe.debug.should == true
|
391
248
|
end
|
392
|
-
|
249
|
+
|
393
250
|
it "should persist across a Dupe.reset" do
|
394
251
|
Dupe.debug = true
|
395
252
|
Dupe.debug.should == true
|
@@ -397,7 +254,7 @@ describe Dupe do
|
|
397
254
|
Dupe.debug.should == true
|
398
255
|
end
|
399
256
|
end
|
400
|
-
|
257
|
+
|
401
258
|
describe "stub" do
|
402
259
|
it ": when called with only a count and a model_name, it should generate that many blank (id-only) records" do
|
403
260
|
Dupe.database.tables[:author].should be_nil
|
@@ -406,17 +263,17 @@ describe Dupe do
|
|
406
263
|
Dupe.database.tables[:author].length.should == 20
|
407
264
|
authors.collect(&:id).should == (1..20).to_a
|
408
265
|
end
|
409
|
-
|
266
|
+
|
410
267
|
it "should accept procs on stubs" do
|
411
268
|
Dupe.database.tables[:author].should be_nil
|
412
269
|
authors =
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
270
|
+
Dupe.stub(
|
271
|
+
2,
|
272
|
+
:authors,
|
273
|
+
:like => {
|
274
|
+
:name => proc {|n| "Author #{n}"},
|
275
|
+
:bio => proc {|n| "Author #{n}'s bio"}
|
276
|
+
}
|
420
277
|
)
|
421
278
|
authors.first.name.should == "Author 1"
|
422
279
|
authors.first.bio.should == "Author 1's bio"
|
@@ -424,7 +281,7 @@ describe Dupe do
|
|
424
281
|
authors.last.bio.should == "Author 2's bio"
|
425
282
|
Dupe.database.tables[:author].length.should == 2
|
426
283
|
end
|
427
|
-
|
284
|
+
|
428
285
|
it "shouldn't care if the model_name is singular or plural" do
|
429
286
|
Dupe.database.tables.should be_empty
|
430
287
|
Dupe.stub 5, :author
|
@@ -436,35 +293,35 @@ describe Dupe do
|
|
436
293
|
Dupe.database.tables.length.should == 1
|
437
294
|
end
|
438
295
|
end
|
439
|
-
|
296
|
+
|
440
297
|
describe "find_or_create" do
|
441
298
|
it "should require a model name" do
|
442
299
|
proc { Dupe.find_or_create }.should raise_error(ArgumentError)
|
443
300
|
end
|
444
|
-
|
301
|
+
|
445
302
|
it "should find a result if one exists" do
|
446
303
|
b = Dupe.create :book, :title => 'Rooby', :serial_number => 21345
|
447
304
|
found_book = Dupe.find_or_create :book, :title => 'Rooby', :serial_number => 21345
|
448
305
|
b.should === found_book
|
449
|
-
|
306
|
+
|
450
307
|
g = Dupe.create :genre, :name => 'Science Fiction', :label => 'sci-fi'
|
451
308
|
found_genre = Dupe.find_or_create :genre, :name => 'Science Fiction', :label => 'sci-fi'
|
452
309
|
g.should === found_genre
|
453
310
|
end
|
454
|
-
|
311
|
+
|
455
312
|
it "should create a result if one does not exist" do
|
456
313
|
Dupe.database.tables.should be_empty
|
457
314
|
author = Dupe.find_or_create :author, :name => 'Matt Parker', :age => 27, :label => 'matt-parker'
|
458
315
|
Dupe.database.tables.should_not be_empty
|
459
316
|
author.should === (Dupe.find(:author) {|a| a.label == 'matt-parker' && a.age == 27})
|
460
317
|
end
|
461
|
-
|
318
|
+
|
462
319
|
it "should return an array of results if passed a plural model name" do
|
463
320
|
books = Dupe.stub 20, :books, :like => { :title => proc {|n| "book ##{n} title"} }
|
464
321
|
bs = Dupe.find_or_create :books
|
465
322
|
books.should == bs
|
466
323
|
end
|
467
|
-
|
324
|
+
|
468
325
|
it "should create and return an array of results if passed a plural model name for which no matching records exist" do
|
469
326
|
Dupe.database.tables.should be_empty
|
470
327
|
books = Dupe.find_or_create :books, :author => 'test'
|
@@ -472,9 +329,9 @@ describe Dupe do
|
|
472
329
|
books.should be_kind_of(Array)
|
473
330
|
books.should == Dupe.find(:books)
|
474
331
|
end
|
475
|
-
|
332
|
+
|
476
333
|
end
|
477
|
-
|
334
|
+
|
478
335
|
describe "creating resources should not change definitions (regression test)" do
|
479
336
|
before do
|
480
337
|
Dupe.define :book do |book|
|
@@ -484,7 +341,7 @@ describe Dupe do
|
|
484
341
|
end
|
485
342
|
end
|
486
343
|
end
|
487
|
-
|
344
|
+
|
488
345
|
it "should not add a record to the definition when the default value is an array" do
|
489
346
|
b = Dupe.create :book
|
490
347
|
a = Dupe.create :author
|
@@ -493,7 +350,7 @@ describe Dupe do
|
|
493
350
|
b2 = Dupe.create :book
|
494
351
|
b2.authors.should be_empty
|
495
352
|
end
|
496
|
-
|
353
|
+
|
497
354
|
it "should not dupe the value returned by a lazy attribute" do
|
498
355
|
b = Dupe.create :book
|
499
356
|
b2 = Dupe.create :book
|
@@ -519,4 +376,234 @@ describe Dupe do
|
|
519
376
|
Dupe.find(:authors).first.name.should == 'Frank Herbert'
|
520
377
|
end
|
521
378
|
end
|
379
|
+
|
380
|
+
describe "define" do
|
381
|
+
# Dupe.define :model_name
|
382
|
+
it "should accept a single symbol parameter" do
|
383
|
+
proc {
|
384
|
+
Dupe.define :book
|
385
|
+
}.should_not raise_error
|
386
|
+
end
|
387
|
+
|
388
|
+
# Dupe.define :model_name do |attrs|
|
389
|
+
# attrs.attr1 'Default Value'
|
390
|
+
# attrs.attr2 do |value|
|
391
|
+
# 'transformed value'
|
392
|
+
# end
|
393
|
+
# end
|
394
|
+
it "should accept a symbol plus a block (that accepts a single parameter)" do
|
395
|
+
proc {
|
396
|
+
Dupe.define :book do
|
397
|
+
end
|
398
|
+
}.should raise_error(
|
399
|
+
ArgumentError,
|
400
|
+
"Unknown Dupe.define parameter format. Please consult the API" +
|
401
|
+
" for information on how to use Dupe.define"
|
402
|
+
)
|
403
|
+
|
404
|
+
proc {
|
405
|
+
Dupe.define :book do |attrs|
|
406
|
+
attrs.author 'Anon'
|
407
|
+
attrs.title 'Untitled'
|
408
|
+
end
|
409
|
+
}.should_not raise_error
|
410
|
+
end
|
411
|
+
|
412
|
+
it "should create a model and a schema with the desired definition" do
|
413
|
+
Dupe.define :book do |attrs|
|
414
|
+
attrs.author 'Anon'
|
415
|
+
attrs.genre do
|
416
|
+
'Unknown' + rand(2).to_s
|
417
|
+
end
|
418
|
+
attrs.title 'Untitled' do |value|
|
419
|
+
"transformed #{value}"
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
Dupe.models.length.should == 1
|
424
|
+
Dupe.models[:book].schema.attribute_templates[:author].name.should == :author
|
425
|
+
Dupe.models[:book].schema.attribute_templates[:author].default.should == 'Anon'
|
426
|
+
Dupe.models[:book].schema.attribute_templates[:title].name.should == :title
|
427
|
+
Dupe.models[:book].schema.attribute_templates[:title].default.should == 'Untitled'
|
428
|
+
Dupe.models[:book].schema.attribute_templates[:title].transformer.should be_kind_of(Proc)
|
429
|
+
Dupe.models[:book].schema.attribute_templates[:title].transformer.call('value').should == 'transformed value'
|
430
|
+
Dupe.models[:book].schema.attribute_templates[:genre].name.should == :genre
|
431
|
+
Dupe.models[:book].schema.attribute_templates[:genre].transformer.should be_nil
|
432
|
+
Dupe.models[:book].schema.attribute_templates[:genre].default.should be_kind_of(Proc)
|
433
|
+
Dupe.models[:book].schema.attribute_templates[:genre].default.call.should match(/^Unknown\d$/)
|
434
|
+
end
|
435
|
+
|
436
|
+
it "should add a table to the database" do
|
437
|
+
Dupe.database.tables.length.should == 0
|
438
|
+
Dupe.database.tables[:book].should be_nil
|
439
|
+
Dupe.define :book
|
440
|
+
Dupe.database.tables.length.should == 1
|
441
|
+
Dupe.database.tables[:book].should_not be_nil
|
442
|
+
Dupe.database.tables[:book].should == []
|
443
|
+
end
|
444
|
+
|
445
|
+
describe "using xml" do
|
446
|
+
before do
|
447
|
+
Dupe.format = ActiveResource::Formats::XmlFormat
|
448
|
+
end
|
449
|
+
|
450
|
+
it "should add find(:all) and find(<id>) mocks to the network" do
|
451
|
+
Dupe.network.mocks[:get].should be_empty
|
452
|
+
Dupe.create :book
|
453
|
+
Dupe.network.mocks[:get].should_not be_empty
|
454
|
+
Dupe.network.mocks[:get].length.should == 2
|
455
|
+
|
456
|
+
find_all_mock = Dupe.network.mocks[:get].first
|
457
|
+
find_all_mock.class.should == Dupe::Network::GetMock
|
458
|
+
find_all_mock.url_pattern.should == %r{^/books\.(?:xml|json)$}
|
459
|
+
find_all_mock.mocked_response('/books.xml').should == Dupe.find(:books).to_xml(:root => 'books')
|
460
|
+
|
461
|
+
find_one_mock = Dupe.network.mocks[:get].last
|
462
|
+
find_one_mock.class.should == Dupe::Network::GetMock
|
463
|
+
find_one_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
464
|
+
find_one_mock.mocked_response('/books/1.xml').should == Dupe.find(:book).to_xml(:root => 'book')
|
465
|
+
end
|
466
|
+
|
467
|
+
it "should add a POST (create resource) mock to the network" do
|
468
|
+
Dupe.network.mocks[:post].should be_empty
|
469
|
+
Dupe.define :book
|
470
|
+
Dupe.network.mocks[:post].should_not be_empty
|
471
|
+
Dupe.network.mocks[:post].length.should == 1
|
472
|
+
|
473
|
+
post_mock = Dupe.network.mocks[:post].first
|
474
|
+
post_mock.class.should == Dupe::Network::PostMock
|
475
|
+
post_mock.url_pattern.should == %r{^/books\.(?:xml|json)$}
|
476
|
+
resp, url = post_mock.mocked_response('/books.xml', {:title => "Rooby"})
|
477
|
+
Hash.from_xml(resp).should == Hash.from_xml(Dupe.find(:book).to_xml(:root => 'book'))
|
478
|
+
url.should == "/books/1.xml"
|
479
|
+
end
|
480
|
+
|
481
|
+
it "should add a PUT (update resource) mock to the network" do
|
482
|
+
Dupe.network.mocks[:put].should be_empty
|
483
|
+
book = Dupe.create :book, :title => 'Rooby!'
|
484
|
+
Dupe.network.mocks[:put].should_not be_empty
|
485
|
+
Dupe.network.mocks[:put].length.should == 1
|
486
|
+
|
487
|
+
put_mock = Dupe.network.mocks[:put].first
|
488
|
+
put_mock.class.should == Dupe::Network::PutMock
|
489
|
+
put_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
490
|
+
resp, url = put_mock.mocked_response('/books/1.xml', {:title => "Rails!"})
|
491
|
+
resp.should == nil
|
492
|
+
url.should == "/books/1.xml"
|
493
|
+
book.title.should == "Rails!"
|
494
|
+
end
|
495
|
+
|
496
|
+
it "should add a DELETE mock to the network" do
|
497
|
+
Dupe.network.mocks[:delete].should be_empty
|
498
|
+
book = Dupe.create :book, :title => 'Rooby!'
|
499
|
+
Dupe.network.mocks[:delete].should_not be_empty
|
500
|
+
Dupe.network.mocks[:delete].length.should == 1
|
501
|
+
|
502
|
+
delete_mock = Dupe.network.mocks[:delete].first
|
503
|
+
delete_mock.class.should == Dupe::Network::DeleteMock
|
504
|
+
delete_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
505
|
+
end
|
506
|
+
|
507
|
+
|
508
|
+
it "should honor ActiveResource site prefix's for the find(:all) and find(<id>) mocks" do
|
509
|
+
Dupe.network.mocks[:get].should be_empty
|
510
|
+
class Author < ActiveResource::Base; self.site='http://somewhere.com/book_services'; end
|
511
|
+
Dupe.create :author
|
512
|
+
Dupe.network.mocks[:get].should_not be_empty
|
513
|
+
Dupe.network.mocks[:get].length.should == 2
|
514
|
+
|
515
|
+
find_all_mock = Dupe.network.mocks[:get].first
|
516
|
+
find_all_mock.class.should == Dupe::Network::GetMock
|
517
|
+
find_all_mock.url_pattern.should == %r{^/book_services/authors\.(?:xml|json)$}
|
518
|
+
find_all_mock.mocked_response('/book_services/authors.xml').should == Dupe.find(:authors).to_xml(:root => 'authors')
|
519
|
+
|
520
|
+
find_one_mock = Dupe.network.mocks[:get].last
|
521
|
+
find_one_mock.class.should == Dupe::Network::GetMock
|
522
|
+
find_one_mock.url_pattern.should == %r{^/book_services/authors/(\d+)\.(?:xml|json)$}
|
523
|
+
find_one_mock.mocked_response('/book_services/authors/1.xml').should == Dupe.find(:author).to_xml(:root => 'author')
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
527
|
+
describe "using json" do
|
528
|
+
before do
|
529
|
+
Dupe.format = ActiveResource::Formats::JsonFormat
|
530
|
+
end
|
531
|
+
|
532
|
+
it "should add find(:all) and find(<id>) mocks to the network" do
|
533
|
+
Dupe.network.mocks[:get].should be_empty
|
534
|
+
Dupe.create :book
|
535
|
+
Dupe.network.mocks[:get].should_not be_empty
|
536
|
+
Dupe.network.mocks[:get].length.should == 2
|
537
|
+
|
538
|
+
find_all_mock = Dupe.network.mocks[:get].first
|
539
|
+
find_all_mock.class.should == Dupe::Network::GetMock
|
540
|
+
find_all_mock.url_pattern.should == %r{^/books\.(?:xml|json)$}
|
541
|
+
find_all_mock.mocked_response('/books.json').should == Dupe.find(:books).to_json(:root => 'books')
|
542
|
+
|
543
|
+
find_one_mock = Dupe.network.mocks[:get].last
|
544
|
+
find_one_mock.class.should == Dupe::Network::GetMock
|
545
|
+
find_one_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
546
|
+
find_one_mock.mocked_response('/books/1.json').should == Dupe.find(:book).to_json(:root => 'book')
|
547
|
+
end
|
548
|
+
|
549
|
+
it "should add a POST (create resource) mock to the network" do
|
550
|
+
Dupe.network.mocks[:post].should be_empty
|
551
|
+
Dupe.define :book
|
552
|
+
Dupe.network.mocks[:post].should_not be_empty
|
553
|
+
Dupe.network.mocks[:post].length.should == 1
|
554
|
+
|
555
|
+
post_mock = Dupe.network.mocks[:post].first
|
556
|
+
post_mock.class.should == Dupe::Network::PostMock
|
557
|
+
post_mock.url_pattern.should == %r{^/books\.(?:xml|json)$}
|
558
|
+
resp, url = post_mock.mocked_response('/books.json', {:title => "Rooby"})
|
559
|
+
Hash.from_json(resp).should == Hash.from_json(Dupe.find(:book).to_json(:root => 'book'))
|
560
|
+
url.should == "/books/1.json"
|
561
|
+
end
|
562
|
+
|
563
|
+
it "should add a PUT (update resource) mock to the network" do
|
564
|
+
Dupe.network.mocks[:put].should be_empty
|
565
|
+
book = Dupe.create :book, :title => 'Rooby!'
|
566
|
+
Dupe.network.mocks[:put].should_not be_empty
|
567
|
+
Dupe.network.mocks[:put].length.should == 1
|
568
|
+
|
569
|
+
put_mock = Dupe.network.mocks[:put].first
|
570
|
+
put_mock.class.should == Dupe::Network::PutMock
|
571
|
+
put_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
572
|
+
resp, url = put_mock.mocked_response('/books/1.json', {:title => "Rails!"})
|
573
|
+
resp.should == nil
|
574
|
+
url.should == "/books/1.json"
|
575
|
+
book.title.should == "Rails!"
|
576
|
+
end
|
577
|
+
|
578
|
+
it "should add a DELETE mock to the network" do
|
579
|
+
Dupe.network.mocks[:delete].should be_empty
|
580
|
+
book = Dupe.create :book, :title => 'Rooby!'
|
581
|
+
Dupe.network.mocks[:delete].should_not be_empty
|
582
|
+
Dupe.network.mocks[:delete].length.should == 1
|
583
|
+
|
584
|
+
delete_mock = Dupe.network.mocks[:delete].first
|
585
|
+
delete_mock.class.should == Dupe::Network::DeleteMock
|
586
|
+
delete_mock.url_pattern.should == %r{^/books/(\d+)\.(?:xml|json)$}
|
587
|
+
end
|
588
|
+
|
589
|
+
|
590
|
+
it "should honor ActiveResource site prefix's for the find(:all) and find(<id>) mocks" do
|
591
|
+
Dupe.network.mocks[:get].should be_empty
|
592
|
+
class Author < ActiveResource::Base; self.site='http://somewhere.com/book_services'; end
|
593
|
+
Dupe.create :author
|
594
|
+
Dupe.network.mocks[:get].should_not be_empty
|
595
|
+
Dupe.network.mocks[:get].length.should == 2
|
596
|
+
|
597
|
+
find_all_mock = Dupe.network.mocks[:get].first
|
598
|
+
find_all_mock.class.should == Dupe::Network::GetMock
|
599
|
+
find_all_mock.url_pattern.should == %r{^/book_services/authors\.(?:xml|json)$}
|
600
|
+
find_all_mock.mocked_response('/book_services/authors.json').should == Dupe.find(:authors).to_json(:root => 'authors')
|
601
|
+
|
602
|
+
find_one_mock = Dupe.network.mocks[:get].last
|
603
|
+
find_one_mock.class.should == Dupe::Network::GetMock
|
604
|
+
find_one_mock.url_pattern.should == %r{^/book_services/authors/(\d+)\.(?:xml|json)$}
|
605
|
+
find_one_mock.mocked_response('/book_services/authors/1.json').should == Dupe.find(:author).to_json(:root => 'author')
|
606
|
+
end
|
607
|
+
end
|
608
|
+
end
|
522
609
|
end
|