forked from zuk/DrowsyDromedary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.rb
376 lines (294 loc) · 11.7 KB
/
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
$: << File.dirname(__FILE__)
require 'drowsy_dromedary'
require 'rubygems'
require 'bundler'
Bundler.setup :default, :development
require 'rack/test'
require 'base64'
require 'cgi'
require 'mongo'
RSpec.configure do |config|
config.include Rack::Test::Methods
def app
DrowsyDromedary
end
end
$DB = "drowsy-test-#{rand(10e10).to_s(32)}"
describe DrowsyDromedary do
before(:all) do
@mongo = Mongo::Connection.new
#puts "Creating test database #{$DB.inspect}..."
@db = @mongo.db($DB)
@db.collection_names # creates the $DB database
end
after(:all) do
#puts "Dropping test database #{$DB.inspect}..."
@mongo.drop_database($DB)
@mongo.close
end
describe "/" do
describe "GET" do
it "returns a list of the available databases" do
get "/"
last_response.status.should == 200
JSON.parse(last_response.body).should include($DB)
end
end
describe "POST" do
after(:all) do
@mongo.drop_database($DB+"-created")
@mongo.drop_database("#{$DB}-created2")
end
it "creates a database" do
post "/", :db => "#{$DB}-created"
last_response.status.should == 201
get "/"
JSON.parse(last_response.body).should include($DB+"-created")
end
it "errors when database name is omitted" do
post "/", :foo => "whatever"
last_response.status.should == 400
end
it "returns a 201 status when the db is created and 200 when it already exists" do
post "/", :db => "#{$DB}-created2"
last_response.status.should == 201
post "/", :db => "#{$DB}-created2"
last_response.status.should == 200
end
end
end
describe "/db" do
describe "GET" do
it "returns a list of collection names in the db" do
get "/#{$DB}"
last_response.status.should == 200
JSON.parse(last_response.body).should == []
@db.create_collection(:foo, :size => 0)
get "/#{$DB}"
last_response.status.should == 200
JSON.parse(last_response.body).should include("foo")
end
end
describe "POST" do
it "creates a collection in the db" do
post "/#{$DB}", :collection => "faa"
last_response.status.should == 201
end
it "returns a 201 status when the collection is created and 200 when it already exists" do
post "/#{$DB}", :collection => "faa2"
last_response.status.should == 201
post "/#{$DB}", :collection => "faa2"
last_response.status.should == 200
end
end
end
describe "/db/collection" do
before(:each) do
@db.drop_collection("testing")
@coll = @db.create_collection("testing")
end
describe "GET" do
it "returns a list of records in the collection" do
get "/#{$DB}/testing"
last_response.status.should == 200
JSON.parse(last_response.body).should == []
id = @coll.save({"foo" => "faa"})
get "/#{$DB}/testing"
last_response.status.should == 200
JSON.parse(last_response.body).should == [{'foo' => 'faa', '_id' => { '$oid' => id.to_s }}]
end
describe "using JSON query" do
before(:each) do
id1 = @coll.save({"fee" => {"cost" => 1000}, "foo" => "faa"})
id2 = @coll.save({"fee" => {"cost" => 1000.12}, "faa" => "ccc"})
id3 = @coll.save({"fee" => {"cost" => 1002.22}, "faa" => "bbb"})
id4 = @coll.save({"fee" => {"cost" => 1003.22}, "faa" => "aaa", "blah" => ["a", 1, "two"]})
end
it "returns a subset of records in the collection using a simple selector" do
get "/#{$DB}/testing", {:selector => {'foo' => 'faa'}.to_json}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 1
r.first['foo'].should == 'faa'
end
it "returns a subset of records in the collection when applying a limit" do
get "/#{$DB}/testing", {:limit => 1}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 1
r.first['foo'].should == 'faa'
end
it "returns a subset of records in the collection using a nested selector" do
get "/#{$DB}/testing", {:selector => {'fee' => {'cost' => 1000.12}, 'faa' => 'ccc'}.to_json}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 1
r.first['fee'].should == {'cost' => 1000.12}
end
it "returns a subset of records in the collection using a nested selector with do notation" do
get "/#{$DB}/testing", {:selector => {'fee.cost' => 1000.12, 'faa' => 'ccc'}.to_json}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 1
r.first['fee'].should == {'cost' => 1000.12}
end
it "returns a subset of records in the collection using an advanced selector" do
get "/#{$DB}/testing", {:selector => {'fee.cost' => {'$exists' => true}, 'foo' => {'$not' => {'$exists' => true}}}.to_json}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 3
end
it "returns a sorted subset of records in the collection using an advanced selector" do
get "/#{$DB}/testing", {:selector => {'fee.cost' => {'$exists' => true}, 'foo' => {'$not' => {'$exists' => true}}}.to_json,
:sort => 'faa'}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 3
r.first['faa'].should == 'aaa'
r.last['faa'].should == 'ccc'
end
it "returns a reverse-sorted subset of records in the collection using an advanced selector" do
get "/#{$DB}/testing", {:selector => {'fee.cost' => {'$exists' => true}, 'foo' => {'$not' => {'$exists' => true}}}.to_json,
:sort => [['fee.cost', 'desc']].to_json}
last_response.status.should == 200
r = JSON.parse(last_response.body)
r.length.should == 3
r.first['faa'].should == 'aaa'
r.last['faa'].should == 'ccc'
end
end
end
describe "POST" do
it "adds an item to the collection" do
post "/#{$DB}/testing", {"foo" => "faa"}
last_response.status.should == 201
foo = @coll.find({'foo' => 'faa'}).to_a
foo.length.should == 1
JSON.parse(last_response.body).should ==
{'foo' => 'faa', '_id' => { '$oid' => foo.first['_id'].to_s }}
post "/#{$DB}/testing", {"fee" => "foe"}
last_response.status.should == 201
@coll.find().to_a.length.should == 2
fee = @coll.find({'fee' => 'foe'}).to_a
fee.length.should == 1
JSON.parse(last_response.body).should ==
{'fee' => 'foe', '_id' => { '$oid' => fee.first['_id'].to_s }}
end
end
describe "DELETE" do
it "removes a collection from the db" do
coll_to_be_deleted = 'to_be_deleted'
# sanity check first
@db.drop_collection(coll_to_be_deleted)
post "/#{$DB}", :collection => coll_to_be_deleted
last_response.status.should == 201
@db.collection(coll_to_be_deleted).should_not be_nil
delete "/#{$DB}/#{coll_to_be_deleted}"
last_response.status.should == 200
@db.collection_names.should_not include(coll_to_be_deleted)
#get "/#{$DB}/#{coll_to_be_deleted}"
#last_response.status.should == 404
end
end
describe "/db/collection/id" do
describe "GET" do
it "returns a record from the collection by its id" do
id = @coll.save({"foo" => "faa"})
get "/#{$DB}/testing/#{id}"
last_response.status.should == 200
JSON.parse(last_response.body).should == {'foo' => 'faa', '_id' => { '$oid' => id.to_s }}
end
it "returns a 404 if the item requested from the collection by its id does not exist" do
id = BSON::ObjectId("000000000000000000000000") # we're assuming an item with this id does not exist
get "/#{$DB}/testing/#{id}"
last_response.status.should == 404
end
it "JSONifies ISODate values as { $date: '...' }" do
date1 = Time.now
date2 = Time.at(Time.now.to_i - 60 * 60 * 10)
id = @coll.save({"foo" => date1, "bar" => {"bah" => date2}})
get "/#{$DB}/testing/#{id}"
doc = JSON.parse(last_response.body)
doc['foo'].should == { "$date" => date1.getutc.iso8601(1) }
doc['bar']['bah'].should == { "$date" => date2.getutc.iso8601(1) }
end
end
describe "PUT" do
it "replaces an item in the collection" do
id = @coll.save({"foo" => "faa"})
fff = {"ggg" => "hhh"}
put "/#{$DB}/testing/#{id}", fff
last_response.status.should == 200
JSON.parse(last_response.body).should ==
fff.merge('_id' => { '$oid' => id.to_s })
foo = @coll.find_one(id)
foo.should == fff.merge('_id' => id)
foo['foo'].should be_nil
foo['ggg'].should == 'hhh'
end
it "stores dates encoded as { $date: '...' } as ISODate" do
id = @coll.save({"foo" => "faa"})
date1 = Time.now
date2 = Time.at(Time.now.to_i - 60 * 60 * 10)
fff = {"foo" => {"$date" => date1.iso8601(1)}, "bar" => {"bah" => {"$date" => date2.iso8601(1)}}}
put "/#{$DB}/testing/#{id}", fff
last_response.status.should == 200
foo = @coll.find_one(id)
# NOTE: Mongo stores times as UTC
foo['foo'].to_i.should == date1.getutc.to_i
foo['bar']['bah'].to_i.should == date2.getutc.to_i
end
# test for bug introduced in c7c944e
it "can deal with JSON input containing arrays" do
id = @coll.save({"foo" => "faa"})
fff = {"foo" => [1,2,3], "bar" => {"bah" => ["a","b","c"]}}
put "/#{$DB}/testing/#{id}", fff.to_json
last_response.status.should == 200
foo = @coll.find_one(id)
foo['foo'].should be_instance_of(Array)
foo['foo'][0].should == 1
foo['bar']['bah'].should be_instance_of(Array)
foo['bar']['bah'][1].should == "b"
end
end
describe "PATCH" do
it "updates an item in the collection" do
id = @coll.save({"foo" => "faa"})
fff = {"ggg" => "hhh"}
patch "/#{$DB}/testing/#{id}", fff
last_response.status.should == 200
JSON.parse(last_response.body).should ==
fff.merge('_id' => { '$oid' => id.to_s }, 'foo' => 'faa')
foo = @coll.find_one(id)
foo['foo'].should == 'faa'
foo['ggg'].should == 'hhh'
end
it "doesn't do weird things to the id attribute" do
id = @coll.save({"boo" => "baa"})
fff = {
"ggg" => "hhh",
"_id" => "badid1",
"id" => "badid2"
}
patch "/#{$DB}/testing/#{id}", fff
last_response.status.should == 200
JSON.parse(last_response.body).should ==
{"_id"=> {"$oid" => id.to_s}, "boo"=>"baa", "ggg"=>"hhh"}
foo = @coll.find_one(id)
foo['ggg'].should == 'hhh'
foo['_id'].should == id
foo['id'].should == nil
end
end
describe "DELETE" do
it "deletes an item from the collection" do
id = @coll.save({"foo" => "faa"})
delete "/#{$DB}/testing/#{id}"
last_response.status.should == 200
foo = @coll.find_one(id)
foo.should be_nil
end
end
end
end
end