-
Notifications
You must be signed in to change notification settings - Fork 382
/
test_safe.rb
executable file
·144 lines (125 loc) · 3.85 KB
/
test_safe.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'test_helper'
class SafeTest < Test::Unit::TestCase
context "A Document" do
should "default safe to off" do
Doc().should_not be_safe
end
should "allow turning safe on" do
Doc() { safe }.should be_safe
end
context "inherited with safe setting on" do
should "set subclass safe setting on" do
inherited = Class.new(Doc() { safe })
inherited.should be_safe
inherited.safe_options.should == true
end
should "set subclass safe setting to same options hash as superclass" do
inherited = Class.new(Doc() { safe(:j => true) })
inherited.should be_safe
inherited.safe_options.should == {:j => true}
end
end
context "inherited with safe setting off" do
should "leave subclass safe setting off" do
inherited = Class.new(Doc())
inherited.should_not be_safe
end
end
end
context "A safe document" do
setup do
@klass = Doc() do
safe
end
end
teardown { drop_indexes(@klass) }
context "#save" do
setup do
@klass.ensure_index :email, :unique => true
end
context "using safe setting from class" do
should "pass :w => 1 option to save" do
instance = @klass.new(:email => 'john@doe.com')
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:w => 1})
instance.save!
end
should "work fine when all is well" do
assert_nothing_raised do
@klass.new(:email => 'john@doe.com').save
end
end
should "raise error when operation fails" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save
end
end
end
end
context "overriding safe setting" do
should "raise error if safe is true" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => true)
end
end
end
should "not raise error if safe is false" do
assert_nothing_raised do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => false)
end
end
end
end
end
end
context "a safe document with options hash" do
setup do
@klass = Doc() do
safe(:j => true)
end
end
teardown { drop_indexes(@klass) }
context "#save" do
setup do
@klass.ensure_index :email, :unique => true
end
context "using safe setting from class" do
should "pass :safe => options_hash to save" do
instance = @klass.new(:email => 'john@doe.com')
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:j => true})
instance.save!
end
should "work fine when all is well" do
assert_nothing_raised do
@klass.new(:email => 'john@doe.com').save
end
end
should "raise error when operation fails" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save
end
end
end
end
context "overriding safe setting" do
should "raise error if safe is true" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => true)
end
end
end
should "not raise error if safe is false" do
assert_nothing_raised do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => false)
end
end
end
end
end
end
end