numon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +31 -0
  4. data/Rakefile +52 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +138 -0
  7. data/lib/mongo_mapper/document.rb +359 -0
  8. data/lib/mongo_mapper/embedded_document.rb +61 -0
  9. data/lib/mongo_mapper/plugins.rb +34 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +105 -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 +120 -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_proxy.rb +68 -0
  23. data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
  24. data/lib/mongo_mapper/plugins/callbacks.rb +234 -0
  25. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  26. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  27. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  28. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  29. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  30. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  31. data/lib/mongo_mapper/plugins/keys.rb +336 -0
  32. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  33. data/lib/mongo_mapper/plugins/modifiers.rb +87 -0
  34. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  35. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  36. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  37. data/lib/mongo_mapper/plugins/rails.rb +53 -0
  38. data/lib/mongo_mapper/plugins/serialization.rb +75 -0
  39. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  40. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  41. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  42. data/lib/mongo_mapper/query.rb +130 -0
  43. data/lib/mongo_mapper/support.rb +216 -0
  44. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  45. data/lib/mongo_mapper/support/find.rb +77 -0
  46. data/lib/mongo_mapper/version.rb +3 -0
  47. data/numon.gemspec +207 -0
  48. data/performance/read_write.rb +52 -0
  49. data/specs.watchr +51 -0
  50. data/test/NOTE_ON_TESTING +1 -0
  51. data/test/active_model_lint_test.rb +11 -0
  52. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  53. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  54. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  55. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  56. data/test/functional/associations/test_many_documents_proxy.rb +453 -0
  57. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  58. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  59. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  60. data/test/functional/associations/test_one_proxy.rb +161 -0
  61. data/test/functional/test_associations.rb +44 -0
  62. data/test/functional/test_binary.rb +27 -0
  63. data/test/functional/test_callbacks.rb +151 -0
  64. data/test/functional/test_dirty.rb +163 -0
  65. data/test/functional/test_document.rb +1165 -0
  66. data/test/functional/test_embedded_document.rb +130 -0
  67. data/test/functional/test_identity_map.rb +508 -0
  68. data/test/functional/test_indexing.rb +44 -0
  69. data/test/functional/test_logger.rb +20 -0
  70. data/test/functional/test_modifiers.rb +322 -0
  71. data/test/functional/test_pagination.rb +93 -0
  72. data/test/functional/test_protected.rb +161 -0
  73. data/test/functional/test_string_id_compatibility.rb +67 -0
  74. data/test/functional/test_timestamps.rb +64 -0
  75. data/test/functional/test_userstamps.rb +28 -0
  76. data/test/functional/test_validations.rb +329 -0
  77. data/test/models.rb +232 -0
  78. data/test/support/custom_matchers.rb +55 -0
  79. data/test/support/timing.rb +16 -0
  80. data/test/test_helper.rb +61 -0
  81. data/test/unit/associations/test_base.rb +207 -0
  82. data/test/unit/associations/test_proxy.rb +105 -0
  83. data/test/unit/serializers/test_json_serializer.rb +202 -0
  84. data/test/unit/test_descendant_appends.rb +71 -0
  85. data/test/unit/test_document.rb +231 -0
  86. data/test/unit/test_dynamic_finder.rb +123 -0
  87. data/test/unit/test_embedded_document.rb +663 -0
  88. data/test/unit/test_keys.rb +173 -0
  89. data/test/unit/test_mongo_mapper.rb +155 -0
  90. data/test/unit/test_pagination.rb +160 -0
  91. data/test/unit/test_plugins.rb +50 -0
  92. data/test/unit/test_query.rb +340 -0
  93. data/test/unit/test_rails.rb +123 -0
  94. data/test/unit/test_rails_compatibility.rb +52 -0
  95. data/test/unit/test_serialization.rb +51 -0
  96. data/test/unit/test_support.rb +366 -0
  97. data/test/unit/test_time_zones.rb +39 -0
  98. data/test/unit/test_validations.rb +544 -0
  99. metadata +305 -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[1]
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,322 @@
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
+ context "using class methods" do
22
+ should "be able to increment with criteria and modifier hashes" do
23
+ page = @page_class.create(:title => 'Home')
24
+ page2 = @page_class.create(:title => 'Home')
25
+
26
+ @page_class.increment({:title => 'Home'}, {
27
+ :day_count => 1, :week_count => 2, :month_count => 3
28
+ })
29
+
30
+ assert_page_counts page, 1, 2, 3
31
+ assert_page_counts page2, 1, 2, 3
32
+ end
33
+
34
+ should "be able to increment with ids and modifier hash" do
35
+ page = @page_class.create(:title => 'Home')
36
+ page2 = @page_class.create(:title => 'Home')
37
+
38
+ @page_class.increment(page.id, page2.id, {
39
+ :day_count => 1, :week_count => 2, :month_count => 3
40
+ })
41
+
42
+ assert_page_counts page, 1, 2, 3
43
+ assert_page_counts page2, 1, 2, 3
44
+ end
45
+
46
+ should "be able to decrement with criteria and modifier hashes" do
47
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
48
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
49
+
50
+ @page_class.decrement({:title => 'Home'}, {
51
+ :day_count => 1, :week_count => 2, :month_count => 3
52
+ })
53
+
54
+ assert_page_counts page, 0, 0, 0
55
+ assert_page_counts page2, 0, 0, 0
56
+ end
57
+
58
+ should "be able to decrement with ids and modifier hash" do
59
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
60
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
61
+
62
+ @page_class.decrement(page.id, page2.id, {
63
+ :day_count => 1, :week_count => 2, :month_count => 3
64
+ })
65
+
66
+ assert_page_counts page, 0, 0, 0
67
+ assert_page_counts page2, 0, 0, 0
68
+ end
69
+
70
+ should "always decrement when decrement is called whether number is positive or negative" 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
+
74
+ @page_class.decrement(page.id, page2.id, {
75
+ :day_count => -1, :week_count => 2, :month_count => -3
76
+ })
77
+
78
+ assert_page_counts page, 0, 0, 0
79
+ assert_page_counts page2, 0, 0, 0
80
+ end
81
+
82
+ should "be able to set with criteria and modifier hashes" do
83
+ page = @page_class.create(:title => 'Home')
84
+ page2 = @page_class.create(:title => 'Home')
85
+
86
+ @page_class.set({:title => 'Home'}, :title => 'Home Revised')
87
+
88
+ page.reload
89
+ page.title.should == 'Home Revised'
90
+
91
+ page2.reload
92
+ page2.title.should == 'Home Revised'
93
+ end
94
+
95
+ should "be able to set with ids and modifier hash" do
96
+ page = @page_class.create(:title => 'Home')
97
+ page2 = @page_class.create(:title => 'Home')
98
+
99
+ @page_class.set(page.id, page2.id, :title => 'Home Revised')
100
+
101
+ page.reload
102
+ page.title.should == 'Home Revised'
103
+
104
+ page2.reload
105
+ page2.title.should == 'Home Revised'
106
+ end
107
+
108
+ should "be able to push with criteria and modifier hashes" do
109
+ page = @page_class.create(:title => 'Home')
110
+ page2 = @page_class.create(:title => 'Home')
111
+
112
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
113
+
114
+ page.reload
115
+ page.tags.should == %w(foo)
116
+
117
+ page2.reload
118
+ page.tags.should == %w(foo)
119
+ end
120
+
121
+ should "be able to push with ids and modifier hash" do
122
+ page = @page_class.create(:title => 'Home')
123
+ page2 = @page_class.create(:title => 'Home')
124
+
125
+ @page_class.push(page.id, page2.id, :tags => 'foo')
126
+
127
+ page.reload
128
+ page.tags.should == %w(foo)
129
+
130
+ page2.reload
131
+ page.tags.should == %w(foo)
132
+ end
133
+
134
+ should "be able to push all with criteria and modifier hashes" do
135
+ page = @page_class.create(:title => 'Home')
136
+ page2 = @page_class.create(:title => 'Home')
137
+ tags = %w(foo bar)
138
+
139
+ @page_class.push_all({:title => 'Home'}, :tags => tags)
140
+
141
+ page.reload
142
+ page.tags.should == tags
143
+
144
+ page2.reload
145
+ page.tags.should == tags
146
+ end
147
+
148
+ should "be able to push all with ids and modifier hash" do
149
+ page = @page_class.create(:title => 'Home')
150
+ page2 = @page_class.create(:title => 'Home')
151
+ tags = %w(foo bar)
152
+
153
+ @page_class.push_all(page.id, page2.id, :tags => tags)
154
+
155
+ page.reload
156
+ page.tags.should == tags
157
+
158
+ page2.reload
159
+ page.tags.should == tags
160
+ end
161
+
162
+ should "be able to pull with criteria and modifier hashes" do
163
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
164
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
165
+
166
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
167
+
168
+ page.reload
169
+ page.tags.should == %w(bar)
170
+
171
+ page2.reload
172
+ page.tags.should == %w(bar)
173
+ end
174
+
175
+ should "be able to pull with ids and modifier hash" do
176
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
177
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
178
+
179
+ @page_class.pull(page.id, page2.id, :tags => 'foo')
180
+
181
+ page.reload
182
+ page.tags.should == %w(bar)
183
+
184
+ page2.reload
185
+ page.tags.should == %w(bar)
186
+ end
187
+
188
+ should "be able to pull all with criteria and modifier hashes" do
189
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
190
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
191
+
192
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
193
+
194
+ page.reload
195
+ page.tags.should == %w(baz)
196
+
197
+ page2.reload
198
+ page.tags.should == %w(baz)
199
+ end
200
+
201
+ should "be able to pull all with ids and modifier hash" do
202
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
203
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
204
+
205
+ @page_class.pull_all(page.id, page2.id, :tags => %w(foo bar))
206
+
207
+ page.reload
208
+ page.tags.should == %w(baz)
209
+
210
+ page2.reload
211
+ page.tags.should == %w(baz)
212
+ end
213
+
214
+ should "be able to push uniq with criteria and modifier hash" do
215
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
216
+ page2 = @page_class.create(:title => 'Home')
217
+
218
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
219
+
220
+ page.reload
221
+ page.tags.should == %w(foo)
222
+
223
+ page2.reload
224
+ page.tags.should == %w(foo)
225
+ end
226
+
227
+ should "be able to push uniq with ids and modifier hash" do
228
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
229
+ page2 = @page_class.create(:title => 'Home')
230
+
231
+ @page_class.push_uniq(page.id, page2.id, :tags => 'foo')
232
+
233
+ page.reload
234
+ page.tags.should == %w(foo)
235
+
236
+ page2.reload
237
+ page.tags.should == %w(foo)
238
+ end
239
+
240
+ should "be able to remove the last element the array" do
241
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
242
+ @page_class.pop(page.id, :tags => 1)
243
+ page.reload
244
+ page.tags.should == %w(foo)
245
+ end
246
+
247
+ should "be able to remove the first element of the array" do
248
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
249
+ @page_class.pop(page.id, :tags => -1)
250
+ page.reload
251
+ page.tags.should == %w(bar)
252
+ end
253
+ end
254
+
255
+ context "using instance methods" do
256
+ should "be able to increment with modifier hashes" do
257
+ page = @page_class.create
258
+
259
+ page.increment({:day_count => 1, :week_count => 2, :month_count => 3})
260
+
261
+ assert_page_counts page, 1, 2, 3
262
+ end
263
+
264
+ should "be able to decrement with modifier hashes" do
265
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
266
+
267
+ page.decrement({:day_count => 1, :week_count => 2, :month_count => 3})
268
+
269
+ assert_page_counts page, 0, 0, 0
270
+ end
271
+
272
+ should "always decrement when decrement is called whether number is positive or negative" do
273
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
274
+
275
+ page.decrement({:day_count => -1, :week_count => 2, :month_count => -3})
276
+
277
+ assert_page_counts page, 0, 0, 0
278
+ end
279
+
280
+ should "be able to set with modifier hashes" do
281
+ page = @page_class.create(:title => 'Home')
282
+
283
+ page.set(:title => 'Home Revised')
284
+
285
+ page.reload
286
+ page.title.should == 'Home Revised'
287
+ end
288
+
289
+ should "be able to push with modifier hashes" do
290
+ page = @page_class.create
291
+
292
+ page.push(:tags => 'foo')
293
+
294
+ page.reload
295
+ page.tags.should == %w(foo)
296
+ end
297
+
298
+ should "be able to pull with criteria and modifier hashes" do
299
+ page = @page_class.create(:tags => %w(foo bar))
300
+
301
+ page.pull(:tags => 'foo')
302
+
303
+ page.reload
304
+ page.tags.should == %w(bar)
305
+ end
306
+
307
+ should "be able to push uniq with criteria and modifier hash" do
308
+ page = @page_class.create(:tags => 'foo')
309
+ page2 = @page_class.create
310
+
311
+ page.push_uniq(:tags => 'foo')
312
+ page.push_uniq(:tags => 'foo')
313
+
314
+ page.reload
315
+ page.tags.should == %w(foo)
316
+
317
+ page2.reload
318
+ page.tags.should == %w(foo)
319
+ end
320
+ end
321
+
322
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ context "Paginating" 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
+
13
+ def self.per_page; 1 end
14
+ end
15
+
16
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
17
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
18
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
19
+ end
20
+
21
+ should "return the total pages" do
22
+ result = @document.paginate(:per_page => 2, :page => 1)
23
+ result.total_pages.should == 2
24
+ end
25
+
26
+ should "return the total pages when defaulting to the document class per_page" do
27
+ result = @document.paginate(:page => 1)
28
+ result.total_pages.should == 3
29
+ end
30
+
31
+ should "return the total of records" do
32
+ result = @document.paginate(:per_page => 2, :page => 1)
33
+ result.total_entries.should == 3
34
+ end
35
+
36
+ should "return the items" do
37
+ result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
38
+ result.size.should == 2
39
+ result.should == [@doc1, @doc3]
40
+ end
41
+
42
+ should "accept conditions" do
43
+ result = @document.paginate({
44
+ :last_name => 'Nunemaker',
45
+ :order => "age DESC",
46
+ :per_page => 2,
47
+ :page => 1,
48
+ })
49
+ result.should == [@doc1, @doc3]
50
+ result.first.age.should == 27
51
+
52
+ result = @document.paginate({
53
+ :conditions => {:last_name => 'Nunemaker'},
54
+ :order => "age DESC",
55
+ :per_page => 2,
56
+ :page => 1} )
57
+ result.should == [@doc1, @doc3]
58
+ result.first.age.should == 27
59
+ end
60
+
61
+ should "withstand rigor" do
62
+ result = @document.paginate({
63
+ :per_page => 1,
64
+ :page => 1,
65
+ :order => 'age desc',
66
+ :last_name => 'Nunemaker'
67
+ })
68
+ result.should == [@doc1]
69
+ result.total_entries.should == 2
70
+ result.total_pages.should == 2
71
+
72
+ result = @document.paginate({
73
+ :per_page => 1,
74
+ :page => 2,
75
+ :order => 'age desc',
76
+ :last_name => 'Nunemaker'
77
+ })
78
+ result.should == [@doc3]
79
+ result.total_entries.should == 2
80
+ result.total_pages.should == 2
81
+
82
+ result = @document.paginate({
83
+ :per_page => 2,
84
+ :page => 1,
85
+ :order => 'age desc',
86
+ :last_name => 'Nunemaker'
87
+ })
88
+ result.should == [@doc1, @doc3]
89
+ result.total_entries.should == 2
90
+ result.total_pages.should == 1
91
+ end
92
+ end
93
+ end