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.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/{README.rdoc → README.md} +28 -20
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +1 -0
  10. data/lib/mongo_mapper/connection.rb +16 -38
  11. data/lib/mongo_mapper/extensions/object_id.rb +5 -1
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
  13. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  14. data/lib/mongo_mapper/plugins/document.rb +1 -1
  15. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
  16. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
  17. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  18. data/lib/mongo_mapper/plugins/indexes.rb +13 -6
  19. data/lib/mongo_mapper/plugins/keys.rb +1 -2
  20. data/lib/mongo_mapper/plugins/modifiers.rb +27 -10
  21. data/lib/mongo_mapper/plugins/persistence.rb +6 -2
  22. data/lib/mongo_mapper/plugins/querying.rb +9 -3
  23. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +3 -4
  24. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  25. data/lib/mongo_mapper/plugins/stats.rb +1 -3
  26. data/lib/mongo_mapper/utils.rb +2 -2
  27. data/lib/mongo_mapper/version.rb +1 -1
  28. data/spec/examples.txt +1643 -0
  29. data/spec/functional/accessible_spec.rb +1 -1
  30. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
  31. data/spec/functional/associations/belongs_to_proxy_spec.rb +4 -4
  32. data/spec/functional/associations/in_array_proxy_spec.rb +14 -14
  33. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  34. data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
  35. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
  36. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  37. data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
  38. data/spec/functional/associations/one_proxy_spec.rb +8 -8
  39. data/spec/functional/associations_spec.rb +3 -3
  40. data/spec/functional/binary_spec.rb +2 -2
  41. data/spec/functional/caching_spec.rb +15 -22
  42. data/spec/functional/callbacks_spec.rb +2 -2
  43. data/spec/functional/counter_cache_spec.rb +10 -10
  44. data/spec/functional/dirty_spec.rb +27 -10
  45. data/spec/functional/document_spec.rb +5 -5
  46. data/spec/functional/dumpable_spec.rb +1 -1
  47. data/spec/functional/embedded_document_spec.rb +5 -5
  48. data/spec/functional/identity_map_spec.rb +6 -6
  49. data/spec/functional/indexes_spec.rb +19 -18
  50. data/spec/functional/keys_spec.rb +22 -22
  51. data/spec/functional/logger_spec.rb +2 -2
  52. data/spec/functional/modifiers_spec.rb +67 -19
  53. data/spec/functional/partial_updates_spec.rb +8 -8
  54. data/spec/functional/protected_spec.rb +1 -1
  55. data/spec/functional/querying_spec.rb +48 -22
  56. data/spec/functional/safe_spec.rb +23 -27
  57. data/spec/functional/sci_spec.rb +7 -7
  58. data/spec/functional/scopes_spec.rb +1 -1
  59. data/spec/functional/static_keys_spec.rb +2 -2
  60. data/spec/functional/stats_spec.rb +28 -12
  61. data/spec/functional/validations_spec.rb +8 -16
  62. data/spec/quality_spec.rb +1 -1
  63. data/spec/spec_helper.rb +32 -8
  64. data/spec/support/matchers.rb +1 -1
  65. data/spec/unit/associations/proxy_spec.rb +1 -1
  66. data/spec/unit/clone_spec.rb +1 -1
  67. data/spec/unit/document_spec.rb +3 -3
  68. data/spec/unit/embedded_document_spec.rb +4 -5
  69. data/spec/unit/extensions_spec.rb +3 -4
  70. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  71. data/spec/unit/key_spec.rb +16 -17
  72. data/spec/unit/keys_spec.rb +7 -7
  73. data/spec/unit/mongo_mapper_spec.rb +41 -88
  74. data/spec/unit/rails_spec.rb +2 -2
  75. metadata +37 -24
  76. 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
@@ -45,7 +45,7 @@ describe "Clone" do
45
45
  end
46
46
 
47
47
  it "should clone a cloned document" do
48
- expect { @doc.clone.clone }.to_not raise_error
48
+ lambda { @doc.clone.clone }.should_not raise_error
49
49
  @doc.clone.clone.id.should be_a BSON::ObjectId
