mongoid 7.2.0.rc1 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +30 -11
  5. data/lib/mongoid/association/referenced/has_one/buildable.rb +8 -0
  6. data/lib/mongoid/association/referenced/has_one/proxy.rb +6 -1
  7. data/lib/mongoid/copyable.rb +5 -1
  8. data/lib/mongoid/extensions.rb +1 -0
  9. data/lib/mongoid/fields.rb +3 -0
  10. data/lib/mongoid/stringified_symbol.rb +53 -0
  11. data/lib/mongoid/version.rb +1 -1
  12. data/spec/README.md +19 -4
  13. data/spec/integration/app_spec.rb +12 -12
  14. data/spec/integration/associations/embeds_many_spec.rb +24 -0
  15. data/spec/integration/associations/embeds_one_spec.rb +24 -0
  16. data/spec/integration/associations/has_many_spec.rb +60 -0
  17. data/spec/integration/associations/has_one_spec.rb +60 -0
  18. data/spec/integration/stringified_symbol_field_spec.rb +190 -0
  19. data/spec/lite_spec_helper.rb +4 -3
  20. data/spec/mongoid/association/referenced/has_many_models.rb +12 -0
  21. data/spec/mongoid/association/referenced/has_one_models.rb +12 -0
  22. data/spec/mongoid/association/referenced/has_one_spec.rb +1 -1
  23. data/spec/mongoid/copyable_spec.rb +44 -17
  24. data/spec/mongoid/copyable_spec_models.rb +14 -0
  25. data/spec/mongoid/equality_spec.rb +0 -1
  26. data/spec/mongoid/extensions/stringified_symbol_spec.rb +85 -0
  27. data/spec/shared/LICENSE +20 -0
  28. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  29. data/spec/shared/lib/mrss/constraints.rb +303 -0
  30. data/spec/shared/lib/mrss/lite_constraints.rb +175 -0
  31. data/spec/shared/lib/mrss/spec_organizer.rb +149 -0
  32. data/spec/spec_helper.rb +2 -0
  33. data/spec/support/constraints.rb +0 -226
  34. data/spec/support/models/order.rb +11 -0
  35. data/spec/support/models/person.rb +2 -0
  36. data/spec/support/models/series.rb +1 -0
  37. data/spec/support/models/wiki_page.rb +1 -0
  38. metadata +512 -496
  39. metadata.gz.sig +0 -0
  40. data/spec/support/child_process_helper.rb +0 -79
  41. data/spec/support/lite_constraints.rb +0 -22
  42. data/spec/support/spec_organizer.rb +0 -130
