diff --git a/.gitignore b/.gitignore index 0052ee7..32be419 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ t pkg doc *.gemspec +mongo-qa diff --git a/README.rdoc b/README.rdoc index 28a96af..efb9ab1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 diff --git a/Rakefile b/Rakefile index 5d21d91..ea89b64 100644 --- a/Rakefile +++ b/Rakefile @@ -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') diff --git a/lib/mongo/util/xml_to_ruby.rb b/lib/mongo/util/xml_to_ruby.rb index 7b2b593..11d4a84 100644 --- a/lib/mongo/util/xml_to_ruby.rb +++ b/lib/mongo/util/xml_to_ruby.rb @@ -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 diff --git a/tests/data/symbols.xml b/tests/data/symbols.xson similarity index 100% rename from tests/data/symbols.xml rename to tests/data/symbols.xson diff --git a/tests/test_round_trip.rb b/tests/test_round_trip.rb index bff1cab..e3dc1b9 100644 --- a/tests/test_round_trip.rb +++ b/tests/test_round_trip.rb @@ -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