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
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
||||||
|
|
||||||
puts "Connecting to #{host}:#{port}"
|
puts "Connecting to #{host}:#{port}"
|
||||||
db = Mongo.new(host, port).db('ruby-mongo-examples')
|
# db = Mongo.new(host, port).db('ruby-mongo-examples')
|
||||||
coll = db.collection('test')
|
db = Mongo.new('192.168.19.123', port).db('ruby-mongo-examples')
|
||||||
|
coll = db.create_collection('test')
|
||||||
|
|
||||||
# Erase all records from collection, if any
|
# Erase all records from collection, if any
|
||||||
coll.clear
|
coll.clear
|
||||||
|
|
||||||
admin = db.admin
|
admin = db.admin
|
||||||
|
|
||||||
# Profiling level set/get
|
# # Profiling level set/get
|
||||||
p admin.profiling_level
|
# p admin.profiling_level
|
||||||
|
|
||||||
# Start profiling everything
|
# # Start profiling everything
|
||||||
admin.profiling_level = :all
|
# admin.profiling_level = :all
|
||||||
|
|
||||||
# Read records, creating a profiling event
|
# # Read records, creating a profiling event
|
||||||
coll.find().to_a
|
# coll.find().to_a
|
||||||
|
|
||||||
# Stop profiling
|
# # Stop profiling
|
||||||
admin.profiling_level = :off
|
# admin.profiling_level = :off
|
||||||
|
|
||||||
# Print all profiling info
|
# # Print all profiling info
|
||||||
pp admin.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
|
# Destroy the collection
|
||||||
coll.drop
|
coll.drop
|
||||||
|
|
|
@ -70,14 +70,15 @@ module XGen
|
||||||
end
|
end
|
||||||
|
|
||||||
# Validate a named collection by raising an exception if there is a
|
# 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)
|
def validate_collection(name)
|
||||||
doc = @db.db_command(:validate => name)
|
doc = @db.db_command(:validate => name)
|
||||||
raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
|
raise "Error with validate command: #{doc.inspect}" unless @db.ok?(doc)
|
||||||
result = doc['result']
|
result = doc['result']
|
||||||
raise "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
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
|
raise "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
||||||
true
|
doc
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,7 +54,11 @@ class AdminTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_validate_collection
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue