mongoid 7.1.0.rc0 → 7.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +6 -6
  5. data/LICENSE +1 -1
  6. data/README.md +5 -5
  7. data/Rakefile +14 -0
  8. data/lib/config/locales/en.yml +5 -5
  9. data/lib/mongoid.rb +3 -2
  10. data/lib/mongoid/association/accessors.rb +37 -2
  11. data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
  13. data/lib/mongoid/association/many.rb +3 -2
  14. data/lib/mongoid/association/proxy.rb +6 -4
  15. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
  16. data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
  17. data/lib/mongoid/association/referenced/eager.rb +29 -9
  18. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -22
  19. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -2
  20. data/lib/mongoid/atomic.rb +13 -3
  21. data/lib/mongoid/attributes.rb +28 -20
  22. data/lib/mongoid/clients/factory.rb +2 -2
  23. data/lib/mongoid/clients/options.rb +8 -8
  24. data/lib/mongoid/clients/sessions.rb +20 -4
  25. data/lib/mongoid/clients/storage_options.rb +5 -5
  26. data/lib/mongoid/config.rb +42 -12
  27. data/lib/mongoid/config/options.rb +5 -2
  28. data/lib/mongoid/contextual.rb +5 -4
  29. data/lib/mongoid/contextual/geo_near.rb +3 -2
  30. data/lib/mongoid/contextual/map_reduce.rb +3 -2
  31. data/lib/mongoid/contextual/mongo.rb +2 -1
  32. data/lib/mongoid/criteria.rb +23 -4
  33. data/lib/mongoid/criteria/modifiable.rb +2 -1
  34. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  35. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
  36. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
  37. data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
  38. data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
  39. data/lib/mongoid/criteria/queryable/selectable.rb +120 -13
  40. data/lib/mongoid/criteria/queryable/storable.rb +104 -99
  41. data/lib/mongoid/errors/eager_load.rb +2 -0
  42. data/lib/mongoid/errors/no_client_config.rb +2 -2
  43. data/lib/mongoid/errors/no_default_client.rb +1 -1
  44. data/lib/mongoid/extensions/hash.rb +4 -2
  45. data/lib/mongoid/extensions/regexp.rb +1 -1
  46. data/lib/mongoid/fields.rb +2 -1
  47. data/lib/mongoid/fields/standard.rb +2 -1
  48. data/lib/mongoid/fields/validators/macro.rb +4 -1
  49. data/lib/mongoid/findable.rb +5 -4
  50. data/lib/mongoid/interceptable.rb +5 -1
  51. data/lib/mongoid/matchable/regexp.rb +2 -2
  52. data/lib/mongoid/persistable/pushable.rb +11 -2
  53. data/lib/mongoid/persistence_context.rb +6 -6
  54. data/lib/mongoid/query_cache.rb +61 -18
  55. data/lib/mongoid/railties/database.rake +7 -0
  56. data/lib/mongoid/serializable.rb +10 -2
  57. data/lib/mongoid/shardable.rb +56 -4
  58. data/lib/mongoid/tasks/database.rake +10 -5
  59. data/lib/mongoid/tasks/database.rb +83 -0
  60. data/lib/mongoid/timestamps/timeless.rb +3 -1
  61. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  62. data/lib/mongoid/version.rb +1 -1
  63. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
  64. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  65. data/spec/app/models/coding.rb +4 -0
  66. data/spec/app/models/coding/pull_request.rb +12 -0
  67. data/spec/app/models/delegating_patient.rb +16 -0
  68. data/spec/app/models/passport.rb +1 -0
  69. data/spec/app/models/phone.rb +1 -0
  70. data/spec/app/models/publication.rb +5 -0
  71. data/spec/app/models/publication/encyclopedia.rb +12 -0
  72. data/spec/app/models/publication/review.rb +14 -0
  73. data/spec/integration/app_spec.rb +254 -0
  74. data/spec/integration/associations/embedded_spec.rb +54 -0
  75. data/spec/integration/associations/has_many_spec.rb +34 -0
  76. data/spec/integration/associations/has_one_spec.rb +34 -0
  77. data/spec/integration/bson_regexp_raw_spec.rb +20 -0
  78. data/spec/integration/criteria/date_field_spec.rb +41 -0
  79. data/spec/integration/criteria/logical_spec.rb +13 -0
  80. data/spec/integration/document_spec.rb +22 -0
  81. data/spec/integration/shardable_spec.rb +149 -0
  82. data/spec/lite_spec_helper.rb +15 -4
  83. data/spec/mongoid/association/accessors_spec.rb +238 -63
  84. data/spec/mongoid/association/embedded/embeds_many_models.rb +19 -0
  85. data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
  86. data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
  87. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
  88. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
  89. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
  90. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +2 -1
  91. data/spec/mongoid/attributes_spec.rb +19 -7
  92. data/spec/mongoid/changeable_spec.rb +23 -0
  93. data/spec/mongoid/clients/factory_spec.rb +8 -8
  94. data/spec/mongoid/clients/options_spec.rb +11 -11
  95. data/spec/mongoid/clients/sessions_spec.rb +8 -4
  96. data/spec/mongoid/clients/transactions_spec.rb +20 -8
  97. data/spec/mongoid/clients_spec.rb +2 -2
  98. data/spec/mongoid/contextual/atomic_spec.rb +22 -11
  99. data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
  100. data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
  101. data/spec/mongoid/contextual/mongo_spec.rb +76 -53
  102. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
  103. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
  104. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
  105. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
  106. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
  107. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
  108. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
  109. data/spec/mongoid/criteria/queryable/selectable_spec.rb +52 -0
  110. data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
  111. data/spec/mongoid/criteria_spec.rb +36 -2
  112. data/spec/mongoid/document_fields_spec.rb +29 -0
  113. data/spec/mongoid/document_persistence_context_spec.rb +33 -0
  114. data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
  115. data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
  116. data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
  117. data/spec/mongoid/fields_spec.rb +24 -1
  118. data/spec/mongoid/indexable_spec.rb +6 -4
  119. data/spec/mongoid/interceptable_spec.rb +62 -0
  120. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  121. data/spec/mongoid/matchable/default_spec.rb +1 -1
  122. data/spec/mongoid/matchable/regexp_spec.rb +2 -2
  123. data/spec/mongoid/matchable_spec.rb +2 -2
  124. data/spec/mongoid/persistable/pushable_spec.rb +55 -1
  125. data/spec/mongoid/query_cache_spec.rb +77 -9
  126. data/spec/mongoid/relations/proxy_spec.rb +1 -1
  127. data/spec/mongoid/scopable_spec.rb +2 -1
  128. data/spec/mongoid/serializable_spec.rb +129 -18
  129. data/spec/mongoid/shardable_models.rb +61 -0
  130. data/spec/mongoid/shardable_spec.rb +69 -16
  131. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  132. data/spec/mongoid/tasks/database_spec.rb +1 -1
  133. data/spec/spec_helper.rb +2 -31
  134. data/spec/support/child_process_helper.rb +76 -0
  135. data/spec/support/cluster_config.rb +3 -3
  136. data/spec/support/constraints.rb +26 -10
  137. data/spec/support/expectations.rb +3 -1
  138. data/spec/support/helpers.rb +11 -0
  139. data/spec/support/lite_constraints.rb +22 -0
  140. data/spec/support/session_registry.rb +50 -0
  141. data/spec/support/spec_config.rb +12 -4
  142. metadata +518 -480
  143. metadata.gz.sig +0 -0
