jmonteiro-mongo_mapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/jmonteiro-mongo_mapper.gemspec +195 -0
  8. data/lib/mongo_mapper.rb +128 -0
  9. data/lib/mongo_mapper/descendant_appends.rb +44 -0
  10. data/lib/mongo_mapper/document.rb +402 -0
  11. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  12. data/lib/mongo_mapper/embedded_document.rb +61 -0
  13. data/lib/mongo_mapper/finder_options.rb +127 -0
  14. data/lib/mongo_mapper/plugins.rb +19 -0
  15. data/lib/mongo_mapper/plugins/associations.rb +104 -0
  16. data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
  17. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +23 -0
  19. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  20. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +49 -0
  21. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
  22. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  25. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  26. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  27. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +66 -0
  28. data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
  29. data/lib/mongo_mapper/plugins/callbacks.rb +65 -0
  30. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  31. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  32. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  33. data/lib/mongo_mapper/plugins/equality.rb +11 -0
  34. data/lib/mongo_mapper/plugins/identity_map.rb +66 -0
  35. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  36. data/lib/mongo_mapper/plugins/keys.rb +295 -0
  37. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  38. data/lib/mongo_mapper/plugins/pagination.rb +85 -0
  39. data/lib/mongo_mapper/plugins/protected.rb +31 -0
  40. data/lib/mongo_mapper/plugins/rails.rb +80 -0
  41. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  42. data/lib/mongo_mapper/plugins/validations.rb +48 -0
  43. data/lib/mongo_mapper/support.rb +213 -0
  44. data/performance/read_write.rb +52 -0
  45. data/specs.watchr +51 -0
  46. data/test/NOTE_ON_TESTING +1 -0
  47. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  48. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  49. data/test/functional/associations/test_in_array_proxy.rb +309 -0
  50. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  51. data/test/functional/associations/test_many_documents_proxy.rb +437 -0
  52. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +175 -0
  53. data/test/functional/associations/test_many_embedded_proxy.rb +216 -0
  54. data/test/functional/associations/test_many_polymorphic_proxy.rb +340 -0
  55. data/test/functional/associations/test_one_proxy.rb +149 -0
  56. data/test/functional/test_associations.rb +44 -0
  57. data/test/functional/test_binary.rb +27 -0
  58. data/test/functional/test_callbacks.rb +81 -0
  59. data/test/functional/test_dirty.rb +156 -0
  60. data/test/functional/test_document.rb +1171 -0
  61. data/test/functional/test_embedded_document.rb +125 -0
  62. data/test/functional/test_identity_map.rb +233 -0
  63. data/test/functional/test_logger.rb +20 -0
  64. data/test/functional/test_modifiers.rb +252 -0
  65. data/test/functional/test_pagination.rb +93 -0
  66. data/test/functional/test_protected.rb +41 -0
  67. data/test/functional/test_string_id_compatibility.rb +67 -0
  68. data/test/functional/test_validations.rb +329 -0
  69. data/test/models.rb +232 -0
  70. data/test/support/custom_matchers.rb +55 -0
  71. data/test/support/timing.rb +16 -0
  72. data/test/test_helper.rb +60 -0
  73. data/test/unit/associations/test_base.rb +207 -0
  74. data/test/unit/associations/test_proxy.rb +103 -0
  75. data/test/unit/serializers/test_json_serializer.rb +189 -0
  76. data/test/unit/test_descendant_appends.rb +71 -0
  77. data/test/unit/test_document.rb +203 -0
  78. data/test/unit/test_dynamic_finder.rb +125 -0
  79. data/test/unit/test_embedded_document.rb +628 -0
  80. data/test/unit/test_finder_options.rb +325 -0
  81. data/test/unit/test_keys.rb +169 -0
  82. data/test/unit/test_mongo_mapper.rb +65 -0
  83. data/test/unit/test_pagination.rb +127 -0
  84. data/test/unit/test_plugins.rb +42 -0
  85. data/test/unit/test_rails.rb +139 -0
  86. data/test/unit/test_rails_compatibility.rb +42 -0
  87. data/test/unit/test_serialization.rb +51 -0
  88. data/test/unit/test_support.rb +350 -0
  89. data/test/unit/test_time_zones.rb +39 -0
  90. data/test/unit/test_validations.rb +492 -0
  91. metadata +260 -0
