mongo_mapper_ign 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +35 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +313 -0
  8. data/lib/mongo_mapper/embedded_document.rb +70 -0
  9. data/lib/mongo_mapper/plugins.rb +35 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +345 -0
  33. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  34. data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +91 -0
  41. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
  42. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +214 -0
  52. data/mongo_mapper_ign.gemspec +217 -0
  53. data/performance/read_write.rb +52 -0
  54. data/specs.watchr +51 -0
  55. data/test/NOTE_ON_TESTING +1 -0
  56. data/test/active_model_lint_test.rb +13 -0
  57. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  58. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  59. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  60. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  61. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  62. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  63. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  64. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  65. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  66. data/test/functional/associations/test_one_proxy.rb +196 -0
  67. data/test/functional/test_associations.rb +44 -0
  68. data/test/functional/test_binary.rb +27 -0
  69. data/test/functional/test_callbacks.rb +151 -0
  70. data/test/functional/test_dirty.rb +163 -0
  71. data/test/functional/test_document.rb +1219 -0
  72. data/test/functional/test_embedded_document.rb +210 -0
  73. data/test/functional/test_identity_map.rb +507 -0
  74. data/test/functional/test_indexing.rb +44 -0
  75. data/test/functional/test_logger.rb +20 -0
  76. data/test/functional/test_modifiers.rb +394 -0
  77. data/test/functional/test_pagination.rb +93 -0
  78. data/test/functional/test_protected.rb +163 -0
  79. data/test/functional/test_string_id_compatibility.rb +67 -0
  80. data/test/functional/test_timestamps.rb +64 -0
  81. data/test/functional/test_userstamps.rb +28 -0
  82. data/test/functional/test_validations.rb +342 -0
  83. data/test/models.rb +227 -0
  84. data/test/support/custom_matchers.rb +37 -0
  85. data/test/support/timing.rb +16 -0
  86. data/test/test_helper.rb +64 -0
  87. data/test/unit/associations/test_base.rb +212 -0
  88. data/test/unit/associations/test_proxy.rb +105 -0
  89. data/test/unit/serializers/test_json_serializer.rb +202 -0
  90. data/test/unit/test_descendant_appends.rb +71 -0
  91. data/test/unit/test_document.rb +225 -0
  92. data/test/unit/test_dynamic_finder.rb +123 -0
  93. data/test/unit/test_embedded_document.rb +657 -0
  94. data/test/unit/test_keys.rb +185 -0
  95. data/test/unit/test_mongo_mapper.rb +118 -0
  96. data/test/unit/test_pagination.rb +160 -0
  97. data/test/unit/test_plugins.rb +50 -0
  98. data/test/unit/test_query.rb +374 -0
  99. data/test/unit/test_rails.rb +181 -0
  100. data/test/unit/test_rails_compatibility.rb +52 -0
  101. data/test/unit/test_serialization.rb +51 -0
  102. data/test/unit/test_support.rb +382 -0
  103. data/test/unit/test_time_zones.rb +39 -0
  104. data/test/unit/test_validations.rb +544 -0
  105. metadata +327 -0
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class IndexingTest < Test::Unit::TestCase
4
+ context "Indexing" do
5
+ setup do
6
+ @document = Doc do
7
+ set_collection_name 'users'
8
+
9
+ key :first_name, String
10
+ key :last_name, String
11
+ key :age, Integer
12
+ key :date, Date
13
+ end
14
+ drop_indexes(@document)
15
+ end
16
+
17
+ should "allow creating index for a key" do
18
+ @document.ensure_index :first_name
19
+ @document.should have_index('first_name_1')
20
+ end
21
+
22
+ should "allow creating unique index for a key" do
23
+ @document.ensure_index :first_name, :unique => true
24
+ @document.should have_index('first_name_1')
25
+ end
26
+
27
+ should "allow creating index on multiple keys" do
28
+ @document.ensure_index [[:first_name, 1], [:last_name, -1]]
29
+
30
+ # order is different for different versions of ruby so instead of
31
+ # just checking have_index('first_name_1_last_name_-1') I'm checking
32
+ # the values of the indexes to make sure the index creation was successful
33
+ @document.collection.index_information.detect do |index|
34
+ keys = index[0]
35
+ keys.include?('first_name_1') && keys.include?('last_name_-1')
36
+ end.should_not be_nil
37
+ end
38
+
39
+ should "work with :index shortcut when defining key" do
40
+ @document.key :father, String, :index => true
41
+ @document.should have_index('father_1')
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ context "with connection that has logger" do
5
+ setup do
6
+ @output = StringIO.new
7
+ @logger = Logger.new(@output)
8
+ MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => @logger)
9
+ end
10
+
11
+ should "be able to get access to that logger" do
12
+ MongoMapper.logger.should == @logger
13
+ end
14
+
15
+ should "be able to log messages" do
16
+ MongoMapper.logger.debug 'testing'
17
+ @output.string.include?('testing').should be_true
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,394 @@
1
+ require 'test_helper'
2
+
3
+ class ModifierTest < Test::Unit::TestCase
4
+ def setup
5
+ @page_class = Doc do
6
+ key :title, String
7
+ key :day_count, Integer, :default => 0
8
+ key :week_count, Integer, :default => 0
9
+ key :month_count, Integer, :default => 0
10
+ key :tags, Array
11
+ end
12
+ end
13
+
14
+ def assert_page_counts(page, day_count, week_count, month_count)
15
+ page.reload
16
+ page.day_count.should == day_count
17
+ page.week_count.should == week_count
18
+ page.month_count.should == month_count
19
+ end
20
+
21
+ def assert_keys_removed(page, *keys)
22
+ keys.each do |key|
23
+ doc = @page_class.collection.find_one({:_id => page.id})
24
+ doc.keys.should_not include(key)
25
+ end
26
+ end
27
+
28
+ context "ClassMethods" do
29
+ context "unset" do
30
+ setup do
31
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
32
+ @page2 = @page_class.create(:title => 'Home')
33
+ end
34
+
35
+ should "work with criteria and keys" do
36
+ @page_class.unset({:title => 'Home'}, :title, :tags)
37
+ assert_keys_removed @page, :title, :tags
38
+ assert_keys_removed @page2, :title, :tags
39
+ end
40
+
41
+ should "work with ids and keys" do
42
+ @page_class.unset(@page.id, @page2.id, :title, :tags)
43
+ assert_keys_removed @page, :title, :tags
44
+ assert_keys_removed @page2, :title, :tags
45
+ end
46
+ end
47
+
48
+ context "increment" do
49
+ setup do
50
+ @page = @page_class.create(:title => 'Home')
51
+ @page2 = @page_class.create(:title => 'Home')
52
+ end
53
+
54
+ should "work with criteria and modifier hashes" do
55
+ @page_class.increment({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
56
+
57
+ assert_page_counts @page, 1, 2, 3
58
+ assert_page_counts @page2, 1, 2, 3
59
+ end
60
+
61
+ should "work with ids and modifier hash" do
62
+ @page_class.increment(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
63
+
64
+ assert_page_counts @page, 1, 2, 3
65
+ assert_page_counts @page2, 1, 2, 3
66
+ end
67
+ end
68
+
69
+ context "decrement" do
70
+ setup do
71
+ @page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
72
+ @page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
73
+ end
74
+
75
+ should "work with criteria and modifier hashes" do
76
+ @page_class.decrement({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
77
+
78
+ assert_page_counts @page, 0, 0, 0
79
+ assert_page_counts @page2, 0, 0, 0
80
+ end
81
+
82
+ should "work with ids and modifier hash" do
83
+ @page_class.decrement(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
84
+
85
+ assert_page_counts @page, 0, 0, 0
86
+ assert_page_counts @page2, 0, 0, 0
87
+ end
88
+
89
+ should "decrement with positive or negative numbers" do
90
+ @page_class.decrement(@page.id, @page2.id, :day_count => -1, :week_count => 2, :month_count => -3)
91
+
92
+ assert_page_counts @page, 0, 0, 0
93
+ assert_page_counts @page2, 0, 0, 0
94
+ end
95
+ end
96
+
97
+ context "set" do
98
+ setup do
99
+ @page = @page_class.create(:title => 'Home')
100
+ @page2 = @page_class.create(:title => 'Home')
101
+ end
102
+
103
+ should "work with criteria and modifier hashes" do
104
+ @page_class.set({:title => 'Home'}, :title => 'Home Revised')
105
+
106
+ @page.reload
107
+ @page.title.should == 'Home Revised'
108
+
109
+ @page2.reload
110
+ @page2.title.should == 'Home Revised'
111
+ end
112
+
113
+ should "work with ids and modifier hash" do
114
+ @page_class.set(@page.id, @page2.id, :title => 'Home Revised')
115
+
116
+ @page.reload
117
+ @page.title.should == 'Home Revised'
118
+
119
+ @page2.reload
120
+ @page2.title.should == 'Home Revised'
121
+ end
122
+ end
123
+
124
+ context "push" do
125
+ setup do
126
+ @page = @page_class.create(:title => 'Home')
127
+ @page2 = @page_class.create(:title => 'Home')
128
+ end
129
+
130
+ should "work with criteria and modifier hashes" do
131
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
132
+
133
+ @page.reload
134
+ @page.tags.should == %w(foo)
135
+
136
+ @page2.reload
137
+ @page.tags.should == %w(foo)
138
+ end
139
+
140
+ should "work with ids and modifier hash" do
141
+ @page_class.push(@page.id, @page2.id, :tags => 'foo')
142
+
143
+ @page.reload
144
+ @page.tags.should == %w(foo)
145
+
146
+ @page2.reload
147
+ @page.tags.should == %w(foo)
148
+ end
149
+ end
150
+
151
+ context "push_all" do
152
+ setup do
153
+ @page = @page_class.create(:title => 'Home')
154
+ @page2 = @page_class.create(:title => 'Home')
155
+ @tags = %w(foo bar)
156
+ end
157
+
158
+ should "work with criteria and modifier hashes" do
159
+ @page_class.push_all({:title => 'Home'}, :tags => @tags)
160
+
161
+ @page.reload
162
+ @page.tags.should == @tags
163
+
164
+ @page2.reload
165
+ @page.tags.should == @tags
166
+ end
167
+
168
+ should "work with ids and modifier hash" do
169
+ @page_class.push_all(@page.id, @page2.id, :tags => @tags)
170
+
171
+ @page.reload
172
+ @page.tags.should == @tags
173
+
174
+ @page2.reload
175
+ @page.tags.should == @tags
176
+ end
177
+ end
178
+
179
+ context "pull" do
180
+ setup do
181
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
182
+ @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
183
+ end
184
+
185
+ should "work with criteria and modifier hashes" do
186
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
187
+
188
+ @page.reload
189
+ @page.tags.should == %w(bar)
190
+
191
+ @page2.reload
192
+ @page.tags.should == %w(bar)
193
+ end
194
+
195
+ should "be able to pull with ids and modifier hash" do
196
+ @page_class.pull(@page.id, @page2.id, :tags => 'foo')
197
+
198
+ @page.reload
199
+ @page.tags.should == %w(bar)
200
+
201
+ @page2.reload
202
+ @page.tags.should == %w(bar)
203
+ end
204
+ end
205
+
206
+ context "pull_all" do
207
+ setup do
208
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
209
+ @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
210
+ end
211
+
212
+ should "work with criteria and modifier hashes" do
213
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
214
+
215
+ @page.reload
216
+ @page.tags.should == %w(baz)
217
+
218
+ @page2.reload
219
+ @page.tags.should == %w(baz)
220
+ end
221
+
222
+ should "work with ids and modifier hash" do
223
+ @page_class.pull_all(@page.id, @page2.id, :tags => %w(foo bar))
224
+
225
+ @page.reload
226
+ @page.tags.should == %w(baz)
227
+
228
+ @page2.reload
229
+ @page.tags.should == %w(baz)
230
+ end
231
+ end
232
+
233
+ context "add_to_set" do
234
+ setup do
235
+ @page = @page_class.create(:title => 'Home', :tags => 'foo')
236
+ @page2 = @page_class.create(:title => 'Home')
237
+ end
238
+
239
+ should "be able to add to set with criteria and modifier hash" do
240
+ @page_class.add_to_set({:title => 'Home'}, :tags => 'foo')
241
+
242
+ @page.reload
243
+ @page.tags.should == %w(foo)
244
+
245
+ @page2.reload
246
+ @page.tags.should == %w(foo)
247
+ end
248
+
249
+ should "be able to add to set with ids and modifier hash" do
250
+ @page_class.add_to_set(@page.id, @page2.id, :tags => 'foo')
251
+
252
+ @page.reload
253
+ @page.tags.should == %w(foo)
254
+
255
+ @page2.reload
256
+ @page.tags.should == %w(foo)
257
+ end
258
+ end
259
+
260
+ context "push_uniq" do
261
+ setup do
262
+ @page = @page_class.create(:title => 'Home', :tags => 'foo')
263
+ @page2 = @page_class.create(:title => 'Home')
264
+ end
265
+
266
+ should "be able to push uniq with criteria and modifier hash" do
267
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
268
+
269
+ @page.reload
270
+ @page.tags.should == %w(foo)
271
+
272
+ @page2.reload
273
+ @page.tags.should == %w(foo)
274
+ end
275
+
276
+ should "be able to push uniq with ids and modifier hash" do
277
+ @page_class.push_uniq(@page.id, @page2.id, :tags => 'foo')
278
+
279
+ @page.reload
280
+ @page.tags.should == %w(foo)
281
+
282
+ @page2.reload
283
+ @page.tags.should == %w(foo)
284
+ end
285
+ end
286
+
287
+ context "pop" do
288
+ setup do
289
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
290
+ end
291
+
292
+ should "be able to remove the last element the array" do
293
+ @page_class.pop(@page.id, :tags => 1)
294
+ @page.reload
295
+ @page.tags.should == %w(foo)
296
+ end
297
+
298
+ should "be able to remove the first element of the array" do
299
+ @page_class.pop(@page.id, :tags => -1)
300
+ @page.reload
301
+ @page.tags.should == %w(bar)
302
+ end
303
+ end
304
+ end
305
+
306
+ context "InstanceMethods" do
307
+ should "be able to unset with keys" do
308
+ page = @page_class.create(:title => 'Foo', :tags => %w(foo))
309
+ page.unset(:title, :tags)
310
+ assert_keys_removed page, :title, :tags
311
+ end
312
+
313
+ should "be able to increment with modifier hashes" do
314
+ page = @page_class.create
315
+ page.increment(:day_count => 1, :week_count => 2, :month_count => 3)
316
+
317
+ assert_page_counts page, 1, 2, 3
318
+ end
319
+
320
+ should "be able to decrement with modifier hashes" do
321
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
322
+ page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
323
+
324
+ assert_page_counts page, 0, 0, 0
325
+ end
326
+
327
+ should "always decrement when decrement is called whether number is positive or negative" do
328
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
329
+ page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
330
+
331
+ assert_page_counts page, 0, 0, 0
332
+ end
333
+
334
+ should "be able to set with modifier hashes" do
335
+ page = @page_class.create(:title => 'Home')
336
+ page.set(:title => 'Home Revised')
337
+
338
+ page.reload
339
+ page.title.should == 'Home Revised'
340
+ end
341
+
342
+ should "be able to push with modifier hashes" do
343
+ page = @page_class.create
344
+ page.push(:tags => 'foo')
345
+
346
+ page.reload
347
+ page.tags.should == %w(foo)
348
+ end
349
+
350
+ should "be able to pull with criteria and modifier hashes" do
351
+ page = @page_class.create(:tags => %w(foo bar))
352
+ page.pull(:tags => 'foo')
353
+
354
+ page.reload
355
+ page.tags.should == %w(bar)
356
+ end
357
+
358
+ should "be able to add_to_set with criteria and modifier hash" do
359
+ page = @page_class.create(:tags => 'foo')
360
+ page2 = @page_class.create
361
+
362
+ page.add_to_set(:tags => 'foo')
363
+ page.add_to_set(:tags => 'foo')
364
+
365
+ page.reload
366
+ page.tags.should == %w(foo)
367
+
368
+ page2.reload
369
+ page.tags.should == %w(foo)
370
+ end
371
+
372
+ should "be able to push uniq with criteria and modifier hash" do
373
+ page = @page_class.create(:tags => 'foo')
374
+ page2 = @page_class.create
375
+
376
+ page.push_uniq(:tags => 'foo')
377
+ page.push_uniq(:tags => 'foo')
378
+
379
+ page.reload
380
+ page.tags.should == %w(foo)
381
+
382
+ page2.reload
383
+ page.tags.should == %w(foo)
384
+ end
385
+
386
+ should "be able to pop with modifier hashes" do
387
+ page = @page_class.create(:tags => %w(foo bar))
388
+ page.pop(:tags => 1)
389
+
390
+ page.reload
391
+ page.tags.should == %w(foo)
392
+ end
393
+ end
394
+ end