lookout-mongo_mapper 0.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +33 -0
  3. data/UPGRADES +26 -0
  4. data/bin/mmconsole +59 -0
  5. data/examples/attr_accessible.rb +22 -0
  6. data/examples/attr_protected.rb +22 -0
  7. data/examples/cache_key.rb +24 -0
  8. data/examples/custom_types.rb +24 -0
  9. data/examples/identity_map.rb +33 -0
  10. data/examples/identity_map/automatic.rb +2 -0
  11. data/examples/keys.rb +40 -0
  12. data/examples/modifiers/set.rb +25 -0
  13. data/examples/plugins.rb +38 -0
  14. data/examples/querying.rb +35 -0
  15. data/examples/safe.rb +43 -0
  16. data/examples/scopes.rb +52 -0
  17. data/examples/validating/embedded_docs.rb +29 -0
  18. data/lib/mongo_mapper.rb +94 -0
  19. data/lib/mongo_mapper/connection.rb +96 -0
  20. data/lib/mongo_mapper/document.rb +42 -0
  21. data/lib/mongo_mapper/embedded_document.rb +32 -0
  22. data/lib/mongo_mapper/exceptions.rb +30 -0
  23. data/lib/mongo_mapper/extensions/array.rb +19 -0
  24. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  25. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  26. data/lib/mongo_mapper/extensions/date.rb +25 -0
  27. data/lib/mongo_mapper/extensions/float.rb +14 -0
  28. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  29. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  30. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  31. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  32. data/lib/mongo_mapper/extensions/object.rb +26 -0
  33. data/lib/mongo_mapper/extensions/object_id.rb +32 -0
  34. data/lib/mongo_mapper/extensions/set.rb +20 -0
  35. data/lib/mongo_mapper/extensions/string.rb +18 -0
  36. data/lib/mongo_mapper/extensions/time.rb +28 -0
  37. data/lib/mongo_mapper/locale/en.yml +5 -0
  38. data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
  39. data/lib/mongo_mapper/plugins.rb +22 -0
  40. data/lib/mongo_mapper/plugins/accessible.rb +52 -0
  41. data/lib/mongo_mapper/plugins/active_model.rb +18 -0
  42. data/lib/mongo_mapper/plugins/associations.rb +90 -0
  43. data/lib/mongo_mapper/plugins/associations/base.rb +92 -0
  44. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +54 -0
  45. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +34 -0
  46. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
  47. data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
  48. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +44 -0
  49. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +133 -0
  50. data/lib/mongo_mapper/plugins/associations/many_association.rb +63 -0
  51. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  52. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +118 -0
  53. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  54. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  55. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  56. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +22 -0
  57. data/lib/mongo_mapper/plugins/associations/one_association.rb +48 -0
  58. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
  59. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
  60. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +95 -0
  61. data/lib/mongo_mapper/plugins/associations/proxy.rb +134 -0
  62. data/lib/mongo_mapper/plugins/associations/single_association.rb +46 -0
  63. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  64. data/lib/mongo_mapper/plugins/callbacks.rb +29 -0
  65. data/lib/mongo_mapper/plugins/clone.rb +22 -0
  66. data/lib/mongo_mapper/plugins/dirty.rb +60 -0
  67. data/lib/mongo_mapper/plugins/document.rb +41 -0
  68. data/lib/mongo_mapper/plugins/dynamic_querying.rb +45 -0
  69. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  70. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +56 -0
  71. data/lib/mongo_mapper/plugins/embedded_document.rb +53 -0
  72. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  73. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  74. data/lib/mongo_mapper/plugins/indexes.rb +13 -0
  75. data/lib/mongo_mapper/plugins/inspect.rb +16 -0
  76. data/lib/mongo_mapper/plugins/keys.rb +313 -0
  77. data/lib/mongo_mapper/plugins/keys/key.rb +61 -0
  78. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  79. data/lib/mongo_mapper/plugins/modifiers.rb +134 -0
  80. data/lib/mongo_mapper/plugins/pagination.rb +16 -0
  81. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  82. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  83. data/lib/mongo_mapper/plugins/querying.rb +165 -0
  84. data/lib/mongo_mapper/plugins/querying/decorator.rb +36 -0
  85. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  86. data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
  87. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  88. data/lib/mongo_mapper/plugins/sci.rb +36 -0
  89. data/lib/mongo_mapper/plugins/scopes.rb +27 -0
  90. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  91. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  92. data/lib/mongo_mapper/plugins/touch.rb +18 -0
  93. data/lib/mongo_mapper/plugins/userstamps.rb +18 -0
  94. data/lib/mongo_mapper/plugins/validations.rb +86 -0
  95. data/lib/mongo_mapper/railtie.rb +48 -0
  96. data/lib/mongo_mapper/railtie/database.rake +65 -0
  97. data/lib/mongo_mapper/translation.rb +10 -0
  98. data/lib/mongo_mapper/version.rb +4 -0
  99. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +24 -0
  100. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +18 -0
  101. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +23 -0
  102. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +13 -0
  103. data/test/_NOTE_ON_TESTING +1 -0
  104. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
  105. data/test/functional/associations/test_belongs_to_proxy.rb +238 -0
  106. data/test/functional/associations/test_in_array_proxy.rb +349 -0
  107. data/test/functional/associations/test_many_documents_as_proxy.rb +231 -0
  108. data/test/functional/associations/test_many_documents_proxy.rb +866 -0
  109. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +239 -0
  110. data/test/functional/associations/test_many_embedded_proxy.rb +289 -0
  111. data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
  112. data/test/functional/associations/test_one_as_proxy.rb +491 -0
  113. data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
  114. data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
  115. data/test/functional/associations/test_one_proxy.rb +383 -0
  116. data/test/functional/test_accessible.rb +198 -0
  117. data/test/functional/test_associations.rb +46 -0
  118. data/test/functional/test_binary.rb +27 -0
  119. data/test/functional/test_caching.rb +77 -0
  120. data/test/functional/test_callbacks.rb +232 -0
  121. data/test/functional/test_dirty.rb +301 -0
  122. data/test/functional/test_document.rb +282 -0
  123. data/test/functional/test_dynamic_querying.rb +75 -0
  124. data/test/functional/test_embedded_document.rb +288 -0
  125. data/test/functional/test_equality.rb +20 -0
  126. data/test/functional/test_identity_map.rb +513 -0
  127. data/test/functional/test_indexes.rb +50 -0
  128. data/test/functional/test_logger.rb +20 -0
  129. data/test/functional/test_modifiers.rb +537 -0
  130. data/test/functional/test_pagination.rb +91 -0
  131. data/test/functional/test_protected.rb +201 -0
  132. data/test/functional/test_querying.rb +935 -0
  133. data/test/functional/test_safe.rb +76 -0
  134. data/test/functional/test_sci.rb +240 -0
  135. data/test/functional/test_scopes.rb +171 -0
  136. data/test/functional/test_timestamps.rb +62 -0
  137. data/test/functional/test_touch.rb +125 -0
  138. data/test/functional/test_userstamps.rb +44 -0
  139. data/test/functional/test_validations.rb +414 -0
  140. data/test/models.rb +261 -0
  141. data/test/support/railtie.rb +4 -0
  142. data/test/support/railtie/autoloaded.rb +2 -0
  143. data/test/support/railtie/not_autoloaded.rb +3 -0
  144. data/test/support/railtie/parent.rb +3 -0
  145. data/test/test_active_model_lint.rb +18 -0
  146. data/test/test_helper.rb +93 -0
  147. data/test/unit/associations/test_base.rb +146 -0
  148. data/test/unit/associations/test_belongs_to_association.rb +29 -0
  149. data/test/unit/associations/test_many_association.rb +63 -0
  150. data/test/unit/associations/test_one_association.rb +47 -0
  151. data/test/unit/associations/test_proxy.rb +100 -0
  152. data/test/unit/serializers/test_json_serializer.rb +216 -0
  153. data/test/unit/serializers/test_xml_serializer.rb +196 -0
  154. data/test/unit/test_clone.rb +69 -0
  155. data/test/unit/test_document.rb +249 -0
  156. data/test/unit/test_dynamic_finder.rb +125 -0
  157. data/test/unit/test_embedded_document.rb +682 -0
  158. data/test/unit/test_equality.rb +38 -0
  159. data/test/unit/test_exceptions.rb +12 -0
  160. data/test/unit/test_extensions.rb +380 -0
  161. data/test/unit/test_identity_map_middleware.rb +34 -0
  162. data/test/unit/test_inspect.rb +47 -0
  163. data/test/unit/test_key.rb +205 -0
  164. data/test/unit/test_keys.rb +65 -0
  165. data/test/unit/test_mongo_mapper.rb +143 -0
  166. data/test/unit/test_pagination.rb +11 -0
  167. data/test/unit/test_plugins.rb +89 -0
  168. data/test/unit/test_rails.rb +183 -0
  169. data/test/unit/test_rails_compatibility.rb +38 -0
  170. data/test/unit/test_rails_reflect_on_association.rb +118 -0
  171. data/test/unit/test_railtie.rb +66 -0
  172. data/test/unit/test_serialization.rb +166 -0
  173. data/test/unit/test_time_zones.rb +44 -0
  174. data/test/unit/test_translation.rb +27 -0
  175. data/test/unit/test_validations.rb +562 -0
  176. metadata +285 -0
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class IndexingTest < Test::Unit::TestCase
4
+ context "Indexing" do
5
+ setup do
6
+ @document = Doc do
7
+ key :first_name, String
8
+ key :last_name, String
9
+ key :age, Integer
10
+ key :date, Date
11
+ end
12
+ end
13
+ teardown { drop_indexes(@document) }
14
+
15
+ [:create_index, :ensure_index, :drop_index, :drop_indexes].each do |method|
16
+ should "delegate #{method} to collection" do
17
+ @document.stubs(:collection).returns(mock(:name => :foo))
18
+ @document.collection.expects(method).with(:arg)
19
+ @document.send(method, :arg)
20
+ end
21
+ end
22
+
23
+ should "allow creating index for a key" do
24
+ @document.ensure_index :first_name
25
+ @document.should have_index('first_name_1')
26
+ end
27
+
28
+ should "allow creating unique index for a key" do
29
+ @document.ensure_index :first_name, :unique => true
30
+ @document.should have_index('first_name_1')
31
+ end
32
+
33
+ should "allow creating index on multiple keys" do
34
+ @document.ensure_index [[:first_name, 1], [:last_name, -1]]
35
+
36
+ # order is different for different versions of ruby so instead of
37
+ # just checking have_index('first_name_1_last_name_-1') I'm checking
38
+ # the values of the indexes to make sure the index creation was successful
39
+ @document.collection.index_information.detect do |index|
40
+ keys = index[0]
41
+ keys.include?('first_name_1') && keys.include?('last_name_-1')
42
+ end.should_not be_nil
43
+ end
44
+
45
+ should "work with :index shortcut when defining key" do
46
+ silence_stderr { @document.key :father, String, :index => true }
47
+ @document.should have_index('father_1')
48
+ end
49
+ end
50
+ 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,537 @@
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
+
47
+ context "additional options (upsert & safe)" do
48
+ should "be able to pass upsert option" do
49
+ new_key_value = DateTime.now.to_s
50
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true})
51
+ @page_class.count(:title => new_key_value).should == 1
52
+ @page_class.first(:title => new_key_value).tags.should == []
53
+ end
54
+
55
+ should "be able to pass safe option" do
56
+ @page_class.create(:title => "Better Be Safe than Sorry")
57
+
58
+ Mongo::Collection.any_instance.expects(:update).with(
59
+ {:title => "Better Be Safe than Sorry"},
60
+ {'$unset' => {:tags => 1}},
61
+ {:safe => true, :multi => true}
62
+ )
63
+ @page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:safe => true})
64
+ end
65
+
66
+ should "be able to pass both safe and upsert options" do
67
+ new_key_value = DateTime.now.to_s
68
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true, :safe => true})
69
+ @page_class.count(:title => new_key_value).should == 1
70
+ @page_class.first(:title => new_key_value).tags.should == []
71
+ end
72
+ end
73
+ end
74
+
75
+ context "increment" do
76
+ setup do
77
+ @page = @page_class.create(:title => 'Home')
78
+ @page2 = @page_class.create(:title => 'Home')
79
+ end
80
+
81
+ should "work with criteria and modifier hashes" do
82
+ @page_class.increment({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
83
+
84
+ assert_page_counts @page, 1, 2, 3
85
+ assert_page_counts @page2, 1, 2, 3
86
+ end
87
+
88
+ should "work with ids and modifier hash" do
89
+ @page_class.increment(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
90
+
91
+ assert_page_counts @page, 1, 2, 3
92
+ assert_page_counts @page2, 1, 2, 3
93
+ end
94
+ end
95
+
96
+ context "decrement" do
97
+ setup do
98
+ @page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
99
+ @page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
100
+ end
101
+
102
+ should "work with criteria and modifier hashes" do
103
+ @page_class.decrement({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
104
+
105
+ assert_page_counts @page, 0, 0, 0
106
+ assert_page_counts @page2, 0, 0, 0
107
+ end
108
+
109
+ should "work with ids and modifier hash" do
110
+ @page_class.decrement(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
111
+
112
+ assert_page_counts @page, 0, 0, 0
113
+ assert_page_counts @page2, 0, 0, 0
114
+ end
115
+
116
+ should "decrement with positive or negative numbers" do
117
+ @page_class.decrement(@page.id, @page2.id, :day_count => -1, :week_count => 2, :month_count => -3)
118
+
119
+ assert_page_counts @page, 0, 0, 0
120
+ assert_page_counts @page2, 0, 0, 0
121
+ end
122
+ end
123
+
124
+ context "set" 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.set({:title => 'Home'}, :title => 'Home Revised')
132
+
133
+ @page.reload
134
+ @page.title.should == 'Home Revised'
135
+
136
+ @page2.reload
137
+ @page2.title.should == 'Home Revised'
138
+ end
139
+
140
+ should "work with ids and modifier hash" do
141
+ @page_class.set(@page.id, @page2.id, :title => 'Home Revised')
142
+
143
+ @page.reload
144
+ @page.title.should == 'Home Revised'
145
+
146
+ @page2.reload
147
+ @page2.title.should == 'Home Revised'
148
+ end
149
+
150
+ should "typecast values before querying" do
151
+ @page_class.key :tags, Set
152
+
153
+ assert_nothing_raised do
154
+ @page_class.set(@page.id, :tags => ['foo', 'bar'].to_set)
155
+ @page.reload
156
+ @page.tags.should == Set.new(['foo', 'bar'])
157
+ end
158
+ end
159
+
160
+ should "not typecast keys that are not defined in document" do
161
+ assert_raises(BSON::InvalidDocument) do
162
+ @page_class.set(@page.id, :colors => ['red', 'green'].to_set)
163
+ end
164
+ end
165
+
166
+ should "set keys that are not defined in document" do
167
+ @page_class.set(@page.id, :colors => %w[red green])
168
+ @page.reload
169
+ @page[:colors].should == %w[red green]
170
+ end
171
+
172
+ context "additional options (upsert & safe)" do
173
+ should "be able to pass upsert option" do
174
+ new_key_value = DateTime.now.to_s
175
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true})
176
+ @page_class.count(:title => new_key_value).should == 1
177
+ @page_class.first(:title => new_key_value).day_count.should == 1
178
+ end
179
+
180
+ should "be able to pass safe option" do
181
+ @page_class.create(:title => "Better Be Safe than Sorry")
182
+
183
+ Mongo::Collection.any_instance.expects(:update).with(
184
+ {:title => "Better Be Safe than Sorry"},
185
+ {'$set' => {:title => "I like safety."}},
186
+ {:safe => true, :multi => true}
187
+ )
188
+ @page_class.set({:title => "Better Be Safe than Sorry"}, {:title => "I like safety."}, {:safe => true})
189
+ end
190
+
191
+ should "be able to pass both safe and upsert options" do
192
+ new_key_value = DateTime.now.to_s
193
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true, :safe => true})
194
+ @page_class.count(:title => new_key_value).should == 1
195
+ @page_class.first(:title => new_key_value).day_count.should == 1
196
+ end
197
+ end
198
+ end
199
+
200
+ context "push" do
201
+ setup do
202
+ @page = @page_class.create(:title => 'Home')
203
+ @page2 = @page_class.create(:title => 'Home')
204
+ end
205
+
206
+ should "work with criteria and modifier hashes" do
207
+ @page_class.push({:title => 'Home'}, :tags => 'foo')
208
+
209
+ @page.reload
210
+ @page.tags.should == %w(foo)
211
+
212
+ @page2.reload
213
+ @page2.tags.should == %w(foo)
214
+ end
215
+
216
+ should "work with ids and modifier hash" do
217
+ @page_class.push(@page.id, @page2.id, :tags => 'foo')
218
+
219
+ @page.reload
220
+ @page.tags.should == %w(foo)
221
+
222
+ @page2.reload
223
+ @page2.tags.should == %w(foo)
224
+ end
225
+ end
226
+
227
+ context "push_all" do
228
+ setup do
229
+ @page = @page_class.create(:title => 'Home')
230
+ @page2 = @page_class.create(:title => 'Home')
231
+ @tags = %w(foo bar)
232
+ end
233
+
234
+ should "work with criteria and modifier hashes" do
235
+ @page_class.push_all({:title => 'Home'}, :tags => @tags)
236
+
237
+ @page.reload
238
+ @page.tags.should == @tags
239
+
240
+ @page2.reload
241
+ @page2.tags.should == @tags
242
+ end
243
+
244
+ should "work with ids and modifier hash" do
245
+ @page_class.push_all(@page.id, @page2.id, :tags => @tags)
246
+
247
+ @page.reload
248
+ @page.tags.should == @tags
249
+
250
+ @page2.reload
251
+ @page2.tags.should == @tags
252
+ end
253
+ end
254
+
255
+ context "pull" do
256
+ setup do
257
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
258
+ @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
259
+ end
260
+
261
+ should "work with criteria and modifier hashes" do
262
+ @page_class.pull({:title => 'Home'}, :tags => 'foo')
263
+
264
+ @page.reload
265
+ @page.tags.should == %w(bar)
266
+
267
+ @page2.reload
268
+ @page2.tags.should == %w(bar)
269
+ end
270
+
271
+ should "be able to pull with ids and modifier hash" do
272
+ @page_class.pull(@page.id, @page2.id, :tags => 'foo')
273
+
274
+ @page.reload
275
+ @page.tags.should == %w(bar)
276
+
277
+ @page2.reload
278
+ @page2.tags.should == %w(bar)
279
+ end
280
+ end
281
+
282
+ context "pull_all" do
283
+ setup do
284
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
285
+ @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
286
+ end
287
+
288
+ should "work with criteria and modifier hashes" do
289
+ @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
290
+
291
+ @page.reload
292
+ @page.tags.should == %w(baz)
293
+
294
+ @page2.reload
295
+ @page2.tags.should == %w(baz)
296
+ end
297
+
298
+ should "work with ids and modifier hash" do
299
+ @page_class.pull_all(@page.id, @page2.id, :tags => %w(foo bar))
300
+
301
+ @page.reload
302
+ @page.tags.should == %w(baz)
303
+
304
+ @page2.reload
305
+ @page2.tags.should == %w(baz)
306
+ end
307
+ end
308
+
309
+ context "add_to_set" do
310
+ setup do
311
+ @page = @page_class.create(:title => 'Home', :tags => 'foo')
312
+ @page2 = @page_class.create(:title => 'Home')
313
+ end
314
+
315
+ should "be able to add to set with criteria and modifier hash" do
316
+ @page_class.add_to_set({:title => 'Home'}, :tags => 'foo')
317
+
318
+ @page.reload
319
+ @page.tags.should == %w(foo)
320
+
321
+ @page2.reload
322
+ @page2.tags.should == %w(foo)
323
+ end
324
+
325
+ should "be able to add to set with ids and modifier hash" do
326
+ @page_class.add_to_set(@page.id, @page2.id, :tags => 'foo')
327
+
328
+ @page.reload
329
+ @page.tags.should == %w(foo)
330
+
331
+ @page2.reload
332
+ @page2.tags.should == %w(foo)
333
+ end
334
+ end
335
+
336
+ context "push_uniq" do
337
+ setup do
338
+ @page = @page_class.create(:title => 'Home', :tags => 'foo')
339
+ @page2 = @page_class.create(:title => 'Home')
340
+ end
341
+
342
+ should "be able to push uniq with criteria and modifier hash" do
343
+ @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
344
+
345
+ @page.reload
346
+ @page.tags.should == %w(foo)
347
+
348
+ @page2.reload
349
+ @page2.tags.should == %w(foo)
350
+ end
351
+
352
+ should "be able to push uniq with ids and modifier hash" do
353
+ @page_class.push_uniq(@page.id, @page2.id, :tags => 'foo')
354
+
355
+ @page.reload
356
+ @page.tags.should == %w(foo)
357
+
358
+ @page2.reload
359
+ @page2.tags.should == %w(foo)
360
+ end
361
+ end
362
+
363
+ context "pop" do
364
+ setup do
365
+ @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
366
+ end
367
+
368
+ should "be able to remove the last element the array" do
369
+ @page_class.pop(@page.id, :tags => 1)
370
+ @page.reload
371
+ @page.tags.should == %w(foo)
372
+ end
373
+
374
+ should "be able to remove the first element of the array" do
375
+ @page_class.pop(@page.id, :tags => -1)
376
+ @page.reload
377
+ @page.tags.should == %w(bar)
378
+ end
379
+ end
380
+
381
+ context "additional options (upsert & safe)" do
382
+ should "be able to pass upsert option" do
383
+ new_key_value = DateTime.now.to_s
384
+ @page_class.increment({:title => new_key_value}, {:day_count => 1}, {:upsert => true})
385
+ @page_class.count(:title => new_key_value).should == 1
386
+ @page_class.first(:title => new_key_value).day_count.should == 1
387
+ end
388
+
389
+ should "be able to pass safe option" do
390
+ @page_class.create(:title => "Better Be Safe than Sorry")
391
+
392
+ # We are trying to increment a key of type string here which should fail
393
+ assert_raises(Mongo::OperationFailure) do
394
+ @page_class.increment({:title => "Better Be Safe than Sorry"}, {:title => 1}, {:safe => true})
395
+ end
396
+ end
397
+
398
+ should "be able to pass both safe and upsert options" do
399
+ new_key_value = DateTime.now.to_s
400
+ @page_class.increment({:title => new_key_value}, {:day_count => 1}, {:upsert => true, :safe => true})
401
+ @page_class.count(:title => new_key_value).should == 1
402
+ @page_class.first(:title => new_key_value).day_count.should == 1
403
+ end
404
+ end
405
+ end
406
+
407
+ context "instance methods" do
408
+ should "be able to unset with keys" do
409
+ page = @page_class.create(:title => 'Foo', :tags => %w(foo))
410
+ page.unset(:title, :tags)
411
+ assert_keys_removed page, :title, :tags
412
+ end
413
+
414
+ should "be able to increment with modifier hashes" do
415
+ page = @page_class.create
416
+ page.increment(:day_count => 1, :week_count => 2, :month_count => 3)
417
+
418
+ assert_page_counts page, 1, 2, 3
419
+ end
420
+
421
+ should "be able to decrement with modifier hashes" do
422
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
423
+ page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
424
+
425
+ assert_page_counts page, 0, 0, 0
426
+ end
427
+
428
+ should "always decrement when decrement is called whether number is positive or negative" do
429
+ page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
430
+ page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
431
+
432
+ assert_page_counts page, 0, 0, 0
433
+ end
434
+
435
+ should "be able to set with modifier hashes" do
436
+ page = @page_class.create(:title => 'Home')
437
+ page.set(:title => 'Home Revised')
438
+
439
+ page.reload
440
+ page.title.should == 'Home Revised'
441
+ end
442
+
443
+ should "be able to push with modifier hashes" do
444
+ page = @page_class.create
445
+ page.push(:tags => 'foo')
446
+
447
+ page.reload
448
+ page.tags.should == %w(foo)
449
+ end
450
+
451
+ should "be able to push_all with modifier hashes" do
452
+ page = @page_class.create
453
+ page.push_all(:tags => %w(foo bar))
454
+
455
+ page.reload
456
+ page.tags.should == %w(foo bar)
457
+ end
458
+
459
+ should "be able to pull with criteria and modifier hashes" do
460
+ page = @page_class.create(:tags => %w(foo bar))
461
+ page.pull(:tags => 'foo')
462
+
463
+ page.reload
464
+ page.tags.should == %w(bar)
465
+ end
466
+
467
+ should "be able to pull_all with criteria and modifier hashes" do
468
+ page = @page_class.create(:tags => %w(foo bar baz))
469
+ page.pull_all(:tags => %w(foo bar))
470
+
471
+ page.reload
472
+ page.tags.should == %w(baz)
473
+ end
474
+
475
+ should "be able to add_to_set with criteria and modifier hash" do
476
+ page = @page_class.create(:tags => 'foo')
477
+ page2 = @page_class.create
478
+
479
+ page.add_to_set(:tags => 'foo')
480
+ page2.add_to_set(:tags => 'foo')
481
+
482
+ page.reload
483
+ page.tags.should == %w(foo)
484
+
485
+ page2.reload
486
+ page2.tags.should == %w(foo)
487
+ end
488
+
489
+ should "be able to push uniq with criteria and modifier hash" do
490
+ page = @page_class.create(:tags => 'foo')
491
+ page2 = @page_class.create
492
+
493
+ page.push_uniq(:tags => 'foo')
494
+ page2.push_uniq(:tags => 'foo')
495
+
496
+ page.reload
497
+ page.tags.should == %w(foo)
498
+
499
+ page2.reload
500
+ page2.tags.should == %w(foo)
501
+ end
502
+
503
+ should "be able to pop with modifier hashes" do
504
+ page = @page_class.create(:tags => %w(foo bar))
505
+ page.pop(:tags => 1)
506
+
507
+ page.reload
508
+ page.tags.should == %w(foo)
509
+ end
510
+
511
+ should "be able to pass upsert option" do
512
+ page = @page_class.create(:title => "Upsert Page")
513
+ page.increment({:new_count => 1}, {:upsert => true})
514
+
515
+ page.reload
516
+ page.new_count.should == 1
517
+ end
518
+
519
+ should "be able to pass safe option" do
520
+ page = @page_class.create(:title => "Safe Page")
521
+
522
+ # We are trying to increment a key of type string here which should fail
523
+ assert_raises(Mongo::OperationFailure) do
524
+ page.increment({:title => 1}, {:safe => true})
525
+ end
526
+ end
527
+
528
+ should "be able to pass upsert and safe options" do
529
+ page = @page_class.create(:title => "Upsert and Safe Page")
530
+ page.increment({:another_count => 1}, {:upsert => true, :safe => true})
531
+
532
+ page.reload
533
+ page.another_count.should == 1
534
+ end
535
+
536
+ end
537
+ end