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
|
require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../')
describe EMMongo::Cursor do
include EM::Spec
it 'should describe itself via inspect' do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new( @coll, :selector => {'a' => 1} )
cursor.inspect.should == "<EM::Mongo::Cursor:0x#{cursor.object_id.to_s} namespace='#{@coll.db.name}.#{@coll.name}' " +
"@selector=#{cursor.selector.inspect}>"
done
end
it 'should explain itself' do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll, :selector => {'a' => 1} )
cursor.explain.callback do |explanation|
explanation['cursor'].should_not be_nil
explanation['n'].should be_kind_of Numeric
explanation['millis'].should be_kind_of Numeric
explanation['nscanned'].should be_kind_of Numeric
done
end
end
it "should allow limit and skip to be chained" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
all = []
10.times do |i|
all << {"x" => i}
@coll.save(all[-1])
end
cursor.limit(5).skip(3).sort("x",1).defer_as_a.callback do |results|
all.slice(3...8).each_with_index do |item,idx|
results[idx]["x"].should == item["x"]
end
done
end
end
it "should allow a limit larger than the batch size" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll, :selector => {})
all = []
1501.times do |i|
@coll.insert(i.to_s => i.to_s)
end
cursor.limit(1500).defer_as_a.callback do |docs|
docs.length.should == 1500
done
end
end
it "should say if it has next" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
1.times do |i|
@coll.save("x" => 1)
end
cursor.has_next?.callback do |result|
result.should be_true
cursor.next_document.callback do |doc|
cursor.has_next?.callback do |result|
result.should be_false
done
end
end
end
end
it "should rewind" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
100.times do |i|
@coll.save("x" => 1)
end
cursor.defer_as_a.callback do |r1|
r1.length.should == 100
cursor.defer_as_a.callback do |r2|
r2.length.should == 0
cursor.rewind!
cursor.defer_as_a.callback do |r3|
r3.length.should == 100
done
end
end
end
end
describe "Get More" do
it "should refill via get more" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
1000.times do |i|
@coll.save("x" => 1)
end
cursor.defer_as_a.callback do |results|
results.length.should == 1000
done
end
end
end
describe "Count" do
it 'should count 0 records in a empty collection' do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
cursor.count.callback do |c|
c.should == 0
done
end
end
it "should count records in a collection" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll)
10.times do |i|
@coll.save("x" => 1)
end
cursor.count.callback do |c|
c.should == 10
done
end
end
it "should ignore skip and limit by default" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll).skip(5).limit(5)
10.times do |i|
@coll.save("x" => i)
end
cursor.count.callback do |c|
c.should == 10
done
end
end
it "should account for skip when requested" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll).limit(5)
10.times do |i|
@coll.save("x" => i)
end
cursor.count(true).callback do |c|
c.should == 5
done
end
end
it "should account for skip when requested" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll).skip(5)
10.times do |i|
@coll.save("x" => i)
end
cursor.count(true).callback do |c|
c.should == 5
done
end
end
it "should count based on a simple selector" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll, :selector => {"x"=>1})
10.times do |i|
@coll.save("x" => i)
end
cursor.count(true).callback do |c|
c.should == 1
done
end
end
it "should count based on a selector with an operator" do
@conn, @coll = connection_and_collection
cursor = EM::Mongo::Cursor.new(@coll, :selector => {"x"=>{"$lt"=>5}})
10.times do |i|
@coll.save("x" => i)
end
cursor.count(true).callback do |c|
c.should == 5
done
end
end
it "should count a non-existing collection as 0 without vomiting blood" do
@conn, @coll = connection_and_collection
@coll = @conn.db.collection('imnotreallyheredontlookatme')
cursor = EM::Mongo::Cursor.new(@coll)
cursor.count(true).callback do |c|
c.should == 0
done
end
end
end
describe "Sort" do
it "should sort ascending" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.save("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort(:x, 1)
cursor.next_document.callback do |first|
first["x"].should == 0
done
end
end
it "should sort descending" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.save("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort(:x, -1)
cursor.next_document.callback do |first|
first["x"].should == 4
done
end
end
it "should sort descending using a symbol sort dir" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.save("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort(["x", :desc])
cursor.next_document.callback do |first|
first["x"].should == 4
done
end
end
it "should not allow sort to be called on an executed cursor" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.save("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort(["x", :desc])
cursor.next_document.callback do |first|
lambda { cursor.sort("x",1) }.should raise_error EM::Mongo::InvalidOperation
done
end
end
it "should sort by dates" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.insert("x" => Time.utc(2000 + i))
end
cursor = EM::Mongo::Cursor.new(@coll).sort(["x", :desc])
cursor.next_document.callback do |first|
first["x"].year.should == 2004
done
end
end
describe "Each" do
it "should iterate through each doc, returning null when done" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.insert("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll)
counter = 0
cursor.each do |doc|
if doc
counter+=1
else
counter.should == 5
done
end
end
end
end
describe "defer_as_a" do
it "should return an array of all documents in a query" do
@conn, @coll = connection_and_collection
5.times do |i|
@coll.insert("x" => i)
end
cursor = EM::Mongo::Cursor.new(@coll).sort("x",1)
cursor.defer_as_a.callback do |docs|
docs.length.should == 5
5.times do |i|
docs[i]["x"].should == i
end
done
end
end
end
describe "Transformer (a robot in disguise)" do
it "should set the transformer when passed in the constructor" do
@conn, @coll = connection_and_collection
transformer = Proc.new {|doc|doc}
cursor = EM::Mongo::Cursor.new(@coll, :transformer => transformer)
cursor.transformer.should == transformer
done
end
it "should transform docs with next" do
@conn, @coll = connection_and_collection
@coll.insert({:a=>1})
klass = Struct.new(:id,:a)
transformer = Proc.new {|doc|klass.new(doc['_id'],doc['a'])}
cursor = EM::Mongo::Cursor.new(@coll, :transformer => transformer)
cursor.next.callback do |doc|
doc.should be_kind_of klass
doc.id.should be_kind_of BSON::ObjectId
doc.a.should == 1
done
end
end
it "should transform docs with each" do
@conn, @coll = connection_and_collection
@coll.insert({:a=>1})
klass = Struct.new(:id, :a)
transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
cursor = EM::Mongo::Cursor.new(@coll, :transformer => transformer)
cursor.each do |doc|
if doc
doc.should be_kind_of klass
doc.id.should be_kind_of BSON::ObjectId
doc.a.should == 1
end
done
end
end
end
end
end
|