mongo_mapper-unstable 2010.3.8 → 2010.06.23
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -8
- data/bin/mmconsole +1 -1
- data/examples/keys.rb +37 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +11 -329
- data/lib/mongo_mapper/embedded_document.rb +9 -38
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
- data/lib/mongo_mapper/plugins/associations.rb +14 -22
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
- data/lib/mongo_mapper/plugins/clone.rb +10 -4
- data/lib/mongo_mapper/plugins/descendants.rb +3 -2
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -10
- data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/keys.rb +85 -110
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
- data/lib/mongo_mapper/plugins/pagination.rb +5 -15
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +9 -1
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/rails.rb +6 -1
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +9 -5
- data/lib/mongo_mapper/plugins.rb +1 -20
- data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/mongo_mapper.rb +71 -128
- data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
- data/test/functional/associations/test_in_array_proxy.rb +7 -9
- data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
- data/test/functional/associations/test_many_documents_proxy.rb +186 -64
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
- data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
- data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +70 -49
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +11 -11
- data/test/functional/test_binary.rb +5 -5
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_dirty.rb +16 -16
- data/test/functional/test_document.rb +12 -924
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +88 -8
- data/test/functional/test_identity_map.rb +41 -43
- data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
- data/test/functional/test_logger.rb +1 -1
- data/test/functional/test_modifiers.rb +275 -181
- data/test/functional/test_pagination.rb +13 -15
- data/test/functional/test_protected.rb +25 -11
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +11 -11
- data/test/functional/test_timestamps.rb +0 -2
- data/test/functional/test_userstamps.rb +0 -1
- data/test/functional/test_validations.rb +44 -31
- data/test/models.rb +18 -17
- data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
- data/test/test_helper.rb +59 -16
- data/test/unit/associations/test_base.rb +47 -42
- data/test/unit/associations/test_proxy.rb +15 -15
- data/test/unit/serializers/test_json_serializer.rb +29 -29
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +3 -3
- data/test/unit/test_document.rb +49 -67
- data/test/unit/test_dynamic_finder.rb +53 -51
- data/test/unit/test_embedded_document.rb +19 -38
- data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +29 -147
- data/test/unit/test_mongo_mapper.rb +3 -48
- data/test/unit/test_pagination.rb +1 -150
- data/test/unit/test_rails.rb +77 -19
- data/test/unit/test_rails_compatibility.rb +12 -12
- data/test/unit/test_serialization.rb +5 -5
- data/test/unit/test_time_zones.rb +9 -9
- data/test/unit/test_validations.rb +46 -46
- metadata +157 -155
- data/.gitignore +0 -10
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
- data/lib/mongo_mapper/query.rb +0 -130
- data/lib/mongo_mapper/support.rb +0 -215
- data/mongo_mapper.gemspec +0 -196
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/support/custom_matchers.rb +0 -55
- data/test/support/timing.rb +0 -16
- data/test/unit/test_query.rb +0 -340
data/test/unit/test_document.rb
CHANGED
@@ -6,11 +6,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
6
6
|
setup do
|
7
7
|
@document = Doc()
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
should "return false for embeddable" do
|
11
11
|
Doc().embeddable?.should be_false
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
should "have logger method" do
|
15
15
|
@document.logger.should == MongoMapper.logger
|
16
16
|
@document.logger.should be_instance_of(Logger)
|
@@ -39,34 +39,33 @@ class DocumentTest < Test::Unit::TestCase
|
|
39
39
|
another_document = Doc()
|
40
40
|
another_document.database.should == MongoMapper.database
|
41
41
|
end
|
42
|
-
|
43
|
-
should "default collection name to class name tableized" do
|
44
|
-
class ::Item
|
45
|
-
include MongoMapper::Document
|
46
|
-
end
|
47
|
-
|
48
|
-
Item.collection.should be_instance_of(Mongo::Collection)
|
49
|
-
Item.collection.name.should == 'items'
|
50
|
-
end
|
51
|
-
|
52
|
-
should "default collection name of namespaced class to tableized with dot separation" do
|
53
|
-
module ::BloggyPoo
|
54
|
-
class Post
|
55
|
-
include MongoMapper::Document
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
BloggyPoo::Post.collection.should be_instance_of(Mongo::Collection)
|
60
|
-
BloggyPoo::Post.collection.name.should == 'bloggy_poo.posts'
|
61
|
-
end
|
62
42
|
|
63
43
|
should "allow setting the collection name" do
|
64
44
|
@document.set_collection_name('foobar')
|
65
|
-
@document.collection.should be_instance_of(Mongo::Collection)
|
66
45
|
@document.collection.name.should == 'foobar'
|
67
46
|
end
|
47
|
+
|
48
|
+
context ".collection" do
|
49
|
+
should "default collection name to class name tableized" do
|
50
|
+
class ::Item
|
51
|
+
include MongoMapper::Document
|
52
|
+
end.collection.name.should == 'items'
|
53
|
+
end
|
54
|
+
|
55
|
+
should "default collection name of namespaced class to tableized with dot separation" do
|
56
|
+
module ::BloggyPoo
|
57
|
+
class Post
|
58
|
+
include MongoMapper::Document
|
59
|
+
end.collection.name.should == 'bloggy_poo.posts'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
should "be an instance of a Mongo::Collection" do
|
64
|
+
@document.collection.should be_instance_of(Mongo::Collection)
|
65
|
+
end
|
66
|
+
end
|
68
67
|
end # Document class
|
69
|
-
|
68
|
+
|
70
69
|
context "Documents that inherit from other documents" do
|
71
70
|
should "default collection name to inherited class" do
|
72
71
|
Message.collection_name.should == 'messages'
|
@@ -82,10 +81,10 @@ class DocumentTest < Test::Unit::TestCase
|
|
82
81
|
Chat.associations.keys.should include("room")
|
83
82
|
end
|
84
83
|
end
|
85
|
-
|
84
|
+
|
86
85
|
context "descendants" do
|
87
|
-
should "default to
|
88
|
-
Enter.descendants.should
|
86
|
+
should "default to an empty array" do
|
87
|
+
Enter.descendants.should == []
|
89
88
|
end
|
90
89
|
|
91
90
|
should "be recorded" do
|
@@ -100,17 +99,15 @@ class DocumentTest < Test::Unit::TestCase
|
|
100
99
|
key :age, Integer
|
101
100
|
end
|
102
101
|
end
|
103
|
-
|
104
|
-
should "
|
105
|
-
@document.new.
|
102
|
+
|
103
|
+
should "respond to cache_key" do
|
104
|
+
@document.new.should respond_to(:cache_key)
|
106
105
|
end
|
107
|
-
|
108
|
-
should "
|
109
|
-
|
110
|
-
doc.to_param.should == doc.id.to_s
|
111
|
-
doc.to_param.should be_instance_of(String)
|
106
|
+
|
107
|
+
should "create id during initialization" do
|
108
|
+
@document.new._id.should be_instance_of(BSON::ObjectID)
|
112
109
|
end
|
113
|
-
|
110
|
+
|
114
111
|
should "have access to logger" do
|
115
112
|
doc = @document.new
|
116
113
|
doc.logger.should == @document.logger
|
@@ -128,23 +125,23 @@ class DocumentTest < Test::Unit::TestCase
|
|
128
125
|
@document.new.active.should be_true
|
129
126
|
@document.new(:active => false).active.should be_false
|
130
127
|
end
|
131
|
-
|
128
|
+
|
132
129
|
should "use default values if defined even when custom data type" do
|
133
130
|
@document.key :window, WindowSize, :default => WindowSize.new(600, 480)
|
134
|
-
|
131
|
+
|
135
132
|
doc = @document.new
|
136
133
|
doc.window.should == WindowSize.new(600, 480)
|
137
134
|
end
|
138
135
|
|
139
136
|
context "root document" do
|
140
|
-
should "set self to the root document on embedded documents" do
|
137
|
+
should "set self to the root document on embedded documents" do
|
141
138
|
klass = Doc()
|
142
139
|
pets = EDoc()
|
143
|
-
|
140
|
+
|
144
141
|
klass.many :pets, :class => pets
|
145
|
-
|
142
|
+
|
146
143
|
doc = klass.new(:pets => [{}])
|
147
|
-
doc.pets.first._root_document.should == doc
|
144
|
+
doc.pets.first._root_document.should == doc
|
148
145
|
end
|
149
146
|
end
|
150
147
|
|
@@ -152,7 +149,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
152
149
|
should "be true if no id" do
|
153
150
|
@document.new.new?.should be_true
|
154
151
|
end
|
155
|
-
|
152
|
+
|
156
153
|
should "be true if id but using custom id and not saved yet" do
|
157
154
|
@document.key :_id, String
|
158
155
|
doc = @document.new
|
@@ -161,21 +158,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
161
158
|
end
|
162
159
|
end
|
163
160
|
|
164
|
-
context "clone" do
|
165
|
-
should "be new" do
|
166
|
-
doc = @document.create(:name => "foo", :age => 27)
|
167
|
-
clone = doc.clone
|
168
|
-
clone.should be_new
|
169
|
-
end
|
170
|
-
|
171
|
-
should "copy the attributes" do
|
172
|
-
doc = @document.create(:name => "foo", :age => 27)
|
173
|
-
clone = doc.clone
|
174
|
-
clone.name.should == "foo"
|
175
|
-
clone.age.should == 27
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
161
|
should "call inspect on the document's attributes instead of to_s when inspecting the document" do
|
180
162
|
doc = @document.new(:animals => %w(dog cat))
|
181
163
|
doc.inspect.should include(%(animals: ["dog", "cat"]))
|
@@ -183,43 +165,43 @@ class DocumentTest < Test::Unit::TestCase
|
|
183
165
|
|
184
166
|
context "equality" do
|
185
167
|
setup do
|
186
|
-
@oid =
|
168
|
+
@oid = BSON::ObjectID.new
|
187
169
|
end
|
188
|
-
|
170
|
+
|
189
171
|
should "delegate hash to _id" do
|
190
172
|
doc = @document.new
|
191
173
|
doc.hash.should == doc._id.hash
|
192
174
|
end
|
193
|
-
|
175
|
+
|
194
176
|
should "delegate eql to ==" do
|
195
177
|
doc = @document.new
|
196
178
|
other = @document.new
|
197
179
|
doc.eql?(other).should == (doc == other)
|
198
180
|
doc.eql?(doc).should == (doc == doc)
|
199
181
|
end
|
200
|
-
|
182
|
+
|
201
183
|
should "know if same object as another" do
|
202
184
|
doc = @document.new
|
203
185
|
doc.should equal(doc)
|
204
186
|
doc.should_not equal(@document.new)
|
205
187
|
end
|
206
|
-
|
188
|
+
|
207
189
|
should "allow set operations on array of documents" do
|
208
190
|
@document.key :parent_id, ObjectId
|
209
191
|
@document.belongs_to :parent, :class => @document
|
210
|
-
|
192
|
+
|
211
193
|
parent = @document.create
|
212
194
|
child = @document.create(:parent => parent)
|
213
|
-
|
195
|
+
|
214
196
|
([child.parent] & [parent]).should == [parent]
|
215
197
|
end
|
216
|
-
|
198
|
+
|
217
199
|
should "be equal if id and class are the same" do
|
218
200
|
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be(true)
|
219
201
|
end
|
220
202
|
|
221
203
|
should "not be equal if class same but id different" do
|
222
|
-
(@document.new('_id' => @oid) == @document.new('_id' =>
|
204
|
+
(@document.new('_id' => @oid) == @document.new('_id' => BSON::ObjectID.new)).should be(false)
|
223
205
|
end
|
224
206
|
|
225
207
|
should "not be equal if id same but class different" do
|
@@ -1,123 +1,125 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class DynamicFinderTest < Test::Unit::TestCase
|
4
|
+
include MongoMapper::Plugins::DynamicQuerying
|
5
|
+
|
4
6
|
should "initialize with method" do
|
5
|
-
finder =
|
7
|
+
finder = DynamicFinder.new(:foobar)
|
6
8
|
finder.method.should == :foobar
|
7
9
|
end
|
8
|
-
|
10
|
+
|
9
11
|
context "found?" do
|
10
12
|
should "be true for find_by" do
|
11
|
-
|
13
|
+
DynamicFinder.new(:find_by_foo).found?.should be_true
|
12
14
|
end
|
13
|
-
|
15
|
+
|
14
16
|
should "be true for find_by with !" do
|
15
|
-
|
17
|
+
DynamicFinder.new(:find_by_foo!).found?.should be_true
|
16
18
|
end
|
17
|
-
|
19
|
+
|
18
20
|
should "be true for find_all_by" do
|
19
|
-
|
21
|
+
DynamicFinder.new(:find_all_by_foo).found?.should be_true
|
20
22
|
end
|
21
|
-
|
23
|
+
|
22
24
|
should "be true for find_or_initialize_by" do
|
23
|
-
|
25
|
+
DynamicFinder.new(:find_or_initialize_by_foo).found?.should be_true
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
should "be true for find_or_create_by" do
|
27
|
-
|
29
|
+
DynamicFinder.new(:find_or_create_by_foo).found?.should be_true
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
should "be false for anything else" do
|
31
33
|
[:foobar, :bazwick].each do |method|
|
32
|
-
|
34
|
+
DynamicFinder.new(method).found?.should be_false
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
36
|
-
|
38
|
+
|
37
39
|
context "find_all_by" do
|
38
40
|
should "parse one attribute" do
|
39
|
-
|
41
|
+
DynamicFinder.new(:find_all_by_foo).attributes.should == %w(foo)
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
should "parse multiple attributes" do
|
43
|
-
|
44
|
-
|
45
|
+
DynamicFinder.new(:find_all_by_foo_and_bar).attributes.should == %w(foo bar)
|
46
|
+
DynamicFinder.new(:find_all_by_foo_and_bar_and_baz).attributes.should == %w(foo bar baz)
|
45
47
|
end
|
46
|
-
|
48
|
+
|
47
49
|
should "set finder to :all" do
|
48
|
-
|
50
|
+
DynamicFinder.new(:find_all_by_foo_and_bar).finder.should == :all
|
49
51
|
end
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
context "find_by" do
|
53
55
|
should "parse one attribute" do
|
54
|
-
|
56
|
+
DynamicFinder.new(:find_by_foo).attributes.should == %w(foo)
|
55
57
|
end
|
56
|
-
|
58
|
+
|
57
59
|
should "parse multiple attributes" do
|
58
|
-
|
60
|
+
DynamicFinder.new(:find_by_foo_and_bar).attributes.should == %w(foo bar)
|
59
61
|
end
|
60
|
-
|
62
|
+
|
61
63
|
should "set finder to :first" do
|
62
|
-
|
64
|
+
DynamicFinder.new(:find_by_foo).finder.should == :first
|
63
65
|
end
|
64
|
-
|
66
|
+
|
65
67
|
should "set bang to false" do
|
66
|
-
|
68
|
+
DynamicFinder.new(:find_by_foo).bang.should be_false
|
67
69
|
end
|
68
70
|
end
|
69
|
-
|
71
|
+
|
70
72
|
context "find_by with !" do
|
71
73
|
should "parse one attribute" do
|
72
|
-
|
74
|
+
DynamicFinder.new(:find_by_foo!).attributes.should == %w(foo)
|
73
75
|
end
|
74
|
-
|
76
|
+
|
75
77
|
should "parse multiple attributes" do
|
76
|
-
|
78
|
+
DynamicFinder.new(:find_by_foo_and_bar!).attributes.should == %w(foo bar)
|
77
79
|
end
|
78
|
-
|
80
|
+
|
79
81
|
should "set finder to :first" do
|
80
|
-
|
82
|
+
DynamicFinder.new(:find_by_foo!).finder.should == :first
|
81
83
|
end
|
82
|
-
|
84
|
+
|
83
85
|
should "set bang to true" do
|
84
|
-
|
86
|
+
DynamicFinder.new(:find_by_foo!).bang.should be_true
|
85
87
|
end
|
86
88
|
end
|
87
|
-
|
89
|
+
|
88
90
|
context "find_or_initialize_by" do
|
89
91
|
should "parse one attribute" do
|
90
|
-
|
92
|
+
DynamicFinder.new(:find_or_initialize_by_foo).attributes.should == %w(foo)
|
91
93
|
end
|
92
|
-
|
94
|
+
|
93
95
|
should "parse multiple attributes" do
|
94
|
-
|
96
|
+
DynamicFinder.new(:find_or_initialize_by_foo_and_bar).attributes.should == %w(foo bar)
|
95
97
|
end
|
96
|
-
|
98
|
+
|
97
99
|
should "set finder to :first" do
|
98
|
-
|
100
|
+
DynamicFinder.new(:find_or_initialize_by_foo).finder.should == :first
|
99
101
|
end
|
100
|
-
|
102
|
+
|
101
103
|
should "set instantiator to new" do
|
102
|
-
|
104
|
+
DynamicFinder.new(:find_or_initialize_by_foo).instantiator.should == :new
|
103
105
|
end
|
104
106
|
end
|
105
107
|
|
106
108
|
context "find_or_create_by" do
|
107
109
|
should "parse one attribute" do
|
108
|
-
|
110
|
+
DynamicFinder.new(:find_or_create_by_foo).attributes.should == %w(foo)
|
109
111
|
end
|
110
|
-
|
112
|
+
|
111
113
|
should "parse multiple attributes" do
|
112
|
-
|
114
|
+
DynamicFinder.new(:find_or_create_by_foo_and_bar).attributes.should == %w(foo bar)
|
113
115
|
end
|
114
|
-
|
116
|
+
|
115
117
|
should "set finder to :first" do
|
116
|
-
|
118
|
+
DynamicFinder.new(:find_or_create_by_foo).finder.should == :first
|
117
119
|
end
|
118
|
-
|
120
|
+
|
119
121
|
should "set instantiator to new" do
|
120
|
-
|
122
|
+
DynamicFinder.new(:find_or_create_by_foo).instantiator.should == :create
|
121
123
|
end
|
122
124
|
end
|
123
125
|
end
|
@@ -11,7 +11,7 @@ module KeyOverride
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class EmbeddedDocumentTest < Test::Unit::TestCase
|
14
|
-
context "" do
|
14
|
+
context "EmbeddedDocuments" do
|
15
15
|
setup do
|
16
16
|
class ::Grandparent
|
17
17
|
include MongoMapper::EmbeddedDocument
|
@@ -179,17 +179,17 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
179
179
|
|
180
180
|
context "keys" do
|
181
181
|
should "be inherited" do
|
182
|
-
Grandparent.keys.keys.sort.should == ['_id', 'grandparent']
|
183
|
-
Parent.keys.keys.sort.should == ['_id', 'grandparent', 'parent']
|
184
|
-
Child.keys.keys.sort.should == ['_id', 'child', 'grandparent', 'parent']
|
182
|
+
Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
|
183
|
+
Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
|
184
|
+
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
|
185
185
|
end
|
186
186
|
|
187
187
|
should "propogate to descendants if key added after class definition" do
|
188
|
-
Grandparent.key :
|
188
|
+
Grandparent.key :foo, String
|
189
189
|
|
190
|
-
Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
|
191
|
-
Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
|
192
|
-
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
|
190
|
+
Grandparent.keys.keys.sort.should == ['_id', '_type', 'foo', 'grandparent']
|
191
|
+
Parent.keys.keys.sort.should == ['_id', '_type', 'foo', 'grandparent', 'parent']
|
192
|
+
Child.keys.keys.sort.should == ['_id', '_type', 'child', 'foo', 'grandparent', 'parent']
|
193
193
|
end
|
194
194
|
|
195
195
|
should "not add anonymous objects to the ancestor tree" do
|
@@ -202,8 +202,8 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
202
202
|
end
|
203
203
|
|
204
204
|
context "descendants" do
|
205
|
-
should "default to
|
206
|
-
Child.descendants.should
|
205
|
+
should "default to an empty array" do
|
206
|
+
Child.descendants.should == []
|
207
207
|
end
|
208
208
|
|
209
209
|
should "be recorded" do
|
@@ -221,10 +221,8 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
224
|
-
should "
|
225
|
-
|
226
|
-
doc.to_param.should == doc.id.to_s
|
227
|
-
doc.to_param.should be_instance_of(String)
|
224
|
+
should "respond to cache_key" do
|
225
|
+
@document.new.should respond_to(:cache_key)
|
228
226
|
end
|
229
227
|
|
230
228
|
should "have access to class logger" do
|
@@ -238,17 +236,17 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
238
236
|
end
|
239
237
|
|
240
238
|
should "create id during initialization" do
|
241
|
-
@document.new._id.should be_instance_of(
|
239
|
+
@document.new._id.should be_instance_of(BSON::ObjectID)
|
242
240
|
end
|
243
241
|
|
244
242
|
should "have id method returns _id" do
|
245
|
-
id =
|
243
|
+
id = BSON::ObjectID.new
|
246
244
|
doc = @document.new(:_id => id)
|
247
245
|
doc.id.should == id
|
248
246
|
end
|
249
247
|
|
250
248
|
should "convert string object id to mongo object id when assigning id with _id object id type" do
|
251
|
-
id =
|
249
|
+
id = BSON::ObjectID.new
|
252
250
|
doc = @document.new(:id => id.to_s)
|
253
251
|
doc._id.should == id
|
254
252
|
doc.id.should == id
|
@@ -308,8 +306,8 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
308
306
|
@klass.new._type.should == 'FooBar'
|
309
307
|
end
|
310
308
|
|
311
|
-
should "
|
312
|
-
@klass.new(:_type => 'Foo')._type.should == '
|
309
|
+
should "ignore _type attribute and always use class" do
|
310
|
+
@klass.new(:_type => 'Foo')._type.should == 'FooBar'
|
313
311
|
end
|
314
312
|
end
|
315
313
|
|
@@ -382,23 +380,6 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
382
380
|
end
|
383
381
|
end
|
384
382
|
|
385
|
-
context "clone" do
|
386
|
-
should "regenerate the id" do
|
387
|
-
doc = @document.new(:name => "foo", :age => 27)
|
388
|
-
doc_id = doc.id
|
389
|
-
clone = doc.clone
|
390
|
-
clone_id = clone.id
|
391
|
-
clone_id.should_not == doc_id
|
392
|
-
end
|
393
|
-
|
394
|
-
should "copy the attributes" do
|
395
|
-
doc = @document.new(:name => "foo", :age => 27)
|
396
|
-
clone = doc.clone
|
397
|
-
clone.name.should == "foo"
|
398
|
-
clone.age.should == 27
|
399
|
-
end
|
400
|
-
end
|
401
|
-
|
402
383
|
context "key shorcut access" do
|
403
384
|
context "[]" do
|
404
385
|
should "work when key found" do
|
@@ -574,7 +555,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
574
555
|
|
575
556
|
context "equality" do
|
576
557
|
setup do
|
577
|
-
@oid =
|
558
|
+
@oid = BSON::ObjectID.new
|
578
559
|
end
|
579
560
|
|
580
561
|
should "delegate hash to _id" do
|
@@ -605,7 +586,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
605
586
|
end
|
606
587
|
|
607
588
|
should "not be equal if class same but id different" do
|
608
|
-
(@document.new('_id' => @oid) == @document.new('_id' =>
|
589
|
+
(@document.new('_id' => @oid) == @document.new('_id' => BSON::ObjectID.new)).should be_false
|
609
590
|
end
|
610
591
|
|
611
592
|
should "not be equal if id same but class different" do
|