@@ -0,0 +1,61 @@
1
+ class SmMovie
2
+ include Mongoid::Document
3
+
4
+ field :year, type: Integer
5
+
6
+ index year: 1
7
+ shard_key :year
8
+ end
9
+
10
+ class SmTrailer
11
+ include Mongoid::Document
12
+
13
+ index year: 1
14
+ shard_key 'year'
15
+ end
16
+
17
+ class SmActor
18
+ include Mongoid::Document
19
+
20
+ # This is not a usable shard configuration for the server.
21
+ # We just have it for unit tests.
22
+ shard_key age: 1, 'gender' => :hashed, 'hello' => :hashed
23
+ end
24
+
25
+ class SmAssistant
26
+ include Mongoid::Document
27
+
28
+ field :gender, type: String
29
+
30
+ index gender: 1
31
+ shard_key 'gender' => :hashed
32
+ end
33
+
34
+ class SmProducer
35
+ include Mongoid::Document
36
+
37
+ index age: 1, gender: 1
38
+ shard_key({age: 1, gender: 'hashed'}, unique: true, numInitialChunks: 2)
39
+ end
40
+
41
+ class SmDirector
42
+ include Mongoid::Document
43
+
44
+ belongs_to :agency
45
+
46
+ index age: 1
47
+ shard_key :agency
48
+ end
49
+
50
+ class SmDriver
51
+ include Mongoid::Document
52
+
53
+ belongs_to :agency
54
+
55
+ index age: 1, agency: 1
56
+ shard_key age: 1, agency: :hashed
57
+ end
58
+
59
+ class SmNotSharded
60
+ include Mongoid::Document
61
+ end
@@ -2,6 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  require "spec_helper"
5
+ require_relative './shardable_models'
5
6
 
