mongoid 7.2.6 → 7.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +1 -1
  4. data/Rakefile +16 -0
  5. data/lib/config/locales/en.yml +2 -15
  6. data/lib/mongoid/association/accessors.rb +1 -1
  7. data/lib/mongoid/association/constrainable.rb +1 -1
  8. data/lib/mongoid/association/depending.rb +4 -4
  9. data/lib/mongoid/association/embedded/batchable.rb +1 -1
  10. data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
  11. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +11 -4
  12. data/lib/mongoid/association/nested/many.rb +1 -1
  13. data/lib/mongoid/association/nested/one.rb +4 -2
  14. data/lib/mongoid/association/proxy.rb +7 -2
  15. data/lib/mongoid/association/referenced/auto_save.rb +2 -2
  16. data/lib/mongoid/association/referenced/has_many/enumerable.rb +493 -495
  17. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  18. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
  19. data/lib/mongoid/association/relatable.rb +0 -2
  20. data/lib/mongoid/attributes/projector.rb +120 -0
  21. data/lib/mongoid/attributes.rb +24 -13
  22. data/lib/mongoid/cacheable.rb +2 -2
  23. data/lib/mongoid/clients/factory.rb +22 -8
  24. data/lib/mongoid/clients.rb +1 -1
  25. data/lib/mongoid/config/environment.rb +1 -9
  26. data/lib/mongoid/config.rb +19 -2
  27. data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
  28. data/lib/mongoid/contextual/atomic.rb +2 -7
  29. data/lib/mongoid/contextual/none.rb +0 -3
  30. data/lib/mongoid/copyable.rb +1 -1
  31. data/lib/mongoid/criteria/findable.rb +1 -1
  32. data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
  33. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
  34. data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
  35. data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
  36. data/lib/mongoid/criteria/queryable/selectable.rb +10 -10
  37. data/lib/mongoid/criteria/queryable/storable.rb +4 -4
  38. data/lib/mongoid/criteria.rb +5 -6
  39. data/lib/mongoid/document.rb +3 -18
  40. data/lib/mongoid/errors/delete_restriction.rb +8 -9
  41. data/lib/mongoid/errors/mongoid_error.rb +1 -1
  42. data/lib/mongoid/errors.rb +0 -2
  43. data/lib/mongoid/evolvable.rb +1 -1
  44. data/lib/mongoid/extensions/boolean.rb +1 -2
  45. data/lib/mongoid/extensions/false_class.rb +1 -1
  46. data/lib/mongoid/extensions/hash.rb +2 -2
  47. data/lib/mongoid/extensions/true_class.rb +1 -1
  48. data/lib/mongoid/fields.rb +43 -5
  49. data/lib/mongoid/inspectable.rb +1 -1
  50. data/lib/mongoid/interceptable.rb +1 -1
  51. data/lib/mongoid/matcher/bits.rb +41 -0
  52. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  53. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  54. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  55. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  56. data/lib/mongoid/matcher/expression.rb +4 -0
  57. data/lib/mongoid/matcher/field_operator.rb +6 -0
  58. data/lib/mongoid/matcher/mod.rb +17 -0
  59. data/lib/mongoid/matcher/type.rb +99 -0
  60. data/lib/mongoid/matcher.rb +7 -0
  61. data/lib/mongoid/persistable/deletable.rb +1 -2
  62. data/lib/mongoid/persistable/destroyable.rb +8 -2
  63. data/lib/mongoid/persistable/updatable.rb +27 -2
  64. data/lib/mongoid/persistence_context.rb +1 -3
  65. data/lib/mongoid/query_cache.rb +36 -40
  66. data/lib/mongoid/selectable.rb +5 -7
  67. data/lib/mongoid/shardable.rb +21 -5
  68. data/lib/mongoid/tasks/database.rb +1 -1
  69. data/lib/mongoid/touchable.rb +23 -4
  70. data/lib/mongoid/validatable/associated.rb +1 -1
  71. data/lib/mongoid/validatable/presence.rb +3 -3
  72. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  73. data/lib/mongoid/version.rb +1 -1
  74. data/lib/mongoid.rb +0 -1
  75. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +1 -1
  76. data/spec/integration/app_spec.rb +0 -3
  77. data/spec/integration/associations/embeds_many_spec.rb +44 -0
  78. data/spec/integration/associations/has_one_spec.rb +48 -0
  79. data/spec/integration/criteria/date_field_spec.rb +1 -1
  80. data/spec/integration/document_spec.rb +9 -0
  81. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  82. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  83. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  84. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  85. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  86. data/spec/integration/matcher_operator_data/in.yml +16 -0
  87. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  88. data/spec/integration/matcher_operator_data/type.yml +70 -0
  89. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  90. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  91. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  92. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  93. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  94. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  95. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  96. data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
  97. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  98. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  99. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  100. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  101. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  102. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  103. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  104. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  105. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  106. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  107. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  108. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  109. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  110. data/spec/integration/stringified_symbol_field_spec.rb +2 -2
  111. data/spec/lite_spec_helper.rb +2 -0
  112. data/spec/mongoid/association/depending_spec.rb +391 -352
  113. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4 -17
  114. data/spec/mongoid/association/nested/one_spec.rb +18 -14
  115. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -25
  116. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +0 -20
  117. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +1 -1
  118. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +1 -1
  119. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1 -1
  120. data/spec/mongoid/association/referenced/has_many_models.rb +0 -17
  121. data/spec/mongoid/association/referenced/has_one_models.rb +8 -0
  122. data/spec/mongoid/atomic/paths_spec.rb +64 -12
  123. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  124. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  125. data/spec/mongoid/attributes/projector_spec.rb +41 -0
  126. data/spec/mongoid/attributes_spec.rb +98 -6
  127. data/spec/mongoid/clients/factory_spec.rb +51 -9
  128. data/spec/mongoid/clients/options_spec.rb +3 -11
  129. data/spec/mongoid/config/environment_spec.rb +8 -86
  130. data/spec/mongoid/config_spec.rb +32 -0
  131. data/spec/mongoid/contextual/atomic_spec.rb +25 -64
  132. data/spec/mongoid/contextual/geo_near_spec.rb +1 -1
  133. data/spec/mongoid/contextual/mongo_spec.rb +2 -2
  134. data/spec/mongoid/criteria/modifiable_spec.rb +1 -1
  135. data/spec/mongoid/criteria/queryable/expandable_spec.rb +0 -73
  136. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +1 -1
  137. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +105 -7
  138. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +229 -24
  139. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +39 -0
  140. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -565
  141. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +590 -0
  142. data/spec/mongoid/criteria_projection_spec.rb +411 -0
  143. data/spec/mongoid/criteria_spec.rb +0 -279
  144. data/spec/mongoid/document_query_spec.rb +0 -51
  145. data/spec/mongoid/document_spec.rb +14 -34
  146. data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
  147. data/spec/mongoid/errors/mongoid_error_spec.rb +8 -20
  148. data/spec/mongoid/extensions/false_class_spec.rb +1 -1
  149. data/spec/mongoid/extensions/string_spec.rb +5 -5
  150. data/spec/mongoid/extensions/true_class_spec.rb +1 -1
  151. data/spec/mongoid/fields/localized_spec.rb +4 -4
  152. data/spec/mongoid/fields_spec.rb +4 -4
  153. data/spec/mongoid/inspectable_spec.rb +12 -4
  154. data/spec/mongoid/persistable/deletable_spec.rb +175 -1
  155. data/spec/mongoid/persistable/destroyable_spec.rb +191 -3
  156. data/spec/mongoid/persistable/savable_spec.rb +3 -5
  157. data/spec/mongoid/persistable/updatable_spec.rb +0 -2
  158. data/spec/mongoid/persistable/upsertable_spec.rb +1 -1
  159. data/spec/mongoid/persistable_spec.rb +2 -2
  160. data/spec/mongoid/query_cache_middleware_spec.rb +8 -0
  161. data/spec/mongoid/query_cache_spec.rb +0 -24
  162. data/spec/mongoid/reloadable_spec.rb +18 -1
  163. data/spec/mongoid/shardable_spec.rb +44 -0
  164. data/spec/mongoid/touchable_spec.rb +104 -16
  165. data/spec/mongoid/touchable_spec_models.rb +52 -0
  166. data/spec/mongoid/validatable_spec.rb +1 -1
  167. data/spec/shared/lib/mrss/cluster_config.rb +3 -8
  168. data/spec/shared/lib/mrss/constraints.rb +10 -41
  169. data/spec/shared/lib/mrss/docker_runner.rb +1 -7
  170. data/spec/shared/lib/mrss/server_version_registry.rb +12 -17
  171. data/spec/shared/lib/mrss/spec_organizer.rb +1 -18
  172. data/spec/shared/share/Dockerfile.erb +33 -125
  173. data/spec/shared/shlib/server.sh +23 -100
  174. data/spec/shared/shlib/set_env.sh +1 -4
  175. data/spec/spec_helper.rb +7 -3
  176. data/spec/support/client_registry.rb +9 -0
  177. data/spec/support/models/address.rb +0 -4
  178. data/spec/support/models/bolt.rb +8 -0
  179. data/spec/support/models/hole.rb +13 -0
  180. data/spec/support/models/mop.rb +0 -1
  181. data/spec/support/models/nut.rb +8 -0
  182. data/spec/support/models/person.rb +6 -9
  183. data/spec/support/models/sealer.rb +8 -0
  184. data/spec/support/models/shirt.rb +12 -0
  185. data/spec/support/models/spacer.rb +8 -0
  186. data/spec/support/models/threadlocker.rb +8 -0
  187. data/spec/support/models/washer.rb +8 -0
  188. data.tar.gz.sig +0 -0
  189. metadata +609 -545
  190. metadata.gz.sig +0 -0
  191. data/lib/mongoid/errors/empty_config_file.rb +0 -26
  192. data/lib/mongoid/errors/invalid_config_file.rb +0 -26
  193. data/spec/integration/contextual/empty_spec.rb +0 -142
  194. data/spec/mongoid/errors/invalid_config_file_spec.rb +0 -32
  195. data/spec/shared/bin/s3-copy +0 -45
  196. data/spec/shared/bin/s3-upload +0 -69
  197. data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
  198. data/spec/shared/share/haproxy-1.conf +0 -16
  199. data/spec/shared/share/haproxy-2.conf +0 -17
  200. data/spec/support/cluster_config.rb +0 -158
@@ -96,6 +96,49 @@ describe Mongoid::Attributes do
96
96
  it "does not raise an error" do
97
97
  expect(from_db.map).to eq(map)
98
98
  end
99
+
100
+ context 'when only one of the hash fields is projected' do
101
+
102
+ let(:map) do
103
+ { 'dates' => { 'y' => { '2016' => 'Berlin', '2017' => 'Munich' } } }
104
+ end
105
+
106
+ let(:expected) do
107
+ { 'dates' => { 'y' => {
108
+ '2016' => 'Berlin',
109
+ } } }
110
+ end
111
+
112
+ it 'retrieves only the projected fields' do
113
+ expect(from_db.map).to eq(expected)
114
+ end
115
+ end
116
+
117
+ context 'when several of the hash fields is projected' do
118
+
119
+ let(:map) do
120
+ { 'dates' => { 'y' => {
121
+ '2016' => 'Berlin',
122
+ '2017' => 'Munich',
123
+ '2018' => 'Krakow',
124
+ } } }
125
+ end
126
+
127
+ let(:expected) do
128
+ { 'dates' => { 'y' => {
129
+ '2016' => 'Berlin',
130
+ '2018' => 'Krakow',
131
+ } } }
132
+ end
133
+
134
+ let(:from_db) do
135
+ Person.only('map.dates.y.2016', 'map.dates.y.2018').first
136
+ end
137
+
138
+ it 'retrieves only the projected fields' do
139
+ expect(from_db.map).to eq(expected)
140
+ end
141
+ end
99
142
  end
100
143
  end
101
144
 
@@ -298,16 +341,12 @@ describe Mongoid::Attributes do
298
341
  context 'when retrieving a field of the association using the dot notation' do
299
342
 
300
343
  it 'retrieves the field' do
301
- pending 'MONGOID-5032, fixed in 7.3'
302
-
303
344
  expect(from_db['name.first_name']).to eq 'Jose'
304
345
  end
305
346
  end
306
347
 
307
348
  context 'when retrieving a field of a nested association using the dot notation' do
308
349
  it 'retrieves the field' do
309
- pending 'MONGOID-5032, fixed in 7.3'
310
-
311
350
  expect(from_db['name.language.name']).to eq 'es'
312
351
  end
313
352
  end
@@ -320,8 +359,6 @@ describe Mongoid::Attributes do
320
359
 
321
360
  context 'when retrieving a field under the projected sub-association' do
322
361
  it 'retrieves the field' do
323
- pending 'MONGOID-5032, fixed in 7.3'
324
-
325
362
  expect(from_db['name.language.name']).to eq 'es'
326
363
  end
327
364
  end
@@ -1892,6 +1929,61 @@ describe Mongoid::Attributes do
1892
1929
  end
1893
1930
  end
1894
1931
 
1932
+ describe '#unalias_attribute' do
1933
+ let(:shirt) { Shirt.new }
1934
+ let(:id) { '1234' }
1935
+
1936
+ context 'when creating object' do
1937
+ let(:shirt) { Shirt.new(id: id) }
1938
+
1939
+ it 'id and _id are not the same' do
1940
+ expect(shirt.id).to eq(id)
1941
+ expect(shirt._id).not_to eq(id)
1942
+ end
1943
+ end
1944
+
1945
+ context 'update' do
1946
+ before do
1947
+ shirt.update(id: id)
1948
+ end
1949
+
1950
+ it 'updates id but not_id' do
1951
+ expect(shirt.id).to eq(id)
1952
+ expect(shirt._id).not_to eq(id)
1953
+ end
1954
+ end
1955
+
1956
+ context 'id=' do
1957
+ before do
1958
+ shirt.id = id
1959
+ end
1960
+
1961
+ it 'sets id and not _id' do
1962
+ expect(shirt.id).to eq(id)
1963
+ expect(shirt._id).not_to eq(id)
1964
+ end
1965
+ end
1966
+
1967
+ context 'id?' do
1968
+ context 'with null id' do
1969
+ it 'returns false' do
1970
+ expect(shirt.id?).to be false
1971
+ expect(shirt._id?).to be true
1972
+ end
1973
+ end
1974
+
1975
+ context 'with id' do
1976
+ before do
1977
+ shirt.id = id
1978
+ end
1979
+
1980
+ it 'returns true' do
1981
+ expect(shirt.id?).to be true
1982
+ end
1983
+ end
1984
+ end
1985
+ end
1986
+
1895
1987
  describe "#alias_attribute" do
1896
1988
 
1897
1989
  let(:product) do
@@ -47,6 +47,7 @@ describe Mongoid::Clients::Factory do
47
47
 
48
48
  before do
49
49
  Mongoid::Config.send(:clients=, config)
50
+ # TODO: We should restore overwritten configuration in after block
50
51
  end
51
52
 
52
53
  after do
@@ -65,15 +66,9 @@ describe Mongoid::Clients::Factory do
65
66
  expect(client).to be_a(Mongo::Client)
66
67
  end
67
68
 
68
- context 'not JRuby' do
69
- # Run this test on JRuby when driver 2.16.0 is released -
70
- # see RUBY-2771.
71
- fails_on_jruby
72
-
73
- it 'does not produce driver warnings' do
74
- Mongo::Logger.logger.should_not receive(:warn)
75
- client
76
- end
69
+ it 'does not produce driver warnings' do
70
+ Mongo::Logger.logger.should_not receive(:warn)
71
+ client
77
72
  end
78
73
 
79
74
  let(:cluster_addresses) do
@@ -130,6 +125,7 @@ describe Mongoid::Clients::Factory do
130
125
 
131
126
  before do
132
127
  Mongoid::Config.send(:clients=, config)
128
+ # TODO: We should restore overwritten configuration in after block
133
129
  end
134
130
 
135
131
  after do
@@ -174,6 +170,7 @@ describe Mongoid::Clients::Factory do
174
170
 
175
171
  before do
176
172
  Mongoid::Config.send(:clients=, config)
173
+ # TODO: We should restore overwritten configuration in after block
177
174
  end
178
175
 
179
176
  after do
@@ -212,6 +209,7 @@ describe Mongoid::Clients::Factory do
212
209
 
213
210
  before do
214
211
  Mongoid::Config.send(:clients=, config)
212
+ # TODO: We should restore overwritten configuration in after block
215
213
  end
216
214
 
217
215
  after do
@@ -259,6 +257,7 @@ describe Mongoid::Clients::Factory do
259
257
 
260
258
  before do
261
259
  Mongoid::Config.send(:clients=, config)
260
+ # TODO: We should restore overwritten configuration in after block
262
261
  end
263
262
 
264
263
  after do
