Skip to content

Commit

Permalink
Added specs for mongo driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMonkeySteve committed Oct 26, 2010
1 parent bfe4510 commit 3aefe23
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 64 deletions.
69 changes: 69 additions & 0 deletions spec/em-mongo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "spec/helper/all"

describe EM::Mongo do

it "should yield until connection is ready" do
EventMachine.synchrony do
connection = EM::Mongo::Connection.new
connection.connected?.should be_true

db = connection.db('db')
db.is_a?(EventMachine::Mongo::Database).should be_true

EventMachine.stop
end
end

it "should insert a record into db" do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
obj.keys.should include '_id'

obj = collection.find
obj.size.should == 1
obj.first['hello'].should == 'world'

EventMachine.stop
end
end

it "should insert a record into db" do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
obj = collection.insert('hello2' => 'world2')

obj = collection.find({})
obj.size.should == 2

obj2 = collection.find({}, {:limit => 1})
obj2.size.should == 1

obj3 = collection.first
obj3.is_a?(Hash).should be_true

EventMachine.stop
end
end

it "should update records in db" do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
collection.update({'hello' => 'world'}, {'hello' => 'newworld'})

new_obj = collection.first({'_id' => obj['_id']})
new_obj['hello'].should == 'newworld'

EventMachine.stop
end
end

end
7 changes: 7 additions & 0 deletions spec/helper/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

def now(); Time.now.to_f; end

def silence_warnings()
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end

RSpec.configure do |config|
config.include(Sander6::CustomMatchers)
end
71 changes: 7 additions & 64 deletions spec/mongo_spec.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,12 @@
require "spec/helper/all"
require 'spec/helper/all'
require 'lib/em-synchrony/mongo'
require 'mongo'

describe EM::Mongo do

it "should yield until connection is ready" do
EventMachine.synchrony do
connection = EM::Mongo::Connection.new
connection.connected?.should be_true

db = connection.db('db')
db.is_a?(EventMachine::Mongo::Database).should be_true

EventMachine.stop
end
end

it "should insert a record into db" do
describe Mongo::Connection do
it 'connects to DB' do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
obj.keys.should include '_id'

obj = collection.find
obj.size.should == 1
obj.first['hello'].should == 'world'

EventMachine.stop
conn = Mongo::Connection.new 'localhost', 27017, :connect => true
EM.stop
end
end

it "should insert a record into db" do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
obj = collection.insert('hello2' => 'world2')

obj = collection.find({})
obj.size.should == 2

obj2 = collection.find({}, {:limit => 1})
obj2.size.should == 1

obj3 = collection.first
obj3.is_a?(Hash).should be_true

EventMachine.stop
end
end

it "should update records in db" do
EventMachine.synchrony do
collection = EM::Mongo::Connection.new.db('db').collection('test')
collection.remove({}) # nuke all keys in collection

obj = collection.insert('hello' => 'world')
collection.update({'hello' => 'world'}, {'hello' => 'newworld'})

new_obj = collection.first({'_id' => obj['_id']})
new_obj['hello'].should == 'newworld'

EventMachine.stop
end
end

end

0 comments on commit 3aefe23

Please sign in to comment.