6
7
  describe Mongoid::Shardable do
7
8
 
@@ -24,30 +25,82 @@ describe Mongoid::Shardable do
24
25
 
25
26
  describe ".shard_key" do
26
27
 
27
- let(:klass) do
28
- Band
29
- end
28
+ context 'when full syntax is used' do
29
+ context 'with symbol value' do
30
+ it 'sets shard key fields to symbol value' do
31
+ SmProducer.shard_key_fields.should == %i(age gender)
32
+ end
30
33
 
31
- before do
32
- Band.shard_key(:name)
33
- end
34
+ it 'sets shard config' do
35
+ SmProducer.shard_config.should == {
36
+ key: {age: 1, gender: 'hashed'},
37
+ options: {
38
+ unique: true,
39
+ numInitialChunks: 2,
40
+ },
41
+ }
42
+ end
34
43
 
35
- it "specifies a shard key on the collection" do
36
- expect(klass.shard_key_fields).to eq([:name])
37
- end
44
+ it 'keeps hashed as string' do
45
+ SmProducer.shard_config[:key][:gender].should == 'hashed'
46
+ end
47
+ end
48
+
49
+ context 'with string value' do
50
+ it 'sets shard key fields to symbol value' do
51
+ SmActor.shard_key_fields.should == %i(age gender hello)
52
+ end
38
53
 
39
- context 'when a relation is used as the shard key' do
54
+ it 'sets shard config' do
55
+ SmActor.shard_config.should == {
56
+ key: {age: 1, gender: 'hashed', hello: 'hashed'},
57
+ options: {},
58
+ }
59
+ end
40
60
 
41
- let(:klass) do
42
- Game
61
+ it 'sets hashed to string' do
62
+ SmActor.shard_config[:key][:gender].should == 'hashed'
63
+ end
43
64
  end
44
65
 
45
- before do
46
- Game.shard_key(:person)
66
+ context 'when passed association name' do
67
+ it 'uses foreign key as shard key in shard config' do
68
+ SmDriver.shard_config.should == {
69
+ key: {age: 1, agency_id: 'hashed'},
70
+ options: {},
71
+ }
72
+ end
73
+
74
+ it 'uses foreign key as shard key in shard key fields' do
75
+ SmDriver.shard_key_fields.should == %i(age agency_id)
76
+ end
77
+ end
78
+ end
79
+
80
+ context 'when shorthand syntax is used' do
81
+ context 'with symbol value' do
82
+ it 'sets shard key fields to symbol value' do
83
+ SmMovie.shard_key_fields.should == %i(year)
84
+ end
47
85
  end
48
86
 
49
- it "converts the shard key to the foreign key field" do
50
- expect(klass.shard_key_fields).to eq([:person_id])
87
+ context 'with string value' do
88
+ it 'sets shard key fields to symbol value' do
89
+ SmTrailer.shard_key_fields.should == %i(year)
90
+ end
91
+ end
92
+
93
+ context 'when passed association name' do
94
+ it 'uses foreign key as shard key in shard config' do
95
+ SmDirector.shard_config.should == {
96
+ key: {agency_id: 1},
97
+ options: {},
98
+ }
99
+ end
100
+
101
+ it 'uses foreign key as shard key in shard key fields' do
102
+ SmDirector.shard_key_fields.should == %i(agency_id)
103
+ end
51
104
  end
52
105
  end
53
106
  end
@@ -48,7 +48,7 @@ shared_context "rails rake task" do
48
48
  end
49
49
  end
50
50
 
51
- describe "db:drop", if: non_legacy_server? do
51
+ describe "db:drop" do
52
52
  include_context "rake task"
53
53
  include_context "rails rake task"
54
54
 
@@ -61,7 +61,7 @@ describe "db:drop", if: non_legacy_server? do
61
61
  end
62
62
  end
63
63
 
64
- describe "db:purge", if: non_legacy_server? do
64
+ describe "db:purge" do
65
65
  include_context "rake task"
66
66
  include_context "rails rake task"
67
67
 
@@ -74,7 +74,7 @@ describe "db:purge", if: non_legacy_server? do
74
74
  end
75
75
  end
76
76
 
77
- describe "db:seed", if: non_legacy_server? do
77
+ describe "db:seed" do
78
78
  include_context "rake task"
79
79
  include_context "rails rake task"
80
80
 
@@ -88,7 +88,7 @@ describe "db:seed", if: non_legacy_server? do
88
88
  end
89
89
  end
90
90
 
91
- describe "db:setup", if: non_legacy_server? do
91
+ describe "db:setup" do
92
92
  include_context "rake task"
93
93
  include_context "rails rake task"
94
94
 
@@ -120,7 +120,7 @@ describe "db:setup", if: non_legacy_server? do
120
120
  end
121
121
  end
122
122
 
123
- describe "db:reset", if: non_legacy_server? do
123
+ describe "db:reset" do
124
124
  include_context "rake task"
125
125
  include_context "rails rake task"
126
126
 
@@ -138,7 +138,7 @@ describe "db:reset", if: non_legacy_server? do
138
138
  end
139
139
  end
140
140
 
141
- describe "db:create", if: non_legacy_server? do
141
+ describe "db:create" do
142
142
  include_context "rake task"
143
143
  include_context "rails rake task"
144
144
 
@@ -147,7 +147,7 @@ describe "db:create", if: non_legacy_server? do
147
147
  end
148
148
  end
149
149
 
150
- describe "db:migrate", if: non_legacy_server? do
150
+ describe "db:migrate" do
151
151
  include_context "rake task"
152
152
  include_context "rails rake task"
153
153
 
@@ -156,7 +156,7 @@ describe "db:migrate", if: non_legacy_server? do
156
156
  end
157
157
  end
158
158
 
159
- describe "db:test:prepare", if: non_legacy_server? do
159
+ describe "db:test:prepare" do
160
160
  include_context "rake task"
161
161
  include_context "rails rake task"
162
162
 
@@ -177,7 +177,7 @@ describe "db:test:prepare", if: non_legacy_server? do
177
177
  end
178
178
  end
179
179
 
180
- describe "db:mongoid:create_indexes", if: non_legacy_server? do
180
+ describe "db:mongoid:create_indexes" do
181
181
  include_context "rake task"
182
182
 
183
183
  it_behaves_like "create_indexes"
@@ -201,7 +201,7 @@ describe "db:mongoid:create_indexes", if: non_legacy_server? do
201
201
  end
202
202
  end
203
203
 
204
- describe "db:mongoid:remove_undefined_indexes", if: non_legacy_server? do
204
+ describe "db:mongoid:remove_undefined_indexes" do
205
205
  include_context "rake task"
206
206
 
207
207
  it "receives remove_undefined_indexes" do
@@ -227,7 +227,7 @@ describe "db:mongoid:remove_undefined_indexes", if: non_legacy_server? do
227
227
  end
228
228
  end
229
229
 
230
- describe "db:mongoid:remove_indexes", if: non_legacy_server? do
230
+ describe "db:mongoid:remove_indexes" do
231
231
  include_context "rake task"
232
232
 
233
233
  it "receives remove_indexes" do
@@ -253,7 +253,7 @@ describe "db:mongoid:remove_indexes", if: non_legacy_server? do
253
253
  end
254
254
  end
255
255
 
256
- describe "db:mongoid:drop", if: non_legacy_server? do
256
+ describe "db:mongoid:drop" do
257
257
  include_context "rake task"
258
258
 
259
259
  it "works" do
@@ -269,7 +269,7 @@ describe "db:mongoid:drop", if: non_legacy_server? do
269
269
  end
270
270
  end
271
271
 
272
- describe "db:mongoid:purge", if: non_legacy_server? do
272
+ describe "db:mongoid:purge" do
273
273
  include_context "rake task"
274
274
 
275
275
  it "receives a purge" do
@@ -136,7 +136,7 @@ describe "Mongoid::Tasks::Database" do
136
136
  expect(removed_indexes).to be_empty
137
137
  end
138
138
 
139
- context 'when the index is a text index', if: non_legacy_server? do
139
+ context 'when the index is a text index' do
140
140
 
141
141
  before do
142
142
  class Band
@@ -31,6 +31,7 @@ end
31
31
 
32
32
  require 'support/authorization'
33
33
  require 'support/expectations'
34
+ require 'support/helpers'
34
35
  require 'support/macros'
35
36
  require 'support/cluster_config'
36
37
  require 'support/constraints'