@@ -290,6 +289,7 @@ describe Mongoid::Clients::Factory do
290
289
 
291
290
  before do
292
291
  Mongoid.clients[:default] = nil
292
+ # TODO: We should restore overwritten configuration in after block
293
293
  end
294
294
 
295
295
  it "raises NoClientsConfig error" do
@@ -306,6 +306,7 @@ describe Mongoid::Clients::Factory do
306
306
 
307
307
  before do
308
308
  Mongoid::Config.send(:clients=, config)
309
+ # TODO: We should restore overwritten configuration in after block
309
310
  end
310
311
 
311
312
  after do
@@ -348,6 +349,7 @@ describe Mongoid::Clients::Factory do
348
349
 
349
350
  before do
350
351
  Mongoid::Config.send(:clients=, config)
352
+ # TODO: We should restore overwritten configuration in after block
351
353
  end
352
354
 
353
355
  after do
@@ -384,4 +386,44 @@ describe Mongoid::Clients::Factory do
384
386
  expect(client.options[:platform]).to eq(Mongoid::PLATFORM_DETAILS)
385
387
  end
386
388
  end
389
+
390
+ context "unexpected config options" do
391
+ let(:unknown_opts) do
392
+ {
393
+ bad_one: 1,
394
+ another_one: "here"
395
+ }
396
+ end
397
+
398
+ let(:config) do
399
+ {
400
+ default: { hosts: SpecConfig.instance.addresses, database: database_id },
401
+ good_one: { hosts: [ "127.0.0.1:1234" ], database: database_id},
402
+ bad_one: { hosts: [ "127.0.0.1:1234" ], database: database_id}.merge(unknown_opts),
403
+ good_two: { uri: "mongodb://127.0.0.1:1234,127.0.0.1:5678/#{database_id}" },
404
+ bad_two: { uri: "mongodb://127.0.0.1:1234,127.0.0.1:5678/#{database_id}" }.merge(unknown_opts)
405
+ }
406
+ end
407
+
408
+ around(:each) do |example|
409
+ old_config = Mongoid::Config.clients
410
+ Mongoid::Config.send(:clients=, config)
411
+ example.run
412
+ Mongoid::Config.send(:clients=, old_config)
413
+ end
414
+
415
+ [:bad_one, :bad_two].each do |env|
416
+ it 'does not log a warning if none' do
417
+ expect(described_class.send(:default_logger)).not_to receive(:warn)
418
+ described_class.create(env).close
419
+ end
420
+ end
421
+
422
+ [:bad_one, :bad_two].each do |env|
423
+ it 'logs a warning if some' do
424
+ expect(described_class.send(:default_logger)).not_to receive(:warn)
425
+ described_class.create(env).close
426
+ end
427
+ end
428
+ end
387
429
  end
@@ -83,11 +83,9 @@ describe Mongoid::Clients::Options, retry: 3 do
83
83
 
84
84
  let!(:connections_and_cluster_during) do
85
85
  connections = nil
86
- cluster = nil
87
- Minim.with(options) do |klass|
86
+ cluster = Minim.with(options) do |klass|
88
87
  klass.where(name: 'emily').to_a
89
88
  connections = Minim.mongo_client.database.command(serverStatus: 1).first['connections']['current']
90
- cluster = Minim.collection.cluster
91
89
  end
92
90
  [ connections, cluster ]
93
91
  end
@@ -126,10 +124,7 @@ describe Mongoid::Clients::Options, retry: 3 do
126
124
  end
127
125
 
128
126
  it 'disconnects the new cluster when the block exits' do
129
- expect(cluster_after).not_to be(cluster_during)
130
-
131
- cluster_during.connected?.should be false
132
- cluster_before.connected?.should be true
127
+ expect(connections_before).to eq(connections_after)
133
128
  end
134
129
  end
135
130
 
@@ -143,14 +138,11 @@ describe Mongoid::Clients::Options, retry: 3 do
143
138
 
144
139
  it 'does not create a new cluster' do
145
140
  expect(connections_during).to eq(connections_before)
146
-
147
- cluster_during.should be cluster_before
148
141
  end
149
142
 
150
143
  it 'does not disconnect the original cluster' do
144
+ expect(connections_after).to eq(connections_before)
151
145
  expect(cluster_before).to be(cluster_after)
152
-
153
- cluster_before.connected?.should be true
154
146
  end
