/
safe_test.rb
125 lines (95 loc) · 3.24 KB
/
safe_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require File.expand_path("../../test_helper", __FILE__)
class SafeTest < Test::Unit::TestCase
context "Safe mode on connection: " do
setup do
@safe_value = {:w => 7}
@con = Mongo::Connection.new('localhost', 27017, :safe => @safe_value, :connect => false)
end
should "propogate to DB" do
db = @con['foo']
assert_equal @safe_value, db.safe
db = @con.db('foo')
assert_equal @safe_value, db.safe
db = DB.new('foo', @con)
assert_equal @safe_value, db.safe
end
should "allow db override" do
db = DB.new('foo', @con, :safe => false)
assert_equal false, db.safe
db = @con.db('foo', :safe => false)
assert_equal false, db.safe
end
context "on DB: " do
setup do
@db = @con['foo']
end
should "propogate to collection" do
col = @db.collection('bar')
assert_equal @safe_value, col.safe
col = @db['bar']
assert_equal @safe_value, col.safe
col = Collection.new('bar', @db)
assert_equal @safe_value, col.safe
end
should "allow override on collection" do
col = @db.collection('bar', :safe => false)
assert_equal false, col.safe
col = Collection.new('bar', @db, :safe => false)
assert_equal false, col.safe
end
end
context "on operations supporting safe mode" do
setup do
@col = @con['foo']['bar']
end
should "use default value on insert" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.insert({:a => 1})
end
should "allow override alternate value on insert" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == {:w => 100}
end
@col.insert({:a => 1}, :safe => {:w => 100})
end
should "allow override to disable on insert" do
@con.expects(:send_message)
@col.insert({:a => 1}, :safe => false)
end
should "use default value on update" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.update({:a => 1}, {:a => 2})
end
should "allow override alternate value on update" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == {:w => 100}
end
@col.update({:a => 1}, {:a => 2}, :safe => {:w => 100})
end
should "allow override to disable on update" do
@con.expects(:send_message)
@col.update({:a => 1}, {:a => 2}, :safe => false)
end
should "use default value on remove" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == @safe_value
end
@col.remove
end
should "allow override alternate value on remove" do
@con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
safe == {:w => 100}
end
@col.remove({}, :safe => {:w => 100})
end
should "allow override to disable on remove" do
@con.expects(:send_message)
@col.remove({}, :safe => false)
end
end
end
end