mongoid 8.0.13 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +17 -66
  6. data/lib/config/locales/en.yml +46 -40
  7. data/lib/mongoid/association/accessors.rb +3 -7
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/depending.rb +4 -8
  10. data/lib/mongoid/association/eager_loadable.rb +0 -3
  11. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  12. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  13. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  14. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  15. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  16. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  17. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +2 -2
  18. data/lib/mongoid/association/macros.rb +0 -6
  19. data/lib/mongoid/association/nested/many.rb +3 -38
  20. data/lib/mongoid/association/nested/nested_buildable.rb +0 -14
  21. data/lib/mongoid/association/nested/one.rb +40 -2
  22. data/lib/mongoid/association/proxy.rb +1 -1
  23. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  24. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  25. data/lib/mongoid/association/referenced/has_many/enumerable.rb +6 -23
  26. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -103
  27. data/lib/mongoid/association/reflections.rb +2 -2
  28. data/lib/mongoid/atomic.rb +7 -16
  29. data/lib/mongoid/attributes/dynamic.rb +1 -1
  30. data/lib/mongoid/attributes/nested.rb +2 -2
  31. data/lib/mongoid/attributes/processing.rb +5 -29
  32. data/lib/mongoid/attributes/projector.rb +1 -1
  33. data/lib/mongoid/attributes/readonly.rb +1 -1
  34. data/lib/mongoid/attributes.rb +8 -2
  35. data/lib/mongoid/changeable.rb +107 -5
  36. data/lib/mongoid/clients/factory.rb +0 -4
  37. data/lib/mongoid/clients/storage_options.rb +2 -5
  38. data/lib/mongoid/clients/validators/storage.rb +1 -13
  39. data/lib/mongoid/collection_configurable.rb +58 -0
  40. data/lib/mongoid/composable.rb +2 -0
  41. data/lib/mongoid/config/defaults.rb +60 -0
  42. data/lib/mongoid/config/options.rb +0 -3
  43. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  44. data/lib/mongoid/config/validators.rb +1 -0
  45. data/lib/mongoid/config.rb +88 -89
  46. data/lib/mongoid/contextual/aggregable/memory.rb +2 -5
  47. data/lib/mongoid/contextual/atomic.rb +1 -1
  48. data/lib/mongoid/contextual/memory.rb +240 -51
  49. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  50. data/lib/mongoid/contextual/mongo.rb +370 -133
  51. data/lib/mongoid/contextual/none.rb +162 -7
  52. data/lib/mongoid/contextual.rb +12 -0
  53. data/lib/mongoid/criteria/findable.rb +2 -2
  54. data/lib/mongoid/criteria/includable.rb +4 -3
  55. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
  56. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  57. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -13
  58. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  59. data/lib/mongoid/criteria/queryable/selectable.rb +43 -121
  60. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  61. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  62. data/lib/mongoid/criteria.rb +6 -5
  63. data/lib/mongoid/deprecable.rb +1 -2
  64. data/lib/mongoid/deprecation.rb +3 -3
  65. data/lib/mongoid/document.rb +1 -8
  66. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  67. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  68. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  69. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  70. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  71. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  72. data/lib/mongoid/errors.rb +4 -2
  73. data/lib/mongoid/extensions/hash.rb +3 -51
  74. data/lib/mongoid/extensions/object.rb +2 -2
  75. data/lib/mongoid/extensions/time.rb +2 -0
  76. data/lib/mongoid/fields/localized.rb +10 -0
  77. data/lib/mongoid/fields/standard.rb +10 -0
  78. data/lib/mongoid/fields.rb +59 -35
  79. data/lib/mongoid/findable.rb +27 -3
  80. data/lib/mongoid/interceptable.rb +6 -116
  81. data/lib/mongoid/matchable.rb +1 -6
  82. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  83. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +5 -3
  84. data/lib/mongoid/matcher/regex.rb +9 -8
  85. data/lib/mongoid/matcher/type.rb +1 -1
  86. data/lib/mongoid/matcher.rb +0 -1
  87. data/lib/mongoid/persistable/creatable.rb +1 -0
  88. data/lib/mongoid/persistable/deletable.rb +1 -1
  89. data/lib/mongoid/persistable/savable.rb +13 -1
  90. data/lib/mongoid/persistable/unsettable.rb +2 -2
  91. data/lib/mongoid/persistable/updatable.rb +51 -1
  92. data/lib/mongoid/persistable/upsertable.rb +20 -1
  93. data/lib/mongoid/persistable.rb +3 -0
  94. data/lib/mongoid/query_cache.rb +5 -1
  95. data/lib/mongoid/railties/database.rake +7 -2
  96. data/lib/mongoid/reloadable.rb +5 -3
  97. data/lib/mongoid/stateful.rb +22 -1
  98. data/lib/mongoid/tasks/database.rake +12 -0
  99. data/lib/mongoid/tasks/database.rb +20 -0
  100. data/lib/mongoid/threaded.rb +0 -3
  101. data/lib/mongoid/timestamps/created.rb +1 -8
  102. data/lib/mongoid/traversable.rb +0 -12
  103. data/lib/mongoid/utils.rb +22 -0
  104. data/lib/mongoid/validatable/associated.rb +17 -98
  105. data/lib/mongoid/validatable/macros.rb +5 -20
  106. data/lib/mongoid/validatable.rb +4 -10
  107. data/lib/mongoid/version.rb +1 -5
  108. data/lib/mongoid/warnings.rb +17 -2
  109. data/lib/mongoid.rb +16 -3
  110. data/spec/integration/app_spec.rb +2 -20
  111. data/spec/integration/associations/embeds_one_spec.rb +5 -25
  112. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -55
  113. data/spec/integration/callbacks_spec.rb +99 -12
  114. data/spec/integration/discriminator_key_spec.rb +4 -5
  115. data/spec/integration/dots_and_dollars_spec.rb +2 -12
  116. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  117. data/spec/integration/matcher_operator_data/regex.yml +0 -21
  118. data/spec/mongoid/association/eager_spec.rb +2 -24
  119. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  120. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  121. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  122. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
  123. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  124. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  125. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +42 -103
  126. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -195
  127. data/spec/mongoid/association/syncable_spec.rb +1 -1
  128. data/spec/mongoid/attributes/nested_spec.rb +10 -204
  129. data/spec/mongoid/attributes_spec.rb +3 -33
  130. data/spec/mongoid/changeable_spec.rb +299 -24
  131. data/spec/mongoid/clients/factory_spec.rb +0 -30
  132. data/spec/mongoid/clients_spec.rb +122 -13
  133. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  134. data/spec/mongoid/config/defaults_spec.rb +160 -0
  135. data/spec/mongoid/config_spec.rb +154 -27
  136. data/spec/mongoid/contextual/aggregable/memory_spec.rb +0 -91
  137. data/spec/mongoid/contextual/memory_spec.rb +332 -253
  138. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  139. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  140. data/spec/mongoid/contextual/none_spec.rb +49 -2
  141. data/spec/mongoid/copyable_spec.rb +2 -10
  142. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  143. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  144. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -2
  145. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  146. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +0 -235
  147. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  148. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  149. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  150. data/spec/mongoid/criteria_spec.rb +5 -11
  151. data/spec/mongoid/document_spec.rb +0 -27
  152. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  153. data/spec/mongoid/extensions/hash_spec.rb +3 -239
  154. data/spec/mongoid/extensions/time_spec.rb +8 -43
  155. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  156. data/spec/mongoid/fields/localized_spec.rb +46 -28
  157. data/spec/mongoid/fields_spec.rb +136 -77
  158. data/spec/mongoid/findable_spec.rb +391 -34
  159. data/spec/mongoid/indexable_spec.rb +16 -10
  160. data/spec/mongoid/interceptable_spec.rb +153 -442
  161. data/spec/mongoid/interceptable_spec_models.rb +111 -51
  162. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  163. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  164. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  165. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  166. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  167. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  168. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  169. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  170. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  171. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  172. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  173. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  174. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  175. data/spec/mongoid/persistence_context_spec.rb +7 -57
  176. data/spec/mongoid/query_cache_spec.rb +56 -61
  177. data/spec/mongoid/reloadable_spec.rb +24 -28
  178. data/spec/mongoid/scopable_spec.rb +70 -0
  179. data/spec/mongoid/serializable_spec.rb +23 -44
  180. data/spec/mongoid/stateful_spec.rb +122 -8
  181. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  182. data/spec/mongoid/tasks/database_spec.rb +127 -0
  183. data/spec/mongoid/timestamps/created_spec.rb +0 -23
  184. data/spec/mongoid/timestamps_spec.rb +9 -11
  185. data/spec/mongoid/touchable_spec.rb +277 -5
  186. data/spec/mongoid/touchable_spec_models.rb +3 -1
  187. data/spec/mongoid/traversable_spec.rb +9 -24
  188. data/spec/mongoid/validatable/associated_spec.rb +34 -27
  189. data/spec/mongoid/validatable/numericality_spec.rb +0 -16
  190. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  191. data/spec/mongoid_spec.rb +36 -10
  192. data/spec/shared/lib/mrss/docker_runner.rb +1 -1
  193. data/spec/shared/lib/mrss/server_version_registry.rb +1 -1
  194. data/spec/shared/lib/mrss/spec_organizer.rb +3 -32
  195. data/spec/shared/share/Dockerfile.erb +85 -15
  196. data/spec/shared/shlib/distro.sh +0 -10
  197. data/spec/shared/shlib/server.sh +27 -34
  198. data/spec/shared/shlib/set_env.sh +68 -9
  199. data/spec/spec_helper.rb +5 -0
  200. data/spec/support/expectations.rb +17 -20
  201. data/spec/support/immutable_ids.rb +118 -0
  202. data/spec/support/macros.rb +47 -15
  203. data/spec/support/models/artist.rb +0 -1
  204. data/spec/support/models/band.rb +1 -0
  205. data/spec/support/models/building.rb +2 -0
  206. data/spec/support/models/name.rb +0 -10
  207. data/spec/support/models/person.rb +0 -1
  208. data/spec/support/models/product.rb +1 -0
  209. data.tar.gz.sig +0 -0
  210. metadata +715 -671
  211. metadata.gz.sig +2 -0
  212. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
  213. data/lib/mongoid/field_readable.rb +0 -70
  214. data/lib/mongoid/matcher/regexp_budget.rb +0 -389
  215. data/lib/mongoid/validatable/numericality.rb +0 -19
  216. data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
  217. data/spec/integration/query_operator_guard_spec.rb +0 -89
  218. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -61
  219. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -61
  220. data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
  221. data/spec/shared/CANDIDATE.md +0 -28
  222. data/spec/shared/lib/mrss/release/candidate.rb +0 -281
  223. data/spec/shared/lib/mrss/release/product_data.rb +0 -144
  224. data/spec/shared/lib/tasks/candidate.rake +0 -64
  225. data/spec/support/models/purse.rb +0 -9
@@ -29,32 +29,6 @@ describe Mongoid::Clients::Factory do
29
29
  end
30
30
  end
31
31
 
32
- shared_examples_for 'includes rails wrapping library' do
33
- context 'when Rails is available' do
34
- around do |example|
35
- original_rails_constant = defined?(::Rails) ? ::Rails : nil
36
- Object.send(:remove_const, :Rails) if original_rails_constant
37
-
38
- module ::Rails
39
- def self.version
40
- '6.1.0'
41
- end
42
- end
43
-
44
- example.run
45
-
46
- Object.send(:remove_const, :Rails) if defined?(::Rails)
47
- Object.const_set(:Rails, original_rails_constant)
48
- end
49
-
50
- it 'adds Rails as another wrapping library' do
51
- expect(client.options[:wrapping_libraries]).to include(
52
- {'name' => 'Rails', 'version' => '6.1.0'},
53
- )
54
- end
55
- end
56
- end
57
-
58
32
  describe ".create" do
59
33
 
60
34
  context "when provided a name" do
@@ -114,8 +88,6 @@ describe Mongoid::Clients::Factory do
114
88
  Mongoid::Clients::Factory::MONGOID_WRAPPING_LIBRARY)]
115
89
  end
116
90
 
117
- it_behaves_like 'includes rails wrapping library'
118
-
119
91
  context 'when configuration specifies a wrapping library' do
120
92
 
121
93
  let(:config) do
@@ -137,8 +109,6 @@ describe Mongoid::Clients::Factory do
137
109
  {'name' => 'Foo'},
138
110
  ]
139
111
  end
140
-
141
- it_behaves_like 'includes rails wrapping library'
142
112
  end
143
113
  end
144
114
 
@@ -738,19 +738,6 @@ describe Mongoid::Clients do
738
738
  end
739
739
  end
740
740
 
741
- context "when provided a class that extends another document" do
742
-
743
- let(:klass) do
744
- Class.new(Band)
745
- end
746
-
747
- it "raises an error" do
748
- expect {
749
- klass.store_in(database: :artists)
750
- }.to raise_error(Mongoid::Errors::InvalidStorageParent)
751
- end
752
- end
753
-
754
741
  context "when provided a hash" do
755
742
 
756
743
  context "when the hash is not valid" do
@@ -762,6 +749,128 @@ describe Mongoid::Clients do
762
749
  end
763
750
  end
764
751
  end
752
+
753
+ context "when it is called on a subclass" do
754
+
755
+ let(:client) { StoreParent.collection.client }
756
+ let(:parent) { StoreParent.create! }
757
+ let(:child1) { StoreChild1.create! }
758
+ let(:child2) { StoreChild2.create! }
759
+
760
+ before do
761
+ class StoreParent
762
+ include Mongoid::Document
763
+ end
764
+
765
+ class StoreChild1 < StoreParent
766
+ end
767
+
768
+ class StoreChild2 < StoreParent
769
+ end
770
+ end
771
+
772
+ after do
773
+ Object.send(:remove_const, :StoreParent)
774
+ Object.send(:remove_const, :StoreChild1)
775
+ Object.send(:remove_const, :StoreChild2)
776
+ end
777
+
778
+ context "when it is not called on the parent" do
779
+
780
+ context "when it is called on all subclasses" do
781
+
782
+ before do
783
+ StoreChild1.store_in collection: :store_ones
784
+ StoreChild2.store_in collection: :store_twos
785
+ [ parent, child1, child2 ]
786
+ end
787
+
788
+ let(:db_parent) { client['store_parents'].find.first }
789
+ let(:db_child1) { client['store_ones'].find.first }
790
+ let(:db_child2) { client['store_twos'].find.first }
791
+
792
+ it "stores the documents in the correct collections" do
793
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
794
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
795
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
796
+ end
797
+
798
+ it "only queries from its own collections" do
799
+ expect(StoreParent.count).to eq(1)
800
+ expect(StoreChild1.count).to eq(1)
801
+ expect(StoreChild2.count).to eq(1)
802
+ end
803
+ end
804
+
805
+ context "when it is called on one of the subclasses" do
806
+
807
+ before do
808
+ StoreChild1.store_in collection: :store_ones
809
+ [ parent, child1, child2 ]
810
+ end
811
+
812
+ let(:db_parent) { client['store_parents'].find.first }
813
+ let(:db_child1) { client['store_ones'].find.first }
814
+ let(:db_child2) { client['store_parents'].find.to_a.last }
815
+
816
+ it "stores the documents in the correct collections" do
817
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
818
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
819
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
820
+ end
821
+
822
+ it "queries from its own collections" do
823
+ expect(StoreParent.count).to eq(2)
824
+ expect(StoreChild1.count).to eq(1)
825
+ expect(StoreChild2.count).to eq(1)
826
+ end
827
+ end
828
+ end
829
+
830
+ context "when it is called on the parent" do
831
+
832
+ before do
833
+ StoreParent.store_in collection: :st_parents
834
+ end
835
+
836
+ context "when it is called on all subclasses" do
837
+
838
+ before do
839
+ StoreChild1.store_in collection: :store_ones
840
+ StoreChild2.store_in collection: :store_twos
841
+ [ parent, child1, child2 ]
842
+ end
843
+
844
+ let(:db_parent) { client['st_parents'].find.first }
845
+ let(:db_child1) { client['store_ones'].find.first }
846
+ let(:db_child2) { client['store_twos'].find.first }
847
+
848
+ it "stores the documents in the correct collections" do
849
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
850
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
851
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
852
+ end
853
+ end
854
+
855
+ context "when it is called on one of the subclasses" do
856
+
857
+ before do
858
+ StoreChild1.store_in collection: :store_ones
859
+ [ parent, child1, child2 ]
860
+ end
861
+
862
+ let(:db_parent) { client['st_parents'].find.first }
863
+ let(:db_child1) { client['store_ones'].find.first }
864
+ let(:db_child2) { client['st_parents'].find.to_a.last }
865
+
866
+ it "stores the documents in the correct collections" do
867
+ expect(db_parent).to eq({ "_id" => parent.id, "_type" => "StoreParent" })
868
+ expect(db_child1).to eq({ "_id" => child1.id, "_type" => "StoreChild1" })
869
+ expect(db_child2).to eq({ "_id" => child2.id, "_type" => "StoreChild2" })
870
+ end
871
+ end
872
+ end
873
+ end
765
874
  end
