Permalink
Browse files

added tests and bug fixes

  • Loading branch information...
1 parent ed22381 commit 44b02d3d2a8e983becedfbd4cd3b3cba2217ed7f @derailed committed Dec 28, 2009
View
@@ -36,6 +36,8 @@
== DEPENDENCIES
+* At this time mongo3 only is supported on ruby 1.8 and sinatra 0.9.4
+
* mongo + mongo_ext
* agnostic-will_paginate
* memcache-client
View
@@ -1,7 +1,6 @@
# Look in the tasks/setup.rb file for the various options that can be
# configured in this Rakefile. The .rake files in the tasks directory
# are where the options are used.
-
begin
require 'bones'
Bones.setup
@@ -12,6 +12,17 @@ module Databases
erb :'databases/list'
end
+ # ---------------------------------------------------------------------------
+ get "/databases/collection/:name/" do
+ cltn_name = params[:name]
+ path_names = session[:path_names]
+ path_ids = session[:path_ids]
+
+ update_paths!( path_ids + "|" + cltn_name, path_names + "|" + cltn_name )
+
+ redirect "/collections/1"
+ end
+
# ---------------------------------------------------------------------------
get "/databases/drop/" do
path_names = session[:path_names]
View
@@ -177,26 +177,27 @@ def build_partial_tree( path_names )
config.each_pair do |env, info|
node = Node.new( env, env, :dyna => true )
root << node
- if node.name == bm_env
- connect_for( env ) do |con|
- count = 0
- data = { :dyna => true }
- con.database_names.each do |db_name|
- db = con.db( db_name, :strict => true )
- cltns = db.collection_names.size
- db_node = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns})", data.clone )
- node << db_node
- count += 1
- if bm_db and db_node.name =~ /^#{bm_db}/
- cltn_count = 0
- data = { :dyna => false }
- db.collection_names.each do |cltn_name|
- size = db[cltn_name].count
- cltn_node = Node.new( "#{db_name}_#{cltn_count}", "#{cltn_name}(#{size})", data.clone )
- db_node << cltn_node
- cltn_count += 1
- end
- end
+
+ next unless node.name == bm_env
+
+ connect_for( env ) do |con|
+ count = 0
+ data = { :dyna => true }
+ con.database_names.each do |db_name|
+ db = con.db( db_name, :strict => true )
+ cltns = db.collection_names.size
+ db_node = Node.new( "#{env}_#{count}", "#{db_name}(#{cltns})", data.clone )
+ node << db_node
+ count += 1
+ if bm_db and db_node.name =~ /^#{bm_db}/
+ cltn_count = 0
+ data = { :dyna => false }
+ db.collection_names.each do |cltn_name|
+ size = db[cltn_name].count
+ cltn_node = Node.new( "#{db_name}_#{cltn_count}", "#{cltn_name}(#{size})", data.clone )
+ db_node << cltn_node
+ cltn_count += 1
+ end
end
end
end
@@ -209,7 +210,7 @@ def build_sub_tree( parent_id, path_names )
path_name_tokens = path_names.split( "|" )
env = path_name_tokens[1]
- if db_request?( path_name_tokens )
+ if db_request?( path_name_tokens )
sub_tree = build_db_tree( parent_id, env )
else
db_name = path_name_tokens.last
@@ -272,7 +273,7 @@ def build_cltn_tree( parent_id, env, db_name )
def connect_for( env, &block )
info = landscape[env]
puts ">>> Connecting for #{env} -- #{info['host']}-#{info['port']}"
- con = Mongo::Connection.new( info['host'], info['port'] )
+ con = Mongo::Connection.new( info['host'], info['port'], { :slave_ok => true } )
if info['user'] and info['password']
con.db( 'admin' ).authenticate( info['user'], info['password'] )
View
@@ -1,14 +1,15 @@
require 'json'
+require 'mongo/util/ordered_hash'
module Mongo3
class Node
attr_accessor :oid, :name, :children, :data, :parent
- def initialize( oid, name, data={} )
+ def initialize( oid, name, data=nil )
@oid = oid
@name = name
@children = []
- @data = data || {}
+ @data = data || OrderedHash.new
@parent = nil
end
@@ -23,14 +24,11 @@ def <<( new_one )
update_paths( new_one )
end
- def update_paths( node )
- node.data[:path_names] = node.path( :name )
- node.data[:path_ids] = node.path( :oid )
- node.children.each do |child|
- child.update_paths( child )
- end
- end
-
+ # def find( path_name )
+ # tokens = path_name.split
+ # self.children.each do
+ # end
+
# convert a tree node to a set of adjacencies
def to_adjacencies
root_level = { :id => self.oid, :name => self.name, :data => self.data, :adjacencies => [] }
@@ -42,27 +40,14 @@ def to_adjacencies
cltn
end
- def path( accessor=:oid )
- path = []
- traverse( path, self, accessor )
- path.reverse.join( "|" )
- end
-
- def traverse( path, node, accessor )
- path << node.send( accessor ).gsub( /\(\d+\)/, "" )
- if node.parent
- traverse( path, node.parent, accessor )
- end
- end
-
# converts to json
def to_json(*a)
- {
- 'id' => oid,
- 'name' => self.name,
- 'children' => self.children,
- 'data' => self.data
- }.to_json(*a)
+ hash = OrderedHash.new
+ hash[:id] = oid
+ hash[:name] = self.name
+ hash[:children] = self.children
+ hash[:data] = self.data
+ hash.to_json(*a)
end
# Debug...
@@ -79,6 +64,29 @@ def self.dump_adj( adjs, level = 0 )
puts ' '*level + "%-#{20-level}s (%d) [%s] -- %s" % [adj[:id], adj[:adjacencies].size, adj[:name], (adj[:data] ? adj[:data].inspect : 'N/A' )]
end
end
+
+ # =========================================================================
+ private
+
+ def update_paths( node )
+ node.data[:path_names] = node.send( :path, :name )
+ node.data[:path_ids] = node.send( :path, :oid )
+ node.children.each do |child|
+ child.send( :update_paths, child )
+ end
+ end
+
+ def path( accessor=:oid )
+ path = []
+ traverse( path, self, accessor )
+ path.reverse.join( "|" )
+ end
+ def traverse( path, node, accessor )
+ path << node.send( accessor ).to_s.gsub( /\(\d+\)/, "" )
+ if node.parent
+ traverse( path, node.parent, accessor )
+ end
+ end
end
end
@@ -16,11 +16,10 @@
<tr id="<%=count%>" valign="top">
<td id="<%=count%>">
<img class="delete" id="<%=path%>" rel="<%=count%>" src="/images/delete.png" title="drop this collection"/>
- <img class="wait" id="wait_<%=count%>" src="/images/loading.gif"></img>
- </td>
- <% cltn.each_pair do |k, v| %>
- <td align="<%=align_for( v )%>"><%= format_number(v) %></td>
- <% end %>
+ <img class="wait" id="wait_<%=count%>" src="/images/loading.gif"></img>
+ </td>
+ <td><a href="/databases/collection/<%=cltn[:name]%>/"><%= cltn[:name] %></a></td>
+ <td align="<%=align_for( cltn[:count] )%>"><%= format_number(cltn[:count]) %></td>
</tr>
<% count+=1;end %>
</tbody>
View
@@ -0,0 +1,3 @@
+test:
+ host: localhost
+ port: 27017
@@ -0,0 +1,147 @@
+require File.join(File.dirname(__FILE__), %w[.. spec_helper])
+require 'ostruct'
+require 'mongo'
+
+describe Mongo3::Connection do
+
+ before( :all ) do
+ @con = Mongo::Connection.new( 'localhost', 27017 )
+ @db = @con.db( 'mongo3_test_db', :strict => true )
+ @mongo3 = Mongo3::Connection.new( File.join(File.dirname(__FILE__), %w[.. landscape.yml]) )
+ end
+
+ before( :each ) do
+ unless @db.collection_names.include? 'test1_cltn'
+ @cltn1 = @db.create_collection('test1_cltn')
+ else
+ @cltn1 = @db['test1_cltn']
+ end
+ unless @db.collection_names.include? 'test2_cltn'
+ @cltn2 = @db.create_collection('test2_cltn')
+ else
+ @cltn2 = @db['test2_cltn']
+ end
+
+ @cltn1.remove
+ 10.times do |i|
+ @cltn1.insert( {:name => "test_#{i}", :value => i } )
+ end
+ @cltn2.remove
+ 10.times do |i|
+ @cltn2.insert( {:name => "test_#{i}", :value => i } )
+ end
+ end
+
+ it "should clear out a cltn correctly" do
+ @mongo3.clear_cltn( "home|test|mongo3_test_db|test1_cltn" )
+ @db['test1_cltn'].count.should == 0
+ end
+
+ it "should delete a row correctly" do
+ obj = @cltn1.find_one()
+ @mongo3.delete_row( "home|test|mongo3_test_db|test1_cltn", obj['_id'].to_s )
+ @db['test1_cltn'].count.should == 9
+ end
+
+ it "should drop a cltn correctly" do
+ @mongo3.drop_cltn( "home|test|mongo3_test_db|test1_cltn" )
+ lambda { @db['test1_cltn'] }.should raise_error( /Collection test1_cltn/ )
+ end
+
+ it "should drop a db correctly" do
+ @mongo3.drop_db( "home|test|mongo3_test_db" )
+ @con.database_names.include?( 'mongo3_test_db' ).should == false
+ @db = @con.db( 'mongo3_test_db', :strict => true )
+ end
+
+ it "should load a landscape file correctly" do
+ test = @mongo3.landscape['test']
+
+ test.should_not be_nil
+ test['host'].should == 'localhost'
+ test['port'].should == 27017
+ end
+
+ it "should build a tree correctly" do
+ root = @mongo3.build_tree
+
+ root.name.should == 'home'
+ root.oid.should == 'home'
+ root.data[:path_names].should == 'home'
+
+ children = root.children
+ children.should have(1).item
+ children.first.name.should == 'test'
+ children.first.oid.should == 'test'
+ children.first.children.should be_empty
+ end
+
+ describe "#show" do
+ it "should pull env info correctly" do
+ info = @mongo3.show( "home|test" )
+ info.size.should == 6
+ info[:title].should == "test"
+ info[:databases].size.should > 1
+ end
+
+ it "should pull db info correctly" do
+ info = @mongo3.show( "home|test|mongo3_test_db" )
+ info.size.should == 7
+ info[:collections].size.should == 4
+ info[:title].should == "mongo3_test_db"
+ end
+
+ it "should pull cltn info correctly" do
+ info = @mongo3.show( "home|test|mongo3_test_db|test1_cltn" )
+ info.size.should == 4
+ info[:size].should == 10
+ info[:title].should == "test1_cltn"
+ end
+ end
+
+ describe "#build_sub_tree" do
+ it "should build a adjacencies from db correctly" do
+ adjs = @mongo3.build_sub_tree( 100, "home|test" )
+ adjs.should_not be_empty
+ adjs.size.should > 1
+ adjs.first[:name].should == 'test'
+ adjs.first[:id].should == 100
+ end
+
+ it "should build a adjacencies from cltn correctly" do
+ adjs = @mongo3.build_sub_tree( 200, "home|test|mongo3_test_db" )
+ adjs.size.should == 4
+ adjs.first[:name].should == 'mongo3_test_db'
+ adjs.first[:id].should == 200
+ adjs.first[:adjacencies].should have(3).items
+ end
+
+ it "should build a partial tree correctly" do
+ root = @mongo3.build_partial_tree( "home|test|mongo3_test_db" )
+# Mongo3::Node.dump( root )
+ # root.find( "home|test|mongo3_test_db" )
+ end
+ end
+
+ describe "paginate db" do
+ it "should paginate a db correctly" do
+ rows = @mongo3.paginate_db( "home|test|mongo3_test_db" )
+ rows.size.should == 3
+ rows.total_entries.should == 3
+ end
+ end
+
+ describe "paginate cltn" do
+ it "should paginate a cltn correctly" do
+ rows = @mongo3.paginate_cltn( "home|test|mongo3_test_db|test1_cltn" )
+ rows.size.should == 10
+ rows.total_entries.should == 10
+ end
+
+ it "should paginate db with q correctly" do
+ rows = @mongo3.paginate_cltn( "home|test|mongo3_test_db|test1_cltn", [{:value =>{'$gt' => 5 }}, []] )
+ rows.size.should == 4
+ rows.total_entries.should == 10
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 44b02d3

Please sign in to comment.