@@ -76,37 +77,6 @@ CONFIG = {
76
77
  }
77
78
  }
78
79
 
79
- def non_legacy_server?
80
- Mongoid::Clients.default.cluster.servers.first.features.write_command_enabled?
81
- end
82
-
83
- def testing_replica_set?
84
- Mongoid::Clients.default.cluster.replica_set?
85
- end
86
-
87
- def collation_supported?
88
- Mongoid::Clients.default.cluster.next_primary.features.collation_enabled?
89
- end
90
- alias :decimal128_supported? :collation_supported?
91
-
92
- def array_filters_supported?
93
- Mongoid::Clients.default.cluster.next_primary.features.array_filters_enabled?
94
- end
95
- alias :sessions_supported? :array_filters_supported?
96
-
97
- def transactions_supported?
98
- features = Mongoid::Clients.default.cluster.next_primary.features
99
- features.respond_to?(:transactions_enabled?) && features.transactions_enabled?
100
- end
101
-
102
- def testing_transactions?
103
- transactions_supported? && if Gem::Version.new(ClusterConfig.instance.fcv_ish) >= Gem::Version.new('4.2')
104
- %i(replica_set sharded).include?(ClusterConfig.instance.topology)
105
- else
106
- ClusterConfig.instance.topology == :replica_set
107
- end
108
- end
109
-
110
80
  # Set the database that the spec suite connects to.
111
81
  Mongoid.configure do |config|
112
82
  config.load_configuration(CONFIG)
@@ -156,6 +126,7 @@ end
156
126
 
157
127
  RSpec.configure do |config|
158
128
  config.raise_errors_for_deprecations!
129
+ config.include(Helpers)
159
130
  config.include(Mongoid::Expectations)
160
131
  config.extend(Constraints)
161
132
  config.extend(Mongoid::Macros)
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ autoload :ChildProcess, 'childprocess'
5
+ autoload :Tempfile, 'tempfile'
6
+
7
+ module ChildProcessHelper
8
+ module_function def call(cmd, env: nil, cwd: nil)
9
+ process = ChildProcess.new(*cmd)
10
+ process.io.inherit!
11
+ if cwd
12
+ process.cwd = cwd
13
+ end
14
+ if env
15
+ env.each do |k, v|
16
+ process.environment[k.to_s] = v
17
+ end
18
+ end
19
+ process.start
20
+ process.wait
21
+ process
22
+ end
23
+
24
+ module_function def check_call(cmd, env: nil, cwd: nil)
25
+ process = call(cmd, env: env, cwd: cwd)
26
+ unless process.exit_code == 0
27
+ raise "Failed to execute: #{cmd}"
28
+ end
29
+ end
30
+
31
+ module_function def get_output(cmd, env: nil, cwd: nil)
32
+ process = ChildProcess.new(*cmd)
33
+ process.io.inherit!
34
+ if cwd
35
+ process.cwd = cwd
36
+ end
37
+ if env
38
+ env.each do |k, v|
39
+ process.environment[k.to_s] = v
40
+ end
41
+ end
42
+
43
+ output = ''
44
+ r, w = IO.pipe
45
+
46
+ begin
47
+ process.io.stdout = w
48
+ process.start
49
+ w.close
50
+
51
+ thread = Thread.new do
52
+ begin
53
+ loop do
54
+ output << r.readpartial(16384)
55
+ end
56
+ rescue EOFError
57
+ end
58
+ end
59
+
60
+ process.wait
61
+ thread.join
62
+ ensure
63
+ r.close
64
+ end
65
+
66
+ [process, output]
67
+ end
68
+
69
+ module_function def check_output(*args)
70
+ process, output = get_output(*args)
71
+ unless process.exit_code == 0
72
+ raise "Failed to execute: #{args}"
73
+ end
74
+ output
75
+ end
76
+ end
@@ -99,7 +99,7 @@ class ClusterConfig
99
99
  if topology == :sharded
100
100
  shards = client.use(:admin).command(listShards: 1).first
101
101
  shard = shards['shards'].first
