jamieorc-mongo_mapper 0.11.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +33 -0
  3. data/UPGRADES +26 -0
  4. data/bin/mmconsole +59 -0
  5. data/examples/attr_accessible.rb +22 -0
  6. data/examples/attr_protected.rb +22 -0
  7. data/examples/cache_key.rb +24 -0
  8. data/examples/custom_types.rb +24 -0
  9. data/examples/identity_map.rb +33 -0
  10. data/examples/identity_map/automatic.rb +2 -0
  11. data/examples/keys.rb +40 -0
  12. data/examples/modifiers/set.rb +25 -0
  13. data/examples/plugins.rb +38 -0
  14. data/examples/querying.rb +35 -0
  15. data/examples/safe.rb +43 -0
  16. data/examples/scopes.rb +52 -0
  17. data/examples/validating/embedded_docs.rb +29 -0
  18. data/lib/mongo_mapper.rb +94 -0
  19. data/lib/mongo_mapper/connection.rb +96 -0
  20. data/lib/mongo_mapper/document.rb +42 -0
  21. data/lib/mongo_mapper/embedded_document.rb +32 -0
  22. data/lib/mongo_mapper/exceptions.rb +27 -0
  23. data/lib/mongo_mapper/extensions/array.rb +19 -0
  24. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  25. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  26. data/lib/mongo_mapper/extensions/date.rb +25 -0
  27. data/lib/mongo_mapper/extensions/float.rb +14 -0
  28. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  29. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  30. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  31. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  32. data/lib/mongo_mapper/extensions/object.rb +26 -0
  33. data/lib/mongo_mapper/extensions/object_id.rb +32 -0
  34. data/lib/mongo_mapper/extensions/set.rb +20 -0
  35. data/lib/mongo_mapper/extensions/string.rb +18 -0
  36. data/lib/mongo_mapper/extensions/time.rb +28 -0
  37. data/lib/mongo_mapper/locale/en.yml +5 -0
  38. data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
  39. data/lib/mongo_mapper/plugins.rb +22 -0
  40. data/lib/mongo_mapper/plugins/accessible.rb +36 -0
  41. data/lib/mongo_mapper/plugins/active_model.rb +18 -0
  42. data/lib/mongo_mapper/plugins/associations.rb +90 -0
  43. data/lib/mongo_mapper/plugins/associations/base.rb +92 -0
  44. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +53 -0
  45. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +34 -0
  46. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  47. data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
  48. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +44 -0
  49. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +133 -0
  50. data/lib/mongo_mapper/plugins/associations/many_association.rb +63 -0
  51. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  52. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
  53. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  54. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  55. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  56. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +22 -0
  57. data/lib/mongo_mapper/plugins/associations/one_association.rb +48 -0
  58. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  59. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  60. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +95 -0
  61. data/lib/mongo_mapper/plugins/associations/proxy.rb +134 -0
  62. data/lib/mongo_mapper/plugins/associations/single_association.rb +46 -0
  63. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  64. data/lib/mongo_mapper/plugins/callbacks.rb +29 -0
  65. data/lib/mongo_mapper/plugins/clone.rb +22 -0
  66. data/lib/mongo_mapper/plugins/dirty.rb +60 -0
  67. data/lib/mongo_mapper/plugins/document.rb +41 -0
  68. data/lib/mongo_mapper/plugins/dynamic_querying.rb +45 -0
  69. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  70. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +56 -0
  71. data/lib/mongo_mapper/plugins/embedded_document.rb +53 -0
  72. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  73. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  74. data/lib/mongo_mapper/plugins/indexes.rb +13 -0
  75. data/lib/mongo_mapper/plugins/inspect.rb +16 -0
  76. data/lib/mongo_mapper/plugins/keys.rb +313 -0
  77. data/lib/mongo_mapper/plugins/keys/key.rb +61 -0
  78. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  79. data/lib/mongo_mapper/plugins/modifiers.rb +132 -0
  80. data/lib/mongo_mapper/plugins/pagination.rb +16 -0
  81. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  82. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  83. data/lib/mongo_mapper/plugins/querying.rb +171 -0
  84. data/lib/mongo_mapper/plugins/querying/decorator.rb +34 -0
  85. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +21 -0
  86. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  87. data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  88. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  89. data/lib/mongo_mapper/plugins/sci.rb +36 -0
  90. data/lib/mongo_mapper/plugins/scopes.rb +27 -0
  91. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  92. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  93. data/lib/mongo_mapper/plugins/touch.rb +18 -0
  94. data/lib/mongo_mapper/plugins/userstamps.rb +18 -0
  95. data/lib/mongo_mapper/plugins/validations.rb +86 -0
  96. data/lib/mongo_mapper/railtie.rb +48 -0
  97. data/lib/mongo_mapper/railtie/database.rake +65 -0
  98. data/lib/mongo_mapper/translation.rb +10 -0
  99. data/lib/mongo_mapper/version.rb +4 -0
  100. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +24 -0
  101. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +18 -0
  102. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +23 -0
  103. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +13 -0
  104. data/test/_NOTE_ON_TESTING +1 -0
  105. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
  106. data/test/functional/associations/test_belongs_to_proxy.rb +238 -0
  107. data/test/functional/associations/test_in_array_proxy.rb +349 -0
  108. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  109. data/test/functional/associations/test_many_documents_proxy.rb +866 -0
  110. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +239 -0
  111. data/test/functional/associations/test_many_embedded_proxy.rb +289 -0
  112. data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
  113. data/test/functional/associations/test_one_as_proxy.rb +491 -0
  114. data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
  115. data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
  116. data/test/functional/associations/test_one_proxy.rb +383 -0
  117. data/test/functional/test_accessible.rb +198 -0
  118. data/test/functional/test_associations.rb +46 -0
  119. data/test/functional/test_binary.rb +27 -0
  120. data/test/functional/test_caching.rb +77 -0
  121. data/test/functional/test_callbacks.rb +232 -0
  122. data/test/functional/test_dirty.rb +301 -0
  123. data/test/functional/test_document.rb +282 -0
  124. data/test/functional/test_dynamic_querying.rb +75 -0
  125. data/test/functional/test_embedded_document.rb +288 -0
  126. data/test/functional/test_equality.rb +20 -0
  127. data/test/functional/test_identity_map.rb +513 -0
  128. data/test/functional/test_indexes.rb +50 -0
  129. data/test/functional/test_logger.rb +20 -0
  130. data/test/functional/test_modifiers.rb +483 -0
  131. data/test/functional/test_pagination.rb +91 -0
  132. data/test/functional/test_protected.rb +201 -0
  133. data/test/functional/test_querying.rb +935 -0
  134. data/test/functional/test_safe.rb +76 -0
  135. data/test/functional/test_sci.rb +240 -0
  136. data/test/functional/test_scopes.rb +171 -0
  137. data/test/functional/test_timestamps.rb +62 -0
  138. data/test/functional/test_touch.rb +125 -0
  139. data/test/functional/test_userstamps.rb +44 -0
  140. data/test/functional/test_validations.rb +405 -0
  141. data/test/models.rb +261 -0
  142. data/test/support/railtie.rb +4 -0
  143. data/test/support/railtie/autoloaded.rb +2 -0
  144. data/test/support/railtie/not_autoloaded.rb +3 -0
  145. data/test/support/railtie/parent.rb +3 -0
  146. data/test/test_active_model_lint.rb +18 -0
  147. data/test/test_helper.rb +94 -0
  148. data/test/unit/associations/test_base.rb +146 -0
  149. data/test/unit/associations/test_belongs_to_association.rb +29 -0
  150. data/test/unit/associations/test_many_association.rb +63 -0
  151. data/test/unit/associations/test_one_association.rb +47 -0
  152. data/test/unit/associations/test_proxy.rb +100 -0
  153. data/test/unit/serializers/test_json_serializer.rb +216 -0
  154. data/test/unit/serializers/test_xml_serializer.rb +196 -0
  155. data/test/unit/test_clone.rb +69 -0
  156. data/test/unit/test_document.rb +249 -0
  157. data/test/unit/test_dynamic_finder.rb +125 -0
  158. data/test/unit/test_embedded_document.rb +682 -0
  159. data/test/unit/test_equality.rb +38 -0
  160. data/test/unit/test_extensions.rb +380 -0
  161. data/test/unit/test_identity_map_middleware.rb +34 -0
  162. data/test/unit/test_inspect.rb +47 -0
  163. data/test/unit/test_key.rb +205 -0
  164. data/test/unit/test_keys.rb +65 -0
  165. data/test/unit/test_mongo_mapper.rb +143 -0
  166. data/test/unit/test_pagination.rb +11 -0
  167. data/test/unit/test_plugins.rb +89 -0
  168. data/test/unit/test_rails.rb +183 -0
  169. data/test/unit/test_rails_compatibility.rb +38 -0
  170. data/test/unit/test_rails_reflect_on_association.rb +118 -0
  171. data/test/unit/test_railtie.rb +61 -0
  172. data/test/unit/test_serialization.rb +166 -0
  173. data/test/unit/test_time_zones.rb +44 -0
  174. data/test/unit/test_translation.rb +27 -0
  175. data/test/unit/test_validations.rb +562 -0
  176. metadata +262 -0
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class InspectTest < Test::Unit::TestCase
4
+ context "#inspect" do
5
+ setup do
6
+ @document = Doc('User') do
7
+ key :name, String
8
+ key :age, Integer
9
+ key :email, String
10
+ end
11
+
12
+ @doc = @document.new(:name => 'John', :age => 29)
13
+ end
14
+
15
+ should "print out non-nil attributes in alpha sorted order" do
16
+ @doc.inspect.should =~ /_id:.*, age: 29, name: "John"/
17
+ end
18
+
19
+ should "print out all attributes when (optional) include_super argument is true" do
20
+ @doc.inspect(true).should =~ /_id:.*, age: 29, email: nil, name: "John"/
21
+ end
22
+
23
+ should "include class name" do
24
+ @doc.inspect.should =~ /^#<User/
25
+ end
26
+
27
+ should "include embedded documents" do
28
+ klass = Doc()
29
+ pets = EDoc()
30
+
31
+ klass.many :pets, :class => pets
32
+
33
+ doc = klass.new(:pets => [{:name => "Kitten"}])
34
+ doc.inspect.should =~ /_id:.*, pets: \[.*_id.*, name: "Kitten".*\]/
35
+ end
36
+
37
+ should "include embedded document" do
38
+ klass = Doc()
39
+ pet = EDoc()
40
+
41
+ klass.one :pet, :class => pet
42
+
43
+ doc = klass.new(:pet => {:name => "Kitten"})
44
+ doc.inspect.should =~ /_id:.*, pet: .*_id.*, name: "Kitten".*/
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,205 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class FooType < Struct.new(:bar)
5
+ def self.to_mongo(value)
6
+ 'to_mongo'
7
+ end
8
+
9
+ def self.from_mongo(value)
10
+ 'from_mongo'
11
+ end
12
+ end
13
+
14
+ class KeyTest < Test::Unit::TestCase
15
+ Key = MongoMapper::Plugins::Keys::Key
16
+
17
+ context "Initializing a new key" do
18
+ should "allow setting the name" do
19
+ Key.new(:foo, String).name.should == 'foo'
20
+ end
21
+
22
+ should "allow setting the type" do
23
+ Key.new(:foo, Integer).type.should be(Integer)
24
+ end
25
+
26
+ should "allow setting options" do
27
+ Key.new(:foo, Integer, :required => true).options[:required].should be(true)
28
+ end
29
+
30
+ should "default options to {}" do
31
+ Key.new(:foo, Integer, nil).options.should == {}
32
+ end
33
+
34
+ should "symbolize option keys" do
35
+ Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
36
+ end
37
+
38
+ should "work with just name" do
39
+ key = Key.new(:foo)
40
+ key.name.should == 'foo'
41
+ end
42
+
43
+ should "work with name and type" do
44
+ key = Key.new(:foo, String)
45
+ key.name.should == 'foo'
46
+ key.type.should == String
47
+ end
48
+
49
+ should "work with name, type, and options" do
50
+ key = Key.new(:foo, String, :required => true)
51
+ key.name.should == 'foo'
52
+ key.type.should == String
53
+ key.options[:required].should be_true
54
+ end
55
+
56
+ should "work with name and options" do
57
+ key = Key.new(:foo, :required => true)
58
+ key.name.should == 'foo'
59
+ key.options[:required].should be_true
60
+ end
61
+ end
62
+
63
+ context "A key" do
64
+ should "be equal to another key with same name and type" do
65
+ Key.new(:name, String).should == Key.new(:name, String)
66
+ end
67
+
68
+ should "not be equal to another key with different name" do
69
+ Key.new(:name, String).should_not == Key.new(:foo, String)
70
+ end
71
+
72
+ should "not be equal to another key with different type" do
73
+ Key.new(:name, String).should_not == Key.new(:name, Integer)
74
+ end
75
+
76
+ should "know if it is a embedded_document" do
77
+ Key.new(:name, EDoc()).embeddable?.should be_true
78
+ end
79
+
80
+ should "know if it is not a embedded_document" do
81
+ Key.new(:name, String).embeddable?.should be_false
82
+ end
83
+
84
+ should "know if it is a number" do
85
+ Key.new(:age, Integer).number?.should be_true
86
+ Key.new(:age, Float).number?.should be_true
87
+ end
88
+
89
+ should "know if it is not a number" do
90
+ Key.new(:age, String).number?.should be_false
91
+ end
92
+ end
93
+
94
+ context "for an array with :typecast option" do
95
+ setup { @key = Key.new(:user_ids, Array, :typecast => 'ObjectId') }
96
+ subject { @key }
97
+
98
+ should "cast each element correctly" do
99
+ ids = [BSON::ObjectId.new, BSON::ObjectId.new, BSON::ObjectId.new.to_s, BSON::ObjectId.new.to_s]
100
+ subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
101
+ end
102
+ end
103
+
104
+ context "for an array with :typecast option of Date" do
105
+ setup { @key = Key.new(:dates, Array, :typecast => 'Date') }
106
+ subject { @key }
107
+
108
+ should "cast each element correctly when get" do
109
+ dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
110
+ subject.get(dates).should == dates.map { |date| Date.from_mongo(date) }
111
+ end
112
+
113
+ should "cast each element correctly when set" do
114
+ dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
115
+ subject.set(dates).should == dates.map { |date| Date.to_mongo(date) }
116
+ end
117
+ end
118
+
119
+ context "for a set with :typecast option" do
120
+ setup { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') }
121
+ subject { @key }
122
+
123
+ should "cast each element correctly" do
124
+ ids = [BSON::ObjectId.new, BSON::ObjectId.new, BSON::ObjectId.new.to_s, BSON::ObjectId.new.to_s]
125
+ subject.set(ids).should == ids.map { |id| ObjectId.to_mongo(id) }
126
+ end
127
+ end
128
+
129
+ context "setting a value with a custom type" do
130
+ should "correctly typecast" do
131
+ key = Key.new(:foo, FooType)
132
+ key.set("something").should == 'to_mongo'
133
+ end
134
+
135
+ should "correctly typecast if object of that type is given" do
136
+ key = Key.new(:foo, FooType)
137
+ key.set(FooType.new('something')).should == 'to_mongo'
138
+ end
139
+ end
140
+
141
+ context "getting a value with a custom type" do
142
+ should "use #from_mongo to convert back to custom type" do
143
+ key = Key.new(:foo, FooType)
144
+ key.get('something').should == 'from_mongo'
145
+ end
146
+ end
147
+
148
+ context "getting a value" do
149
+ should "work with a type" do
150
+ key = Key.new(:foo, String)
151
+ key.get('bar').should == 'bar'
152
+ end
153
+
154
+ should "work without type" do
155
+ key = Key.new(:foo)
156
+ key.get([1, '2']).should == [1, '2']
157
+ key.get(false).should == false
158
+ key.get({}).should == {}
159
+ end
160
+
161
+ context "for a embedded_document" do
162
+ should "default to nil" do
163
+ key = Key.new(:foo, Address)
164
+ key.get(nil).should be_nil
165
+ end
166
+
167
+ should "return instance if instance" do
168
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
169
+ key = Key.new(:foo, Address)
170
+ key.get(address).should == address
171
+ end
172
+ end
173
+ end
174
+
175
+ context "getting a value with a default set" do
176
+ setup do
177
+ @key = Key.new(:foo, String, :default => 'baz')
178
+ end
179
+
180
+ should "return default value if value nil" do
181
+ @key.get(nil).should == 'baz'
182
+ end
183
+
184
+ should "return a dup of the default value" do
185
+ @key.get(nil).replace('bar')
186
+ @key.get(nil).should == 'baz'
187
+ end
188
+
189
+ should "return value if not blank" do
190
+ @key.get('foobar').should == 'foobar'
191
+ end
192
+
193
+ should "work with Boolean type and false value" do
194
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
195
+ end
196
+
197
+ should "work with Boolean type and true value" do
198
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
199
+ end
200
+
201
+ should "work with procs" do
202
+ Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
203
+ end
204
+ end
205
+ end # KeyTest
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class KeyTest < Test::Unit::TestCase
5
+ context ".new with no id and _id of type integer" do
6
+ should "not error" do
7
+ lambda {
8
+ klass = Doc() do
9
+ key :_id, Integer
10
+ end
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 }
13
+ }.should_not raise_error
14
+ end
15
+ end
16
+
17
+ context ".key?(:symbol)" do
18
+ should "be true if document has key" do
19
+ Address.key?(:city).should be_true
20
+ end
21
+
22
+ should "be false if document does not have key" do
23
+ Address.key?(:foo).should be_false
24
+ end
25
+ end
26
+
27
+ context ".key?('string')" do
28
+ should "be true if document has key" do
29
+ Address.key?('city').should be_true
30
+ end
31
+
32
+ should "be false if document does not have key" do
33
+ Address.key?('foo').should be_false
34
+ end
35
+ end
36
+
37
+ context ".new (from database)" do
38
+ setup do
39
+ @klass = Doc do
40
+ key :user, Hash
41
+
42
+ def user=(user)
43
+ super(:id => user.id, :name => user.name)
44
+ end
45
+ end
46
+
47
+ user_class = Struct.new(:id, :name)
48
+ @klass.create(:user => user_class.new(1, 'John Nunemaker'))
49
+ end
50
+
51
+ should "use []= for keys instead of public writer" do
52
+ assert_nothing_raised do
53
+ doc = @klass.first
54
+ doc.user['id'].should == 1
55
+ doc.user['name'].should == 'John Nunemaker'
56
+ end
57
+ end
58
+ end
59
+
60
+ context ".load" do
61
+ should "return nil if argument is nil" do
62
+ Doc().load(nil).should be_nil
63
+ end
64
+ end
65
+ end # KeyTest
@@ -0,0 +1,143 @@
1
+ require 'test_helper'
2
+
3
+ class Address; end
4
+
5
+ class MongoMapperTest < Test::Unit::TestCase
6
+ should "be able to write and read connection" do
7
+ conn = Mongo::Connection.new
8
+ MongoMapper.connection = conn
9
+ MongoMapper.connection.should == conn
10
+ end
11
+
12
+ should "default connection to new mongo ruby driver" do
13
+ MongoMapper.connection = nil
14
+ MongoMapper.connection.should be_instance_of(Mongo::Connection)
15
+ end
16
+
17
+ should "be able to write and read default database" do
18
+ MongoMapper.database = 'test'
19
+ MongoMapper.database.should be_instance_of(Mongo::DB)
20
+ MongoMapper.database.name.should == 'test'
21
+ end
22
+
23
+ should "have document not found error" do
24
+ lambda {
25
+ MongoMapper::DocumentNotFound
26
+ }.should_not raise_error
27
+ end
28
+
29
+ should "be able to read/write config" do
30
+ config = {
31
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'},
32
+ 'production' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test-prod'}
33
+ }
34
+ MongoMapper.config = config
35
+ MongoMapper.config.should == config
36
+ end
37
+
38
+ context "connecting to environment from config" do
39
+ should "work without authentication" do
40
+ MongoMapper.config = {
41
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
42
+ }
43
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
44
+ MongoMapper.expects(:database=).with('test')
45
+ Mongo::DB.any_instance.expects(:authenticate).never
46
+ MongoMapper.connect('development')
47
+ end
48
+
49
+ should "work without authentication using uri" do
50
+ MongoMapper.config = {
51
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
52
+ }
53
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
54
+ MongoMapper.expects(:database=).with('test')
55
+ Mongo::DB.any_instance.expects(:authenticate).never
56
+ MongoMapper.connect('development')
57
+ end
58
+
59
+ should "work with sinatra environment symbol" do
60
+ MongoMapper.config = {
61
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
+ }
63
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
64
+ MongoMapper.expects(:database=).with('test')
65
+ Mongo::DB.any_instance.expects(:authenticate).never
66
+ MongoMapper.connect(:development)
67
+ end
68
+
69
+ should "work with options" do
70
+ MongoMapper.config = {
71
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
72
+ }
73
+ connection, logger = mock('connection'), mock('logger')
74
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
75
+ MongoMapper.connect('development', :logger => logger)
76
+ end
77
+
78
+ should "work with options from config" do
79
+ MongoMapper.config = {
80
+ 'development' => {'host' => '192.168.1.1', 'port' => 2222, 'database' => 'test', 'options' => {'safe' => true}}
81
+ }
82
+ connection, logger = mock('connection'), mock('logger')
83
+ Mongo::Connection.expects(:new).with('192.168.1.1', 2222, :logger => logger, :safe => true)
84
+ MongoMapper.connect('development', :logger => logger)
85
+ end
86
+
87
+ should "work with options using uri" do
88
+ MongoMapper.config = {
89
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
90
+ }
91
+ connection, logger = mock('connection'), mock('logger')
92
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
93
+ MongoMapper.connect('development', :logger => logger)
94
+ end
95
+
96
+ should "work with authentication" do
97
+ MongoMapper.config = {
98
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
99
+ }
100
+ Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
101
+ MongoMapper.connect('development')
102
+ end
103
+
104
+ should "work with authentication using uri" do
105
+ MongoMapper.config = {
106
+ 'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
107
+ }
108
+ Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
109
+ MongoMapper.connect('development')
110
+ end
111
+
112
+ should "raise error for invalid scheme" do
113
+ MongoMapper.config = {
114
+ 'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
115
+ }
116
+ assert_raises(MongoMapper::InvalidScheme) { MongoMapper.connect('development') }
117
+ end
118
+
119
+ should "create a replica set connection if config contains multiple hosts" do
120
+ MongoMapper.config = {
121
+ 'development' => {
122
+ 'hosts' => [ ['127.0.0.1', 27017], ['localhost', 27017] ],
123
+ 'database' => 'test'
124
+ }
125
+ }
126
+
127
+ Mongo::ReplSetConnection.expects(:new).with( ['127.0.0.1', 27017], ['localhost', 27017], {'read_secondary' => true} )
128
+ MongoMapper.expects(:database=).with('test')
129
+ Mongo::DB.any_instance.expects(:authenticate).never
130
+ MongoMapper.connect('development', 'read_secondary' => true)
131
+ end
132
+ end
133
+
134
+ context "setup" do
135
+ should "work as shortcut for setting config, environment and options" do
136
+ config, logger = mock('config'), mock('logger')
137
+ MongoMapper.expects(:config=).with(config)
138
+ MongoMapper.expects(:connect).with('development', :logger => logger)
139
+ MongoMapper.expects(:handle_passenger_forking).once
140
+ MongoMapper.setup(config, 'development', :logger => logger)
141
+ end
142
+ end
143
+ end