Improved Admin#validate_collection, tests, and example.
This commit is contained in:
parent
11d2cd2b45
commit
768a3ab1a5
|
@ -8,28 +8,34 @@ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|||
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
||||
|
||||
puts "Connecting to #{host}:#{port}"
|
||||
db = Mongo.new(host, port).db('ruby-mongo-examples')
|
||||
coll = db.collection('test')
|
||||
# db = Mongo.new(host, port).db('ruby-mongo-examples')
|
||||
db = Mongo.new('192.168.19.123', port).db('ruby-mongo-examples')
|
||||
coll = db.create_collection('test')
|
||||
|
||||
# Erase all records from collection, if any
|
||||
coll.clear
|
||||
|
||||
admin = db.admin
|
||||
|
||||
# Profiling level set/get
|
||||
p admin.profiling_level
|
||||
# # Profiling level set/get
|
||||
# p admin.profiling_level
|
||||
|
||||
# Start profiling everything
|
||||
admin.profiling_level = :all
|
||||
# # Start profiling everything
|
||||
# admin.profiling_level = :all
|
||||
|
||||
# Read records, creating a profiling event
|
||||
coll.find().to_a
|
||||
# # Read records, creating a profiling event
|
||||
# coll.find().to_a
|
||||
|
||||
# Stop profiling
|
||||
admin.profiling_level = :off
|
||||
# # Stop profiling
|
||||
# admin.profiling_level = :off
|
||||
|
||||
# Print all profiling info
|
||||
pp admin.profiling_info
|
||||
# # Print all profiling info
|
||||
# pp admin.profiling_info
|
||||
|
||||
# Validate returns a hash if all is well or raises an exception if there is a
|
||||
# problem.
|
||||
info = admin.validate_collection(coll.name)
|
||||
puts info['result']
|
||||
|
||||
# Destroy the collection
|
||||
coll.drop
|
||||
|
|
|
@ -70,14 +70,15 @@ module XGen
|
|||
end
|
||||
|
||||
# Validate a named collection by raising an exception if there is a
|
||||
# problem or returning +true+ if all is well.
|
||||
# problem or returning an interesting hash (see especially the
|
||||
# 'result' string value) if all is well.
|
||||
def validate_collection(name)
|
||||
doc = @db.db_command(:validate => name)
|
||||
raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
|
||||
result = doc['result']
|
||||
raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
||||
raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
||||
true
|
||||
doc
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -54,7 +54,11 @@ class AdminTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_validate_collection
|
||||
assert @admin.validate_collection(@coll.name)
|
||||
doc = @admin.validate_collection(@coll.name)
|
||||
assert_not_nil doc
|
||||
result = doc['result']
|
||||
assert_not_nil result
|
||||
assert_match /firstExtent/, result
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue