mongo_mapper 0.12.0 → 0.13.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +35 -13
  3. data/bin/mmconsole +1 -1
  4. data/lib/mongo_mapper.rb +4 -0
  5. data/lib/mongo_mapper/connection.rb +17 -6
  6. data/lib/mongo_mapper/document.rb +1 -0
  7. data/lib/mongo_mapper/exceptions.rb +4 -1
  8. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  9. data/lib/mongo_mapper/extensions/boolean.rb +20 -23
  10. data/lib/mongo_mapper/extensions/date.rb +3 -3
  11. data/lib/mongo_mapper/extensions/integer.rb +5 -1
  12. data/lib/mongo_mapper/extensions/kernel.rb +2 -0
  13. data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
  14. data/lib/mongo_mapper/extensions/string.rb +2 -2
  15. data/lib/mongo_mapper/extensions/time.rb +7 -5
  16. data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
  17. data/lib/mongo_mapper/plugins.rb +1 -1
  18. data/lib/mongo_mapper/plugins/associations.rb +11 -5
  19. data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
  20. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
  21. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
  22. data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
  23. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
  24. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
  25. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
  26. data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
  27. data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
  28. data/lib/mongo_mapper/plugins/clone.rb +4 -2
  29. data/lib/mongo_mapper/plugins/dirty.rb +22 -21
  30. data/lib/mongo_mapper/plugins/document.rb +4 -4
  31. data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
  32. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
  33. data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
  34. data/lib/mongo_mapper/plugins/keys.rb +133 -54
  35. data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
  36. data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
  37. data/lib/mongo_mapper/plugins/persistence.rb +15 -5
  38. data/lib/mongo_mapper/plugins/querying.rb +15 -40
  39. data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
  40. data/lib/mongo_mapper/plugins/rails.rb +22 -2
  41. data/lib/mongo_mapper/plugins/safe.rb +8 -5
  42. data/lib/mongo_mapper/plugins/sci.rb +26 -4
  43. data/lib/mongo_mapper/plugins/scopes.rb +5 -4
  44. data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
  45. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  46. data/lib/mongo_mapper/utils.rb +12 -0
  47. data/lib/mongo_mapper/version.rb +1 -1
  48. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
  49. data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
  50. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
  51. data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
  52. data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
  53. data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
  54. data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
  55. data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
  56. data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
  57. data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
  58. data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
  59. data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
  60. data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
  61. data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
  62. data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
  63. data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
  64. data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
  65. data/spec/functional/associations_spec.rb +48 -0
  66. data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
  67. data/spec/functional/caching_spec.rb +75 -0
  68. data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
  69. data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
  70. data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
  71. data/spec/functional/dumpable_spec.rb +24 -0
  72. data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
  73. data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
  74. data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
  75. data/spec/functional/extensions_spec.rb +16 -0
  76. data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
  77. data/spec/functional/indexes_spec.rb +48 -0
  78. data/spec/functional/keys_spec.rb +224 -0
  79. data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
  80. data/spec/functional/modifiers_spec.rb +550 -0
  81. data/spec/functional/pagination_spec.rb +89 -0
  82. data/spec/functional/protected_spec.rb +199 -0
  83. data/spec/functional/querying_spec.rb +1003 -0
  84. data/spec/functional/rails_spec.rb +55 -0
  85. data/spec/functional/safe_spec.rb +163 -0
  86. data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
  87. data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
  88. data/spec/functional/timestamps_spec.rb +97 -0
  89. data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
  90. data/spec/functional/userstamps_spec.rb +46 -0
  91. data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
  92. data/spec/spec_helper.rb +81 -0
  93. data/spec/support/matchers.rb +24 -0
  94. data/{test → spec/support}/models.rb +1 -6
  95. data/spec/unit/associations/base_spec.rb +146 -0
  96. data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
  97. data/spec/unit/associations/many_association_spec.rb +64 -0
  98. data/spec/unit/associations/one_association_spec.rb +48 -0
  99. data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
  100. data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
  101. data/spec/unit/config_generator_spec.rb +24 -0
  102. data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
  103. data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
  104. data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
  105. data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
  106. data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
  107. data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
  108. data/spec/unit/identity_map_middleware_spec.rb +134 -0
  109. data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
  110. data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
  111. data/spec/unit/keys_spec.rb +155 -0
  112. data/spec/unit/model_generator_spec.rb +47 -0
  113. data/spec/unit/mongo_mapper_spec.rb +184 -0
  114. data/spec/unit/pagination_spec.rb +11 -0
  115. data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
  116. data/spec/unit/rails_compatibility_spec.rb +40 -0
  117. data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
  118. data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
  119. data/spec/unit/serialization_spec.rb +169 -0
  120. data/spec/unit/serializers/json_serializer_spec.rb +218 -0
  121. data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
  122. data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
  123. data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
  124. data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
  125. metadata +199 -179
  126. data/test/_NOTE_ON_TESTING +0 -1
  127. data/test/functional/test_associations.rb +0 -46
  128. data/test/functional/test_caching.rb +0 -77
  129. data/test/functional/test_indexes.rb +0 -50
  130. data/test/functional/test_modifiers.rb +0 -537
  131. data/test/functional/test_pagination.rb +0 -91
  132. data/test/functional/test_protected.rb +0 -201
  133. data/test/functional/test_querying.rb +0 -935
  134. data/test/functional/test_safe.rb +0 -76
  135. data/test/functional/test_timestamps.rb +0 -62
  136. data/test/functional/test_userstamps.rb +0 -44
  137. data/test/support/railtie.rb +0 -4
  138. data/test/support/railtie/autoloaded.rb +0 -2
  139. data/test/support/railtie/not_autoloaded.rb +0 -3
  140. data/test/support/railtie/parent.rb +0 -3
  141. data/test/test_active_model_lint.rb +0 -18
  142. data/test/test_helper.rb +0 -93
  143. data/test/unit/associations/test_base.rb +0 -146
  144. data/test/unit/associations/test_belongs_to_association.rb +0 -29
  145. data/test/unit/associations/test_many_association.rb +0 -63
  146. data/test/unit/associations/test_one_association.rb +0 -47
  147. data/test/unit/serializers/test_json_serializer.rb +0 -216
  148. data/test/unit/serializers/test_xml_serializer.rb +0 -196
  149. data/test/unit/test_identity_map_middleware.rb +0 -132
  150. data/test/unit/test_keys.rb +0 -65
  151. data/test/unit/test_mongo_mapper.rb +0 -157
  152. data/test/unit/test_pagination.rb +0 -11
  153. data/test/unit/test_rails_compatibility.rb +0 -38
  154. data/test/unit/test_serialization.rb +0 -166
