mongo_mapper 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +28 -20
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +1 -0
- data/lib/mongo_mapper/connection.rb +16 -38
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/indexes.rb +13 -6
- data/lib/mongo_mapper/plugins/keys.rb +1 -2
- data/lib/mongo_mapper/plugins/modifiers.rb +27 -10
- data/lib/mongo_mapper/plugins/persistence.rb +6 -2
- data/lib/mongo_mapper/plugins/querying.rb +9 -3
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +3 -4
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/spec/examples.txt +1643 -0
- data/spec/functional/accessible_spec.rb +1 -1
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/belongs_to_proxy_spec.rb +4 -4
- data/spec/functional/associations/in_array_proxy_spec.rb +14 -14
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
- data/spec/functional/associations/one_proxy_spec.rb +8 -8
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +15 -22
- data/spec/functional/callbacks_spec.rb +2 -2
- data/spec/functional/counter_cache_spec.rb +10 -10
- data/spec/functional/dirty_spec.rb +27 -10
- data/spec/functional/document_spec.rb +5 -5
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +5 -5
- data/spec/functional/identity_map_spec.rb +6 -6
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +22 -22
- data/spec/functional/logger_spec.rb +2 -2
- data/spec/functional/modifiers_spec.rb +67 -19
- data/spec/functional/partial_updates_spec.rb +8 -8
- data/spec/functional/protected_spec.rb +1 -1
- data/spec/functional/querying_spec.rb +48 -22
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +7 -7
- data/spec/functional/scopes_spec.rb +1 -1
- data/spec/functional/static_keys_spec.rb +2 -2
- data/spec/functional/stats_spec.rb +28 -12
- data/spec/functional/validations_spec.rb +8 -16
- data/spec/quality_spec.rb +1 -1
- data/spec/spec_helper.rb +32 -8
- data/spec/support/matchers.rb +1 -1
- data/spec/unit/associations/proxy_spec.rb +1 -1
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +3 -3
- data/spec/unit/embedded_document_spec.rb +4 -5
- data/spec/unit/extensions_spec.rb +3 -4
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +16 -17
- data/spec/unit/keys_spec.rb +7 -7
- data/spec/unit/mongo_mapper_spec.rb +41 -88
- data/spec/unit/rails_spec.rb +2 -2
- metadata +37 -24
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -92,7 +92,7 @@ describe "Proxy" do
|
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should not work if neither the proxy or target respond to method" do
|
95
|
-
lambda { @proxy.send(:gsub) }.should raise_error
|
95
|
+
lambda { @proxy.send(:gsub) }.should raise_error(NoMethodError)
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should work if a proc is passed" do
|
data/spec/unit/clone_spec.rb
CHANGED
data/spec/unit/document_spec.rb
CHANGED
@@ -21,11 +21,11 @@ describe "Document" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should have a connection" do
|
24
|
-
@document.connection.should be_instance_of(Mongo::
|
24
|
+
@document.connection.should be_instance_of(Mongo::Client)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should allow setting different connection without affecting the default" do
|
28
|
-
conn = Mongo::
|
28
|
+
conn = Mongo::Client.new(['127.0.0.1:27001'])
|
29
29
|
@document.connection conn
|
30
30
|
@document.connection.should == conn
|
31
31
|
@document.connection.should_not == MongoMapper.connection
|
@@ -152,7 +152,7 @@ describe "Document" do
|
|
152
152
|
|
153
153
|
it "should be true if id but using custom id and not saved yet" do
|
154
154
|
@document.key :_id, String
|
155
|
-
doc =
|
155
|
+
doc = suppress_stderr { @document.new }
|
156
156
|
doc.id = '1234'
|
157
157
|
doc.new?.should be_truthy
|
158
158
|
end
|
@@ -210,7 +210,7 @@ describe "EmbeddedDocument" do
|
|
210
210
|
end
|
211
211
|
|
212
212
|
it "should not include descendant keys" do
|
213
|
-
lambda { Parent.new.other_child }.should raise_error
|
213
|
+
lambda { Parent.new.other_child }.should raise_error(NoMethodError)
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -308,7 +308,7 @@ describe "EmbeddedDocument" do
|
|
308
308
|
end
|
309
309
|
|
310
310
|
it "should not throw error if initialized with nil" do
|
311
|
-
|
311
|
+
lambda { @document.new(nil) }.should_not raise_error
|
312
312
|
end
|
313
313
|
end
|
314
314
|
|
@@ -661,16 +661,15 @@ describe "EmbeddedDocument" do
|
|
661
661
|
|
662
662
|
it "should should raise errors when invalid" do
|
663
663
|
@doc.name = ''
|
664
|
-
|
664
|
+
lambda { @doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
665
665
|
end
|
666
666
|
|
667
667
|
it "should should raise errors when root document is invalid" do
|
668
668
|
@root.name = ''
|
669
669
|
@root.save(:validate => false)
|
670
|
-
|
670
|
+
lambda { @doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
671
671
|
end
|
672
672
|
end
|
673
673
|
end # instance of a embedded document
|
674
674
|
end
|
675
675
|
end
|
676
|
-
|
@@ -207,7 +207,7 @@ describe "Support" do
|
|
207
207
|
context "ObjectId#to_mongo" do
|
208
208
|
it "should call class to_mongo with self" do
|
209
209
|
object = Object.new
|
210
|
-
|
210
|
+
object.class.should_receive(:to_mongo).with(object)
|
211
211
|
object.to_mongo
|
212
212
|
end
|
213
213
|
end
|
@@ -300,8 +300,7 @@ describe "Support" do
|
|
300
300
|
end
|
301
301
|
|
302
302
|
it "should be time in utc if time" do
|
303
|
-
|
304
|
-
Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == utc_str
|
303
|
+
Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
|
305
304
|
end
|
306
305
|
|
307
306
|
it "should be nil if blank string" do
|
@@ -388,7 +387,7 @@ describe "Support" do
|
|
388
387
|
|
389
388
|
it "should support ruby driver syntax also" do
|
390
389
|
id = BSON::ObjectId.new
|
391
|
-
id.original_to_json.should == %Q({"$oid":
|
390
|
+
id.original_to_json.should == %Q({"$oid":"#{id}"})
|
392
391
|
end
|
393
392
|
end
|
394
393
|
end
|
@@ -19,116 +19,85 @@ module IdentityMapSpec
|
|
19
19
|
end.to_app
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
before do
|
23
|
+
@enabled = MongoMapper::Plugins::IdentityMap.enabled
|
24
|
+
MongoMapper::Plugins::IdentityMap.enabled = false
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
after do
|
28
|
+
MongoMapper::Plugins::IdentityMap.enabled = @enabled
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
31
|
+
it "should delegate" do
|
32
|
+
called = false
|
33
|
+
mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
|
34
|
+
called = true
|
35
|
+
[200, {}, nil]
|
36
|
+
}
|
37
|
+
mw.call({})
|
38
|
+
called.should be_truthy
|
39
|
+
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
it "should enable identity map during delegation" do
|
42
|
+
mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
|
43
|
+
MongoMapper::Plugins::IdentityMap.should be_enabled
|
44
|
+
[200, {}, nil]
|
45
|
+
}
|
46
|
+
mw.call({})
|
47
|
+
end
|
49
48
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
49
|
+
class Enum < Struct.new(:iter)
|
50
|
+
def each(&b)
|
51
|
+
iter.call(&b)
|
54
52
|
end
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
55
|
+
it "should enable IM for body each" do
|
56
|
+
mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
|
57
|
+
[200, {}, Enum.new(lambda { |&b|
|
58
|
+
MongoMapper::Plugins::IdentityMap.should be_enabled
|
59
|
+
b.call "hello"
|
60
|
+
})]
|
61
|
+
}
|
62
|
+
body = mw.call({}).last
|
63
|
+
body.each { |x| x.should eql('hello') }
|
64
|
+
end
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
66
|
+
it "should disable IM after body close" do
|
67
|
+
mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
|
68
|
+
body = mw.call({}).last
|
69
|
+
MongoMapper::Plugins::IdentityMap.should be_enabled
|
70
|
+
body.close
|
71
|
+
MongoMapper::Plugins::IdentityMap.should_not be_enabled
|
72
|
+
end
|
74
73
|
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
it "should clear IM after body close" do
|
75
|
+
mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
|
76
|
+
body = mw.call({}).last
|
78
77
|
|
79
|
-
|
80
|
-
|
78
|
+
MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
|
79
|
+
MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
|
81
80
|
|
82
|
-
|
81
|
+
body.close
|
83
82
|
|
84
|
-
|
85
|
-
|
83
|
+
MongoMapper::Plugins::IdentityMap.repository.should be_empty
|
84
|
+
end
|
86
85
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
86
|
+
context "with a successful request" do
|
87
|
+
it "should clear the identity map" do
|
88
|
+
# for some reason, body.close gets called twice - once in rack-2.2.3/lib/rack/response.rb:281
|
89
|
+
# the other time rack-test-0.8.3/lib/rack/mock_session.rb:32
|
90
|
+
# use at_least(:twice) here to handle the error happening more than twice
|
91
|
+
MongoMapper::Plugins::IdentityMap.should_receive(:clear).at_least(:twice)
|
92
|
+
get '/'
|
92
93
|
end
|
94
|
+
end
|
93
95
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
96
|
+
context "when the request raises an error" do
|
97
|
+
it "should clear the identity map" do
|
98
|
+
MongoMapper::Plugins::IdentityMap.should_receive(:clear).once
|
99
|
+
get '/fail' rescue nil
|
99
100
|
end
|
100
101
|
end
|
101
102
|
end
|
102
|
-
|
103
|
-
# describe "IdentityMapMiddleware" do
|
104
|
-
# include Rack::Test::Methods
|
105
|
-
|
106
|
-
# def app
|
107
|
-
# @app ||= Rack::Builder.new do
|
108
|
-
# use MongoMapper::Middleware::IdentityMap
|
109
|
-
# map "/" do
|
110
|
-
# run lambda {|env| [200, {}, []] }
|
111
|
-
# end
|
112
|
-
# map "/fail" do
|
113
|
-
# run lambda {|env| raise "FAIL!" }
|
114
|
-
# end
|
115
|
-
# end.to_app
|
116
|
-
# end
|
117
|
-
|
118
|
-
# context "with a successful request" do
|
119
|
-
# it "should clear the identity map" do
|
120
|
-
# expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).twice
|
121
|
-
# get '/'
|
122
|
-
# end
|
123
|
-
# end
|
124
|
-
|
125
|
-
# context "when the request raises an error" do
|
126
|
-
# it "should clear the identity map" do
|
127
|
-
# expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).twice
|
128
|
-
# get '/fail' rescue nil
|
129
|
-
# end
|
130
|
-
# end
|
131
|
-
|
132
|
-
|
133
|
-
# end
|
134
|
-
end
|
103
|
+
end
|
data/spec/unit/key_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
3
|
class FooType < Struct.new(:bar)
|
5
4
|
def self.to_mongo(value)
|
6
5
|
'to_mongo'
|
@@ -60,19 +59,19 @@ describe "Key" do
|
|
60
59
|
end
|
61
60
|
|
62
61
|
it "should not permit reserved names" do
|
63
|
-
|
62
|
+
lambda { Key.new(:id) }.should raise_error(/reserved/)
|
64
63
|
end
|
65
64
|
|
66
65
|
it "should not permit bad names" do
|
67
|
-
|
66
|
+
lambda { Key.new(:"id.bar") }.should raise_error(/must match/)
|
68
67
|
end
|
69
68
|
|
70
69
|
it "should permit bad names if __dynamic" do
|
71
|
-
|
70
|
+
lambda { Key.new(:"id.bar", :__dynamic => true) }.should_not raise_error
|
72
71
|
end
|
73
72
|
|
74
73
|
it "should permit bad names if it is not to create accessors" do
|
75
|
-
|
74
|
+
lambda { Key.new(:"id.bar", :accessors => :skip) }.should_not raise_error
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
@@ -149,33 +148,33 @@ describe "Key" do
|
|
149
148
|
context "with :read" do
|
150
149
|
let(:accessor) { :read }
|
151
150
|
|
152
|
-
it {
|
153
|
-
it {
|
154
|
-
it {
|
151
|
+
it { subject.read_accessor?.should be_truthy }
|
152
|
+
it { subject.write_accessor?.should be_falsey }
|
153
|
+
it { subject.predicate_accessor?.should be_falsey }
|
155
154
|
end
|
156
155
|
|
157
156
|
context "with :write" do
|
158
157
|
let(:accessor) { :write }
|
159
158
|
|
160
|
-
it {
|
161
|
-
it {
|
162
|
-
it {
|
159
|
+
it { subject.read_accessor?.should be_falsey }
|
160
|
+
it { subject.write_accessor?.should be_truthy }
|
161
|
+
it { subject.predicate_accessor?.should be_falsey }
|
163
162
|
end
|
164
163
|
|
165
164
|
context "with :predicate" do
|
166
165
|
let(:accessor) { :predicate }
|
167
166
|
|
168
|
-
it {
|
169
|
-
it {
|
170
|
-
it {
|
167
|
+
it { subject.read_accessor?.should be_falsey }
|
168
|
+
it { subject.write_accessor?.should be_falsey }
|
169
|
+
it { subject.predicate_accessor?.should be_truthy }
|
171
170
|
end
|
172
171
|
|
173
172
|
context "with an array of options" do
|
174
173
|
let(:accessor) { [:read, :write] }
|
175
174
|
|
176
|
-
it {
|
177
|
-
it {
|
178
|
-
it {
|
175
|
+
it { subject.read_accessor?.should be_truthy }
|
176
|
+
it { subject.write_accessor?.should be_truthy }
|
177
|
+
it { subject.predicate_accessor?.should be_falsey }
|
179
178
|
end
|
180
179
|
end
|
181
180
|
|
data/spec/unit/keys_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe "Key" do
|
|
9
9
|
key :_id, Integer
|
10
10
|
end
|
11
11
|
# No sensible default id for integer, people better pass them in if they user this
|
12
|
-
|
12
|
+
suppress_stderr { klass.new.id.should be_nil }
|
13
13
|
}.should_not raise_error
|
14
14
|
end
|
15
15
|
end
|
@@ -30,7 +30,7 @@ describe "Key" do
|
|
30
30
|
key :_id, Integer
|
31
31
|
end
|
32
32
|
doc = klass.new
|
33
|
-
|
33
|
+
doc.should_receive(:warn).once
|
34
34
|
doc.assign({:x => :y})
|
35
35
|
end
|
36
36
|
end
|
@@ -82,11 +82,11 @@ describe "Key" do
|
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should use []= for keys instead of public writer" do
|
85
|
-
|
85
|
+
lambda {
|
86
86
|
doc = @klass.first
|
87
87
|
doc.user['id'].should == 1
|
88
88
|
doc.user['name'].should == 'John Nunemaker'
|
89
|
-
}.
|
89
|
+
}.should_not raise_error
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -141,15 +141,15 @@ describe "Key" do
|
|
141
141
|
key :value, Integer, :default => lambda { counter += 1 }
|
142
142
|
end
|
143
143
|
|
144
|
-
|
145
|
-
|
144
|
+
lambda { instance = klass.create }.should change { counter }.by(1)
|
145
|
+
lambda {
|
146
146
|
instance.reload.value.should == 1
|
147
147
|
|
148
148
|
instance.value = 10
|
149
149
|
instance.save
|
150
150
|
|
151
151
|
instance.reload.value.should == 10
|
152
|
-
}.
|
152
|
+
}.should_not change { counter }
|
153
153
|
end
|
154
154
|
end
|
155
155
|
end # KeyTest
|
@@ -3,23 +3,28 @@ require 'spec_helper'
|
|
3
3
|
class Address; end
|
4
4
|
|
5
5
|
describe "MongoMapper" do
|
6
|
-
it "should be able to write and read connection" do
|
7
|
-
conn = Mongo::
|
6
|
+
it "should be able to write and read connection", :without_connection do
|
7
|
+
conn = Mongo::Client.new(['127.0.0.1:27001'])
|
8
8
|
MongoMapper.connection = conn
|
9
9
|
MongoMapper.connection.should == conn
|
10
10
|
end
|
11
11
|
|
12
|
-
it "should default connection to new mongo ruby driver" do
|
12
|
+
it "should default connection to new mongo ruby driver", :without_connection do
|
13
13
|
MongoMapper.connection = nil
|
14
|
-
MongoMapper.connection.should be_instance_of(Mongo::
|
14
|
+
MongoMapper.connection.should be_instance_of(Mongo::Client)
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should be able to
|
18
|
-
MongoMapper.database
|
19
|
-
MongoMapper.database.should be_instance_of(Mongo::DB)
|
17
|
+
it "should be able to read default database" do
|
18
|
+
MongoMapper.database.should be_instance_of(Mongo::Database)
|
20
19
|
MongoMapper.database.name.should == 'test'
|
21
20
|
end
|
22
21
|
|
22
|
+
it "should be able to write default database", :without_connection do
|
23
|
+
MongoMapper.database = 'test-2'
|
24
|
+
MongoMapper.database.should be_instance_of(Mongo::Database)
|
25
|
+
MongoMapper.database.name.should == 'test-2'
|
26
|
+
end
|
27
|
+
|
23
28
|
it "should have document not found error" do
|
24
29
|
lambda {
|
25
30
|
MongoMapper::DocumentNotFound
|
@@ -35,96 +40,64 @@ describe "MongoMapper" do
|
|
35
40
|
MongoMapper.config.should == config
|
36
41
|
end
|
37
42
|
|
38
|
-
context "connecting to environment from config" do
|
39
|
-
it "should work without authentication" do
|
40
|
-
MongoMapper.config = {
|
41
|
-
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
|
42
|
-
}
|
43
|
-
expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, {})
|
44
|
-
expect(MongoMapper).to receive(:database=).with('test')
|
45
|
-
expect_any_instance_of(Mongo::DB).to receive(:authenticate).never
|
46
|
-
MongoMapper.connect('development')
|
47
|
-
end
|
43
|
+
context "connecting to environment from config", :without_connection do
|
48
44
|
|
49
45
|
it "should work without authentication using uri" do
|
50
46
|
MongoMapper.config = {
|
51
47
|
'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
|
52
48
|
}
|
53
|
-
|
54
|
-
expect(MongoMapper).to receive(:database=).with('test')
|
55
|
-
expect_any_instance_of(Mongo::DB).to receive(:authenticate).never
|
49
|
+
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', {})
|
56
50
|
MongoMapper.connect('development')
|
57
51
|
end
|
58
52
|
|
59
|
-
it "should work
|
53
|
+
it "should work without authentication using hosts" do
|
60
54
|
MongoMapper.config = {
|
61
|
-
'development' => {'
|
55
|
+
'development' => {'hosts' => ['127.0.0.1:27017']}
|
62
56
|
}
|
63
|
-
|
64
|
-
|
65
|
-
expect_any_instance_of(Mongo::DB).to receive(:authenticate).never
|
66
|
-
MongoMapper.connect(:development)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should work with options" do
|
70
|
-
MongoMapper.config = {
|
71
|
-
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
|
72
|
-
}
|
73
|
-
connection, logger = double('connection'), double('logger')
|
74
|
-
expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, :logger => logger)
|
75
|
-
MongoMapper.connect('development', :logger => logger)
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should pass along ssl when true" do
|
79
|
-
MongoMapper.config = {
|
80
|
-
'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'ssl' => true}
|
81
|
-
}
|
82
|
-
connection, logger = double('connection'), double('logger')
|
83
|
-
expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, :logger => logger, :ssl => true)
|
84
|
-
MongoMapper.connect('development', :logger => logger)
|
57
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], {})
|
58
|
+
MongoMapper.connect('development')
|
85
59
|
end
|
86
60
|
|
87
|
-
it "should
|
61
|
+
it "should work without authentication using host" do
|
88
62
|
MongoMapper.config = {
|
89
|
-
'development' => {'host' => '127.0.0.1
|
63
|
+
'development' => {'host' => '127.0.0.1:27017'}
|
90
64
|
}
|
91
|
-
|
92
|
-
|
93
|
-
MongoMapper.connect('development', :logger => logger)
|
65
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], {})
|
66
|
+
MongoMapper.connect('development')
|
94
67
|
end
|
95
68
|
|
96
69
|
it "should convert read preferences to symbols" do
|
97
70
|
MongoMapper.config = {
|
98
|
-
'development' => {'
|
71
|
+
'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'read' => 'primary'}
|
99
72
|
}
|
100
|
-
|
101
|
-
|
73
|
+
logger = double('logger')
|
74
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], :logger => logger, :read => :primary, :database => 'test')
|
102
75
|
MongoMapper.connect('development', :logger => logger)
|
103
76
|
end
|
104
77
|
|
105
78
|
it "should work with options from config" do
|
106
79
|
MongoMapper.config = {
|
107
|
-
'development' => {'
|
80
|
+
'development' => {'hosts' => ['192.168.1.1:2222'], 'database' => 'test', 'safe' => true}
|
108
81
|
}
|
109
|
-
|
110
|
-
|
82
|
+
logger = double('logger')
|
83
|
+
Mongo::Client.should_receive(:new).with(['192.168.1.1:2222'], :logger => logger, :safe => true, :database => 'test')
|
111
84
|
MongoMapper.connect('development', :logger => logger)
|
112
85
|
end
|
113
86
|
|
114
87
|
it "should work with options using uri" do
|
115
88
|
MongoMapper.config = {
|
116
|
-
'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
|
89
|
+
'development' => {'uri' => 'mongodb://127.0.0.1:27017/test', 'options'=> {:foo => 1}}
|
117
90
|
}
|
118
|
-
|
119
|
-
|
91
|
+
logger = double('logger')
|
92
|
+
Mongo::Client.should_receive(:new).with('mongodb://127.0.0.1:27017/test', :logger => logger, :foo => 1)
|
120
93
|
MongoMapper.connect('development', :logger => logger)
|
121
94
|
end
|
122
95
|
|
123
96
|
it "should work with authentication" do
|
124
97
|
MongoMapper.config = {
|
125
|
-
'development' => {'
|
98
|
+
'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'user' => 'john', 'password' => 'secret'}
|
126
99
|
}
|
127
|
-
|
100
|
+
Mongo::Client.should_receive(:new).with(['127.0.0.1:27017'], :database => 'test', :user => 'john', :password => 'secret')
|
128
101
|
MongoMapper.connect('development')
|
129
102
|
end
|
130
103
|
|
@@ -132,7 +105,7 @@ describe "MongoMapper" do
|
|
132
105
|
MongoMapper.config = {
|
133
106
|
'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
|
134
107
|
}
|
135
|
-
|
108
|
+
Mongo::Client.should_receive(:new).with('mongodb://john:secret@127.0.0.1:27017/test', {})
|
136
109
|
MongoMapper.connect('development')
|
137
110
|
end
|
138
111
|
|
@@ -140,44 +113,24 @@ describe "MongoMapper" do
|
|
140
113
|
MongoMapper.config = {
|
141
114
|
'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
|
142
115
|
}
|
143
|
-
|
116
|
+
lambda { MongoMapper.connect('development') }.should raise_error(Mongo::Error::InvalidURI)
|
144
117
|
end
|
145
118
|
|
146
|
-
it
|
119
|
+
it 'should forbid use of port' do
|
147
120
|
MongoMapper.config = {
|
148
|
-
'development' => {
|
149
|
-
'hosts' => [ ['127.0.0.1', 27017], ['localhost', 27017] ],
|
150
|
-
'database' => 'test'
|
151
|
-
}
|
121
|
+
'development' => {'host' => '192.168.1.1', 'port' => '27017', 'database' => 'test', 'safe' => true}
|
152
122
|
}
|
153
|
-
|
154
|
-
expect(Mongo::MongoReplicaSetClient).to receive(:new).with( ['127.0.0.1', 27017], ['localhost', 27017], {'read_secondary' => true} )
|
155
|
-
expect(MongoMapper).to receive(:database=).with('test')
|
156
|
-
expect_any_instance_of(Mongo::DB).to receive(:authenticate).never
|
157
|
-
MongoMapper.connect('development', 'read_secondary' => true)
|
123
|
+
lambda { MongoMapper.connect('development') }.should raise_error(RuntimeError)
|
158
124
|
end
|
159
125
|
|
160
|
-
it "should create a replica set connection if config contains multiple hosts in the new format" do
|
161
|
-
MongoMapper.config = {
|
162
|
-
'development' => {
|
163
|
-
'hosts' => ['127.0.0.1:27017', 'localhost:27017'],
|
164
|
-
'database' => 'test'
|
165
|
-
}
|
166
|
-
}
|
167
|
-
|
168
|
-
expect(Mongo::MongoReplicaSetClient).to receive(:new).with( ['127.0.0.1:27017', 'localhost:27017'], {'read_secondary' => true} )
|
169
|
-
expect(MongoMapper).to receive(:database=).with('test')
|
170
|
-
expect_any_instance_of(Mongo::DB).to receive(:authenticate).never
|
171
|
-
MongoMapper.connect('development', 'read_secondary' => true)
|
172
|
-
end
|
173
126
|
end
|
174
127
|
|
175
128
|
context "setup" do
|
176
129
|
it "should work as shortcut for setting config, environment and options" do
|
177
130
|
config, logger = double('config'), double('logger')
|
178
|
-
|
179
|
-
|
180
|
-
|
131
|
+
MongoMapper.should_receive(:config=).with(config)
|
132
|
+
MongoMapper.should_receive(:connect).with('development', :logger => logger)
|
133
|
+
MongoMapper.should_receive(:handle_passenger_forking).once
|
181
134
|
MongoMapper.setup(config, 'development', :logger => logger)
|
182
135
|
end
|
183
136
|
end
|