mongoid 4.0.2 → 5.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +14 -13
  4. data/lib/config/locales/en.yml +28 -28
  5. data/lib/mongoid.rb +28 -21
  6. data/lib/mongoid/atomic.rb +2 -4
  7. data/lib/mongoid/attributes.rb +7 -7
  8. data/lib/mongoid/attributes/processing.rb +4 -1
  9. data/lib/mongoid/attributes/readonly.rb +2 -2
  10. data/lib/mongoid/changeable.rb +4 -6
  11. data/lib/mongoid/clients.rb +142 -0
  12. data/lib/mongoid/clients/factory.rb +78 -0
  13. data/lib/mongoid/{sessions → clients}/options.rb +30 -19
  14. data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
  15. data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
  16. data/lib/mongoid/clients/validators.rb +2 -0
  17. data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
  18. data/lib/mongoid/composable.rb +3 -3
  19. data/lib/mongoid/config.rb +39 -41
  20. data/lib/mongoid/config/environment.rb +1 -1
  21. data/lib/mongoid/config/validators.rb +1 -1
  22. data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
  23. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  24. data/lib/mongoid/contextual/atomic.rb +11 -11
  25. data/lib/mongoid/contextual/command.rb +9 -6
  26. data/lib/mongoid/contextual/geo_near.rb +17 -1
  27. data/lib/mongoid/contextual/map_reduce.rb +12 -11
  28. data/lib/mongoid/contextual/memory.rb +2 -5
  29. data/lib/mongoid/contextual/mongo.rb +92 -82
  30. data/lib/mongoid/contextual/none.rb +13 -0
  31. data/lib/mongoid/copyable.rb +6 -1
  32. data/lib/mongoid/criteria.rb +36 -3
  33. data/lib/mongoid/document.rb +3 -4
  34. data/lib/mongoid/errors.rb +6 -6
  35. data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
  36. data/lib/mongoid/errors/no_client_config.rb +22 -0
  37. data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
  38. data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
  39. data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
  40. data/lib/mongoid/errors/no_default_client.rb +23 -0
  41. data/lib/mongoid/extensions/hash.rb +5 -1
  42. data/lib/mongoid/extensions/object.rb +3 -2
  43. data/lib/mongoid/extensions/set.rb +5 -5
  44. data/lib/mongoid/factory.rb +4 -2
  45. data/lib/mongoid/fields.rb +7 -2
  46. data/lib/mongoid/findable.rb +4 -1
  47. data/lib/mongoid/indexable.rb +15 -9
  48. data/lib/mongoid/persistable.rb +1 -2
  49. data/lib/mongoid/persistable/creatable.rb +2 -2
  50. data/lib/mongoid/persistable/deletable.rb +3 -3
  51. data/lib/mongoid/persistable/incrementable.rb +1 -1
  52. data/lib/mongoid/persistable/logical.rb +1 -1
  53. data/lib/mongoid/persistable/poppable.rb +1 -1
  54. data/lib/mongoid/persistable/pullable.rb +2 -2
  55. data/lib/mongoid/persistable/pushable.rb +2 -2
  56. data/lib/mongoid/persistable/renamable.rb +1 -1
  57. data/lib/mongoid/persistable/settable.rb +1 -1
  58. data/lib/mongoid/persistable/unsettable.rb +1 -1
  59. data/lib/mongoid/persistable/updatable.rb +2 -2
  60. data/lib/mongoid/persistable/upsertable.rb +1 -1
  61. data/lib/mongoid/query_cache.rb +98 -104
  62. data/lib/mongoid/railtie.rb +1 -21
  63. data/lib/mongoid/railties/database.rake +1 -1
  64. data/lib/mongoid/relations/builders.rb +3 -1
  65. data/lib/mongoid/relations/counter_cache.rb +1 -1
  66. data/lib/mongoid/relations/embedded/batchable.rb +3 -10
  67. data/lib/mongoid/relations/embedded/many.rb +4 -2
  68. data/lib/mongoid/relations/many.rb +1 -0
  69. data/lib/mongoid/relations/proxy.rb +6 -6
  70. data/lib/mongoid/relations/referenced/many.rb +2 -1
  71. data/lib/mongoid/relations/targets/enumerable.rb +11 -11
  72. data/lib/mongoid/relations/touchable.rb +1 -1
  73. data/lib/mongoid/reloadable.rb +2 -2
  74. data/lib/mongoid/scopable.rb +6 -17
  75. data/lib/mongoid/selectable.rb +1 -36
  76. data/lib/mongoid/serializable.rb +2 -2
  77. data/lib/mongoid/stateful.rb +0 -1
  78. data/lib/mongoid/tasks/database.rake +2 -2
  79. data/lib/mongoid/tasks/database.rb +23 -16
  80. data/lib/mongoid/threaded.rb +54 -33
  81. data/lib/mongoid/threaded/lifecycle.rb +21 -16
  82. data/lib/mongoid/traversable.rb +16 -1
  83. data/lib/mongoid/validatable.rb +1 -1
  84. data/lib/mongoid/validatable/queryable.rb +1 -1
  85. data/lib/mongoid/validatable/uniqueness.rb +3 -20
  86. data/lib/mongoid/version.rb +1 -1
  87. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
  88. data/lib/rails/mongoid.rb +2 -2
  89. data/spec/app/models/audio.rb +1 -1
  90. data/spec/app/models/band.rb +1 -0
  91. data/spec/app/models/company.rb +5 -0
  92. data/spec/app/models/label.rb +7 -0
  93. data/spec/app/models/pub.rb +6 -0
  94. data/spec/app/models/staff.rb +7 -0
  95. data/spec/app/models/store_as_dup_test1.rb +5 -0
  96. data/spec/app/models/store_as_dup_test2.rb +5 -0
  97. data/spec/config/mongoid.yml +7 -25
  98. data/spec/mongoid/atomic/paths_spec.rb +3 -11
  99. data/spec/mongoid/attributes/nested_spec.rb +16 -16
  100. data/spec/mongoid/attributes/readonly_spec.rb +80 -18
  101. data/spec/mongoid/attributes_spec.rb +3 -3
  102. data/spec/mongoid/changeable_spec.rb +70 -0
  103. data/spec/mongoid/clients/factory_spec.rb +284 -0
  104. data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
  105. data/spec/mongoid/clients_spec.rb +739 -0
  106. data/spec/mongoid/config/environment_spec.rb +14 -11
  107. data/spec/mongoid/config_spec.rb +33 -48
  108. data/spec/mongoid/contextual/atomic_spec.rb +1 -17
  109. data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
  110. data/spec/mongoid/contextual/mongo_spec.rb +26 -83
  111. data/spec/mongoid/contextual/none_spec.rb +15 -0
  112. data/spec/mongoid/copyable_spec.rb +35 -1
  113. data/spec/mongoid/criteria/findable_spec.rb +197 -0
  114. data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
  115. data/spec/mongoid/criteria_spec.rb +74 -91
  116. data/spec/mongoid/document_spec.rb +1 -1
  117. data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
  118. data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
  119. data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
  120. data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
  121. data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
  122. data/spec/mongoid/fields/localized_spec.rb +1 -0
  123. data/spec/mongoid/fields_spec.rb +1 -0
  124. data/spec/mongoid/findable_spec.rb +2 -23
  125. data/spec/mongoid/indexable_spec.rb +12 -8
  126. data/spec/mongoid/interceptable_spec.rb +15 -0
  127. data/spec/mongoid/persistable/settable_spec.rb +16 -0
  128. data/spec/mongoid/persistable/updatable_spec.rb +3 -2
  129. data/spec/mongoid/persistable_spec.rb +4 -4
  130. data/spec/mongoid/query_cache_spec.rb +13 -8
  131. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  132. data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
  133. data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
  134. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
  135. data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
  136. data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
  137. data/spec/mongoid/relations/proxy_spec.rb +28 -0
  138. data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
  139. data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
  140. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
  141. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  142. data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
  143. data/spec/mongoid/reloadable_spec.rb +6 -6
  144. data/spec/mongoid/scopable_spec.rb +41 -28
  145. data/spec/mongoid/selectable_spec.rb +6 -16
  146. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  147. data/spec/mongoid/tasks/database_spec.rb +2 -2
  148. data/spec/mongoid/threaded_spec.rb +0 -7
  149. data/spec/mongoid/traversable_spec.rb +2 -2
  150. data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
  151. data/spec/mongoid_spec.rb +13 -15
  152. data/spec/rails/mongoid_spec.rb +13 -4
  153. data/spec/spec_helper.rb +44 -27
  154. data/spec/support/authorization.rb +12 -0
  155. data/spec/support/expectations.rb +14 -0
  156. metadata +52 -59
  157. data/lib/mongoid/contextual/find_and_modify.rb +0 -69
  158. data/lib/mongoid/contextual/text_search.rb +0 -178
  159. data/lib/mongoid/criteria/#findable.rb# +0 -141
  160. data/lib/mongoid/errors/no_default_session.rb +0 -23
  161. data/lib/mongoid/errors/no_session_config.rb +0 -22
  162. data/lib/mongoid/log_subscriber.rb +0 -55
  163. data/lib/mongoid/positional.rb +0 -71
  164. data/lib/mongoid/sessions.rb +0 -125
  165. data/lib/mongoid/sessions/factory.rb +0 -131
  166. data/lib/mongoid/sessions/mongo_uri.rb +0 -93
  167. data/lib/mongoid/sessions/validators.rb +0 -2
  168. data/lib/mongoid/support/query_counter.rb +0 -23
  169. data/spec/helpers.rb +0 -18
  170. data/spec/mongoid/#atomic_spec.rb# +0 -365
  171. data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
  172. data/spec/mongoid/contextual/text_search_spec.rb +0 -209
  173. data/spec/mongoid/log_subscriber_spec.rb +0 -75
  174. data/spec/mongoid/positional_spec.rb +0 -222
  175. data/spec/mongoid/sessions/factory_spec.rb +0 -333
  176. data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
  177. data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -1,333 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Sessions::Factory do
