novelys_mongo_mapper 0.6.10

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.
Files changed (91) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +54 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +124 -0
  8. data/lib/mongo_mapper/descendant_appends.rb +44 -0
  9. data/lib/mongo_mapper/document.rb +423 -0
  10. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  11. data/lib/mongo_mapper/embedded_document.rb +67 -0
  12. data/lib/mongo_mapper/finder_options.rb +127 -0
  13. data/lib/mongo_mapper/plugins.rb +14 -0
  14. data/lib/mongo_mapper/plugins/associations.rb +104 -0
  15. data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
  16. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  17. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  18. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  19. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
  20. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
  21. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  22. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
  23. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  25. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  26. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  27. data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
  28. data/lib/mongo_mapper/plugins/callbacks.rb +76 -0
  29. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  30. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  31. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  32. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  33. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  34. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  35. data/lib/mongo_mapper/plugins/keys.rb +300 -0
  36. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  37. data/lib/mongo_mapper/plugins/pagination.rb +85 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +41 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +45 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +105 -0
  41. data/lib/mongo_mapper/plugins/validations.rb +62 -0
  42. data/lib/mongo_mapper/support.rb +214 -0
  43. data/mongo_mapper.gemspec +179 -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 +101 -0
  49. data/test/functional/associations/test_in_array_proxy.rb +309 -0
  50. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  51. data/test/functional/associations/test_many_documents_proxy.rb +431 -0
  52. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  53. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  54. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  55. data/test/functional/associations/test_one_proxy.rb +161 -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 +163 -0
  60. data/test/functional/test_document.rb +1245 -0
  61. data/test/functional/test_embedded_document.rb +125 -0
  62. data/test/functional/test_identity_map.rb +508 -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 +139 -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 +55 -0
  73. data/test/unit/associations/test_base.rb +207 -0
  74. data/test/unit/associations/test_proxy.rb +105 -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 +231 -0
  78. data/test/unit/test_dynamic_finder.rb +125 -0
  79. data/test/unit/test_embedded_document.rb +663 -0
  80. data/test/unit/test_finder_options.rb +329 -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 +50 -0
  85. data/test/unit/test_rails.rb +123 -0
  86. data/test/unit/test_rails_compatibility.rb +52 -0
  87. data/test/unit/test_serialization.rb +51 -0
  88. data/test/unit/test_support.rb +354 -0
  89. data/test/unit/test_time_zones.rb +39 -0
  90. data/test/unit/test_validations.rb +544 -0
  91. metadata +248 -0
@@ -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,252 @@
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
+ should "be able to increment with criteria and modifier hashes" do
22
+ page = @page_class.create(:title => 'Home')
23
+ page2 = @page_class.create(:title => 'Home')
24
+
25
+ @page_class.increment({:title => 'Home'}, {
26
+ :day_count => 1, :week_count => 2, :month_count => 3
27
+ })
28
+
29
+ assert_page_counts page, 1, 2, 3
30
+ assert_page_counts page2, 1, 2, 3
31
+ end
32
+
33
+ should "be able to increment with ids and modifier hash" do
34
+ page = @page_class.create(:title => 'Home')
35
+ page2 = @page_class.create(:title => 'Home')
36
+
37
+ @page_class.increment(page.id, page2.id, {
38
+ :day_count => 1, :week_count => 2, :month_count => 3
39
+ })
40
+
41
+ assert_page_counts page, 1, 2, 3
42
+ assert_page_counts page2, 1, 2, 3
43
+ end
44
+
45
+ should "be able to decrement with criteria and modifier hashes" do
46
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
47
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
48
+
49
+ @page_class.decrement({:title => 'Home'}, {
50
+ :day_count => 1, :week_count => 2, :month_count => 3
51
+ })
52
+
53
+ assert_page_counts page, 0, 0, 0
54
+ assert_page_counts page2, 0, 0, 0
55
+ end
56
+
57
+ should "be able to decrement with ids and modifier hash" do
58
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
59
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
60
+
61
+ @page_class.decrement(page.id, page2.id, {
62
+ :day_count => 1, :week_count => 2, :month_count => 3
63
+ })
64
+
65
+ assert_page_counts page, 0, 0, 0
66
+ assert_page_counts page2, 0, 0, 0
67
+ end
68
+
69
+ should "always decrement when decrement is called whether number is positive or negative" do
70
+ page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
71
+ page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
72
+
73
+ @page_class.decrement(page.id, page2.id, {
74
+ :day_count => -1, :week_count => 2, :month_count => -3
75
+ })
76
+
77
+ assert_page_counts page, 0, 0, 0
78
+ assert_page_counts page2, 0, 0, 0
79
+ end
80
+
81
+ should "be able to set with criteria and modifier hashes" do
82
+ page = @page_class.create(:title => 'Home')
83
+ page2 = @page_class.create(:title => 'Home')
84
+
85
+ @page_class.set({:title => 'Home'}, :title => 'Home Revised')
86
+
87
+ page.reload
88
+ page.title.should == 'Home Revised'
89
+
90
+ page2.reload
91
+ page2.title.should == 'Home Revised'
92
+ end
93
+
94
+ should "be able to set with ids and modifier hash" do
95
+ page = @page_class.create(:title => 'Home')
96
+ page2 = @page_class.create(:title => 'Home')
97
+
98
+ @page_class.set(page.id, page2.id, :title => 'Home Revised')
99
+
100
+ page.reload
101
+ page.title.should == 'Home Revised'
102
+
103
+ page2.reload
104
+ page2.title.should == 'Home Revised'
105
+ end
106
+
107
+ should "be able to push with criteria and modifier hashes" do
108
+ page = @page_class.create(:title => 'Home')
109
+ page2 = @page_class.create(:title => 'Home')
110
+
111
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
112
+
113
+ page.reload
114
+ page.tags.should == %w(foo)
115
+
116
+ page2.reload
117
+ page.tags.should == %w(foo)
118
+ end
119
+
120
+ should "be able to push with ids and modifier hash" do
121
+ page = @page_class.create(:title => 'Home')
122
+ page2 = @page_class.create(:title => 'Home')
123
+
124
+ @page_class.push(page.id, page2.id, :tags => 'foo')
125
+
126
+ page.reload
127
+ page.tags.should == %w(foo)
128
+
129
+ page2.reload
130
+ page.tags.should == %w(foo)
131
+ end
132
+
133
+ should "be able to push all with criteria and modifier hashes" do
134
+ page = @page_class.create(:title => 'Home')
135
+ page2 = @page_class.create(:title => 'Home')
136
+ tags = %w(foo bar)
137
+
138
+ @page_class.push_all({:title => 'Home'}, :tags => tags)
139
+
140
+ page.reload
141
+ page.tags.should == tags
142
+
143
+ page2.reload
144
+ page.tags.should == tags
145
+ end
146
+
147
+ should "be able to push all with ids and modifier hash" do
148
+ page = @page_class.create(:title => 'Home')
149
+ page2 = @page_class.create(:title => 'Home')
150
+ tags = %w(foo bar)
151
+
152
+ @page_class.push_all(page.id, page2.id, :tags => tags)
153
+
154
+ page.reload
155
+ page.tags.should == tags
156
+
157
+ page2.reload
158
+ page.tags.should == tags
159
+ end
160
+
161
+ should "be able to pull with criteria and modifier hashes" do
162
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
163
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
164
+
165
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
166
+
167
+ page.reload
168
+ page.tags.should == %w(bar)
169
+
170
+ page2.reload
171
+ page.tags.should == %w(bar)
172
+ end
173
+
174
+ should "be able to pull with ids and modifier hash" do
175
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
176
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
177
+
178
+ @page_class.pull(page.id, page2.id, :tags => 'foo')
179
+
180
+ page.reload
181
+ page.tags.should == %w(bar)
182
+
183
+ page2.reload
184
+ page.tags.should == %w(bar)
185
+ end
186
+
187
+ should "be able to pull all with criteria and modifier hashes" do
188
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
189
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
190
+
191
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
192
+
193
+ page.reload
194
+ page.tags.should == %w(baz)
195
+
196
+ page2.reload
197
+ page.tags.should == %w(baz)
198
+ end
199
+
200
+ should "be able to pull all with ids and modifier hash" do
201
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
202
+ page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
203
+
204
+ @page_class.pull_all(page.id, page2.id, :tags => %w(foo bar))
205
+
206
+ page.reload
207
+ page.tags.should == %w(baz)
208
+
209
+ page2.reload
210
+ page.tags.should == %w(baz)
211
+ end
212
+
213
+ should "be able to push uniq with criteria and modifier hash" do
214
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
215
+ page2 = @page_class.create(:title => 'Home')
216
+
217
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
218
+
219
+ page.reload
220
+ page.tags.should == %w(foo)
221
+
222
+ page2.reload
223
+ page.tags.should == %w(foo)
224
+ end
225
+
226
+ should "be able to push uniq with ids and modifier hash" do
227
+ page = @page_class.create(:title => 'Home', :tags => 'foo')
228
+ page2 = @page_class.create(:title => 'Home')
229
+
230
+ @page_class.push_uniq(page.id, page2.id, :tags => 'foo')
231
+
232
+ page.reload
233
+ page.tags.should == %w(foo)
234
+
235
+ page2.reload
236
+ page.tags.should == %w(foo)
237
+ end
238
+
239
+ should "be able to remove the last element the array" do
240
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
241
+ @page_class.pop(page.id, :tags => 1)
242
+ page.reload
243
+ page.tags.should == %w(foo)
244
+ end
245
+
246
+ should "be able to remove the first element of the array" do
247
+ page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
248
+ @page_class.pop(page.id, :tags => -1)
249
+ page.reload
250
+ page.tags.should == %w(bar)
251
+ end
252
+ 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
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ class ProtectedTest < Test::Unit::TestCase
4
+ context 'A document with protected attributes' do
5
+ setup do
6
+ @doc_class = Doc do
7
+ key :name, String
8
+ key :admin, Boolean, :default => false
9
+
10
+ attr_protected :admin
11
+ end
12
+
13
+ @doc = @doc_class.create(:name => 'Steve Sloan')
14
+ end
15
+
16
+ should 'have protected attributes class method' do
17
+ @doc_class.protected_attributes.should == [:admin].to_set
18
+ end
19
+
20
+ should "default protected attributes to nil" do
21
+ Doc().protected_attributes.should be_nil
22
+ end
23
+
24
+ should "have protected attributes instance method" do
25
+ @doc.protected_attributes.should equal(@doc_class.protected_attributes)
26
+ end
27
+
28
+ should "work with :protected shortcut when defining key" do
29
+ Doc() do
30
+ key :user_id, ObjectId, :protected => true
31
+ end.protected_attributes.should == [:user_id].to_set
32
+ end
33
+
34
+ should 'assign protected attribute through accessor' do
35
+ @doc.admin = true
36
+ @doc.admin.should be_true
37
+ end
38
+
39
+ should 'ignore protected attribute on #update_attributes' do
40
+ @doc.update_attributes(:name => 'Ren Hoek', :admin => true)
41
+ @doc.name.should == 'Ren Hoek'
42
+ @doc.admin.should be_false
43
+ end
44
+
45
+ should 'ignore protected attribute on #update_attributes!' do
46
+ @doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
47
+ @doc.name.should == 'Stimpson J. Cat'
48
+ @doc.admin.should be_false
49
+ end
50
+ end
51
+
52
+ context "Single collection inherited protected attributes" do
53
+ setup do
54
+ class ::GrandParent
55
+ include MongoMapper::Document
56
+
57
+ key :_type, String
58
+ key :site_id, ObjectId
59
+
60
+ attr_protected :site_id
61
+ end
62
+ GrandParent.collection.remove
63
+
64
+ class ::Child < ::GrandParent
65
+ key :position, Integer
66
+
67
+ attr_protected :position
68
+ end
69
+
70
+ class ::GrandChild < ::Child; end
71
+
72
+ class ::OtherChild < ::GrandParent
73
+ key :blog_id, ObjectId
74
+
75
+ attr_protected :blog_id
76
+ end
77
+ end
78
+
79
+ teardown do
80
+ Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
81
+ Object.send :remove_const, 'Child' if defined?(::Child)
82
+ Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
83
+ Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
84
+ end
85
+
86
+ should "share keys down the inheritance trail" do
87
+ GrandParent.protected_attributes.should == [:site_id].to_set
88
+ Child.protected_attributes.should == [:site_id, :position].to_set
89
+ GrandChild.protected_attributes.should == [:site_id, :position].to_set
90
+ OtherChild.protected_attributes.should == [:site_id, :blog_id].to_set
91
+ end
92
+ end
93
+
94
+ context 'An embedded document with protected attributes' do
95
+ setup do
96
+ @doc_class = Doc('Project')
97
+ @edoc_class = EDoc('Person') do
98
+ key :name, String
99
+ key :admin, Boolean, :default => false
100
+
101
+ attr_protected :admin
102
+ end
103
+ @doc_class.many :people, :class => @edoc_class
104
+
105
+ @doc = @doc_class.create(:title => 'MongoMapper')
106
+ @edoc = @edoc_class.new(:name => 'Steve Sloan')
107
+ @doc.people << @edoc
108
+ end
109
+
110
+ should 'have protected attributes class method' do
111
+ @edoc_class.protected_attributes.should == [:admin].to_set
112
+ end
113
+
114
+ should "default protected attributes to nil" do
115
+ EDoc().protected_attributes.should be_nil
116
+ end
117
+
118
+ should "have protected attributes instance method" do
119
+ @edoc.protected_attributes.should equal(@edoc_class.protected_attributes)
120
+ end
121
+
122
+ should 'assign protected attribute through accessor' do
123
+ @edoc.admin = true
124
+ @edoc.admin.should be_true
125
+ end
126
+
127
+ should 'ignore protected attribute on #update_attributes' do
128
+ @edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
129
+ @edoc.name.should == 'Ren Hoek'
130
+ @edoc.admin.should be_false
131
+ end
132
+
133
+ should 'ignore protected attribute on #update_attributes!' do
134
+ @edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
135
+ @edoc.name.should == 'Stimpson J. Cat'
136
+ @edoc.admin.should be_false
137
+ end
138
+ end
139
+ end