mongo_record 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ # Copyright 2009 10gen, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '../lib')
16
+ require 'rubygems'
17
+ require 'test/unit'
18
+ require 'logger'
19
+ require 'mongo'
20
+ require 'mongo_record/log_device.rb'
21
+
22
+ class LoggerTest < Test::Unit::TestCase
23
+
24
+ MAX_RECS = 3
25
+
26
+ @@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
27
+ @@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT
28
+ @@db = Mongo::Connection.new(@@host, @@port).db('mongorecord-test')
29
+
30
+ def setup
31
+ @@db.drop_collection('testlogger') # can't remove recs from capped colls
32
+ MongoRecord::LogDevice.connection = @@db
33
+ # Create a log device with a max of MAX_RECS records
34
+ @logger = Logger.new(MongoRecord::LogDevice.new('testlogger', :size => 1_000_000, :max => MAX_RECS))
35
+ end
36
+
37
+ def teardown
38
+ @@db.drop_collection('testlogger') # can't remove recs from capped colls
39
+ end
40
+
41
+ # We really don't have to test much more than this. We can trust that Mongo
42
+ # works properly.
43
+ def test_max
44
+ assert_not_nil @@db
45
+ assert_equal @@db.name, MongoRecord::LogDevice.connection.name
46
+ collection = MongoRecord::LogDevice.connection.collection('testlogger')
47
+ MAX_RECS.times { |i|
48
+ @logger.debug("test message #{i+1}")
49
+ assert_equal i+1, collection.count()
50
+ }
51
+
52
+ MAX_RECS.times { |i|
53
+ @logger.debug("test message #{i+MAX_RECS+1}")
54
+ assert_equal MAX_RECS, collection.count()
55
+ }
56
+ end
57
+
58
+ def test_alternate_connection
59
+ old_db = @@db
60
+ alt_db = Mongo::Connection.new(@@host, @@port).db('mongorecord-test-log-device')
61
+ begin
62
+ @@db = nil
63
+ MongoRecord::LogDevice.connection = alt_db
64
+
65
+ logger = Logger.new(MongoRecord::LogDevice.new('testlogger', :size => 1_000_000, :max => MAX_RECS))
66
+ logger.debug('test message')
67
+
68
+ coll = alt_db.collection('testlogger')
69
+ assert_equal 1, coll.count()
70
+ rec = coll.find_one
71
+ assert_not_nil rec
72
+ assert_match /test message/, rec['msg']
73
+ rescue => ex
74
+ fail ex.to_s
75
+ ensure
76
+ @@db = old_db
77
+ MongoRecord::LogDevice.connection = @@db
78
+ alt_db.drop_collection('testlogger')
79
+ end
80
+ end
81
+
82
+ end