@@ -0,0 +1,190 @@
1
+ require "spec_helper"
2
+
3
+ describe "StringifiedSymbol fields" do
4
+
5
+ before do
6
+ Order.destroy_all
7
+ end
8
+
9
+ context "when querying the database" do
10
+
11
+ let!(:document) do
12
+ Order.create!(saved_status: :test)
13
+ end
14
+
15
+ let(:string_query) do
16
+ {'saved_status' => {'$eq' => 'test'}}
17
+ end
18
+
19
+ let(:symbol_query) do
20
+ {'saved_status' => {'$eq' => :test}}
21
+ end
22
+
23
+ it "can be queried with a string" do
24
+ doc = Order.where(string_query).first
25
+ expect(doc.saved_status).to eq(:test)
26
+ end
27
+
28
+ it "can be queried with a symbol" do
29
+ doc = Order.where(symbol_query).first
30
+ expect(doc.saved_status).to eq(:test)
31
+ end
32
+ end
33
+
34
+ # Using command monitoring to test that StringifiedSymbol sends a string and returns a symbol
35
+ let(:client) { Order.collection.client }
36
+
37
+ before do
38
+ client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
39
+ end
40
+
41
+ after do
42
+ client.unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
43
+ end
44
+
45
+ let(:subscriber) do
46
+ EventSubscriber.new
47
+ end
48
+
49
+ let(:find_events) do
50
+ subscriber.started_events.select { |event| event.command_name.to_s == 'find' }
51
+ end
52
+
53
+ let(:insert_events) do
54
+ subscriber.started_events.select { |event| event.command_name.to_s == 'insert' }
55
+ end
56
+
57
+ let(:update_events) do
58
+ subscriber.started_events.select { |event| event.command_name.to_s == 'update' }
59
+ end
60
+
61
+ before do
62
+ subscriber.clear_events!
63
+ end
64
+
65
+ let(:query) do
66
+ {'saved_status' => {'$eq' => 'test'}}
67
+ end
68
+
69
+ let!(:document1) do
70
+ Order.create!(saved_status: :test)
71
+ end
72
+
73
+ let!(:document2) do
74
+ Order.where(query).first
75
+ end
76
+
77
+ context "when inserting document" do
78
+
79
+ it "sends the value as a string" do
80
+ Order.create!(saved_status: :test)
81
+ event = insert_events.first
82
+ doc = event.command["documents"].first
83
+ expect(doc["saved_status"]).to eq("test")
84
+ end
85
+
86
+ it "sends the value as a string" do
87
+ Order.create!(saved_status: 42)
88
+ event = insert_events.second
89
+ doc = event.command["documents"].first
90
+ expect(doc["saved_status"]).to eq("42")
91
+ end
92
+
93
+ it "sends the value as a string" do
94
+ Order.create(saved_status: [0, 1, 2])
95
+ event = insert_events.second
96
+ doc = event.command["documents"].first
97
+ expect(doc["saved_status"]).to eq("[0, 1, 2]")
98
+ end
99
+ end
100
+
101
+ context "when finding document" do
102
+
103
+ it "receives the value as a symbol" do
104
+ event = find_events.first
105
+ expect(document2.saved_status).to eq(:test)
106
+ end
107
+ end
108
+
109
+ context "when reading a BSON Symbol field" do
110
+
111
+ before do
112
+ client["orders"].insert_one(saved_status: BSON::Symbol::Raw.new("test"), _id: 12)
113
+ end
114
+
115
+ it "receives the value as a symbol" do
116
+ expect(Order.find(12).saved_status).to eq(:test)
117
+ end
118
+
119
+ it "saves the value as a string" do
120
+ s = Order.find(12)
121
+ s.saved_status = :other
122
+ s.save!
123
+ event = update_events.first
124
+ expect(event.command["updates"].first["u"]["$set"]["saved_status"]).to eq("other")
125
+ end
126
+ end
127
+
128
+ context "when value is nil" do
129
+
130
+ before do
131
+ client["orders"].insert_one(saved_status: nil, _id: 15)
132
+ end
133
+
134
+ it "returns nil" do
135
+ expect(Order.find(15).saved_status).to be_nil
136
+ end
137
+ end
138
+
139
+ context "when writing nil" do
140
+
141
+ before do
142
+ client["orders"].insert_one(saved_status: "hello", _id: 16)
143
+ end
144
+
145
+ it "saves the value as nil" do
146
+ s = Order.find(16)
147
+ s.saved_status = nil
148
+ s.save!
149
+ event = update_events.first
150
+ expect(event.command["updates"].first["u"]["$set"]["saved_status"]).to be_nil
151
+ end
152
+ end
153
+
154
+ context "when reading an integer" do
155
+
156
+ before do
157
+ client["orders"].insert_one(saved_status: 42, _id: 13)
158
+ end
159
+
160
+ it "receives the value as a symbol" do
161
+ expect(Order.find(13).saved_status).to eq(:"42")
162
+ end
163
+
164
+ it "saves the value as a string" do
165
+ s = Order.find(13)
166
+ s.saved_status = 24
167
+ s.save!
168
+ event = update_events.first
169
+ expect(event.command["updates"].first["u"]["$set"]["saved_status"]).to eq("24")
170
+ end
171
+ end
172
+
173
+ context "when reading an array" do
174
+ before do
175
+ client["orders"].insert_one(saved_status: [0, 1, 2], _id: 14)
176
+ end
177
+
178
+ it "receives the value as a symbol" do
179
+ expect(Order.find(14).saved_status).to be(:"[0, 1, 2]")
180
+ end
181
+
182
+ it "saves the value as a string" do
183
+ s = Order.find(14)
184
+ s.saved_status = [3, 4, 5]
185
+ s.save!
186
+ event = update_events.first
187
+ expect(event.command["updates"].first["u"]["$set"]["saved_status"]).to eq("[3, 4, 5]")
188
+ end
189
+ end
190
+ end
@@ -3,6 +3,7 @@
3
3
 
