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
doc
*.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 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

View File

@ -42,6 +42,17 @@ Rake::TestTask.new do |t|
t.test_files = FileList['tests/test*.rb']
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"
task :rdoc do
FileUtils.rm_rf('html')

View File

@ -17,7 +17,7 @@
require 'rexml/document'
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.
class XMLToRuby

View File

@ -8,9 +8,9 @@ require 'test/unit'
# OrderedHash and then test both Ruby-to-BSON and BSON-to-Ruby translations.
#
# 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
# checks out those tests and modify this test to run those if they ave been
# checked out.
# (http://github.com/mongodb/mongo-qa). If the directory ../mongo-qa exists,
# then we find the BSON test files there and use those, too. Use the Rake task
# "mongo_qa" to obtain those tests.
class RoundTripTest < Test::Unit::TestCase
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
# generated)
def test_round_trip
@@ruby.each { |name, obj|
File.open(File.join(HERE, 'data', "#{name}.bson"), 'r') { |f|
# Read the BSON from the file
bson = f.read
bson = if RUBY_VERSION >= '1.9'
bson.bytes.to_a
else
bson.split(//).collect { |c| c[0] }
end
round_trip_files_in_dir(File.join(HERE, 'data'))
mongo_qa_dir = File.join(HERE, '..', 'mongo-qa/modules/bson_tests')
if File.exist?(mongo_qa_dir)
%w(basic_types complex single_types).each { |subdir_name|
round_trip_files_in_dir(File.join(mongo_qa_dir, subdir_name))
}
end
end
# Turn the Ruby object into BSON bytes and compare with the BSON bytes
# from the file.
bson_from_ruby = BSON.new.serialize(obj).to_a
def round_trip_files_in_dir(dir)
names = Dir[File.join(dir, '*.xson')].collect {|f| File.basename(f).sub(/\.xson$/, '') }
names.each { |name| one_round_trip(dir, name) }
end
# # DEBUG
# File.open(File.join(HERE, 'data', "#{name}_out.bson"), 'wb') { |f|
# bson_from_ruby.each { |b| f.putc(b) }
# }
def one_round_trip(dir, name)
obj = File.open(File.join(dir, "#{name}.xson")) { |f|
XMLToRuby.new.xml_to_ruby(f)
}
begin
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
rescue => ex
$stderr.puts "failure while round-tripping #{name}" # DEBUG
raise ex
end
File.open(File.join(dir, "#{name}.bson"), 'r') { |f|
# Read the BSON from the file
bson = f.read
bson = if RUBY_VERSION >= '1.9'
bson.bytes.to_a
else
bson.split(//).collect { |c| c[0] }
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 the Ruby object into BSON bytes and compare with the BSON bytes
# from the file.
bson_from_ruby = BSON.new.serialize(obj).to_a
# 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
# # DEBUG
# File.open(File.join(dir, "#{name}_out.bson"), 'wb') { |f|
# bson_from_ruby.each { |b| f.putc(b) }
# }
begin
assert_equal bson.length, bson_from_ruby.length
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