155
147
  end
156
148
 
@@ -5,19 +5,9 @@ require "spec_helper"
5
5
 
6
6
  describe Mongoid::Config::Environment do
7
7
 
8
- around do |example|
9
- if defined?(Rails)
10
- SavedRails = Rails
11
- example.run
12
- Object.send(:remove_const, :Rails) if defined?(Rails)
13
- Rails = SavedRails
14
- Object.send(:remove_const, :SavedRails)
15
- else
16
- example.run
17
- if defined?(Rails)
18
- Object.send(:remove_const, :Rails)
19
- end
20
- end
8
+ after(:all) do
9
+ Rails = RailsTemp
10
+ Object.send(:remove_const, :RailsTemp)
21
11
  end
22
12
 
23
13
  describe "#env_name" do
@@ -34,6 +24,11 @@ describe Mongoid::Config::Environment do
34
24
  end
35
25
  end
36
26
 
27
+ after do
28
+ RailsTemp = Rails
29
+ Object.send(:remove_const, :Rails)
30
+ end
31
+
37
32
  it "returns the rails environment" do
38
33
  expect(described_class.env_name).to eq("production")
39
34
  end
@@ -91,77 +86,4 @@ describe Mongoid::Config::Environment do
91
86
  end
92
87
  end
93
88
  end
94
-
95
- describe "#load_yaml" do
96
- let(:path) { 'mongoid.yml' }
97
- let(:environment) {}
98
- before { allow(Rails).to receive('env').and_return('test') }
99
-
100
- subject { described_class.load_yaml(path, environment) }
101
-
102
- context 'when file not found' do
103
- let(:path) { 'not/a/valid/path'}
104
-
105
- it { expect { subject }.to raise_error(Errno::ENOENT) }
106
- end
107
-
108
- context 'when file found' do
109
- before do
110
- allow(File).to receive(:new).with('mongoid.yml').and_return(StringIO.new(file_contents))
111
- end
112
-
113
- let(:file_contents) do
114
- <<~FILE
115
- test:
116
- clients: ['test']
117
- development:
118
- clients: ['dev']
119
- FILE
120
- end
121
-
122
- context 'when file cannot be parsed as YAML' do
123
- let(:file_contents) { "*\nbad:%123abc" }
124
-
125
- it { expect { subject }.to raise_error(Psych::SyntaxError) }
126
- end
127
-
128
- context 'when file contains ERB errors' do
129
- let(:file_contents) { '<%= foo %>' }
130
-
131
- it { expect { subject }.to raise_error(NameError) }
132
- end
133
-
134
- context 'when file is empty' do
135
- let(:file_contents) { '' }
136
-
137
- it { expect { subject }.to raise_error(Mongoid::Errors::EmptyConfigFile) }
138
- end
139
-
140
- context 'when file does not contain a YAML Hash object' do
141
- let(:file_contents) { '["this", "is", "an", "array"]' }
142
-
143
- it { expect { subject }.to raise_error(Mongoid::Errors::InvalidConfigFile) }
144
- end
145
-
146
- context 'when environment not specified' do
147
- it 'uses the rails environment' do
148
- is_expected.to eq("clients"=>["test"])
149
- end
150
- end
151
-
152
- context 'when environment is specified' do
153
- let(:environment) { 'development' }
154
-
155
- it 'uses the specified environment' do
156
- is_expected.to eq("clients"=>["dev"])
157
- end
158
- end
159
-
160
- context 'when environment is missing' do
161
- let(:environment) { 'staging' }
162
-
163
- it { is_expected.to be_nil }
164
- end
165
- end
166
- end
167
89
  end
@@ -545,4 +545,36 @@ describe Mongoid::Config do
545
545
  expect(Mongoid::Config.log_level).to eq(2)
546
546
  end
547
547
  end
548
+
549
+ context "with an overridden database" do
550
+ let(:database) do
551
+ "test_purge_#{Time.now.to_i}"
552
+ end
553
+
554
+ before do
555
+ Mongoid.override_database(database)
556
+ end
557
+
558
+ after do
559
+ Mongoid.override_database(nil)
560
+ end
561
+
562
+ describe "#purge!" do
563
+ it 'respects persistence context overrides' do
564
+ House.create!(name: '1', model: 'Big')
565
+ expect(House.count).to eq(1)
566
+ Mongoid.purge!
567
+ expect(House.count).to eq(0)
568
+ end
569
+ end
570
+
571
+ describe "#truncate!" do
572
+ it 'respects persistence context overrides' do
573
+ House.create!(name: '1', model: 'Big')
574
+ expect(House.count).to eq(1)
575
+ Mongoid.truncate!
576
+ expect(House.count).to eq(0)
577
+ end
578
+ end
579
+ end
548
580
  end
