Skip to content

Commit

Permalink
minor: removed shoulda dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Feb 20, 2010
1 parent 0368e79 commit 54a68c7
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 297 deletions.
26 changes: 3 additions & 23 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -292,36 +292,17 @@ It's also possible to test replica pairs with connection pooling:

$ rake test:pooled_pair_insert

===Shoulda and Mocha
===Mocha

All tests now require shoulda and mocha. You can install these gems as
follows:
Running the test suite requires mocha. You can install it as follows:

$ gem install shoulda
$ gem install mocha

The tests assume that the Mongo database is running on the default port. You
can override the default host (localhost) and port (Connection::DEFAULT_PORT) by
using the environment variables MONGO_RUBY_DRIVER_HOST and
MONGO_RUBY_DRIVER_PORT.

The project mongo-qa (http://github.com/mongodb/mongo-qa) contains many more
Mongo driver tests that are language independent. To run thoses tests as part
of the "rake test" task, download the code "next to" this directory. So, after
installing the mongo-qa code you would have these two directories next to each
other:

$ ls
mongo-qa
mongo-ruby-driver
$ rake test

The tests run just fine if the mongo-qa directory is not there.

Additionally, the script bin/validate is used by the mongo-qa project's
validator script.


= Documentation

This documentation is available online at http://api.mongodb.org/ruby. You can
Expand All @@ -342,7 +323,7 @@ See CREDITS.

= License

Copyright 2008-2009 10gen Inc.
Copyright 2008-2010 10gen Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -355,4 +336,3 @@ See CREDITS.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

18 changes: 7 additions & 11 deletions test/binary_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# encoding:utf-8

require 'test/test_helper'

class BinaryTest < Test::Unit::TestCase

context "Inspecting" do
setup do
@data = ("THIS IS BINARY " * 50).unpack("c*")
end
context "Inspecting" do
setup do
@data = ("THIS IS BINARY " * 50).unpack("c*")
end

should "not display actual data" do
binary = Mongo::Binary.new(@data)
assert_equal "<Mongo::Binary:#{binary.object_id}>", binary.inspect
end
should "not display actual data" do
binary = Mongo::Binary.new(@data)
assert_equal "<Mongo::Binary:#{binary.object_id}>", binary.inspect
end
end
8 changes: 4 additions & 4 deletions test/grid_file_system_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'test/test_helper'
include Mongo

class GridTest < Test::Unit::TestCase

def setup
context "GridFileSystem:" do
setup do
@db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@files = @db.collection('fs.files')
@chunks = @db.collection('fs.chunks')
end

def teardown
teardown do
@files.remove
@chunks.remove
end
Expand Down
10 changes: 4 additions & 6 deletions test/grid_io_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
require 'test/test_helper'
include Mongo

class GridIOTest < Test::Unit::TestCase
include GridFS

def setup
context "" do
setup do
@db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@files = @db.collection('fs.files')
@chunks = @db.collection('fs.chunks')
end

def teardown
teardown do
@files.remove
@chunks.remove
end
Expand All @@ -30,6 +29,5 @@ def teardown
file = GridIO.new(@files, @chunks, @filename, @mode, false, :chunk_size => 1000)
assert_equal 1000, file.chunk_size
end

end
end
40 changes: 20 additions & 20 deletions test/grid_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'test/test_helper'
include Mongo

class GridTest < Test::Unit::TestCase

def setup
context "Tests:" do
setup do
@db ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
@files = @db.collection('test-fs.files')
@chunks = @db.collection('test-fs.chunks')
end

def teardown
teardown do
@files.remove
@chunks.remove
end
Expand Down Expand Up @@ -44,24 +44,24 @@ def teardown
end
end

def read_and_write_stream(filename, read_length, opts={})
io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
id = @grid.put(io, filename + read_length.to_s, opts)
file = @grid.get(id)
io.rewind
data = io.read
if data.respond_to?(:force_encoding)
data.force_encoding(:binary)
end
read_data = ""
while(chunk = file.read(read_length))
read_data << chunk
end
assert_equal data.length, read_data.length
end

context "Streaming: " do
setup do
def read_and_write_stream(filename, read_length, opts={})
io = File.open(File.join(File.dirname(__FILE__), 'data', filename), 'r')
id = @grid.put(io, filename + read_length.to_s, opts)
file = @grid.get(id)
io.rewind
data = io.read
if data.respond_to?(:force_encoding)
data.force_encoding(:binary)
end
read_data = ""
while(chunk = file.read(read_length))
read_data << chunk
end
assert_equal data.length, read_data.length
end

@grid = Grid.new(@db, 'test-fs')
end

Expand Down
34 changes: 30 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

begin
require 'rubygems'
require 'shoulda'
require 'mocha'
rescue LoadError
puts <<MSG
This test suite now requires shoulda and mocha.
You can install these gems as follows:
gem install shoulda
This test suite requires mocha.
You can install it as follows:
gem install mocha
MSG
Expand Down Expand Up @@ -40,3 +38,31 @@ def rescue_connection_failure
end
end
end

# shoulda-mini
# based on test/spec/mini 5
# http://gist.github.com/307649
# chris@ozmm.org
#
def context(*args, &block)
return super unless (name = args.first) && block
require 'test/unit'
klass = Class.new(Test::Unit::TestCase) do
def self.should(name, &block)
define_method("test_#{name.to_s.gsub(/\W/,'_')}", &block) if block
end
def self.xshould(*args) end
def self.context(*args, &block) instance_eval(&block) end
def self.setup(&block)
define_method(:setup) { self.class.setups.each { |s| instance_eval(&s) } }
setups << block
end
def self.setups; @setups ||= [] end
def self.teardown(&block) define_method(:teardown, &block) end
end
(class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
klass.class_eval do
include Mongo
end
klass.class_eval &block
end
93 changes: 44 additions & 49 deletions test/unit/collection_test.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,58 @@
require 'test/test_helper'

class ConnectionTest < Test::Unit::TestCase

context "Basic operations: " do
setup do
@logger = mock()
end
context "Basic operations: " do
setup do
@logger = mock()
end

should "send update message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message).with do |op, msg, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'})
should "send update message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message).with do |op, msg, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'})
end

should "send insert message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message).with do |op, msg, log|
op == 2002 && log.include?("db.books.insert")
end
@coll.insert({:title => 'Moby Dick'})
should "send insert message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message).with do |op, msg, log|
op == 2002 && log.include?("db.books.insert")
end
@coll.insert({:title => 'Moby Dick'})
end

should "not log binary data" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
data = Mongo::Binary.new(("BINARY " * 1000).unpack("c*"))
@conn.expects(:send_message).with do |op, msg, log|
op == 2002 && log.include?("Mongo::Binary")
end
@coll.insert({:data => data})
should "not log binary data" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
data = Mongo::Binary.new(("BINARY " * 1000).unpack("c*"))
@conn.expects(:send_message).with do |op, msg, log|
op == 2002 && log.include?("Mongo::Binary")
end
@coll.insert({:data => data})
end

should "send safe update message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
should "send safe update message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
end

should "send safe insert message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
should "send safe insert message" do
@conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
@db = @conn['testing']
@coll = @db.collection('books')
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
op == 2001 && log.include?("db.books.update")
end
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
end
end


Loading

0 comments on commit 54a68c7

Please sign in to comment.