766
875
 
767
876
  describe ".with" do
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Mongoid::CollectionConfigurable do
6
+
7
+ before(:all) do
8
+ class CollectionConfigurableValidOptions
9
+ include Mongoid::Document
10
+
11
+ store_in collection_options: {
12
+ capped: true,
13
+ size: 2560
14
+ }
15
+ end
16
+
17
+ class CollectionConfigurableUnknownOptions
18
+ include Mongoid::Document
19
+
20
+ store_in collection_options: {
21
+ i_am_not_an_option: true,
22
+ let_me: { fail: true }
23
+ }
24
+ end
25
+
26
+ class CollectionConfigurableInvalidOptions
27
+ include Mongoid::Document
28
+
29
+ store_in collection_options: {
30
+ capped: true
31
+ # Creating a capped collection requires `size` field; therefore,
32
+ # an attempt to create a collection with only `capped : true` option
33
+ # fails.
34
+ }
35
+ end
36
+ end
37
+
38
+ after(:all) do
39
+ Mongoid.deregister_model(CollectionConfigurableValidOptions)
40
+ Object.send(:remove_const, :CollectionConfigurableValidOptions)
41
+ Mongoid.deregister_model(CollectionConfigurableUnknownOptions)
42
+ Object.send(:remove_const, :CollectionConfigurableUnknownOptions)
43
+ Mongoid.deregister_model(CollectionConfigurableInvalidOptions)
44
+ Object.send(:remove_const, :CollectionConfigurableInvalidOptions)
45
+ end
46
+
47
+ after(:each) do
48
+ [
49
+ CollectionConfigurableValidOptions,
50
+ CollectionConfigurableUnknownOptions
51
+ ].each do |klazz|
52
+ klazz.collection.drop
53
+ end
54
+ end
55
+
56
+ context 'when collection does not exist' do
57
+ context 'with valid options' do
58
+ let(:subject) do
59
+ CollectionConfigurableValidOptions
60
+ end
61
+
62
+ before(:each) do
63
+ subject.create_collection
64
+ end
65
+
66
+ let(:coll_options) do
67
+ subject.collection.database.list_collections(filter: { name: subject.collection_name.to_s }).first
68
+ end
69
+
70
+ it 'creates the collection' do
71
+ expect(coll_options).not_to be_nil
72
+ end
73
+
74
+ it 'passes collection options' do
75
+ expect(coll_options.dig('options', 'capped')).to eq(true)
76
+ expect(coll_options.dig('options', 'size')).to eq(2560)
77
+ end
78
+ end
79
+
80
+ context 'with an unknown collection options' do
81
+ let(:subject) do
82
+ CollectionConfigurableUnknownOptions
83
+ end
84
+
85
+ it 'raises an error' do
86
+ expect do
87
+ subject.create_collection
88
+ end.to raise_error(Mongoid::Errors::CreateCollectionFailure)
89
+ end
90
+ end
91
+
92
+ context 'with invalid options' do
93
+ let(:subject) do
94
+ CollectionConfigurableInvalidOptions
95
+ end
96
+
97
+ it 'raises an error' do
98
+ expect do
99
+ subject.create_collection
100
+ end.to raise_error(Mongoid::Errors::CreateCollectionFailure)
101
+ end
102
+ end
103
+ end
104
+
105
+ context 'when collection exists' do
106
+ let(:subject) do
107
+ CollectionConfigurableValidOptions
108
+ end
109
+
110
+ context 'when force is false' do
111
+ let(:logger) do
112
+ double("logger").tap do |log|
113
+ expect(log).to receive(:debug).once.with(/Collection '#{subject.collection_name}' already exist/)
114
+ end
115
+ end
116
+
117
+ before do
118
+ allow(subject).to receive(:logger).and_return(logger)
119
+ subject.collection.create
120
+ end
121
+
122
+ it 'logs a message' do
123
+ subject.create_collection
124
+ end
125
+ end
126
+
127
+ context 'when force is true' do
128
+ let(:logger) do
129
+ double("logger")
130
+ end
131
+
132
+ let(:coll_options) do
133
+ subject.collection.database.list_collections(filter: { name: subject.collection_name.to_s }).first
134
+ end
135
+
136
+ before do
137
+ allow(subject).to receive(:logger).and_return(logger)
138
+ subject.collection.create
139
+ end
140
+
141
+ it 'does not log a message' do
142
+ expect(logger).to receive(:debug).never.with(/Collection '#{subject.collection_name}' already exist/)
143
+ subject.create_collection(force: true)
144
+ end
145
+
146
+ it 'creates the collection' do
147
+ subject.create_collection(force: true)
148
+ expect(coll_options).not_to be_nil
149
+ end
150
+
151
+ it 'passes collection options' do
152
+ subject.create_collection(force: true)
153
+ expect(coll_options.dig('options', 'capped')).to eq(true)
154
+ expect(coll_options.dig('options', 'size')).to eq(2560)
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Mongoid::Config::Defaults do
6
+
7
+ let(:config) do
8
+ Mongoid::Config
9
+ end
10
+
11
+ describe ".load_defaults" do
12
+
13
+ shared_examples "uses settings for 7.3" do
14
+ it "uses settings for 7.3" do
15
+ expect(Mongoid.broken_aggregables).to be true
16
+ expect(Mongoid.broken_alias_handling).to be true
17
+ expect(Mongoid.broken_and).to be true
18
+ expect(Mongoid.broken_scoping).to be true
19
+ expect(Mongoid.broken_updates).to be true
20
+ expect(Mongoid.compare_time_by_ms).to be false
21
+ expect(Mongoid.legacy_pluck_distinct).to be true
22
+ expect(Mongoid.legacy_triple_equals).to be true
23
+ expect(Mongoid.object_id_as_json_oid).to be true
24
+ end
25
+ end
26
+
27
+ shared_examples "does not use settings for 7.3" do
28
+ it "does not use settings for 7.3" do
29
+ expect(Mongoid.broken_aggregables).to be false
30
+ expect(Mongoid.broken_alias_handling).to be false
31
+ expect(Mongoid.broken_and).to be false
32
+ expect(Mongoid.broken_scoping).to be false
33
+ expect(Mongoid.broken_updates).to be false
34
+ expect(Mongoid.compare_time_by_ms).to be true
35
+ expect(Mongoid.legacy_pluck_distinct).to be false
36
+ expect(Mongoid.legacy_triple_equals).to be false
37
+ end
38
+ end
39
+
40
+ shared_examples "uses settings for 7.4" do
41
+ it "uses settings for 7.4" do
42
+ expect(Mongoid.legacy_attributes).to be true
43
+ expect(Mongoid.overwrite_chained_operators).to be true
44
+ end
45
+ end
46
+
47
+ shared_examples "does not use settings for 7.4" do
48
+ it "does not use settings for 7.4" do
49
+ expect(Mongoid.legacy_attributes).to be false
50
+ expect(Mongoid.overwrite_chained_operators).to be false
51
+ end
52
+ end
53
+
54
+ shared_examples "uses settings for 7.5" do
55
+ it "uses settings for 7.5" do
56
+ expect(Mongoid.map_big_decimal_to_decimal128).to be false
57
+ end
58
+ end
59
+
60
+ shared_examples "does not use settings for 7.5" do
61
+ it "does not use settings for 7.5" do
62
+ expect(Mongoid.map_big_decimal_to_decimal128).to be true
63
+ end
64
+ end
65
+
66
+ shared_examples "uses settings for 8.0" do
67
+ it "uses settings for 8.0" do
68
+ expect(Mongoid.legacy_readonly).to be true
69
+ end
70
+ end
71
+
72
+ shared_examples "does not use settings for 8.0" do
73
+ it "does not use settings for 8.0" do
74
+ expect(Mongoid.legacy_readonly).to be false
75
+ end
76
+ end
77
+
78
+ shared_examples "uses settings for 8.1" do
79
+ it "uses settings for 8.1" do
80
+ expect(Mongoid.immutable_ids).to be false
81
+ end
82
+ end
83
+
84
+ context "when giving a valid version" do
85
+
86
+ before do
87
+ config.load_defaults(version)
88
+ end
89
+
90
+ after do
91
+ Mongoid::Config.reset
92
+ end
93
+
94
+ context "when the given version is 7.3" do
95
+
96
+ let(:version) { 7.3 }
97
+
98
+ it_behaves_like "uses settings for 7.3"
99
+ it_behaves_like "uses settings for 7.4"
100
+ it_behaves_like "uses settings for 7.5"
101
+ it_behaves_like "uses settings for 8.0"
102
+ it_behaves_like "uses settings for 8.1"
103
+ end
104
+
105
+ context "when the given version is 7.4" do
106
+
107
+ let(:version) { 7.4 }
108
+
109
+ it_behaves_like "does not use settings for 7.3"
110
+ it_behaves_like "uses settings for 7.4"
111
+ it_behaves_like "uses settings for 7.5"
112
+ it_behaves_like "uses settings for 8.0"
113
+ it_behaves_like "uses settings for 8.1"
114
+ end
115
+
116
+ context "when the given version is 7.5" do
117
+
118
+ let(:version) { 7.5 }
119
+
120
+ it_behaves_like "does not use settings for 7.3"
121
+ it_behaves_like "does not use settings for 7.4"
122
+ it_behaves_like "uses settings for 7.5"
123
+ it_behaves_like "uses settings for 8.0"
124
+ it_behaves_like "uses settings for 8.1"
125
+ end
126
+
127
+ context "when the given version is 8.0" do
128
+
129
+ let(:version) { 8.0 }
130
+
131
+ it_behaves_like "does not use settings for 7.3"
132
+ it_behaves_like "does not use settings for 7.4"
133
+ it_behaves_like "does not use settings for 7.5"
134
+ it_behaves_like "uses settings for 8.0"
135
+ it_behaves_like "uses settings for 8.1"
136
+ end
137
+
138
+ context "when the given version is 8.1" do
139
+
140
+ let(:version) { 8.1 }
141
+
142
+ it_behaves_like "does not use settings for 7.3"
143
+ it_behaves_like "does not use settings for 7.4"
144
+ it_behaves_like "does not use settings for 7.5"
145
+ it_behaves_like "does not use settings for 8.0"
146
+ it_behaves_like "uses settings for 8.1"
147
+ end
148
+ end
149
+
150
+ context "when given version an invalid version" do
151
+ let(:version) { 4.2 }
152
+
153
+ it "raises an error" do
154
+ expect do
155
+ config.load_defaults(version)
156
+ end.to raise_error(ArgumentError, /Unknown version: 4.2/)
157
+ end
158
+ end
159
+ end
160
+ end