@@ -0,0 +1 @@
1
+ I am doing my best to keep unit and functional tests separate. As I see them, functional tests hit the database and should never care about internals. Unit tests do not hit the database.
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ Status.collection.remove
7
+ Project.collection.remove
8
+ end
9
+
10
+ should "default to nil" do
11
+ status = Status.new
12
+ status.target.nil?.should be_true
13
+ status.target.inspect.should == "nil"
14
+ end
15
+
16
+ should "have boolean presence method" do
17
+ status = Status.new
18
+ status.target?.should be_false
19
+
20
+ status.target = Project.new(:name => 'mongomapper')
21
+ status.target?.should be_true
22
+ end
23
+
24
+ should "be able to replace the association" do
25
+ status = Status.new(:name => 'Foo!')
26
+ project = Project.new(:name => "mongomapper")
27
+ status.target = project
28
+ status.save.should be_true
29
+
30
+ status = status.reload
31
+ status.target.nil?.should be_false
32
+ status.target_id.should == project._id
33
+ status.target_type.should == "Project"
34
+ status.target.name.should == "mongomapper"
35
+ end
36
+
37
+ should "unset the association" do
38
+ status = Status.new(:name => 'Foo!')
39
+ project = Project.new(:name => "mongomapper")
40
+ status.target = project
41
+ status.save.should be_true
42
+
43
+ status = status.reload
44
+ status.target = nil
45
+ status.target_type.nil?.should be_true
46
+ status.target_id.nil?.should be_true
47
+ status.target.nil?.should be_true
48
+ end
49
+
50
+ context "association id set but document not found" do
51
+ setup do
52
+ @status = Status.new(:name => 'Foo!')
53
+ project = Project.new(:name => "mongomapper")
54
+ @status.target = project
55
+ @status.save.should be_true
56
+ project.destroy
57
+ end
58
+
59
+ should "return nil instead of raising error" do
60
+ @status.target.nil?.should be_true
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class BelongsToProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ @post_class = Doc()
7
+ @comment_class = Doc do
8
+ key :post_id, String
9
+ end
10
+
11
+ @comment_class.belongs_to :post, :class => @post_class
12
+ end
13
+
14
+ should "default to nil" do
15
+ @comment_class.new.post.nil?.should be_true
16
+ end
17
+
18
+ should "have boolean presence method" do
19
+ comment = @comment_class.new(:name => 'Foo!')
20
+ comment.post?.should be_false
21
+
22
+ comment.post = @post_class.new(:name => 'mongomapper')
23
+ comment.post?.should be_true
24
+ end
25
+
26
+ should "be able to replace the association" do
27
+ post = @post_class.new(:name => 'mongomapper')
28
+ comment = @comment_class.new(:name => 'Foo!', :post => post)
29
+ comment.save.should be_true
30
+
31
+ comment = comment.reload
32
+ comment.post.should == post
33
+ comment.post.nil?.should be_false
34
+ end
35
+
36
+ should "unset the association" do
37
+ post = @post_class.new(:name => 'mongomapper')
38
+ comment = @comment_class.new(:name => 'Foo!', :post => post)
39
+ comment.save.should be_true
40
+
41
+ comment = comment.reload
42
+ comment.post = nil
43
+ comment.post.nil?.should be_true
44
+ end
45
+
46
+ should "return nil if id set but document not found" do
47
+ id = Mongo::ObjectID.new
48
+ @comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
49
+ end
50
+
51
+ context ":dependent" do
52
+ setup do
53
+ # FIXME: make use of already defined models
54
+ class ::Property
55
+ include MongoMapper::Document
56
+ end
57
+ Property.collection.remove
58
+
59
+ class ::Thing
60
+ include MongoMapper::Document
61
+ key :name, String
62
+ end
63
+ Thing.collection.remove
64
+ end
65
+
66
+ teardown do
67
+ Object.send :remove_const, 'Property' if defined?(::Property)
68
+ Object.send :remove_const, 'Thing' if defined?(::Thing)
69
+ end
70
+
71
+ context "=> destroy" do
72
+ setup do
73
+ Property.key :thing_id, ObjectId
74
+ Property.belongs_to :thing, :dependent => :destroy
75
+ Thing.many :properties
76
+
77
+ @thing = Thing.create(:name => "Tree")
78
+ @property1 = Property.create
79
+ @property2 = Property.create
80
+ @property3 = Property.create
81
+ @thing.properties << @property1
82
+ @thing.properties << @property2
83
+ @thing.properties << @property3
84
+ end
85
+
86
+ should "not execute on a belongs_to association" do
87
+ Thing.count.should == 1
88
+ @property1.destroy
89
+ Thing.count.should == 1
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,309 @@
1
+ require 'test_helper'
2
+
3
+ class InArrayProxyTest < Test::Unit::TestCase
4
+ context "description" do
5
+ setup do
6
+ class ::List
7
+ include MongoMapper::Document
8
+ key :name, String, :required => true
9
+ end
10
+
11
+ class ::User
12
+ include MongoMapper::Document
13
+ key :name, String, :required => true
14
+ key :list_ids, Array
15
+ many :lists, :in => :list_ids
16
+ end
17
+ User.collection.remove
18
+ List.collection.remove
19
+ end
20
+
21
+ teardown do
22
+ Object.send :remove_const, 'List' if defined?(::List)
23
+ Object.send :remove_const, 'User' if defined?(::User)
24
+ end
25
+
26
+ should "default reader to empty array" do
27
+ User.new.lists.should == []
28
+ end
29
+
30
+ should "allow adding to association like it was an array" do
31
+ user = User.new(:name => 'John')
32
+ user.lists << List.new(:name => 'Foo1!')
33
+ user.lists.push List.new(:name => 'Foo2!')
34
+ user.lists.concat List.new(:name => 'Foo3!')
35
+ user.lists.size.should == 3
36
+ end
37
+
38
+ should "ignore adding duplicate ids" do
39
+ user = User.create(:name => 'John')
40
+ list = List.create(:name => 'Foo')
41
+ user.lists << list
42
+ user.lists << list
43
+ user.lists << list
44
+
45
+ user.list_ids.should == [list.id]
46
+ user.lists.count.should == 1
47
+ end
48
+
49
+ should "be able to replace the association" do
50
+ user = User.new(:name => 'John')
51
+ list = List.new(:name => 'Foo')
52
+ user.lists = [list]
53
+ user.save.should be_true
54
+
55
+ user.reload
56
+ user.list_ids.should == [list.id]
57
+ user.lists.size.should == 1
58
+ user.lists[0].name.should == 'Foo'
59
+ end
60
+
61
+ context "create" do
62
+ setup do
63
+ @user = User.create(:name => 'John')
64
+ @list = @user.lists.create(:name => 'Foo!')
65
+ end
66
+
67
+ should "add id to key" do
68
+ @user.list_ids.should include(@list.id)
69
+ end
70
+
71
+ should "persist id addition to key in database" do
72
+ @user.reload
73
+ @user.list_ids.should include(@list.id)
74
+ end
75
+
76
+ should "add doc to association" do
77
+ @user.lists.should include(@list)
78
+ end
79
+
80
+ should "save doc" do
81
+ @list.should_not be_new
82
+ end
83
+ end
84
+
85
+ context "create!" do
86
+ setup do
87
+ @user = User.create(:name => 'John')
88
+ @list = @user.lists.create!(:name => 'Foo!')
89
+ end
90
+
91
+ should "add id to key" do
92
+ @user.list_ids.should include(@list.id)
93
+ end
94
+
95
+ should "persist id addition to key in database" do
96
+ @user.reload
97
+ @user.list_ids.should include(@list.id)
98
+ end
99
+
100
+ should "add doc to association" do
101
+ @user.lists.should include(@list)
102
+ end
103
+
104
+ should "save doc" do
105
+ @list.should_not be_new
106
+ end
107
+
108
+ should "raise exception if invalid" do
109
+ assert_raises(MongoMapper::DocumentNotValid) do
110
+ @user.lists.create!
111
+ end
112
+ end
113
+ end
114
+
115
+ context "Finding scoped to association" do
116
+ setup do
117
+ @user = User.create(:name => 'John')
118
+ @user2 = User.create(:name => 'Brandon')
119
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
120
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
121
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
122
+ end
123
+
124
+ context "all" do
125
+ should "work" do
126
+ @user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
127
+ @user.lists.all(:order => :position.asc).should == [@list1, @list2]
128
+ end
129
+
130
+ should "work with conditions" do
131
+ @user.lists.find(:all, :name => 'Foo 1').should == [@list1]
132
+ @user.lists.all(:name => 'Foo 1').should == [@list1]
133
+ end
134
+ end
135
+
136
+ context "first" do
137
+ should "work" do
138
+ @user.lists.find(:first, :order => 'position').should == @list1
139
+ @user.lists.first(:order => 'position').should == @list1
140
+ end
141
+
142
+ should "work with conditions" do
143
+ @user.lists.find(:first, :position => 2).should == @list2
144
+ @user.lists.first(:position => 2).should == @list2
145
+ end
146
+ end
147
+
148
+ context "last" do
149
+ should "work" do
150
+ @user.lists.find(:last, :order => 'position').should == @list2
151
+ @user.lists.last(:order => 'position').should == @list2
152
+ end
153
+
154
+ should "work with conditions" do
155
+ @user.lists.find(:last, :position => 2, :order => 'position').should == @list2
156
+ @user.lists.last(:position => 2, :order => 'position').should == @list2
157
+ end
158
+ end
159
+
160
+ context "with one id" do
161
+ should "work for id in association" do
162
+ @user.lists.find(@list1.id).should == @list1
163
+ end
164
+
165
+ should "not work for id not in association" do
166
+ @user.lists.find(@list3.id).should be_nil
167
+ end
168
+
169
+ should "raise error when using ! and not found" do
170
+ assert_raises MongoMapper::DocumentNotFound do
171
+ @user.lists.find!(@list3.id)
172
+ end
173
+ end
174
+ end
175
+
176
+ context "with multiple ids" do
177
+ should "work for ids in association" do
178
+ @user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
179
+ end
180
+
181
+ should "not work for ids not in association" do
182
+ @user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
183
+ end
184
+ end
185
+
186
+ context "with #paginate" do
187
+ setup do
188
+ @lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
189
+ end
190
+
191
+ should "return total pages" do
192
+ @lists.total_pages.should == 2
193
+ end
194
+
195
+ should "return total entries" do
196
+ @lists.total_entries.should == 2
197
+ end
198
+
199
+ should "return the subject" do
200
+ @lists.collect(&:name).should == ['Foo 1']
201
+ end
202
+ end
203
+
204
+ context "dynamic finders" do
205
+ should "work with single key" do
206
+ @user.lists.find_by_name('Foo 1').should == @list1
207
+ @user.lists.find_by_name!('Foo 1').should == @list1
208
+ @user.lists.find_by_name('Foo 3').should be_nil
209
+ end
210
+
211
+ should "work with multiple keys" do
212
+ @user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
213
+ @user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
214
+ @user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
215
+ end
216
+
217
+ should "raise error when using ! and not found" do
218
+ assert_raises(MongoMapper::DocumentNotFound) do
219
+ @user.lists.find_by_name!('Foo 3')
220
+ end
221
+ end
222
+
223
+ context "find_or_create_by" do
224
+ should "not create document if found" do
225
+ lambda {
226
+ list = @user.lists.find_or_create_by_name('Foo 1')
227
+ list.should == @list1
228
+ }.should_not change { List.count }
229
+ end
230
+
231
+ should "create document if not found" do
232
+ lambda {
233
+ list = @user.lists.find_or_create_by_name('Home')
234
+ @user.lists.should include(list)
235
+ }.should change { List.count }
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ context "count" do
242
+ setup do
243
+ @user = User.create(:name => 'John')
244
+ @user2 = User.create(:name => 'Brandon')
245
+ @list1 = @user.lists.create!(:name => 'Foo 1')
246
+ @list2 = @user.lists.create!(:name => 'Foo 2')
247
+ @list3 = @user2.lists.create!(:name => 'Foo 3')
248
+ end
249
+
250
+ should "return number of ids" do
251
+ @user.lists.count.should == 2
252
+ @user2.lists.count.should == 1
253
+ end
254
+
255
+ should "return correct count when given criteria" do
256
+ @user.lists.count(:name => 'Foo 1').should == 1
257
+ @user2.lists.count(:name => 'Foo 1').should == 0
258
+ end
259
+ end
260
+
261
+ context "Removing documents" do
262
+ setup do
263
+ @user = User.create(:name => 'John')
264
+ @user2 = User.create(:name => 'Brandon')
265
+ @list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
266
+ @list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
267
+ @list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
268
+ end
269
+
270
+ context "destroy_all" do
271
+ should "work" do
272
+ @user.lists.count.should == 2
273
+ @user.lists.destroy_all
274
+ @user.lists.count.should == 0
275
+ end
276
+
277
+ should "work with conditions" do
278
+ @user.lists.count.should == 2
279
+ @user.lists.destroy_all(:name => 'Foo 1')
280
+ @user.lists.count.should == 1
281
+ end
282
+ end
283
+
284
+ context "delete_all" do
285
+ should "work" do
286
+ @user.lists.count.should == 2
287
+ @user.lists.delete_all
288
+ @user.lists.count.should == 0
289
+ end
290
+
291
+ should "work with conditions" do
292
+ @user.lists.count.should == 2
293
+ @user.lists.delete_all(:name => 'Foo 1')
294
+ @user.lists.count.should == 1
295
+ end
296
+ end
297
+
298
+ should "work with nullify" do
299
+ @user.lists.count.should == 2
300
+
301
+ lambda {
302
+ @user.lists.nullify
303
+ }.should_not change { List.count }
304
+
305
+ @user.lists.count.should == 0
306
+ end
307
+ end
308
+ end
309
+ end