50
50
  end
51
51
  end
@@ -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::MongoClient)
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::MongoClient.new
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 = silence_stderr { @document.new }
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
- expect { @document.new(nil) }.to_not raise_error
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
- expect { @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
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
- expect{ @doc.save! }.to raise_error(MongoMapper::DocumentNotValid)
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
- expect(object.class).to receive(:to_mongo).with(object)
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
- utc_str = RUBY_PLATFORM != "java" ? 'UTC' : "GMT"
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": "#{id}"})
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
- context "" do
23
- before do
24
- @enabled = MongoMapper::Plugins::IdentityMap.enabled
25
- MongoMapper::Plugins::IdentityMap.enabled = false
26
- end
22
+ before do
23
+ @enabled = MongoMapper::Plugins::IdentityMap.enabled
24
+ MongoMapper::Plugins::IdentityMap.enabled = false
25
+ end
27
26
 
28
- after do
29
- MongoMapper::Plugins::IdentityMap.enabled = @enabled
30
- end
27
+ after do
28
+ MongoMapper::Plugins::IdentityMap.enabled = @enabled
29
+ end
31
30
 
32
- it "should delegate" do
33
- called = false
34
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
35
- called = true
36
- [200, {}, nil]
37
- }
38
- mw.call({})
39
- called.should be_truthy
40
- end
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
- it "should enable identity map during delegation" do
43
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
44
- MongoMapper::Plugins::IdentityMap.should be_enabled
45
- [200, {}, nil]
46
- }
47
- mw.call({})
48
- end
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
- class Enum < Struct.new(:iter)
51
- def each(&b)
52
- iter.call(&b)
53
- end
49
+ class Enum < Struct.new(:iter)
50
+ def each(&b)
51
+ iter.call(&b)
54
52
  end
53
+ end
55
54
 
56
- it "should enable IM for body each" do
57
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env|
58
- [200, {}, Enum.new(lambda { |&b|
59
- MongoMapper::Plugins::IdentityMap.should be_enabled
60
- b.call "hello"
61
- })]
62
- }
63
- body = mw.call({}).last
64
- body.each { |x| x.should eql('hello') }
65
- end
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
- it "should disable IM after body close" do
68
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
69
- body = mw.call({}).last
70
- MongoMapper::Plugins::IdentityMap.should be_enabled
71
- body.close
72
- MongoMapper::Plugins::IdentityMap.should_not be_enabled
73
- end
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
- it "should clear IM after body close" do
76
- mw = MongoMapper::Middleware::IdentityMap.new lambda { |env| [200, {}, []] }
77
- body = mw.call({}).last
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
- MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
80
- MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
78
+ MongoMapper::Plugins::IdentityMap.repository['hello'] = 'world'
79
+ MongoMapper::Plugins::IdentityMap.repository.should_not be_empty
81
80
 
82
- body.close
81
+ body.close
83
82
 
84
- MongoMapper::Plugins::IdentityMap.repository.should be_empty
85
- end
83
+ MongoMapper::Plugins::IdentityMap.repository.should be_empty
84
+ end
86
85
 
87
- context "with a successful request" do
88
- it "should clear the identity map" do
89
- expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).twice
90
- get '/'
91
- end
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
- context "when the request raises an error" do
95
- it "should clear the identity map" do
96
- expect(MongoMapper::Plugins::IdentityMap).to receive(:clear).once
97
- get '/fail' rescue nil
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
@@ -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
- expect { Key.new(:id) }.to raise_error(/reserved/)
62
+ lambda { Key.new(:id) }.should raise_error(/reserved/)
64
63
  end
65
64
 
66
65
  it "should not permit bad names" do
