pwnash-mongo_mapper 0.7.5 → 0.7.6
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.
- data/lib/mongo_mapper.rb +6 -8
- data/lib/mongo_mapper/document.rb +6 -281
- data/lib/mongo_mapper/embedded_document.rb +5 -45
- data/lib/mongo_mapper/extensions.rb +190 -0
- data/lib/mongo_mapper/plugins.rb +27 -18
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +4 -3
- data/lib/mongo_mapper/plugins/clone.rb +1 -0
- data/lib/mongo_mapper/plugins/descendants.rb +1 -0
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +40 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +42 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +2 -36
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +1 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +8 -11
- data/lib/mongo_mapper/plugins/indexes.rb +11 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys.rb +7 -9
- data/lib/mongo_mapper/plugins/keys/key.rb +12 -2
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +3 -2
- data/lib/mongo_mapper/plugins/pagination.rb +1 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/persistence.rb +1 -0
- data/lib/mongo_mapper/plugins/protected.rb +1 -0
- data/lib/mongo_mapper/plugins/query_logger.rb +5 -10
- data/lib/mongo_mapper/plugins/querying.rb +236 -0
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/sci.rb +31 -0
- data/lib/mongo_mapper/plugins/serialization.rb +1 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +5 -1
- data/lib/mongo_mapper/query.rb +53 -120
- data/lib/mongo_mapper/support.rb +2 -213
- data/lib/mongo_mapper/support/descendant_appends.rb +3 -6
- data/lib/mongo_mapper/version.rb +2 -1
- metadata +156 -190
- data/.gitignore +0 -10
- data/Rakefile +0 -37
- data/mongo_mapper.gemspec +0 -216
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/NOTE_ON_TESTING +0 -1
- data/test/active_model_lint_test.rb +0 -13
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
- data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
- data/test/functional/associations/test_in_array_proxy.rb +0 -325
- data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
- data/test/functional/associations/test_many_documents_proxy.rb +0 -536
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
- data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
- data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
- data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
- data/test/functional/associations/test_one_proxy.rb +0 -196
- data/test/functional/test_associations.rb +0 -44
- data/test/functional/test_binary.rb +0 -27
- data/test/functional/test_callbacks.rb +0 -151
- data/test/functional/test_dirty.rb +0 -163
- data/test/functional/test_document.rb +0 -1219
- data/test/functional/test_embedded_document.rb +0 -210
- data/test/functional/test_identity_map.rb +0 -507
- data/test/functional/test_indexing.rb +0 -44
- data/test/functional/test_logger.rb +0 -20
- data/test/functional/test_modifiers.rb +0 -416
- data/test/functional/test_pagination.rb +0 -93
- data/test/functional/test_protected.rb +0 -163
- data/test/functional/test_string_id_compatibility.rb +0 -67
- data/test/functional/test_timestamps.rb +0 -64
- data/test/functional/test_userstamps.rb +0 -28
- data/test/functional/test_validations.rb +0 -342
- data/test/models.rb +0 -227
- data/test/support/custom_matchers.rb +0 -37
- data/test/support/timing.rb +0 -16
- data/test/test_helper.rb +0 -64
- data/test/unit/associations/test_base.rb +0 -212
- data/test/unit/associations/test_proxy.rb +0 -105
- data/test/unit/serializers/test_json_serializer.rb +0 -202
- data/test/unit/test_descendant_appends.rb +0 -71
- data/test/unit/test_document.rb +0 -225
- data/test/unit/test_dynamic_finder.rb +0 -123
- data/test/unit/test_embedded_document.rb +0 -657
- data/test/unit/test_keys.rb +0 -216
- data/test/unit/test_mongo_mapper.rb +0 -118
- data/test/unit/test_pagination.rb +0 -160
- data/test/unit/test_plugins.rb +0 -50
- data/test/unit/test_query.rb +0 -374
- data/test/unit/test_rails.rb +0 -181
- data/test/unit/test_rails_compatibility.rb +0 -52
- data/test/unit/test_serialization.rb +0 -51
- data/test/unit/test_support.rb +0 -390
- data/test/unit/test_time_zones.rb +0 -39
- data/test/unit/test_validations.rb +0 -544
@@ -1,210 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class EmbeddedDocumentTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
@klass = Doc('Person') do
|
7
|
-
key :name, String
|
8
|
-
end
|
9
|
-
|
10
|
-
@pet_klass = EDoc('Pet') do
|
11
|
-
key :name, String
|
12
|
-
end
|
13
|
-
|
14
|
-
@klass.many :pets, :class => @pet_klass
|
15
|
-
|
16
|
-
@address_class = EDoc('Address') do
|
17
|
-
key :city, String
|
18
|
-
key :state, String
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "Saving a document with a key that is an embedded document" do
|
23
|
-
setup do
|
24
|
-
@klass.key :foo, @address_class
|
25
|
-
end
|
26
|
-
|
27
|
-
should "embed embedded document" do
|
28
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
29
|
-
doc = @klass.create(:foo => address)
|
30
|
-
doc.foo.city.should == 'South Bend'
|
31
|
-
doc.foo.state.should == 'IN'
|
32
|
-
|
33
|
-
doc = doc.reload
|
34
|
-
doc.foo.city.should == 'South Bend'
|
35
|
-
doc.foo.state.should == 'IN'
|
36
|
-
end
|
37
|
-
|
38
|
-
should "assign _parent_document and _root_document" do
|
39
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
40
|
-
address._parent_document.should be_nil
|
41
|
-
doc = @klass.create(:foo => address)
|
42
|
-
address._parent_document.should be(doc)
|
43
|
-
address._root_document.should be(doc)
|
44
|
-
end
|
45
|
-
|
46
|
-
should "assign _parent_document and _root_document when loading" do
|
47
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
48
|
-
doc = @klass.create(:foo => address)
|
49
|
-
doc.reload
|
50
|
-
doc.foo._parent_document.should be(doc)
|
51
|
-
doc.foo._root_document.should be(doc)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
should "correctly instantiate single collection inherited embedded documents" do
|
56
|
-
document = Doc('Foo') do
|
57
|
-
key :message, Message
|
58
|
-
end
|
59
|
-
|
60
|
-
doc1 = document.create(:message => Enter.new)
|
61
|
-
doc1.reload.message.class.should be(Enter)
|
62
|
-
end
|
63
|
-
|
64
|
-
context "new? (embedded key)" do
|
65
|
-
setup do
|
66
|
-
@klass.key :foo, @address_class
|
67
|
-
end
|
68
|
-
|
69
|
-
should "be true until document is saved" do
|
70
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
71
|
-
doc = @klass.new(:foo => address)
|
72
|
-
address.new?.should be_true
|
73
|
-
end
|
74
|
-
|
75
|
-
should "be false after document is saved" do
|
76
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
77
|
-
doc = @klass.new(:foo => address)
|
78
|
-
doc.save
|
79
|
-
doc.foo.new?.should be_false
|
80
|
-
end
|
81
|
-
|
82
|
-
should "be false when loaded from database" do
|
83
|
-
address = @address_class.new(:city => 'South Bend', :state => 'IN')
|
84
|
-
doc = @klass.new(:foo => address)
|
85
|
-
doc.save
|
86
|
-
|
87
|
-
doc.reload
|
88
|
-
doc.foo.new?.should be_false
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "new? (embedded association)" do
|
93
|
-
setup do
|
94
|
-
@doc = @klass.new(:pets => [{:name => 'poo bear'}])
|
95
|
-
end
|
96
|
-
|
97
|
-
should "be true until document is saved" do
|
98
|
-
@doc.should be_new
|
99
|
-
@doc.pets.first.should be_new
|
100
|
-
end
|
101
|
-
|
102
|
-
should "be false after document is saved" do
|
103
|
-
@doc.save
|
104
|
-
@doc.pets.first.should_not be_new
|
105
|
-
end
|
106
|
-
|
107
|
-
should "be false when loaded from database" do
|
108
|
-
@doc.save
|
109
|
-
@doc.pets.first.should_not be_new
|
110
|
-
@doc.reload
|
111
|
-
@doc.pets.first.should_not be_new
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
context "#destroyed?" do
|
116
|
-
setup do
|
117
|
-
@doc = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
|
118
|
-
end
|
119
|
-
|
120
|
-
should "be false if root document is not destroyed" do
|
121
|
-
@doc.should_not be_destroyed
|
122
|
-
@doc.pets.first.should_not be_destroyed
|
123
|
-
end
|
124
|
-
|
125
|
-
should "be true if root document is destroyed" do
|
126
|
-
@doc.destroy
|
127
|
-
@doc.should be_destroyed
|
128
|
-
@doc.pets.first.should be_destroyed
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context "#persisted?" do
|
133
|
-
setup do
|
134
|
-
@doc = @klass.new(:name => 'persisted doc', :pets => [@pet_klass.new(:name => 'persisted pet')])
|
135
|
-
end
|
136
|
-
|
137
|
-
should "be false if new" do
|
138
|
-
@doc.pets.first.should_not be_persisted
|
139
|
-
end
|
140
|
-
|
141
|
-
should "be false if destroyed" do
|
142
|
-
@doc.save
|
143
|
-
@doc.destroy
|
144
|
-
@doc.pets.first.should be_destroyed
|
145
|
-
@doc.pets.first.should_not be_persisted
|
146
|
-
end
|
147
|
-
|
148
|
-
should "be true if not new or destroyed" do
|
149
|
-
@doc.save
|
150
|
-
@doc.pets.first.should be_persisted
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
should "be able to save" do
|
155
|
-
person = @klass.create
|
156
|
-
|
157
|
-
pet = @pet_klass.new(:name => 'sparky')
|
158
|
-
person.pets << pet
|
159
|
-
pet.should be_new
|
160
|
-
pet.save
|
161
|
-
pet.should_not be_new
|
162
|
-
|
163
|
-
person.reload
|
164
|
-
person.pets.first.should == pet
|
165
|
-
end
|
166
|
-
|
167
|
-
should "be able to dynamically add new keys and save" do
|
168
|
-
person = @klass.create
|
169
|
-
|
170
|
-
pet = @pet_klass.new(:name => 'sparky', :crazy_key => 'crazy')
|
171
|
-
person.pets << pet
|
172
|
-
pet.save
|
173
|
-
|
174
|
-
person.reload
|
175
|
-
person.pets.first.crazy_key.should == 'crazy'
|
176
|
-
end
|
177
|
-
|
178
|
-
should "be able to update_attributes" do
|
179
|
-
pet = @pet_klass.new(:name => 'sparky')
|
180
|
-
person = @klass.create(:pets => [pet])
|
181
|
-
person.reload
|
182
|
-
pet = person.pets.first
|
183
|
-
|
184
|
-
pet.update_attributes(:name => 'koda').should be_true
|
185
|
-
person.reload
|
186
|
-
person.pets.first._id.should == pet._id
|
187
|
-
person.pets.first.name.should == 'koda'
|
188
|
-
end
|
189
|
-
|
190
|
-
should "be able to update_attributes!" do
|
191
|
-
person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
|
192
|
-
person.reload
|
193
|
-
pet = person.pets.first
|
194
|
-
|
195
|
-
attributes = {:name => 'koda'}
|
196
|
-
pet.expects(:attributes=).with(attributes)
|
197
|
-
pet.expects(:save!)
|
198
|
-
pet.update_attributes!(attributes)
|
199
|
-
end
|
200
|
-
|
201
|
-
should "have database instance method that is equal to root document" do
|
202
|
-
person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
|
203
|
-
person.pets.first.database.should == person.database
|
204
|
-
end
|
205
|
-
|
206
|
-
should "have collection instance method that is equal to root document" do
|
207
|
-
person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
|
208
|
-
person.pets.first.collection.name.should == person.collection.name
|
209
|
-
end
|
210
|
-
end
|
@@ -1,507 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class IdentityMapTest < Test::Unit::TestCase
|
4
|
-
def assert_in_map(*resources)
|
5
|
-
[resources].flatten.each do |resource|
|
6
|
-
resource.identity_map.keys.should include(resource._id)
|
7
|
-
mapped_resource = resource.identity_map[resource._id]
|
8
|
-
resource.should equal(mapped_resource)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def assert_not_in_map(*resources)
|
13
|
-
[resources].flatten.each do |resource|
|
14
|
-
resource.identity_map.keys.should_not include(resource._id)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def expect_no_queries
|
19
|
-
Mongo::Collection.any_instance.expects(:find_one).never
|
20
|
-
Mongo::Collection.any_instance.expects(:find).never
|
21
|
-
end
|
22
|
-
|
23
|
-
def expects_one_query
|
24
|
-
Mongo::Collection.any_instance.expects(:find_one).once.returns({})
|
25
|
-
end
|
26
|
-
|
27
|
-
context "Document" do
|
28
|
-
setup do
|
29
|
-
MongoMapper::Plugins::IdentityMap.models.clear
|
30
|
-
|
31
|
-
@person_class = Doc('Person') do
|
32
|
-
set_collection_name 'people'
|
33
|
-
plugin MongoMapper::Plugins::IdentityMap
|
34
|
-
|
35
|
-
key :name, String
|
36
|
-
end
|
37
|
-
|
38
|
-
@post_class = Doc('Post') do
|
39
|
-
set_collection_name 'posts'
|
40
|
-
plugin MongoMapper::Plugins::IdentityMap
|
41
|
-
|
42
|
-
key :title, String
|
43
|
-
key :person_id, ObjectId
|
44
|
-
end
|
45
|
-
|
46
|
-
@post_class.belongs_to :person, :class => @person_class
|
47
|
-
@person_class.many :posts, :class => @post_class
|
48
|
-
|
49
|
-
@post_class.identity_map_on
|
50
|
-
@person_class.identity_map_on
|
51
|
-
MongoMapper::Plugins::IdentityMap.clear
|
52
|
-
end
|
53
|
-
|
54
|
-
should "track identity mapped models" do
|
55
|
-
MongoMapper::Plugins::IdentityMap.models.should == [@person_class, @post_class].to_set
|
56
|
-
end
|
57
|
-
|
58
|
-
should "be able to clear the map of all models" do
|
59
|
-
person = @person_class.create(:name => 'John')
|
60
|
-
post = @post_class.create(:title => 'IM 4eva')
|
61
|
-
assert_in_map(person, post)
|
62
|
-
|
63
|
-
MongoMapper::Plugins::IdentityMap.clear
|
64
|
-
|
65
|
-
assert_not_in_map(person, post)
|
66
|
-
|
67
|
-
[@person_class, @post_class].each { |klass| klass.identity_map.should == {} }
|
68
|
-
end
|
69
|
-
|
70
|
-
context "IM on off status" do
|
71
|
-
teardown do
|
72
|
-
@post_class.identity_map_on
|
73
|
-
@person_class.identity_map_on
|
74
|
-
end
|
75
|
-
|
76
|
-
should "default identity map status to on" do
|
77
|
-
Doc { plugin MongoMapper::Plugins::IdentityMap }.identity_map_status.should be_true
|
78
|
-
end
|
79
|
-
|
80
|
-
should "be true if on" do
|
81
|
-
@post_class.identity_map_on
|
82
|
-
@post_class.should be_identity_map_on
|
83
|
-
@post_class.should_not be_identity_map_off
|
84
|
-
end
|
85
|
-
|
86
|
-
should "be false if off" do
|
87
|
-
@post_class.identity_map_off
|
88
|
-
@post_class.should be_identity_map_off
|
89
|
-
@post_class.should_not be_identity_map_on
|
90
|
-
end
|
91
|
-
|
92
|
-
should "not share with other classes" do
|
93
|
-
@post_class.identity_map_off
|
94
|
-
@person_class.identity_map_on
|
95
|
-
@post_class.identity_map_status.should_not == @person_class.identity_map_status
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
should "default identity map to hash" do
|
100
|
-
Doc { plugin MongoMapper::Plugins::IdentityMap }.identity_map.should == {}
|
101
|
-
end
|
102
|
-
|
103
|
-
should "add key to map when saved" do
|
104
|
-
person = @person_class.new
|
105
|
-
assert_not_in_map(person)
|
106
|
-
person.save.should be_true
|
107
|
-
assert_in_map(person)
|
108
|
-
end
|
109
|
-
|
110
|
-
should "allow saving with options" do
|
111
|
-
person = @person_class.new
|
112
|
-
assert_nothing_raised do
|
113
|
-
person.save(:validate => false).should be_true
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
should "remove key from map when deleted" do
|
118
|
-
person = @person_class.create(:name => 'Fred')
|
119
|
-
assert_in_map(person)
|
120
|
-
person.destroy
|
121
|
-
assert_not_in_map(person)
|
122
|
-
end
|
123
|
-
|
124
|
-
context "reload" do
|
125
|
-
setup do
|
126
|
-
@person = @person_class.create(:name => 'Fred')
|
127
|
-
end
|
128
|
-
|
129
|
-
should "remove object from identity and re-query" do
|
130
|
-
assert_in_map(@person)
|
131
|
-
expects_one_query
|
132
|
-
@person.reload
|
133
|
-
end
|
134
|
-
|
135
|
-
should "add object back into map" do
|
136
|
-
assert_in_map(@person)
|
137
|
-
before_reload = @person
|
138
|
-
@person.reload.should equal(before_reload)
|
139
|
-
assert_in_map(@person)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context "#load" do
|
144
|
-
setup do
|
145
|
-
@id = BSON::ObjectID.new
|
146
|
-
end
|
147
|
-
|
148
|
-
should "add document to map" do
|
149
|
-
loaded = @person_class.load({'_id' => @id, 'name' => 'Frank'})
|
150
|
-
assert_in_map(loaded)
|
151
|
-
end
|
152
|
-
|
153
|
-
should "return document if already in map" do
|
154
|
-
first_load = @person_class.load({'_id' => @id, 'name' => 'Frank'})
|
155
|
-
@person_class.identity_map.expects(:[]=).never
|
156
|
-
second_load = @person_class.load({'_id' => @id, 'name' => 'Frank'})
|
157
|
-
first_load.should equal(second_load)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
context "#find (with one id)" do
|
162
|
-
context "for object not in map" do
|
163
|
-
setup do
|
164
|
-
@person = @person_class.create(:name => 'Fred')
|
165
|
-
@person_class.identity_map.clear
|
166
|
-
end
|
167
|
-
|
168
|
-
should "query the database" do
|
169
|
-
expects_one_query
|
170
|
-
@person_class.find(@person.id)
|
171
|
-
end
|
172
|
-
|
173
|
-
should "add object to map" do
|
174
|
-
assert_not_in_map(@person)
|
175
|
-
found_person = @person_class.find(@person.id)
|
176
|
-
assert_in_map(found_person)
|
177
|
-
end
|
178
|
-
|
179
|
-
should "return nil if not found " do
|
180
|
-
@person_class.find(1234).should be_nil
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
context "for object in map" do
|
185
|
-
setup do
|
186
|
-
@person = @person_class.create(:name => 'Fred')
|
187
|
-
end
|
188
|
-
|
189
|
-
should "not query database" do
|
190
|
-
expect_no_queries
|
191
|
-
@person_class.find(@person.id)
|
192
|
-
end
|
193
|
-
|
194
|
-
should "return exact object" do
|
195
|
-
assert_in_map(@person)
|
196
|
-
found_person = @person_class.find(@person.id)
|
197
|
-
found_person.should equal(@person)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
context "#find (with one id and options)" do
|
203
|
-
setup do
|
204
|
-
@person = @person_class.create(:name => 'Fred')
|
205
|
-
@post1 = @person.posts.create(:title => 'I Love Mongo')
|
206
|
-
@post2 = @person.posts.create(:title => 'Migrations Suck!')
|
207
|
-
end
|
208
|
-
|
209
|
-
# There are times when even though the id matches, other criteria doesn't
|
210
|
-
# so we need to do the query to ensure that when criteria doesn't match
|
211
|
-
# the document is in fact not found.
|
212
|
-
#
|
213
|
-
# I'm open to not making this query if someone can figure out reliable
|
214
|
-
# way to check if document matches criteria without querying.
|
215
|
-
should "query the database" do
|
216
|
-
assert_in_map(@post1)
|
217
|
-
expects_one_query
|
218
|
-
@person.posts.find(@post1.id)
|
219
|
-
end
|
220
|
-
|
221
|
-
should "return exact object" do
|
222
|
-
assert_in_map(@post1)
|
223
|
-
@person.posts.find(@post1.id)
|
224
|
-
assert_in_map(@post1)
|
225
|
-
end
|
226
|
-
|
227
|
-
should "return nil if not found " do
|
228
|
-
@person.posts.find(1234).should be_nil
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
context "#find (with multiple ids)" do
|
233
|
-
should "add all documents to map" do
|
234
|
-
person1 = @person_class.create(:name => 'Fred')
|
235
|
-
person2 = @person_class.create(:name => 'Bill')
|
236
|
-
person3 = @person_class.create(:name => 'Jesse')
|
237
|
-
@person_class.identity_map.clear
|
238
|
-
|
239
|
-
people = @person_class.find(person1.id, person2.id, person3.id)
|
240
|
-
assert_in_map(people)
|
241
|
-
end
|
242
|
-
|
243
|
-
should "add missing documents to map and return existing ones" do
|
244
|
-
person1 = @person_class.create(:name => 'Fred')
|
245
|
-
@person_class.identity_map.clear
|
246
|
-
person2 = @person_class.create(:name => 'Bill')
|
247
|
-
person3 = @person_class.create(:name => 'Jesse')
|
248
|
-
|
249
|
-
assert_not_in_map(person1)
|
250
|
-
assert_in_map(person2, person3)
|
251
|
-
|
252
|
-
people = @person_class.find(person1.id, person2.id, person3.id)
|
253
|
-
assert_in_map(people.first) # making sure one that wasn't mapped now is
|
254
|
-
assert_in_map(person2, person3)
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
context "#first" do
|
259
|
-
context "for object not in map" do
|
260
|
-
setup do
|
261
|
-
@person = @person_class.create(:name => 'Fred')
|
262
|
-
@person_class.identity_map.clear
|
263
|
-
end
|
264
|
-
|
265
|
-
should "query the database" do
|
266
|
-
expects_one_query
|
267
|
-
@person_class.first(:_id => @person.id)
|
268
|
-
end
|
269
|
-
|
270
|
-
should "add object to map" do
|
271
|
-
assert_not_in_map(@person)
|
272
|
-
found_person = @person_class.first(:_id => @person.id)
|
273
|
-
assert_in_map(found_person)
|
274
|
-
end
|
275
|
-
|
276
|
-
should "return nil if not found" do
|
277
|
-
@person_class.first(:name => 'Bill').should be_nil
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
context "for object in map" do
|
282
|
-
setup do
|
283
|
-
@person = @person_class.create(:name => 'Fred')
|
284
|
-
end
|
285
|
-
|
286
|
-
should "not query database" do
|
287
|
-
expect_no_queries
|
288
|
-
@person_class.first(:_id => @person.id)
|
289
|
-
end
|
290
|
-
|
291
|
-
should "return exact object" do
|
292
|
-
assert_in_map(@person)
|
293
|
-
found_person = @person_class.first(:_id => @person.id)
|
294
|
-
found_person.should equal(@person)
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
context "#all" do
|
300
|
-
should "add all documents to map" do
|
301
|
-
person1 = @person_class.create(:name => 'Fred')
|
302
|
-
person2 = @person_class.create(:name => 'Bill')
|
303
|
-
person3 = @person_class.create(:name => 'Jesse')
|
304
|
-
@person_class.identity_map.clear
|
305
|
-
|
306
|
-
people = @person_class.all(:_id => [person1.id, person2.id, person3.id])
|
307
|
-
assert_in_map(people)
|
308
|
-
end
|
309
|
-
|
310
|
-
should "add missing documents to map and return existing ones" do
|
311
|
-
person1 = @person_class.create(:name => 'Fred')
|
312
|
-
@person_class.identity_map.clear
|
313
|
-
person2 = @person_class.create(:name => 'Bill')
|
314
|
-
person3 = @person_class.create(:name => 'Jesse')
|
315
|
-
|
316
|
-
assert_not_in_map(person1)
|
317
|
-
assert_in_map(person2, person3)
|
318
|
-
|
319
|
-
people = @person_class.all(:_id => [person1.id, person2.id, person3.id])
|
320
|
-
# people.first is making sure one that wasn't mapped now is
|
321
|
-
assert_in_map(people.first, person2, person3)
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
context "#find_by_id" do
|
326
|
-
setup do
|
327
|
-
@person = @person_class.create(:name => 'Bill')
|
328
|
-
end
|
329
|
-
|
330
|
-
should "return nil for document id not found in collection" do
|
331
|
-
assert_in_map(@person)
|
332
|
-
@person_class.find_by_id(1234).should be_nil
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
context "querying and selecting certain fields" do
|
337
|
-
setup do
|
338
|
-
@person = @person_class.create(:name => 'Bill')
|
339
|
-
@person_class.identity_map.clear
|
340
|
-
end
|
341
|
-
|
342
|
-
should "not add to map" do
|
343
|
-
assert_not_in_map(@person)
|
344
|
-
@person_class.first(:_id => @person.id, :select => 'name').should == @person
|
345
|
-
@person_class.first(:_id => @person.id, 'fields' => ['name']).should == @person
|
346
|
-
@person_class.last(:_id => @person.id, :select => 'name', :order => 'name').should == @person
|
347
|
-
@person_class.find(@person.id, :select => 'name').should == @person
|
348
|
-
@person_class.all(:_id => @person.id, :select => 'name').should == [@person]
|
349
|
-
assert_not_in_map(@person)
|
350
|
-
end
|
351
|
-
|
352
|
-
should "return nil if not found" do
|
353
|
-
@person_class.find(1234, :select => 'name').should be_nil
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
context "single collection inherited models" do
|
358
|
-
setup do
|
359
|
-
class ::Item
|
360
|
-
include MongoMapper::Document
|
361
|
-
plugin MongoMapper::Plugins::IdentityMap
|
362
|
-
|
363
|
-
key :title, String
|
364
|
-
key :parent_id, ObjectId
|
365
|
-
|
366
|
-
belongs_to :parent, :class_name => 'Item'
|
367
|
-
one :blog, :class_name => 'Blog', :foreign_key => 'parent_id'
|
368
|
-
end
|
369
|
-
Item.collection.remove
|
370
|
-
|
371
|
-
class ::Blog < ::Item; end
|
372
|
-
|
373
|
-
class ::BlogPost < ::Item
|
374
|
-
key :blog_id, ObjectId
|
375
|
-
belongs_to :blog
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
teardown do
|
380
|
-
Object.send :remove_const, 'Item' if defined?(::Item)
|
381
|
-
Object.send :remove_const, 'Blog' if defined?(::Blog)
|
382
|
-
Object.send :remove_const, 'BlogPost' if defined?(::BlogPost)
|
383
|
-
end
|
384
|
-
|
385
|
-
should "share the same identity map" do
|
386
|
-
blog = Blog.create(:title => 'Jill')
|
387
|
-
assert_in_map(blog)
|
388
|
-
Item.identity_map.should equal(Blog.identity_map)
|
389
|
-
end
|
390
|
-
|
391
|
-
should "not query when finding by _id and _type" do
|
392
|
-
blog = Blog.create(:title => 'Blog')
|
393
|
-
post = BlogPost.create(:title => 'Mongo Rocks', :blog => blog)
|
394
|
-
Item.identity_map.clear
|
395
|
-
|
396
|
-
blog = Item.find(blog.id)
|
397
|
-
post = Item.find(post.id)
|
398
|
-
assert_in_map(blog, post)
|
399
|
-
|
400
|
-
expect_no_queries
|
401
|
-
post.blog
|
402
|
-
Blog.find(blog.id)
|
403
|
-
end
|
404
|
-
|
405
|
-
should "load from map when using parent collection inherited class" do
|
406
|
-
blog = Blog.create(:title => 'Jill')
|
407
|
-
Item.find(blog.id).should equal(blog)
|
408
|
-
end
|
409
|
-
|
410
|
-
should "work correctly with belongs to proxy" do
|
411
|
-
root = Item.create(:title => 'Root')
|
412
|
-
assert_in_map(root)
|
413
|
-
|
414
|
-
blog = Blog.create(:title => 'Jill', :parent => root)
|
415
|
-
assert_in_map(blog)
|
416
|
-
root.should equal(blog.parent)
|
417
|
-
end
|
418
|
-
|
419
|
-
should "work correctly with one proxy" do
|
420
|
-
blog = Blog.create(:title => 'Jill')
|
421
|
-
assert_in_map(blog)
|
422
|
-
|
423
|
-
root = Item.create(:title => 'Root', :blog => blog)
|
424
|
-
assert_in_map(root)
|
425
|
-
root.blog.should equal(blog)
|
426
|
-
end
|
427
|
-
|
428
|
-
should "work correctly with one proxy create" do
|
429
|
-
root = Item.create(:title => 'Root')
|
430
|
-
blog = root.blog.create(:title => 'Blog')
|
431
|
-
blog.parent.should equal(root)
|
432
|
-
end
|
433
|
-
end
|
434
|
-
|
435
|
-
context "without identity map" do
|
436
|
-
should "not add to map on save" do
|
437
|
-
@post_class.without_identity_map do
|
438
|
-
post = @post_class.create(:title => 'Bill')
|
439
|
-
assert_not_in_map(post)
|
440
|
-
end
|
441
|
-
end
|
442
|
-
|
443
|
-
should "not remove from map on delete" do
|
444
|
-
post = @post_class.create(:title => 'Bill')
|
445
|
-
assert_in_map(post)
|
446
|
-
|
447
|
-
@post_class.without_identity_map do
|
448
|
-
post.destroy
|
449
|
-
end
|
450
|
-
|
451
|
-
assert_in_map(post)
|
452
|
-
end
|
453
|
-
|
454
|
-
should "not add to map when loading" do
|
455
|
-
@post_class.without_identity_map do
|
456
|
-
post = @post_class.load({'_id' => BSON::ObjectID.new, 'title' => 'Awesome!'})
|
457
|
-
assert_not_in_map(post)
|
458
|
-
end
|
459
|
-
end
|
460
|
-
|
461
|
-
should "not load from map when loading" do
|
462
|
-
post = @post_class.create(:title => 'Awesome!')
|
463
|
-
|
464
|
-
@post_class.without_identity_map do
|
465
|
-
loaded = @post_class.load('_id' => post._id, 'title' => 'Awesome!')
|
466
|
-
loaded.should_not equal(post)
|
467
|
-
end
|
468
|
-
end
|
469
|
-
|
470
|
-
context "all" do
|
471
|
-
should "not add to map" do
|
472
|
-
@post_class.without_identity_map do
|
473
|
-
post1 = @post_class.create(:title => 'Foo')
|
474
|
-
post2 = @post_class.create(:title => 'Bar')
|
475
|
-
@post_class.identity_map.clear
|
476
|
-
|
477
|
-
assert_not_in_map(@post_class.all)
|
478
|
-
end
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
context "first" do
|
483
|
-
should "not add to map" do
|
484
|
-
@post_class.without_identity_map do
|
485
|
-
post1 = @post_class.create(:title => 'Foo')
|
486
|
-
post2 = @post_class.create(:title => 'Bar')
|
487
|
-
@post_class.identity_map.clear
|
488
|
-
|
489
|
-
assert_not_in_map(@post_class.first)
|
490
|
-
end
|
491
|
-
end
|
492
|
-
end
|
493
|
-
|
494
|
-
context "last" do
|
495
|
-
should "not add to map" do
|
496
|
-
@post_class.without_identity_map do
|
497
|
-
post1 = @post_class.create(:title => 'Foo')
|
498
|
-
post2 = @post_class.create(:title => 'Bar')
|
499
|
-
@post_class.identity_map.clear
|
500
|
-
|
501
|
-
assert_not_in_map(@post_class.last(:order => 'title'))
|
502
|
-
end
|
503
|
-
end
|
504
|
-
end
|
505
|
-
end
|
506
|
-
end
|
507
|
-
end
|