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,8 +42,26 @@ 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')
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
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
def one_round_trip(dir, name)
obj = File.open(File.join(dir, "#{name}.xson")) { |f|
XMLToRuby.new.xml_to_ruby(f)
}
File.open(File.join(dir, "#{name}.bson"), 'r') { |f|
# Read the BSON from the file # Read the BSON from the file
bson = f.read bson = f.read
bson = if RUBY_VERSION >= '1.9' bson = if RUBY_VERSION >= '1.9'
@ -57,7 +75,7 @@ class RoundTripTest < Test::Unit::TestCase
bson_from_ruby = BSON.new.serialize(obj).to_a bson_from_ruby = BSON.new.serialize(obj).to_a
# # DEBUG # # DEBUG
# File.open(File.join(HERE, 'data', "#{name}_out.bson"), 'wb') { |f| # File.open(File.join(dir, "#{name}_out.bson"), 'wb') { |f|
# bson_from_ruby.each { |b| f.putc(b) } # bson_from_ruby.each { |b| f.putc(b) }
# } # }
@ -65,7 +83,7 @@ class RoundTripTest < Test::Unit::TestCase
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 rescue => ex
$stderr.puts "failure while round-tripping #{name}" # DEBUG $stderr.puts "failure while round-tripping #{dir}/#{name}" # DEBUG
raise ex raise ex
end end
@ -82,7 +100,6 @@ class RoundTripTest < Test::Unit::TestCase
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
} }
}
end end
end end