@@ -801,28 +801,27 @@ describe Mongoid::Contextual::Atomic do
801
801
  context.unset(:name)
802
802
  end
803
803
 
804
- it "unsets the fields from all documents" do
805
- depeche_mode.reload
806
- new_order.reload
807
- expect(depeche_mode.name).to be_nil
808
- expect(depeche_mode.years).to_not be_nil
809
- expect(new_order.name).to be_nil
810
- expect(new_order.years).to_not be_nil
804
+ it "unsets the first existing field" do
805
+ expect(depeche_mode.reload.name).to be_nil
806
+ end
807
+
808
+ it "unsets the last existing field" do
809
+ expect(new_order.reload.name).to be_nil
811
810
  end
812
811
  end
813
812
 
814
813
  context "when the field is aliased" do
814
+
815
815
  before do
816
816
  context.unset(:years)
817
817
  end
818
818
 
819
- it "unsets the fields from all documents" do
820
- depeche_mode.reload
821
- new_order.reload
822
- expect(depeche_mode.name).to_not be_nil
823
- expect(depeche_mode.years).to be_nil
824
- expect(new_order.name).to_not be_nil
825
- expect(new_order.years).to be_nil
819
+ it "unsets the first existing field" do
820
+ expect(depeche_mode.reload.years).to be_nil
821
+ end
822
+
823
+ it "unsets the last existing field" do
824
+ expect(new_order.reload.years).to be_nil
826
825
  end
827
826
  end
828
827
  end
@@ -830,8 +829,7 @@ describe Mongoid::Contextual::Atomic do
830
829
  context "when unsetting multiple fields" do
831
830
 
832
831
  let!(:new_order) do
833
- Band.create(name: "New Order", genres: %w[electro dub], years: 10,
834
- likes: 200, rating: 4.3, origin: 'Space')
832
+ Band.create(name: "New Order", genres: [ "electro", "dub" ], years: 10)
835
833
  end
836
834
 
837
835
  let(:criteria) do
@@ -848,62 +846,27 @@ describe Mongoid::Contextual::Atomic do
848
846
  context.unset(:name, :genres)
849
847
  end
850
848
 
851
- it "unsets the specified fields" do
852
- new_order.reload
853
- expect(new_order.name).to be_nil
854
- expect(new_order.genres).to be_nil
855
- expect(new_order.years).to_not be_nil
856
- expect(new_order.likes).to_not be_nil
857
- expect(new_order.rating).to_not be_nil
849
+ it "unsets name field" do
850
+ expect(new_order.reload.name).to be_nil
858
851
  end
859
- end
860
-
861
- context "when the field is aliased" do
862
852
 
863
- before do
864
- context.unset(:name, :years)
865
- end
866
-
867
- it "unsets the specified fields" do
868
- new_order.reload
869
- expect(new_order.name).to be_nil
870
- expect(new_order.genres).to_not be_nil
871
- expect(new_order.years).to be_nil
872
- expect(new_order.likes).to_not be_nil
873
- expect(new_order.rating).to_not be_nil
853
+ it "unsets genres field" do
854
+ expect(new_order.reload.genres).to be_nil
874
855
  end
875
856
  end
876
857
 
877
- context "when using Hash arguments" do
858
+ context "when the field is aliased" do
878
859
 
879
860
  before do
880
- context.unset({ years: true, likes: "" }, { rating: false, origin: nil })
881
- end
882
-
883
- it "unsets the specified fields" do
884
- new_order.reload
885
- expect(new_order.name).to_not be_nil
886
- expect(new_order.genres).to_not be_nil
887
- expect(new_order.years).to be_nil
888
- expect(new_order.likes).to be_nil
889
- expect(new_order.rating).to be_nil
890
- expect(new_order.origin).to be_nil
861
+ context.unset(:name, :years)
891
862
  end
892
- end
893
863
 