4
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "shared", "lib"))
6
7
 
7
8
  # Load byebug before mongoid, to place breakpoints in the lib methods.
8
9
  # But SpecConfig needs the driver code - require the driver here.
@@ -15,8 +16,8 @@ require "mongo"
15
16
  require 'pp'
16
17
 
17
18
  require 'support/spec_config'
18
- require 'support/lite_constraints'
19
- require "support/session_registry"
19
+ require 'mrss/lite_constraints'
20
+ require "support/session_registry"
20
21
 
21
22
  unless SpecConfig.instance.ci?
22
23
  begin
@@ -68,7 +69,7 @@ RSpec.configure do |config|
68
69
  end
69
70
  end
70
71
 
71
- config.extend(LiteConstraints)
72
+ config.extend(Mrss::LiteConstraints)
72
73
  end
73
74
 
74
75
  # require all shared examples
@@ -47,3 +47,15 @@ class HmmTicket
47
47
 
48
48
  belongs_to :person
49
49
  end
50
+
51
+ class HmmBus
52
+ include Mongoid::Document
53
+
54
+ has_many :seats, class_name: 'HmmBusSeat'
55
+ end
56
+
57
+ class HmmBusSeat
58
+ include Mongoid::Document
59
+
60
+ # No belongs_to :bus
61
+ end
@@ -58,3 +58,15 @@ class HomPolymorphicChild
58
58
 
59
59
  belongs_to :p_parent, polymorphic: true
60
60
  end
61
+
62
+ class HomBus
63
+ include Mongoid::Document
64
+
65
+ has_one :driver, class_name: 'HomBusDriver'
66
+ end
67
+
68
+ class HomBusDriver
69
+ include Mongoid::Document
70
+
71
+ # No belongs_to :bus
72
+ end
@@ -748,7 +748,7 @@ describe Mongoid::Association::Referenced::HasOne do
748
748
  end
749
749
  end
750
750
 
751
- describe '##inverse' do
751
+ describe '#inverse' do
752
752
 
753
753
  context 'when polymorphic' do
754
754
 
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # encoding: utf-8
3
3
 
4
- # -*- coding: utf-8 -*-
5
4
  require "spec_helper"
6
5
 
7
6
  require_relative './copyable_spec_models'
@@ -65,30 +64,58 @@ describe Mongoid::Copyable do
65
64
 
66
65
  context "when a document has fields from a legacy schema" do
67
66
 
68
- let!(:actor) do
69
- Actor.create(name: "test")
70
- end
67
+ shared_examples 'behaves as expected' do
68
+ let!(:instance) do
69
+ cls.create(name: "test")
70
+ end
71
71
 
72
- before do
73
- legacy_fields = { "this_is_not_a_field" => 1, "this_legacy_field_is_nil" => nil }
74
- Actor.collection.find(_id: actor.id).update_one("$set" => legacy_fields)
75
- end
72
+ before do
73
+ legacy_fields = { "this_is_not_a_field" => 1, "this_legacy_field_is_nil" => nil }
74
+ cls.collection.find(_id: instance.id).update_one("$set" => legacy_fields)
75
+ end
76
76
 
77
- let(:cloned) do
78
- actor.reload.send(method)
79
- end
77
+ let(:cloned) do
78
+ instance.reload.send(method)
79
+ end
80
+
81
+ it "sets the legacy attribute" do
82
+ expect(cloned.attributes['this_is_not_a_field']).to eq(1)
83
+ end
84
+
85
+ it "contains legacy attributes that are nil" do
86
+ expect(cloned.attributes.key?('this_legacy_field_is_nil')).to eq(true)
87
+ end
80
88
 
81
- it "sets the legacy attribute" do
82
- expect(cloned.attributes['this_is_not_a_field']).to eq(1)
89
+ it "copies the known attributes" do
90
+ expect(cloned.name).to eq('test')
91
+ end
92
+
93
+ it 'calls constructor with explicitly declared attributes only' do
94
+ expect(cls).to receive(:new).with('name' => 'test').and_call_original
95
+ cloned
96
+ end
83
97
  end
84
98
 
