Skip to content

Commit

Permalink
Fixes to LogDevice. New LogDevice tests. Testing reorg.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Menard committed Jan 22, 2009
1 parent 0eca85d commit 45c71cf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/mongo_record/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ def initialize(name, options = {})
options[:size] ||= DEFAULT_CAP_SIZE
options[:size] = DEFAULT_CAP_SIZE if options[:size] <= 0
options[:capped] = true
@console = options.delete[:echo]
@console = options.delete(:echo)

# It's OK to call createCollection if the collection already exists.
# Size and max won't change, though.
#
# Note we can't use the name "create_collection" because a DB JSObject
# does not have normal keys and returns collection objects as the value
# of all unknown names.
self.class.connection.createCollection(@collection_name, options)
self.class.connection.create_collection(@collection_name, options)
end

# Write a log message to the database. We save the message and a timestamp.
def write(str)
@console.puts str if @console
self.class.connection[@collection_name].save({:time => Time.now, :msg => str})
self.class.connection.collection(@collection_name).insert({:time => Time.now, :msg => str})
end

# Close the log. This method is a sham. Nothing happens. You may
Expand Down
33 changes: 33 additions & 0 deletions test/log_device_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class LoggerTest < ActiveSupport::TestCase

MAX_RECS = 3

def setup
$db.collection('testlogger').drop()
# Create a log device with a max of MAX_RECS records
@logger = Logger.new(MongoRecord::LogDevice.new('testlogger', :size => 1_000_000, :max => MAX_RECS))
@log_collection = $db.collection('testlogger')
end

def teardown
$db.collection('testlogger').drop()
end

# We really don't have to test much more than this. We can trust that Mongo
# works properly.
test "max records enforced" do
assert_equal $db.name, MongoRecord::LogDevice.connection.name
MAX_RECS.times { |i|
@logger.debug("test message #{i+1}")
assert_equal i+1, @log_collection.count()
}

MAX_RECS.times { |i|
@logger.debug("test message #{i+MAX_RECS+1}")
assert_equal MAX_RECS, @log_collection.count()
}
end

end
18 changes: 6 additions & 12 deletions test/mongo_record_test.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
require 'test_helper'
require 'mongo'

class MongoRecordTest < ActiveSupport::TestCase

def setup
@mongo = XGen::Mongo::Driver::Mongo.new
test "can connect to database" do
assert_not_nil $db
list = $db.collection_names
assert_not_nil list
assert_kind_of Array, list
end

test "can see database names" do
list = @mongo.database_names
list = $mongo.database_names
assert_not_nil list
assert list.size > 0
assert list.include?('admin')
end

test "can connect to database" do
db = @mongo.db('mongo_record_test')
assert_not_nil db
list = db.collection_names
assert_not_nil list
assert_kind_of Array, list
end
end
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
require 'active_support'
require 'active_support/test_case'
require 'test/unit'
require 'mongo'

$mongo = XGen::Mongo::Driver::Mongo.new
$db = $mongo.db('activerecord-mongo-adapter-test')

require 'mongo_record'

0 comments on commit 45c71cf

Please sign in to comment.