894
- context "when mixing argument types" do
895
-
896
- before do
897
- context.unset(:name, [:years], { likes: "" }, { rating: false })
864
+ it "unsets the unaliased field" do
865
+ expect(new_order.reload.name).to be_nil
898
866
  end
899
867
 
900
- it "unsets the specified fields" do
901
- new_order.reload
902
- expect(new_order.name).to be_nil
903
- expect(new_order.genres).to_not be_nil
904
- expect(new_order.years).to be_nil
905
- expect(new_order.likes).to be_nil
906
- expect(new_order.rating).to be_nil
868
+ it "unsets the aliased field" do
869
+ expect(new_order.reload.years).to be_nil
907
870
  end
908
871
  end
909
872
  end
@@ -932,9 +895,7 @@ describe Mongoid::Contextual::Atomic do
932
895
  end
933
896
 
934
897
  it "unsets the unaliased field" do
935
- depeche_mode.reload
936
- expect(depeche_mode.name).to be_nil
937
- expect(depeche_mode.years).to_not be_nil
898
+ expect(depeche_mode.reload.name).to be_nil
938
899
  end
939
900
  end
940
901
  end
@@ -59,7 +59,7 @@ describe Mongoid::Contextual::GeoNear do
59
59
  end
60
60
 
61
61
  it "is nil except for 4.0 sharded when it is 0" do
62
- expect(geo_near.average_distance).to eq expected_value
62
+ expect(geo_near.average_distance).to be expected_value
63
63
  end
64
64
  end
65
65
  end
@@ -1442,8 +1442,8 @@ describe Mongoid::Contextual::Mongo do
1442
1442
  describe "##{method}" do
1443
1443
 
1444
1444
  before do
1445
- Band.create(name: "Depeche Mode")
1446
- Band.create(name: "New Order")
1445
+ Band.create!(name: "Depeche Mode")
1446
+ Band.create!(name: "New Order")
1447
1447
  end
1448
1448
 
1449
1449
  context "when the criteria has a limit" do
@@ -705,7 +705,7 @@ describe Mongoid::Criteria::Modifiable do
705
705
  end
706
706
 
707
707
  it 'uses the values from the attributes' do
708
- expect(document.map).to eq({ foo: :bar })
708
+ expect(document.map).to eq('foo' => :bar )
709
709
  end
710
710
  end
711
711
 
@@ -9,79 +9,6 @@ describe Mongoid::Criteria::Queryable::Expandable do
9
9
  Mongoid::Query.new
10
10
  end
11
11
 
12
- describe '#expand_condition' do
13
-
14
- let(:expanded) do
15
- query.send(:expand_condition, condition)
16
- end
17
-
18
- context 'field name => value' do
19
- shared_examples_for 'expands' do
20
-
21
- it 'expands' do
22
- expanded.should == {'foo' => 'bar'}
23
- end
24
- end
25
-
26
- context 'string key' do
27
- let(:condition) do
28
- {'foo' => 'bar'}
29
- end
30
-
31
- it_behaves_like 'expands'
32
- end
33
-
34
- context 'symbol key' do
35
- let(:condition) do
36
- {foo: 'bar'}
37
- end
38
-
39
- it_behaves_like 'expands'
40
- end
41
- end
42
-
43
- context 'Key instance => value' do
44
- let(:key) do
45
- Mongoid::Criteria::Queryable::Key.new(:foo, :__override__, '$gt')
46
- end
47
-
48
- let(:condition) do
49
- {key => 'bar'}
50
- end
51
-
52
- it 'expands' do
53
- expanded.should == {'foo' => {'$gt' => 'bar'}}
54
- end
55
- end
56
-
57
- =begin
58
- context 'operator => operator value expression' do
59
- shared_examples_for 'expands' do
60
-
61
- it 'expands' do
62
- expanded.should == {'foo' => 'bar'}
63
- end
64
- end
65
-
66
- context 'string key' do
67
- let(:condition) do
68
- {'$in' => %w(bar)}
69
- end
70
-
71
- it_behaves_like 'expands'
72
- end
73
-
74
- context 'symbol key' do
75
- let(:condition) do
76
- {:$in => %w(bar)}
77
- end
78
-
79
- it_behaves_like 'expands'
80
- end
81
- end
82
- =end
83
- end
84
-
85
12
  describe '#expand_condition_to_array_values' do
86
13
  shared_examples_for 'expands' do
87
14