67
- expect { Key.new(:"id.bar") }.to raise_error(/must match/)
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
- expect { Key.new(:"id.bar", :__dynamic => true) }.to_not raise_error
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
- expect { Key.new(:"id.bar", :accessors => :skip) }.to_not raise_error
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 { expect( subject.read_accessor? ).to be_truthy }
153
- it { expect( subject.write_accessor? ).to be_falsey }
154
- it { expect( subject.predicate_accessor? ).to be_falsey }
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 { expect( subject.read_accessor? ).to be_falsey }
161
- it { expect( subject.write_accessor? ).to be_truthy }
162
- it { expect( subject.predicate_accessor? ).to be_falsey }
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 { expect( subject.read_accessor? ).to be_falsey }
169
- it { expect( subject.write_accessor? ).to be_falsey }
170
- it { expect( subject.predicate_accessor? ).to be_truthy }
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 { expect( subject.read_accessor? ).to be_truthy }
177
- it { expect( subject.write_accessor? ).to be_truthy }
178
- it { expect( subject.predicate_accessor? ).to be_falsey }
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
 
@@ -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
- silence_stderr { klass.new.id.should be_nil }
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
- expect(doc).to receive(:warn).once
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
- expect {
85
+ lambda {
86
86
  doc = @klass.first
87
87
  doc.user['id'].should == 1
88
88
  doc.user['name'].should == 'John Nunemaker'
89
- }.to_not raise_error
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
- expect { instance = klass.create }.to change { counter }.by(1)
145
- expect {
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
- }.to_not change { counter }
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::MongoClient.new
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::MongoClient)
14
+ MongoMapper.connection.should be_instance_of(Mongo::Client)
15
15
  end
16
16
 
17
- it "should be able to write and read default database" do
18
- MongoMapper.database = 'test'
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
- expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, {})
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 with sinatra environment symbol" do
53
+ it "should work without authentication using hosts" do
60
54
  MongoMapper.config = {
61
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
55
+ 'development' => {'hosts' => ['127.0.0.1:27017']}
62
56
  }
63
- expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, {})
64
- expect(MongoMapper).to receive(:database=).with('test')
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 pass along ssl when false" do
61
+ it "should work without authentication using host" do
88
62
  MongoMapper.config = {
89
- 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'ssl' => false}
63
+ 'development' => {'host' => '127.0.0.1:27017'}
90
64
  }
91
- connection, logger = double('connection'), double('logger')
92
- expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, :logger => logger, :ssl => false)
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' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'options' => {'read' => 'primary'}}
71
+ 'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'read' => 'primary'}
99
72
  }
100
- connection, logger = double('connection'), double('logger')
101
- expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, :logger => logger, :read => :primary)
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' => {'host' => '192.168.1.1', 'port' => 2222, 'database' => 'test', 'options' => {'safe' => true}}
80
+ 'development' => {'hosts' => ['192.168.1.1:2222'], 'database' => 'test', 'safe' => true}
108
81
  }
109
- connection, logger = double('connection'), double('logger')
110
- expect(Mongo::MongoClient).to receive(:new).with('192.168.1.1', 2222, :logger => logger, :safe => true)
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
- connection, logger = double('connection'), double('logger')
119
- expect(Mongo::MongoClient).to receive(:new).with('127.0.0.1', 27017, :logger => logger)
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' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
98
+ 'development' => {'hosts' => ['127.0.0.1:27017'], 'database' => 'test', 'user' => 'john', 'password' => 'secret'}
126
99
  }
127
- expect_any_instance_of(Mongo::DB).to receive(:authenticate).with('john', 'secret')
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
- expect_any_instance_of(Mongo::DB).to receive(:authenticate).with('john', 'secret')
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
- expect { MongoMapper.connect('development') }.to raise_error(MongoMapper::InvalidScheme)
116
+ lambda { MongoMapper.connect('development') }.should raise_error(Mongo::Error::InvalidURI)
144
117
  end
145
118
 
146
- it "should create a replica set connection if config contains multiple hosts in the old format" do
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
- expect(MongoMapper).to receive(:config=).with(config)
179
- expect(MongoMapper).to receive(:connect).with('development', :logger => logger)
180
- expect(MongoMapper).to receive(:handle_passenger_forking).once
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