Use mongo-qa tests if present

This commit is contained in:
Jim Menard 2009-01-12 09:48:24 -05:00
parent 538f1441d4
commit 80dd421dd9
6 changed files with 80 additions and 37 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ t
pkg pkg
doc doc
*.gemspec *.gemspec
mongo-qa

View File

@ -57,6 +57,20 @@ If you have the source code, you can run the tests.
The tests assume that the Mongo database is running on the default port. The tests assume that the Mongo database is running on the default 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, run
$ rake mongo_qa
$ rake test
The mongo_qa task uses the "git clone" command to make a copy of that project
in a directory named mongo-qa. If the directory already exists, then the
mongo_qa task uses "git pull" to updated the code that's there. The Ruby
driver tests will then use some of the data files from that project when it
runs BSON tests. You can delete this directory at any time if you don't want
to run those tests any more.
= Documentation = Documentation

View File

@ -42,6 +42,17 @@ Rake::TestTask.new do |t|
t.test_files = FileList['tests/test*.rb'] t.test_files = FileList['tests/test*.rb']
end end
desc "Clone or pull (update) the mongo-qa project used for testing"
task :mongo_qa do
if File.exist?('mongo-qa')
Dir.chdir('mongo-qa') do
system('git pull')
end
else
system('git clone git://github.com/mongodb/mongo-qa.git')
end
end
desc "Generate documentation" desc "Generate documentation"
task :rdoc do task :rdoc do
FileUtils.rm_rf('html') FileUtils.rm_rf('html')

View File

@ -17,7 +17,7 @@
require 'rexml/document' require 'rexml/document'
require 'mongo' require 'mongo'
# Converts a .bson file (an XML file that describes a Mongo-type document) to # Converts a .xson file (an XML file that describes a Mongo-type document) to
# an OrderedHash. # an OrderedHash.
class XMLToRuby class XMLToRuby

View File

@ -8,9 +8,9 @@ require 'test/unit'
# OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations. # OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations.
# #
# There is a whole other project that includes similar tests # There is a whole other project that includes similar tests
# (http://github.com/mongodb/mongo-qa). I'm going to write a Rake task that # (http://github.com/mongodb/mongo-qa). If the directory ../mongo-qa exists,
# checks out those tests and modify this test to run those if they ave been # then we find the BSON test files there and use those, too. Use the Rake task
# checked out. # "mongo_qa" to obtain those tests.
class RoundTripTest < Test::Unit::TestCase class RoundTripTest < Test::Unit::TestCase
include XGen::Mongo::Driver include XGen::Mongo::Driver
@ -42,46 +42,63 @@ class RoundTripTest < Test::Unit::TestCase
# * Compare that with the BSON files we have (or the bytes that were already # * Compare that with the BSON files we have (or the bytes that were already
# generated) # generated)
def test_round_trip def test_round_trip
@@ruby.each { |name, obj| round_trip_files_in_dir(File.join(HERE, 'data'))
File.open(File.join(HERE, 'data', "#{name}.bson"), 'r') { |f| mongo_qa_dir = File.join(HERE, '..', 'mongo-qa/modules/bson_tests')
# Read the BSON from the file if File.exist?(mongo_qa_dir)
bson = f.read %w(basic_types complex single_types).each { |subdir_name|
bson = if RUBY_VERSION >= '1.9' round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
bson.bytes.to_a }
else end
bson.split(//).collect { |c| c[0] } end
end
# Turn the Ruby object into BSON bytes and compare with the BSON bytes def round_trip_files_in_dir(dir)
# from the file. names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
bson_from_ruby = BSON.new.serialize(obj).to_a names.each { |name| one_round_trip(dir, name) }
end
# # DEBUG def one_round_trip(dir, name)
# File.open(File.join(HERE, 'data', "#{name}_out.bson"), 'wb') { |f| obj = File.open(File.join(dir, "#{name}.xson")) { |f|
# bson_from_ruby.each { |b| f.putc(b) } XMLToRuby.new.xml_to_ruby(f)
# } }
begin File.open(File.join(dir, "#{name}.bson"), 'r') { |f|
assert_equal bson.length, bson_from_ruby.length # Read the BSON from the file
assert_equal bson, bson_from_ruby bson = f.read
rescue => ex bson = if RUBY_VERSION >= '1.9'
$stderr.puts "failure while round-tripping #{name}" # DEBUG bson.bytes.to_a
raise ex else
end bson.split(//).collect { |c| c[0] }
end
# Turn those BSON bytes back into a Ruby object. # Turn the Ruby object into BSON bytes and compare with the BSON bytes
# # from the file.
# We're passing a nil db to the contructor here, but that's OK because bson_from_ruby = BSON.new.serialize(obj).to_a
# the BSON bytes don't contain the db object in any case.
obj_from_bson = BSON.new(nil).deserialize(ByteBuffer.new(bson_from_ruby))
assert_kind_of OrderedHash, obj_from_bson
# Turn that Ruby object into BSON and compare it to the original BSON # # DEBUG
# bytes. # File.open(File.join(dir, "#{name}_out.bson"), 'wb') { |f|
bson_from_ruby = BSON.new.serialize(obj_from_bson).to_a # bson_from_ruby.each { |b| f.putc(b) }
# }
begin
assert_equal bson.length, bson_from_ruby.length assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby assert_equal bson, bson_from_ruby
} rescue => ex
$stderr.puts "failure while round-tripping #{dir}/#{name}" # DEBUG
raise ex
end
# Turn those BSON bytes back into a Ruby object.
#
# We're passing a nil db to the contructor here, but that's OK because
# the BSON bytes don't contain the db object in any case.
obj_from_bson = BSON.new(nil).deserialize(ByteBuffer.new(bson_from_ruby))
assert_kind_of OrderedHash, obj_from_bson
# Turn that Ruby object into BSON and compare it to the original BSON
# bytes.
bson_from_ruby = BSON.new.serialize(obj_from_bson).to_a
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
} }
end end