4
-
5
- describe ".create" do
6
-
7
- context "when provided a name" do
8
-
9
- context "when the configuration exists" do
10
-
11
- context "when the configuration is standard" do
12
-
13
- let(:config) do
14
- {
15
- default: { hosts: [ "localhost:27017" ], database: database_id },
16
- secondary: { hosts: [ "localhost:27017" ], database: database_id }
17
- }
18
- end
19
-
20
- before do
21
- Mongoid::Config.sessions = config
22
- end
23
-
24
- let(:session) do
25
- described_class.create(:secondary)
26
- end
27
-
28
- let(:cluster) do
29
- session.cluster
30
- end
31
-
32
- it "returns a session" do
33
- expect(session).to be_a(Moped::Session)
34
- end
35
-
36
- it "sets the cluster's seeds" do
37
- expect(cluster.seeds.first.address.resolved).to eq("127.0.0.1:27017")
38
- end
39
- end
40
-
41
- context "when the configuration has no ports" do
42
-
43
- let(:config) do
44
- {
45
- default: { hosts: [ "127.0.0.1" ], database: database_id },
46
- secondary: { hosts: [ "localhost" ], database: database_id }
47
- }
48
- end
49
-
50
- before do
51
- Mongoid::Config.sessions = config
52
- end
53
-
54
- let(:session) do
55
- described_class.create(:secondary)
56
- end
57
-
58
- let(:default) do
59
- described_class.create(:default)
60
- end
61
-
62
- let(:cluster) do
63
- session.cluster
64
- end
65
-
66
- it "returns a session" do
67
- expect(session).to be_a(Moped::Session)
68
- end
69
-
70
- it "sets the cluster's seed ports to 27017" do
71
- expect(cluster.seeds.first.address.original).to eq("localhost:27017")
72
- end
73
-
74
- it "sets ips with no ports to 27017" do
75
- expect(default.cluster.seeds.first.address.original).to eq("127.0.0.1:27017")
76
- end
77
- end
78
-
79
- context "when configured via a uri" do
80
-
81
- context "when the uri has a single host:port" do
82
-
83
- let(:config) do
84
- {
85
- default: { hosts: [ "localhost:27017" ], database: database_id },
86
- secondary: { uri: "mongodb://localhost:27017/mongoid_test" }
87
- }
88
- end
89
-
90
- before do
91
- Mongoid::Config.sessions = config
92
- end
93
-
94
- let(:session) do
95
- described_class.create(:secondary)
96
- end
97
-
98
- let(:cluster) do
99
- session.cluster
100
- end
101
-
102
- it "returns a session" do
103
- expect(session).to be_a(Moped::Session)
104
- end
105
-
106
- it "sets the cluster's seeds" do
107
- expect(cluster.seeds.first.address.original).to eq("localhost:27017")
108
- end
109
-
110
- it "sets the database" do
111
- expect(session.options[:database]).to eq("mongoid_test")
112
- end
113
-
114
- it "sets the database in the configuration" do
115
- session
116
- expect(Mongoid.sessions[:secondary]).to include(:database)
117
- end
118
-
119
- it "sets the hosts in the configuration" do
120
- session
121
- expect(Mongoid.sessions[:secondary]).to include(:hosts)
122
- end
123
-
124
- it "removes the uri from the configuration" do
125
- session
126
- expect(Mongoid.sessions[:secondary]).to_not include(:uri)
127
- end
128
- end
129
-
130
- context "when the uri has multiple host:port pairs" do
131
-
132
- let(:config) do
133
- {
134
- default: { hosts: [ "localhost:27017" ], database: database_id },
135
- secondary: { uri: "mongodb://localhost:27017,localhost:27017/mongoid_test" }
136
- }
137
- end
138
-
139
- before do
140
- Mongoid::Config.sessions = config
141
- end
142
-
143
- let(:session) do
144
- described_class.create(:secondary)
145
- end
146
-
147
- let(:cluster) do
148
- session.cluster
149
- end
150
-
151
- let(:seeds) do
152
- cluster.seeds.map{ |node| node.address.original }
153
- end
154
-
155
- it "returns a session" do
156
- expect(session).to be_a(Moped::Session)
157
- end
158
-
159
- it "sets the cluster's seeds" do
160
- expect(seeds).to eq([ "localhost:27017", "localhost:27017" ])
161
- end
162
-
163
- it "sets the database" do
164
- expect(session.options[:database]).to eq("mongoid_test")
165
- end
166
-
167
- it "sets the database in the configuration" do
168
- session
169
- expect(Mongoid.sessions[:secondary]).to include(:database)
170
- end
171
-
172
- it "sets the hosts in the configuration" do
173
- session
174
- expect(Mongoid.sessions[:secondary]).to include(:hosts)
175
- end
176
-
177
- it "removes the uri from the configuration" do
178
- session
179
- expect(Mongoid.sessions[:secondary]).to_not include(:uri)
180
- end
181
- end
182
- end
183
- end
184
-
185
- context "when the configuration does not exist" do
186
-
187
- it "raises an error" do
188
- expect {
189
- described_class.create(:unknown)
190
- }.to raise_error(Mongoid::Errors::NoSessionConfig)
191
- end
192
- end
193
- end
194
-
195
- context "when no name is provided" do
196
-
197
- let(:config) do
198
- { default: { hosts: ["localhost:27017"], database: database_id }}
199
- end
200
-
201
- before do
202
- Mongoid::Config.sessions = config
203
- end
204
-
205
- let(:session) do
206
- described_class.create
207
- end
208
-
209
- let(:cluster) do
210
- session.cluster
211
- end
212
-
213
- let(:seeds) do
214
- cluster.seeds.map{ |node| node.address.original }
215
- end
216
-
217
- it "returns the default session" do
218
- expect(session).to be_a(Moped::Session)
219
- end
220
-
221
- it "sets the cluster's seeds" do
222
- expect(seeds).to eq([ "localhost:27017" ])
223
- end
224
- end
225
-
226
- context "when nil is provided and no default config" do
227
-
228
- let(:config) { nil }
229
-
230
- before do
231
- Mongoid.sessions[:default] = nil
232
- end
233
-
234
- it "raises NoSessionsConfig error" do
235
- expect{ Mongoid::Sessions::Factory.create(config) }.to raise_error(Mongoid::Errors::NoSessionsConfig)
236
- end
237
- end
238
- end
239
-
240
- describe ".default" do
241
-
242
- let(:config) do
243
- { default: { hosts: ["localhost:27017"], database: database_id }}
244
- end
245
-
246
- before do
247
- Mongoid::Config.sessions = config
248
- end
249
-
250
- let(:session) do
251
- described_class.default
252
- end
253
-
254
- let(:cluster) do
255
- session.cluster
256
- end
257
-
258
- let(:seeds) do
259
- cluster.seeds.map{ |node| node.address.original }
260
- end
261
-
262
- it "returns the default session" do
263
- expect(session).to be_a(Moped::Session)
264
- end
265
-
266
- it "sets the cluster's seeds" do
267
- expect(seeds).to eq([ "localhost:27017" ])
268
- end
269
- end
270
-
271
- context "when options are provided with string keys" do
272
-
273
- let(:config) do
274
- {
275
- default: {
276
- hosts: [ "localhost:27017" ],
277
- database: database_id,
278
- options: {
279
- "down_interval" => 10,
280
- "max_retries" => 5,
281
- "refresh_interval" => 30,
282
- "retry_interval" => 0.1,
283
- "write" => { "w" => 1 }
284
- }
285
- }
286
- }
287
- end
288
-
289
- before do
290
- Mongoid::Config.sessions = config
291
- end
292
-
293
- let(:session) do
294
- described_class.default
295
- end
296
-
297
- let(:cluster) do
298
- session.cluster
299
- end
300
-
301
- let(:seeds) do
302
- cluster.seeds.map{ |node| node.address.original }
303
- end
304
-
305
- it "returns the default session" do
306
- expect(session).to be_a(Moped::Session)
307
- end
308
-
309
- it "sets the cluster's seeds" do
310
- expect(seeds).to eq([ "localhost:27017" ])
311
- end
312
-
313
- it "sets the cluster down interval" do
314
- expect(cluster.down_interval).to eq(10)
315
- end
316
-
317
- it "sets the cluster max retries" do
318
- expect(cluster.max_retries).to eq(5)
319
- end
320
-
321
- it "sets the cluster refresh interval" do
322
- expect(cluster.refresh_interval).to eq(30)
323
- end
324
-
325
- it "sets the cluster retry interval" do
326
- expect(cluster.retry_interval).to eq(0.1)
327
- end
328
-
329
- it "sets the write concern" do
330
- expect(session.write_concern).to be_a(Moped::WriteConcern::Propagate)
331
- end
332
- end
333
- end
@@ -1,103 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Sessions::MongoUri do
4
-
5
- let(:single) do
6
- "mongodb://user:pass@localhost:27017/mongoid_test"
7
- end
8
-
9
- let(:multiple) do
10
- "mongodb://localhost:27017,localhost:27017/mongoid_test"
11
- end
12
-
13
- describe "#database" do
14
-
15
- let(:uri) do
16
- described_class.new(single)
17
- end
18
-
19
- it "returns the database name" do
20
- expect(uri.database).to eq("mongoid_test")
21
- end
22
- end
23
-
24
- describe "#hosts" do
25
-
26
- context "when a single node is provided" do
27
-
28
- let(:uri) do
29
- described_class.new(single)
30
- end
31
-
32
- it "returns an array with 1 node" do
33
- expect(uri.hosts).to eq([ "localhost:27017" ])
34
- end
35
- end
36
-
37
- context "when multiple nodes are provided" do
38
-
39
- let(:uri) do
40
- described_class.new(multiple)
41
- end
42
-
43
- it "returns an array with 2 nodes" do
44
- expect(uri.hosts).to eq([ "localhost:27017", "localhost:27017" ])
45
- end
46
- end
47
- end
48
-
49
- describe "#password" do
50
-
51
- let(:uri) do
52
- described_class.new(single)
53
- end
54
-
55
- it "returns the password" do
56
- expect(uri.password).to eq("pass")
57
- end
58
- end
59
-
60
- describe "#to_hash" do
61
-
62
- context "when a user and password are not provided" do
63
-
64
- let(:uri) do
65
- described_class.new(multiple)
66
- end
67
-
68
- it "does not include the username and password" do
69
- expect(uri.to_hash).to eq({
70
- hosts: [ "localhost:27017", "localhost:27017" ],
71
- database: "mongoid_test"
72
- })
73
- end
74
- end
75
-
76
- context "when a user and password are provided" do
77
-
78
- let(:uri) do
79
- described_class.new(single)
80
- end
81
-
82
- it "includes the username and password" do
83
- expect(uri.to_hash).to eq({
84
- hosts: [ "localhost:27017" ],
85
- database: "mongoid_test",
86
- username: "user",
87
- password: "pass"
88
- })
89
- end
90
- end
91
- end
92
-
93
- describe "#username" do
94
-
95
- let(:uri) do
96
- described_class.new(single)
97
- end
98
-
99
- it "returns the userame" do
100
- expect(uri.username).to eq("user")
101
- end
102
- end
103
- end
@@ -1,1252 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Sessions do
4
-
5
- describe "#collection" do
6
-
7
- shared_examples_for "an overridden collection at the class level" do
8
-
9
- let(:band) do
10
- klass.new
11
- end
12
-
13
- it "returns the collection for the model" do
14
- expect(band.collection).to be_a(Moped::Collection)
15
- end
16
-
17
- it "sets the correct collection name" do
18
- expect(band.collection.name).to eq("artists")
19
- end
20
-
21
- context "when accessing from the class level" do
22
-
23
- it "returns the collection for the model" do
24
- expect(klass.collection).to be_a(Moped::Collection)
25
- end
26
-
27
- it "sets the correct collection name" do
28
- expect(klass.collection.name).to eq("artists")
29
- end
30
- end
31
- end
32
-
33
- context "when overriding the persistence options" do
34
-
35
- let(:klass) do
36
- Band.with(collection: "artists")
37
- end
38
-
39
- it_behaves_like "an overridden collection at the class level"
40
- end
41
-
42
- context "when overriding store_in and persistence options" do
43
-
44
- let(:klass) do
45
- Band.with(collection: "artists")
46
- end
47
-
48
- before do
49
- Band.store_in collection: "foo"
50
- end
51
-
52
- after do
53
- Band.reset_storage_options!
54
- end
55
-
56
- it_behaves_like "an overridden collection at the class level"
57
- end
58
-
59
- context "when overriding the default with store_in" do
60
-
61
- after do
62
- Band.reset_storage_options!
63
- end
64
-
65
- context "when called multiple times with different options" do
66
-
67
- before do
68
- Band.store_in collection: "artists"
69
- Band.store_in session: "another"
70
- end
71
-
72
- it "should merge the options together" do
73
- expect(Band.storage_options[:collection]).to eq("artists")
74
- expect(Band.storage_options[:session]).to eq("another")
75
- end
76
- end
77
-
78
- context "when overriding with a proc" do
79
-
80
- let(:klass) { Band }
81
-
82
- before do
83
- Band.store_in(collection: ->{ "artists" })
84
- end
85
-
86
- it_behaves_like "an overridden collection at the class level"
87
- end
88
-
89
- context "when overriding with a string" do
90
-
91
- let(:klass) { Band }
92
-
93
- before do
94
- Band.store_in(collection: "artists")
95
- end
96
-
97
- after do
98
- Band.reset_storage_options!
99
- end
100
-
101
- it_behaves_like "an overridden collection at the class level"
102
- end
103
-
104
- context "when overriding with a symbol" do
105
-
106
- let(:klass) { Band }
107
-
108
- before do
109
- klass.store_in(collection: :artists)
110
- end
111
-
112
- after do
113
- klass.reset_storage_options!
114
- end
115
-
116
- it_behaves_like "an overridden collection at the class level"
117
- end
118
- end
119
-
120
- context "when not overriding the default" do
121
-
122
- let(:band) do
123
- Band.new
124
- end
125
-
126
- it "returns the collection for the model" do
127
- expect(band.collection).to be_a(Moped::Collection)
128
- end
129
-
130
- it "sets the correct collection name" do
131
- expect(band.collection.name.to_s).to eq("bands")
132
- end
133
-
134
- context "when accessing from the class level" do
135
-
136
- it "returns the collection for the model" do
137
- expect(Band.collection).to be_a(Moped::Collection)
138
- end
139
-
140
- it "sets the correct collection name" do
141
- expect(Band.collection.name.to_s).to eq("bands")
142
- end
143
- end
144
- end
145
- end
146
-
147
- describe "#collection_name" do
148
-
149
- shared_examples_for "an overridden collection name at the class level" do
150
-
151
- let(:band) do
152
- klass.new
153
- end
154
-
155
- context "when accessing from the instance" do
156
-
157
- it "returns the overridden value" do
158
- expect(band.collection_name).to eq(:artists)
159
- end
160
- end
161
-
162
- context "when accessing from the class level" do
163
-
164
- it "returns the overridden value" do
165
- expect(klass.collection_name).to eq(:artists)
166
- end
167
- end
168
- end
169
-
170
- context "when overriding the persistence options" do
171
-
172
- let(:klass) do
173
- Band.with(collection: "artists")
174
- end
175
-
176
- it_behaves_like "an overridden collection name at the class level"
177
- end
178
-
179
- context "when overriding store_in and persistence options" do
180
-
181
- let(:klass) do
182
- Band.with(collection: "artists")
183
- end
184
-
185
- before do
186
- Band.store_in collection: "foo"
187
- end
188
-
189
- after do
190
- Band.reset_storage_options!
191
- end
192
-
193
- it_behaves_like "an overridden collection name at the class level"
194
- end
195
-
196
- context "when overriding the default with store_in" do
197
-
198
- let(:klass) { Band }
199
-
200
- after do
201
- Band.reset_storage_options!
202
- end
203
-
204
- context "when overriding with a proc" do
205
-
206
- before do
207
- Band.store_in(collection: ->{ "artists" })
208
- end
209
-
210
- it_behaves_like "an overridden collection name at the class level"
211
- end
212
-
213
- context "when overriding with a string" do
214
-
215
- before do
216
- Band.store_in(collection: "artists")
217
- end
218
-
219
- it_behaves_like "an overridden collection name at the class level"
220
- end
221
-
222
- context "when overriding with a symbol" do
223
-
224
- before do
225
- Band.store_in(collection: :artists)
226
- end
227
-
228
- it_behaves_like "an overridden collection name at the class level"
229
- end
230
- end
231
-
232
- context "when not overriding the default" do
233
-
234
- let(:band) do
235
- Band.new
236
- end
237
-
238
- it "returns the pluralized model name" do
239
- expect(band.collection_name).to eq(:bands)
240
- end
241
-
242
- context "when accessing from the class level" do
243
-
244
- it "returns the pluralized model name" do
245
- expect(Band.collection_name).to eq(:bands)
246
- end
247
- end
248
- end
249
-
250
- context "when the model is a subclass" do
251
-
252
- let(:firefox) do
253
- Firefox.new
254
- end
255
-
256
- it "returns the root class pluralized model name" do
257
- expect(firefox.collection_name).to eq(:canvases)
258
- end
259
-
260
- context "when accessing from the class level" do
261
-
262
- it "returns the root class pluralized model name" do
263
- expect(Firefox.collection_name).to eq(:canvases)
264
- end
265
- end
266
- end
267
- end
268
-
269
- describe "#database_name" do
270
-
271
- shared_examples_for "an overridden database name" do
272
-
273
- let(:band) do
274
- klass.new
275
- end
276
-
277
- context "when accessing from the instance" do
278
-
279
- it "returns the overridden value" do
280
- expect(band.mongo_session.options[:database].to_s).to eq(database_id_alt)
281
- end
282
- end
283
-
284
- context "when accessing from the class level" do
285
-
286
- it "returns the overridden value" do
287
- expect(klass.database_name.to_s).to eq(database_id_alt)
288
- end
289
-
290
- it "session returns the overridden value" do
291
- expect(klass.mongo_session.options[:database].to_s).to eq(database_id_alt)
292
- end
293
- end
294
- end
295
-
296
- context "when overriding the persistence options" do
297
-
298
- let(:klass) do
299
- Band.with(database: database_id_alt)
300
- end
301
-
302
- it_behaves_like "an overridden database name"
303
- end
304
-
305
- context "when overriding with store_in" do
306
-
307
- let(:klass) { Band }
308
-
309
- before do
310
- Band.store_in database: database_id_alt
311
- end
312
-
313
- after do
314
- Band.reset_storage_options!
315
- end
316
-
317
- it_behaves_like "an overridden database name"
318
- end
319
-
320
- context "when overriding store_in and persistence options" do
321
-
322
- let(:klass) do
323
- Band.with(database: database_id_alt)
324
- end
325
-
326
- before do
327
- Band.store_in database: "foo"
328
- end
329
-
330
- after do
331
- Band.reset_storage_options!
332
- end
333
-
334
- it_behaves_like "an overridden database name"
335
- end
336
-
337
- context "when overriding using the session" do
338
-
339
- let(:session_name) { :alternative }
340
-
341
- before do
342
- Mongoid.sessions[session_name] = { database: database_id_alt, hosts: [ "#{HOST}:#{PORT}" ] }
343
- end
344
-
345
- after do
346
- Mongoid.sessions.delete(session_name)
347
- end
348
-
349
- context "when overriding the persistence options" do
350
-
351
- let(:klass) do
352
- Band.with(session: session_name)
353
- end
354
-
355
- it_behaves_like "an overridden database name"
356
- end
357
-
358
- context "when overriding with store_in" do
359
-
360
- let(:klass) { Band }
361
-
362
- before do
363
- Band.store_in(session: session_name)
364
- end
365
-
366
- after do
367
- Band.reset_storage_options!
368
- end
369
-
370
- it_behaves_like "an overridden database name"
371
- end
372
- end
373
- end
374
-
375
- describe "#mongo_session" do
376
-
377
- let(:file) do
378
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
379
- end
380
-
381
- before do
382
- described_class.clear
383
- Mongoid.load!(file, :test)
384
- Mongoid.sessions[:default][:database] = database_id
385
- end
386
-
387
- context "when getting the default" do
388
-
389
- let(:file) do
390
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
391
- end
392
-
393
- before do
394
- described_class.clear
395
- Mongoid.load!(file, :test)
396
- Mongoid.sessions[:default][:database] = database_id
397
- end
398
-
399
- let!(:band) do
400
- Band.new
401
- end
402
-
403
- let!(:mongo_session) do
404
- band.mongo_session
405
- end
406
-
407
- it "returns the default session" do
408
- expect(mongo_session.options[:database].to_s).to eq(database_id)
409
- end
410
- end
411
-
412
- context "when overridden the database with store_in" do
413
-
414
- before do
415
- Band.store_in(database: database_id_alt)
416
- end
417
-
418
- context "on instance level" do
419
-
420
- let(:band) do
421
- Band.new.with({:read=>:primary})
422
- end
423
-
424
- it "uses the new database" do
425
- expect(band.mongo_session.send(:current_database).name).to eq database_id_alt
426
- end
427
-
428
- context "when using another database before" do
429
-
430
- before do
431
- band
432
- User.create!
433
- end
434
-
435
- it "uses the new database" do
436
- expect(band.mongo_session.send(:current_database).name).to eq database_id_alt
437
- end
438
- end
439
- end
440
- end
441
-
442
- context "when overriding to a monghq single server", config: :mongohq do
443
-
444
- shared_examples_for "an overridden session to a mongohq single server" do
445
-
446
- let(:band) do
447
- Band.new
448
- end
449
-
450
- let(:single_session) do
451
- band.mongo_session
452
- end
453
-
454
- it "returns the default session" do
455
- expect(single_session.options[:database].to_s).to eq(ENV["MONGOHQ_SINGLE_NAME"])
456
- end
457
- end
458
-
459
- context "when overriding with a proc" do
460
-
461
- before do
462
- Band.store_in(session: ->{ :mongohq_single })
463
- end
464
-
465
- it_behaves_like "an overridden session to a mongohq single server"
466
- end
467
-
468
- context "when overriding with a string" do
469
-
470
- before do
471
- Band.store_in(session: "mongohq_single")
472
- end
473
-
474
- it_behaves_like "an overridden session to a mongohq single server"
475
- end
476
-
477
- context "when overriding with a symbol" do
478
-
479
- before do
480
- Band.store_in(session: :mongohq_single)
481
- end
482
-
483
- it_behaves_like "an overridden session to a mongohq single server"
484
- end
485
- end
486
-
487
- context "when overriding to a mongohq replica set", config: :mongohq do
488
-
489
- let(:band) do
490
- Band.new
491
- end
492
-
493
- let(:replica_session) do
494
- band.mongo_session
495
- end
496
-
497
- shared_examples_for "an overridden session to a mongohq replica set" do
498
-
499
- let(:seeds) do
500
- replica_session.cluster.seeds.map{ |node| node.address.original }
501
- end
502
-
503
- it "returns the overridden session" do
504
- expect(seeds).to eq([ ENV["MONGOHQ_REPL_1_URL"], ENV["MONGOHQ_REPL_2_URL"] ])
505
- end
506
- end
507
-
508
- context "when overriding with a proc" do
509
-
510
- before do
511
- Band.store_in(session: ->{ :mongohq_repl })
512
- end
513
-
514
- it_behaves_like "an overridden session to a mongohq replica set"
515
- end
516
-
517
- context "when overriding with a string" do
518
-
519
- before do
520
- Band.store_in(session: "mongohq_repl")
521
- end
522
-
523
- it_behaves_like "an overridden session to a mongohq replica set"
524
- end
525
-
526
- context "when overriding with a symbol" do
527
-
528
- before do
529
- Band.store_in(session: :mongohq_repl)
530
- end
531
-
532
- it_behaves_like "an overridden session to a mongohq replica set"
533
- end
534
- end
535
-
536
- context "when overriding to a mongohq replica set with uri config", config: :mongohq do
537
-
538
- before(:all) do
539
- Band.store_in(session: :mongohq_repl_uri)
540
- end
541
-
542
- let(:band) do
543
- Band.new
544
- end
545
-
546
- let(:repl_session) do
547
- band.mongo_session
548
- end
549
-
550
- let(:seeds) do
551
- repl_session.cluster.seeds.map{ |node| node.address.original }
552
- end
553
-
554
- it "returns the overridden session" do
555
- expect(seeds).to eq([ ENV["MONGOHQ_REPL_1_URL"], ENV["MONGOHQ_REPL_2_URL"] ])
556
- end
557
- end
558
-
559
- context "when no session exists with the key" do
560
-
561
- before(:all) do
562
- Band.store_in(session: :nonexistant)
563
- end
564
-
565
- let(:band) do
566
- Band.new
567
- end
568
-
569
- it "raises an error" do
570
- expect {
571
- band.mongo_session
572
- }.to raise_error(Mongoid::Errors::NoSessionConfig)
573
- end
574
- end
575
- end
576
-
577
- describe ".mongo_session" do
578
-
579
- let(:file) do
580
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
581
- end
582
-
583
- before do
584
- described_class.clear
585
- Mongoid.load!(file, :test)
586
- Mongoid.sessions[:default][:database] = database_id
587
- end
588
-
589
- after do
590
- Band.reset_storage_options!
591
- end
592
-
593
- context "when getting the default" do
594
-
595
- let(:file) do
596
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
597
- end
598
-
599
- before do
600
- Band.reset_storage_options!
601
- described_class.clear
602
- Mongoid.load!(file, :test)
603
- Mongoid.sessions[:default][:database] = database_id
604
- end
605
-
606
- let!(:mongo_session) do
607
- Band.mongo_session
608
- end
609
-
610
- it "returns the default session" do
611
- expect(mongo_session.options[:database].to_s).to eq(database_id)
612
- end
613
- end
614
-
615
- context "when overriding to a monghq single server", config: :mongohq do
616
-
617
- before(:all) do
618
- Band.store_in(session: :mongohq_single)
619
- end
620
-
621
- let(:session) do
622
- Band.mongo_session
623
- end
624
-
625
- it "returns the default session" do
626
- expect(session.options[:database].to_s).to eq(ENV["MONGOHQ_SINGLE_NAME"])
627
- end
628
- end
629
-
630
- context "when overriding to a mongohq replica set", config: :mongohq do
631
-
632
- before(:all) do
633
- Band.store_in(session: :mongohq_repl)
634
- end
635
-
636
- let(:repl_session) do
637
- Band.mongo_session
638
- end
639
-
640
- let(:seeds) do
641
- repl_session.cluster.seeds.map{ |node| node.address.original }
642
- end
643
-
644
- it "returns the overridden session" do
645
- expect(seeds).to eq([ ENV["MONGOHQ_REPL_1_URL"], ENV["MONGOHQ_REPL_2_URL"] ])
646
- end
647
- end
648
-
649
- context "when no session exists with the key" do
650
-
651
- before(:all) do
652
- Band.store_in(session: :nonexistant)
653
- end
654
-
655
- it "raises an error" do
656
- expect {
657
- Band.mongo_session
658
- }.to raise_error(Mongoid::Errors::NoSessionConfig)
659
- end
660
- end
661
- end
662
-
663
- describe ".store_in" do
664
-
665
- context "when provided a non hash" do
666
-
667
- it "raises an error" do
668
- expect {
669
- Band.store_in :artists
670
- }.to raise_error(Mongoid::Errors::InvalidStorageOptions)
671
- end
672
- end
673
-
674
- context "when provided a class that extend another document" do
675
-
676
- let(:klass) do
677
- Class.new(Band)
678
- end
679
-
680
- it "raises an error" do
681
- expect {
682
- klass.store_in(database: :artists)
683
- }.to raise_error(Mongoid::Errors::InvalidStorageParent)
684
- end
685
- end
686
-
687
- context "when provided a hash" do
688
-
689
- context "when the hash is not valid" do
690
-
691
- it "raises an error" do
692
- expect {
693
- Band.store_in coll: "artists"
694
- }.to raise_error(Mongoid::Errors::InvalidStorageOptions)
695
- end
696
- end
697
- end
698
- end
699
-
700
- describe ".with" do
701
-
702
- context "when sending operations to a different database" do
703
-
704
- after do
705
- Band.with(database: database_id_alt).delete_all
706
- end
707
-
708
- describe ".create" do
709
-
710
- let!(:band) do
711
- Band.with(database: database_id_alt).create
712
- end
713
-
714
- it "does not persist to the default database" do
715
- expect {
716
- Band.find(band.id)
717
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
718
- end
719
-
720
- let(:from_db) do
721
- Band.with(database: database_id_alt).find(band.id)
722
- end
723
-
724
- it "persists to the specified database" do
725
- expect(from_db).to eq(band)
726
- end
727
-
728
- it "persists the correct number of documents" do
729
- expect(Band.with(database: database_id_alt).count).to eq(1)
730
- end
731
- end
732
-
733
- describe ".map_reduce", config: :mongohq do
734
-
735
- let(:map) do
736
- %Q{
737
- function() {
738
- emit(this.name, { likes: this.likes });
739
- }}
740
- end
741
-
742
- let(:reduce) do
743
- %Q{
744
- function(key, values) {
745
- var result = { likes: 0 };
746
- values.forEach(function(value) {
747
- result.likes += value.likes;
748
- });
749
- return result;
750
- }}
751
- end
752
-
753
- before do
754
- Band.with(database: database_id_alt).delete_all
755
- end
756
-
757
- let!(:depeche_mode) do
758
- Band.with(database: database_id_alt).
759
- create(name: "Depeche Mode", likes: 200)
760
- end
761
-
762
- let!(:tool) do
763
- Band.with(database: database_id_alt).
764
- create(name: "Tool", likes: 100)
765
- end
766
-
767
- context "when outputting in memory" do
768
-
769
- let(:results) do
770
- Band.with(database: database_id_alt).
771
- map_reduce(map, reduce).out(inline: 1)
772
- end
773
-
774
- it "executes the map/reduce on the correct database" do
775
- expect(results.first["value"]).to eq({ "likes" => 200 })
776
- end
777
- end
778
-
779
- context "when outputting to a collection" do
780
-
781
- let(:results) do
782
- Band.with(database: database_id_alt).
783
- map_reduce(map, reduce).out(replace: "bands_output")
784
- end
785
-
786
- it "executes the map/reduce on the correct database" do
787
- expect(results.first["value"]).to eq({ "likes" => 200 })
788
- end
789
- end
790
- end
791
- end
792
-
793
- context "when sending operations to a different collection" do
794
-
795
- describe ".create" do
796
-
797
- let!(:band) do
798
- Band.with(collection: "artists").create
799
- end
800
-
801
- it "does not persist to the default database" do
802
- expect {
803
- Band.find(band.id)
804
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
805
- end
806
-
807
- let(:from_db) do
808
- Band.with(collection: "artists").find(band.id)
809
- end
810
-
811
- it "persists to the specified database" do
812
- expect(from_db).to eq(band)
813
- end
814
-
815
- it "persists the correct number of documents" do
816
- expect(Band.with(collection: "artists").count).to eq(1)
817
- end
818
- end
819
-
820
- describe ".map_reduce", config: :mongohq do
821
-
822
- let(:map) do
823
- %Q{
824
- function() {
825
- emit(this.name, { likes: this.likes });
826
- }}
827
- end
828
-
829
- let(:reduce) do
830
- %Q{
831
- function(key, values) {
832
- var result = { likes: 0 };
833
- values.forEach(function(value) {
834
- result.likes += value.likes;
835
- });
836
- return result;
837
- }}
838
- end
839
-
840
- before do
841
- Band.with(collection: "artists").delete_all
842
- end
843
-
844
- let!(:depeche_mode) do
845
- Band.with(collection: "artists").
846
- create(name: "Depeche Mode", likes: 200)
847
- end
848
-
849
- let!(:tool) do
850
- Band.with(collection: "artists").
851
- create(name: "Tool", likes: 100)
852
- end
853
-
854
- let(:results) do
855
- Band.with(collection: "artists").
856
- map_reduce(map, reduce).out(inline: 1)
857
- end
858
-
859
- it "executes the map/reduce on the correct collection" do
860
- expect(results.first["value"]).to eq({ "likes" => 200 })
861
- end
862
- end
863
- end
864
-
865
- context "when sending operations to a different session" do
866
-
867
- describe ".create" do
868
-
869
- let(:file) do
870
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
871
- end
872
-
873
- before do
874
- described_class.clear
875
- Mongoid.load!(file, :test)
876
- end
877
-
878
- context "when sending to a mongohq single server", config: :mongohq do
879
-
880
- let!(:band) do
881
- Band.with(
882
- session: "mongohq_single",
883
- database: database_id
884
- ).create
885
- end
886
-
887
- let(:from_db) do
888
- Band.with(
889
- session: "mongohq_single",
890
- database: database_id
891
- ).find(band.id)
892
- end
893
-
894
- it "persists to the specified database" do
895
- expect(from_db).to eq(band)
896
- end
897
- end
898
-
899
- context "when sending to a mongohq replica set", config: :mongohq do
900
-
901
- let!(:band) do
902
- Band.with(
903
- session: "mongohq_repl",
904
- database: "mongoid_replica"
905
- ).create
906
- end
907
-
908
- let(:from_db) do
909
- Band.with(
910
- session: "mongohq_repl",
911
- database: "mongoid_replica"
912
- ).find(band.id)
913
- end
914
-
915
- it "persists to the specified database" do
916
- expect(from_db).to eq(band)
917
- end
918
- end
919
-
920
- context "when sending to a mongohq replica set with uri config", config: :mongohq do
921
-
922
- let!(:band) do
923
- Band.with(
924
- session: "mongohq_repl_uri",
925
- database: "mongoid_replica"
926
- ).create
927
- end
928
-
929
- let(:from_db) do
930
- Band.with(
931
- session: "mongohq_repl_uri",
932
- database: "mongoid_replica"
933
- ).find(band.id)
934
- end
935
-
936
- it "persists to the specified database" do
937
- expect(from_db).to eq(band)
938
- end
939
- end
940
- end
941
-
942
- describe ".map_reduce", config: :mongohq do
943
-
944
- let(:file) do
945
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
946
- end
947
-
948
- before do
949
- described_class.clear
950
- Mongoid.load!(file, :test)
951
- end
952
-
953
- let(:map) do
954
- %Q{
955
- function() {
956
- emit(this.name, { likes: this.likes });
957
- }}
958
- end
959
-
960
- let(:reduce) do
961
- %Q{
962
- function(key, values) {
963
- var result = { likes: 0 };
964
- values.forEach(function(value) {
965
- result.likes += value.likes;
966
- });
967
- return result;
968
- }}
969
- end
970
-
971
- before do
972
- Band.with(
973
- session: "mongohq_repl",
974
- database: "mongoid_replica"
975
- ).delete_all
976
- end
977
-
978
- let!(:depeche_mode) do
979
- Band.with(
980
- session: "mongohq_repl",
981
- database: "mongoid_replica"
982
- ).create(name: "Depeche Mode", likes: 200)
983
- end
984
-
985
- let!(:tool) do
986
- Band.with(
987
- session: "mongohq_repl",
988
- database: "mongoid_replica"
989
- ).create(name: "Tool", likes: 100)
990
- end
991
-
992
- let(:results) do
993
- Band.with(
994
- session: "mongohq_repl",
995
- database: "mongoid_replica"
996
- ).map_reduce(map, reduce).out(inline: 1)
997
- end
998
-
999
- it "executes the map/reduce on the correct session" do
1000
- expect(results.first["value"]).to eq({ "likes" => 200 })
1001
- end
1002
- end
1003
- end
1004
-
1005
- context "when sending operations with safe mode" do
1006
-
1007
- describe ".create" do
1008
-
1009
- before do
1010
- Person.create_indexes
1011
- Person.create(ssn: "432-97-1111")
1012
- end
1013
-
1014
- context "when no error occurs" do
1015
-
1016
- it "inserts the document" do
1017
- expect(Person.count).to eq(1)
1018
- end
1019
- end
1020
-
1021
- context "when a mongodb error occurs" do
1022
-
1023
- it "bubbles up to the caller" do
1024
- expect {
1025
- Person.create(ssn: "432-97-1111")
1026
- }.to raise_error(Moped::Errors::OperationFailure)
1027
- end
1028
- end
1029
-
1030
- context "when using write -1" do
1031
-
1032
- let(:new_person) do
1033
- Person.with(write: {w: -1}).create(ssn: "432-97-1111")
1034
- end
1035
-
1036
- it "ignores mongodb error" do
1037
- expect(new_person).to_not be nil
1038
- end
1039
- end
1040
- end
1041
-
1042
- describe ".create!" do
1043
-
1044
- before do
1045
- Person.create!(ssn: "432-97-1112")
1046
- end
1047
-
1048
- context "when no error occurs" do
1049
-
1050
- it "inserts the document" do
1051
- expect(Person.count).to eq(1)
1052
- end
1053
- end
1054
-
1055
- context "when a mongodb error occurs" do
1056
-
1057
- before do
1058
- Person.create_indexes
1059
- end
1060
-
1061
- it "bubbles up to the caller" do
1062
- expect {
1063
- Person.create!(ssn: "432-97-1112")
1064
- }.to raise_error(Moped::Errors::OperationFailure)
1065
- end
1066
- end
1067
-
1068
- context "when a validation error occurs" do
1069
-
1070
- it "raises the validation error" do
1071
- expect {
1072
- Account.create!(name: "this name is way too long")
1073
- }.to raise_error(Mongoid::Errors::Validations)
1074
- end
1075
- end
1076
- end
1077
-
1078
- describe ".save" do
1079
-
1080
- before do
1081
- Person.create(ssn: "432-97-1113")
1082
- end
1083
-
1084
- context "when a mongodb error occurs" do
1085
-
1086
- let(:person) do
1087
- Person.new(ssn: "432-97-1113")
1088
- end
1089
-
1090
- before do
1091
- Person.create_indexes
1092
- end
1093
-
1094
- it "bubbles up to the caller" do
1095
- expect {
1096
- person.save
1097
- }.to raise_error(Moped::Errors::OperationFailure)
1098
- end
1099
- end
1100
- end
1101
-
1102
- describe ".save!" do
1103
-
1104
- before do
1105
- Person.create!(ssn: "432-97-1114")
1106
- end
1107
-
1108
- context "when a mongodb error occurs" do
1109
-
1110
- let(:person) do
1111
- Person.new(ssn: "432-97-1114")
1112
- end
1113
-
1114
- before do
1115
- Person.create_indexes
1116
- end
1117
-
1118
- it "bubbles up to the caller" do
1119
- expect {
1120
- person.save!
1121
- }.to raise_error(Moped::Errors::OperationFailure)
1122
- end
1123
- end
1124
-
1125
- context "when a validation error occurs" do
1126
-
1127
- let(:account) do
1128
- Account.new(name: "this name is way too long")
1129
- end
1130
-
1131
- it "raises the validation error" do
1132
- expect {
1133
- account.save!
1134
- }.to raise_error(Mongoid::Errors::Validations)
1135
- end
1136
- end
1137
- end
1138
- end
1139
- end
1140
-
1141
- context "when the default database uses a uri" do
1142
-
1143
- let(:file) do
1144
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
1145
- end
1146
-
1147
- let(:config) do
1148
- { default: { uri: "mongodb://localhost:#{PORT}/#{database_id}" }}
1149
- end
1150
-
1151
- before do
1152
- Mongoid::Threaded.sessions.clear
1153
- Mongoid.sessions = config
1154
- end
1155
-
1156
- context "when creating a document" do
1157
-
1158
- let!(:band) do
1159
- Band.create(name: "Placebo")
1160
- end
1161
-
1162
- it "persists the document to the correct database" do
1163
- expect(Band.find(band.id)).to eq(band)
1164
- end
1165
- end
1166
- end
1167
-
1168
- context "when overriding the default database "do
1169
-
1170
- let(:file) do
1171
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
1172
- end
1173
-
1174
- before do
1175
- Mongoid::Config.load!(file, :test)
1176
- end
1177
-
1178
- context "when the override is global" do
1179
-
1180
- before do
1181
- Mongoid.override_database(:mongoid_optional)
1182
- end
1183
-
1184
- after do
1185
- Band.delete_all
1186
- Mongoid.override_database(nil)
1187
- end
1188
-
1189
- let!(:band) do
1190
- Band.create(name: "Tool")
1191
- end
1192
-
1193
- it "persists to the overridden database" do
1194
- Band.mongo_session.with(database: :mongoid_optional) do |sess|
1195
- expect(sess[:bands].find(name: "Tool")).to_not be_nil
1196
- end
1197
- end
1198
- end
1199
- end
1200
-
1201
- context "when overriding the default session", config: :mongohq do
1202
-
1203
- context "when the override is configured with a uri" do
1204
-
1205
- let(:file) do
1206
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
1207
- end
1208
-
1209
- before do
1210
- Mongoid::Config.load!(file, :test)
1211
- Mongoid.override_session(:mongohq_repl_uri)
1212
- end
1213
-
1214
- after do
1215
- Mongoid.override_session(nil)
1216
- end
1217
-
1218
- it "has some database name on session" do
1219
- expect(Band.mongo_session.options[:database]).to eq(:mongoid_replica)
1220
- end
1221
- end
1222
-
1223
- context "when the override is global" do
1224
-
1225
- let(:file) do
1226
- File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
1227
- end
1228
-
1229
- before do
1230
- Mongoid::Config.load!(file, :test)
1231
- Mongoid.override_session(:mongohq_single)
1232
- end
1233
-
1234
- after do
1235
- Band.with(database: database_id).delete_all
1236
- Mongoid.override_session(nil)
1237
- end
1238
-
1239
- let!(:band) do
1240
- Band.with(database: database_id).create(name: "Tool")
1241
- end
1242
-
1243
- let(:persisted) do
1244
- Band.with(session: :mongohq_single, database: database_id).where(name: "Tool").first
1245
- end
1246
-
1247
- it "persists to the overridden session" do
1248
- expect(persisted).to eq(band)
1249
- end
1250
- end
1251
- end
1252
- end