@@ -1 +0,0 @@
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.
@@ -1,46 +0,0 @@
1
- require 'test_helper'
2
- require 'models'
3
-
4
- class AssociationsTest < Test::Unit::TestCase
5
- should "allow changing class names" do
6
- class AwesomeUser
7
- include MongoMapper::Document
8
-
9
- many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
10
- end
11
- AwesomeUser.collection.remove
12
-
13
- class AwesomeTag
14
- include MongoMapper::EmbeddedDocument
15
-
16
- key :name, String
17
- key :post_id, ObjectId
18
-
19
- belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
20
- end
21
-
22
- class AwesomePost
23
- include MongoMapper::Document
24
-
25
- key :creator_id, ObjectId
26
-
27
- belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
28
- many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
29
- end
30
-
31
- AwesomeUser.collection.remove
32
- AwesomePost.collection.remove
33
-
34
- user = AwesomeUser.create
35
- tag1 = AwesomeTag.new(:name => 'awesome')
36
- tag2 = AwesomeTag.new(:name => 'grand')
37
- post1 = AwesomePost.create(:creator => user, :tags => [tag1])
38
- post2 = AwesomePost.create(:creator => user, :tags => [tag2])
39
-
40
- user.reload
41
- user.posts.should == [post1, post2]
42
-
43
- post1 = post1.reload
44
- post1.tags.should == [tag1]
45
- end
46
- end
@@ -1,77 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CachingTest < Test::Unit::TestCase
4
- context "Caching" do
5
- setup do
6
- @klass = Class.new do
7
- extend MongoMapper::Plugins
8
- plugin MongoMapper::Plugins::Caching
9
- end
10
- @klass.stubs(:name).returns('Post')
11
- @klass.any_instance.stubs(:persisted?).returns(true)
12
- @klass.any_instance.stubs(:[]).returns(nil)
13
- @klass.any_instance.stubs(:[]=).returns(nil)
14
- end
15
-
16
- context "new" do
17
- setup do
18
- @doc = @klass.new
19
- @doc.stubs(:persisted?).returns(false)
20
- end
21
-
22
- should "be class/new" do
23
- @doc.cache_key.should == 'Post/new'
24
- end
25
-
26
- should "work with suffix" do
27
- @doc.cache_key(:foo).
28
- should == 'Post/new/foo'
29
-
30
- @doc.cache_key(:foo, :bar).
31
- should == 'Post/new/foo/bar'
32
- end
33
- end
34
-
35
- context "not new" do
36
- setup do
37
- @object_id = BSON::ObjectId.new
38
- @doc = @klass.new
39
- @doc.stubs(:persisted).returns(true)
40
- @doc.stubs(:id).returns(@object_id)
41
- end
42
-
43
- context "with updated_at" do
44
- setup do
45
- time = Time.utc(2010, 6, 20, 8, 10, 7)
46
- @doc.stubs(:[]).with(:updated_at).returns(time)
47
- end
48
-
49
- should "be class/id-timestamp" do
50
- @doc.cache_key.should == "Post/#{@object_id}-20100620081007"
51
- end
52
-
53
- should "work with suffix" do
54
- @doc.cache_key(:foo).
55
- should == "Post/#{@object_id}-20100620081007/foo"
56
-
57
- @doc.cache_key(:foo, :bar).
58
- should == "Post/#{@object_id}-20100620081007/foo/bar"
59
- end
60
- end
61
-
62
- context "without updated_at" do
63
- should "be class/id" do
64
- @doc.cache_key.should == "Post/#{@object_id}"
65
- end
66
-
67
- should "work with suffix" do
68
- @doc.cache_key(:foo).
69
- should == "Post/#{@object_id}/foo"
70
-
71
- @doc.cache_key(:foo, :bar, :baz).
72
- should == "Post/#{@object_id}/foo/bar/baz"
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,50 +0,0 @@
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
@@ -1,537 +0,0 @@
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