85
- it "contains legacy attributes that are nil" do
86
- expect(cloned.attributes.key?('this_legacy_field_is_nil')).to eq(true)
99
+ context 'without Attributes::Dynamic' do
100
+ let(:cls) { CopyableSpec::Reg }
101
+
102
+ before do
103
+ cls.should_not include(Mongoid::Attributes::Dynamic)
104
+ end
105
+
106
+ include_examples 'behaves as expected'
87
107
  end
88
108
 
89
- it "copies the known attributes" do
90
- expect(cloned.name).to eq('test')
109
+ context 'with Attributes::Dynamic' do
110
+ let(:cls) { CopyableSpec::Dyn }
111
+
112
+ before do
113
+ cls.should include(Mongoid::Attributes::Dynamic)
114
+ end
115
+
116
+ include_examples 'behaves as expected'
91
117
  end
118
+
92
119
  end
93
120
 
94
121
  context "when using store_as" do
@@ -31,4 +31,18 @@ module CopyableSpec
31
31
  class Blurb
32
32
  include Mongoid::Document
33
33
  end
34
+
35
+ # Do not include Attributes::Dynamic
36
+ class Reg
37
+ include Mongoid::Document
38
+
39
+ field :name, type: String
40
+ end
41
+
42
+ class Dyn
43
+ include Mongoid::Document
44
+ include Mongoid::Attributes::Dynamic
45
+
46
+ field :name, type: String
47
+ end
34
48
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # encoding: utf-8
3
3
 
4
- # -*- coding: utf-8 -*-
5
4
  require "spec_helper"
6
5
 
7
6
  describe Mongoid::Equality do
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require "spec_helper"
5
+
6
+ describe Mongoid::StringifiedSymbol do
7
+
8
+ describe ".demongoize" do
9
+
10
+ context "when the object is not a symbol" do
11
+
12
+ it "returns the symbol" do
13
+ expect(Mongoid::StringifiedSymbol.demongoize("test")).to eq(:test)
14
+ end
15
+ end
16
+
17
+ context "when the object is a symbol" do
18
+
19
+ it "returns the symbol" do
20
+ expect(Mongoid::StringifiedSymbol.demongoize(:test)).to eq(:test)
21
+ end
22
+ end
23
+
24
+ context "when the object is a BSON Symbol" do
25
+
26
+ it "returns a symbol" do
27
+ expect(Mongoid::StringifiedSymbol.demongoize(BSON::Symbol::Raw.new(:test))).to eq(:test)
28
+ end
29
+ end
30
+
31
+
32
+ context "when the object is an integer" do
33
+
34
+ it "returns a symbol" do
35
+ expect(Mongoid::StringifiedSymbol.demongoize(14)).to eq(:"14")
36
+ end
37
+ end
38
+
39
+ context "when the object is nil" do
40
+
41
+ it "returns nil" do
42
+ expect(Mongoid::StringifiedSymbol.demongoize(nil)).to be_nil
43
+ end
44
+ end
45
+ end
46
+
47
+ describe ".mongoize" do
48
+
49
+ context "when the object is not a symbol" do
50
+
51
+ it "returns the object" do
52
+ expect(Mongoid::StringifiedSymbol.mongoize("test")).to eq("test")
53
+ end
54
+
55
+ it "returns the string" do
56
+ expect(Mongoid::StringifiedSymbol.mongoize([0,1,2])).to eq("[0, 1, 2]")
57
+ end
58
+
59
+ it "returns the string" do
60
+ expect(Mongoid::StringifiedSymbol.mongoize(2)).to eq("2")
61
+ end
62
+ end
63
+
64
+ context "when the object is a symbol" do
65
+
66
+ it "returns a string" do
67
+ expect(Mongoid::StringifiedSymbol.mongoize(:test)).to eq("test")
68
+ end
69
+ end
70
+
71
+ context "when the object is nil" do
72
+
73
+ it "returns nil" do
74
+ expect(Mongoid::StringifiedSymbol.mongoize(nil)).to be_nil
75
+ end
76
+ end
77
+ end
78
+
79
+ describe "#mongoize" do
80
+
81
+ it "returns self" do
82
+ expect(:test.mongoize).to eq(:test)
83
+ end
84
+ end
85
+ end