102
- address_str = shard['host'].sub(/^.*\//, '').sub(/,.*/, '')
102
+ address_str = shard['host'].sub(/\A.*\//, '').sub(/,.*/, '')
103
103
  client = ClusterTools.instance.direct_client(address_str,
104
104
  SpecConfig.instance.test_options.merge(SpecConfig.instance.auth_options).merge(connect: :direct))
105
105
  end
@@ -133,8 +133,8 @@ class ClusterConfig
133
133
 
134
134
  @topology ||= begin
135
135
  topology = client.cluster.topology.class.name.sub(/.*::/, '')
136
- topology = topology.gsub(/([A-Z])/) { |match| '_' + match.downcase }.sub(/^_/, '')
137
- if topology =~ /^replica_set/
136
+ topology = topology.gsub(/([A-Z])/) { |match| '_' + match.downcase }.sub(/\A_/, '')
137
+ if topology =~ /\Areplica_set/
138
138
  topology = 'replica_set'
139
139
  end
140
140
  topology.to_sym
@@ -5,11 +5,11 @@ module Constraints
5
5
  RAILS_VERSION = ActiveSupport.version.to_s.split('.')[0..1].join('.').freeze
6
6
 
7
7
  def min_rails_version(version)
8
- unless version =~ /^\d+\.\d+$/
8
+ unless version =~ /\A\d+\.\d+\z/
9
9
  raise ArgumentError, "Version can only be major.minor: #{version}"
10
10
  end
11
11
 
12
- before do
12
+ before(:all) do
13
13
  if version > RAILS_VERSION
14
14
  skip "Rails version #{version} or higher required, we have #{RAILS_VERSION}"
15
15
  end
@@ -17,11 +17,11 @@ module Constraints
17
17
  end
18
18
 
19
19
  def max_rails_version(version)
20
- unless version =~ /^\d+\.\d+$/
20
+ unless version =~ /\A\d+\.\d+\z/
21
21
  raise ArgumentError, "Version can only be major.minor: #{version}"
22
22
  end
23
23
 
24
- before do
24
+ before(:all) do
25
25
  if version < RAILS_VERSION
26
26
  skip "Rails version #{version} or lower required, we have #{RAILS_VERSION}"
27
27
  end
@@ -29,11 +29,11 @@ module Constraints
29
29
  end
30
30
 
31
31
  def min_server_version(version)
32
- unless version =~ /^\d+\.\d+$/
32
+ unless version =~ /\A\d+\.\d+\z/
33
33
  raise ArgumentError, "Version can only be major.minor: #{version}"
34
34
  end
35
35
 
36
- before do
36
+ before(:all) do
37
37
  if version > ClusterConfig.instance.server_version
38
38
  skip "Server version #{version} or higher required, we have #{ClusterConfig.instance.server_version}"
39
39
  end
@@ -41,11 +41,11 @@ module Constraints
41
41
  end
42
42
 
43
43
  def max_server_version(version)
44
- unless version =~ /^\d+\.\d+$/
44
+ unless version =~ /\A\d+\.\d+\z/
45
45
  raise ArgumentError, "Version can only be major.minor: #{version}"
46
46
  end
47
47
 
48
- before do
48
+ before(:all) do
49
49
  if version < ClusterConfig.instance.short_server_version
50
50
  skip "Server version #{version} or lower required, we have #{ClusterConfig.instance.server_version}"
51
51
  end
@@ -54,9 +54,11 @@ module Constraints
54
54
 
55
55
  def require_topology(*topologies)
56
56
  invalid_topologies = topologies - [:single, :replica_set, :sharded]
57
+
57
58
  unless invalid_topologies.empty?
58
59
  raise ArgumentError, "Invalid topologies requested: #{invalid_topologies.join(', ')}"
59
60
  end
61
+
60
62
  before(:all) do
61
63
  unless topologies.include?(topology = ClusterConfig.instance.topology)
62
64
  skip "Topology #{topologies.join(' or ')} required, we have #{topology}"
@@ -73,8 +75,22 @@ module Constraints
73
75
  end
74
76
 
75
77
  def require_transaction_support
76
- min_server_fcv '4.0'
77
- require_topology :replica_set
78
+ before(:all) do
79
+ case ClusterConfig.instance.topology
80
+ when :single
81
+ skip 'Transactions tests require a replica set (4.0+) or a sharded cluster (4.2+)'
82
+ when :replica_set
83
+ unless ClusterConfig.instance.server_version >= '4.0'
84
+ skip 'Transactions tests in a replica set topology require server 4.0+'
85
+ end
86
+ when :sharded
87
+ unless ClusterConfig.instance.server_version >= '4.2'
88
+ skip 'Transactions tests in a sharded cluster topology require server 4.2+'
89
+ end
90
+ else
91
+ raise NotImplementedError
92
+ end
93
+ end
78
94
  end
79
95
 
80
96
  def require_tls