mongo_mapper 0.7.0 → 0.7.1
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.
- data/README.rdoc +3 -1
- data/Rakefile +9 -12
- data/lib/mongo_mapper.rb +30 -10
- data/lib/mongo_mapper/document.rb +16 -74
- data/lib/mongo_mapper/embedded_document.rb +7 -1
- data/lib/mongo_mapper/plugins.rb +3 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -12
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +6 -1
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +4 -1
- data/lib/mongo_mapper/plugins/callbacks.rb +183 -12
- data/lib/mongo_mapper/plugins/keys.rb +17 -5
- data/lib/mongo_mapper/plugins/modifiers.rb +87 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +7 -3
- data/lib/mongo_mapper/plugins/protected.rb +1 -1
- data/lib/mongo_mapper/plugins/rails.rb +16 -8
- data/lib/mongo_mapper/plugins/serialization.rb +51 -81
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
- data/lib/mongo_mapper/query.rb +1 -1
- data/lib/mongo_mapper/version.rb +3 -0
- data/mongo_mapper.gemspec +22 -11
- data/test/active_model_lint_test.rb +11 -0
- data/test/functional/associations/test_in_array_proxy.rb +16 -0
- data/test/functional/associations/test_many_documents_proxy.rb +22 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_document.rb +70 -149
- data/test/functional/test_embedded_document.rb +39 -34
- data/test/functional/test_indexing.rb +44 -0
- data/test/functional/test_modifiers.rb +297 -227
- data/test/functional/test_protected.rb +11 -5
- data/test/functional/test_timestamps.rb +64 -0
- data/test/functional/test_userstamps.rb +28 -0
- data/test/support/timing.rb +1 -1
- data/test/unit/serializers/test_json_serializer.rb +30 -17
- data/test/unit/test_embedded_document.rb +15 -15
- data/test/unit/test_keys.rb +15 -11
- data/test/unit/test_mongo_mapper.rb +31 -1
- data/test/unit/test_pagination.rb +33 -0
- data/test/unit/test_query.rb +6 -0
- data/test/unit/test_serialization.rb +3 -3
- data/test/unit/test_support.rb +9 -5
- metadata +17 -6
- data/VERSION +0 -1
data/mongo_mapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_mapper}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-09}
|
13
13
|
s.default_executable = %q{mmconsole}
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
15
15
|
s.executables = ["mmconsole"]
|
@@ -22,7 +22,6 @@ Gem::Specification.new do |s|
|
|
22
22
|
"LICENSE",
|
23
23
|
"README.rdoc",
|
24
24
|
"Rakefile",
|
25
|
-
"VERSION",
|
26
25
|
"bin/mmconsole",
|
27
26
|
"lib/mongo_mapper.rb",
|
28
27
|
"lib/mongo_mapper/document.rb",
|
@@ -51,20 +50,25 @@ Gem::Specification.new do |s|
|
|
51
50
|
"lib/mongo_mapper/plugins/inspect.rb",
|
52
51
|
"lib/mongo_mapper/plugins/keys.rb",
|
53
52
|
"lib/mongo_mapper/plugins/logger.rb",
|
53
|
+
"lib/mongo_mapper/plugins/modifiers.rb",
|
54
54
|
"lib/mongo_mapper/plugins/pagination.rb",
|
55
55
|
"lib/mongo_mapper/plugins/pagination/proxy.rb",
|
56
56
|
"lib/mongo_mapper/plugins/protected.rb",
|
57
57
|
"lib/mongo_mapper/plugins/rails.rb",
|
58
58
|
"lib/mongo_mapper/plugins/serialization.rb",
|
59
|
+
"lib/mongo_mapper/plugins/timestamps.rb",
|
60
|
+
"lib/mongo_mapper/plugins/userstamps.rb",
|
59
61
|
"lib/mongo_mapper/plugins/validations.rb",
|
60
62
|
"lib/mongo_mapper/query.rb",
|
61
63
|
"lib/mongo_mapper/support.rb",
|
62
64
|
"lib/mongo_mapper/support/descendant_appends.rb",
|
63
65
|
"lib/mongo_mapper/support/find.rb",
|
66
|
+
"lib/mongo_mapper/version.rb",
|
64
67
|
"mongo_mapper.gemspec",
|
65
68
|
"performance/read_write.rb",
|
66
69
|
"specs.watchr",
|
67
70
|
"test/NOTE_ON_TESTING",
|
71
|
+
"test/active_model_lint_test.rb",
|
68
72
|
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
69
73
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
70
74
|
"test/functional/associations/test_in_array_proxy.rb",
|
@@ -81,11 +85,14 @@ Gem::Specification.new do |s|
|
|
81
85
|
"test/functional/test_document.rb",
|
82
86
|
"test/functional/test_embedded_document.rb",
|
83
87
|
"test/functional/test_identity_map.rb",
|
88
|
+
"test/functional/test_indexing.rb",
|
84
89
|
"test/functional/test_logger.rb",
|
85
90
|
"test/functional/test_modifiers.rb",
|
86
91
|
"test/functional/test_pagination.rb",
|
87
92
|
"test/functional/test_protected.rb",
|
88
93
|
"test/functional/test_string_id_compatibility.rb",
|
94
|
+
"test/functional/test_timestamps.rb",
|
95
|
+
"test/functional/test_userstamps.rb",
|
89
96
|
"test/functional/test_validations.rb",
|
90
97
|
"test/models.rb",
|
91
98
|
"test/support/custom_matchers.rb",
|
@@ -114,9 +121,10 @@ Gem::Specification.new do |s|
|
|
114
121
|
s.rdoc_options = ["--charset=UTF-8"]
|
115
122
|
s.require_paths = ["lib"]
|
116
123
|
s.rubygems_version = %q{1.3.5}
|
117
|
-
s.summary = %q{
|
124
|
+
s.summary = %q{A Ruby Object Mapper for Mongo}
|
118
125
|
s.test_files = [
|
119
|
-
"test/
|
126
|
+
"test/active_model_lint_test.rb",
|
127
|
+
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
120
128
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
121
129
|
"test/functional/associations/test_in_array_proxy.rb",
|
122
130
|
"test/functional/associations/test_many_documents_as_proxy.rb",
|
@@ -132,11 +140,14 @@ Gem::Specification.new do |s|
|
|
132
140
|
"test/functional/test_document.rb",
|
133
141
|
"test/functional/test_embedded_document.rb",
|
134
142
|
"test/functional/test_identity_map.rb",
|
143
|
+
"test/functional/test_indexing.rb",
|
135
144
|
"test/functional/test_logger.rb",
|
136
145
|
"test/functional/test_modifiers.rb",
|
137
146
|
"test/functional/test_pagination.rb",
|
138
147
|
"test/functional/test_protected.rb",
|
139
148
|
"test/functional/test_string_id_compatibility.rb",
|
149
|
+
"test/functional/test_timestamps.rb",
|
150
|
+
"test/functional/test_userstamps.rb",
|
140
151
|
"test/functional/test_validations.rb",
|
141
152
|
"test/models.rb",
|
142
153
|
"test/support/custom_matchers.rb",
|
@@ -168,16 +179,16 @@ Gem::Specification.new do |s|
|
|
168
179
|
|
169
180
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
170
181
|
s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
|
171
|
-
s.add_runtime_dependency(%q<mongo>, ["= 0.
|
172
|
-
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.
|
182
|
+
s.add_runtime_dependency(%q<mongo>, ["= 0.19.1"])
|
183
|
+
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
|
173
184
|
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
174
185
|
s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
|
175
186
|
s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
|
176
187
|
s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
|
177
188
|
else
|
178
189
|
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
179
|
-
s.add_dependency(%q<mongo>, ["= 0.
|
180
|
-
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.
|
190
|
+
s.add_dependency(%q<mongo>, ["= 0.19.1"])
|
191
|
+
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
|
181
192
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
182
193
|
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
183
194
|
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
@@ -185,8 +196,8 @@ Gem::Specification.new do |s|
|
|
185
196
|
end
|
186
197
|
else
|
187
198
|
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
188
|
-
s.add_dependency(%q<mongo>, ["= 0.
|
189
|
-
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.
|
199
|
+
s.add_dependency(%q<mongo>, ["= 0.19.1"])
|
200
|
+
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
|
190
201
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
191
202
|
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
192
203
|
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
@@ -80,6 +80,12 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
80
80
|
should "save doc" do
|
81
81
|
@list.should_not be_new
|
82
82
|
end
|
83
|
+
|
84
|
+
should "reset cache" do
|
85
|
+
@user.lists.size.should == 1
|
86
|
+
@user.lists.create(:name => 'Moo!')
|
87
|
+
@user.lists.size.should == 2
|
88
|
+
end
|
83
89
|
end
|
84
90
|
|
85
91
|
context "create!" do
|
@@ -110,6 +116,12 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
110
116
|
@user.lists.create!
|
111
117
|
end
|
112
118
|
end
|
119
|
+
|
120
|
+
should "reset cache" do
|
121
|
+
@user.lists.size.should == 1
|
122
|
+
@user.lists.create!(:name => 'Moo!')
|
123
|
+
@user.lists.size.should == 2
|
124
|
+
end
|
113
125
|
end
|
114
126
|
|
115
127
|
context "Finding scoped to association" do
|
@@ -162,6 +174,10 @@ class InArrayProxyTest < Test::Unit::TestCase
|
|
162
174
|
@user.lists.find(@list1.id).should == @list1
|
163
175
|
end
|
164
176
|
|
177
|
+
should "work with string ids" do
|
178
|
+
@user.lists.find(@list1.id.to_s).should == @list1
|
179
|
+
end
|
180
|
+
|
165
181
|
should "not work for id not in association" do
|
166
182
|
@user.lists.find(@list3.id).should be_nil
|
167
183
|
end
|
@@ -54,6 +54,14 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
|
|
54
54
|
status = project.statuses.build(:name => 'Foo')
|
55
55
|
status.name.should == 'Foo'
|
56
56
|
end
|
57
|
+
|
58
|
+
should "reset cache" do
|
59
|
+
project = Project.create
|
60
|
+
project.statuses.size.should == 0
|
61
|
+
status = project.statuses.build(:name => 'Foo')
|
62
|
+
status.save!
|
63
|
+
project.statuses.size.should == 1
|
64
|
+
end
|
57
65
|
end
|
58
66
|
|
59
67
|
context "create" do
|
@@ -75,6 +83,13 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
|
|
75
83
|
status = project.statuses.create(:name => 'Foo!')
|
76
84
|
status.name.should == 'Foo!'
|
77
85
|
end
|
86
|
+
|
87
|
+
should "reset cache" do
|
88
|
+
project = Project.create
|
89
|
+
project.statuses.size.should == 0
|
90
|
+
project.statuses.create(:name => 'Foo!')
|
91
|
+
project.statuses.size.should == 1
|
92
|
+
end
|
78
93
|
end
|
79
94
|
|
80
95
|
context "create!" do
|
@@ -103,6 +118,13 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
|
|
103
118
|
project.statuses.create!(:name => nil)
|
104
119
|
}.should raise_error(MongoMapper::DocumentNotValid)
|
105
120
|
end
|
121
|
+
|
122
|
+
should "reset cache" do
|
123
|
+
project = Project.create
|
124
|
+
project.statuses.size.should == 0
|
125
|
+
project.statuses.create!(:name => 'Foo!')
|
126
|
+
project.statuses.size.should == 1
|
127
|
+
end
|
106
128
|
end
|
107
129
|
|
108
130
|
context "count" do
|
@@ -1,60 +1,68 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
module CallbacksSupport
|
4
|
+
def self.included base
|
5
|
+
base.key :name, String
|
6
|
+
|
7
|
+
[ :before_validation_on_create, :before_validation_on_update,
|
8
|
+
:before_validation, :after_validation,
|
9
|
+
:before_create, :after_create,
|
10
|
+
:before_update, :after_update,
|
11
|
+
:before_save, :after_save,
|
12
|
+
:before_destroy, :after_destroy].each do |callback|
|
13
|
+
callback_method = "#{callback}_callback"
|
14
|
+
base.send(callback, callback_method)
|
15
|
+
define_method(callback_method) do
|
16
|
+
history << callback.to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def history
|
22
|
+
@history ||= []
|
23
|
+
end
|
24
|
+
|
25
|
+
def clear_history
|
26
|
+
embedded_associations.each { |a| self.send(a.name).each(&:clear_history) }
|
27
|
+
@history = nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
3
31
|
class CallbacksTest < Test::Unit::TestCase
|
4
|
-
|
32
|
+
CreateCallbackOrder = [:before_validation, :before_validation_on_create, :after_validation, :before_save, :before_create, :after_create, :after_save]
|
33
|
+
UpdateCallbackOrder = [:before_validation, :before_validation_on_update, :after_validation, :before_save, :before_update, :after_update, :after_save]
|
34
|
+
|
35
|
+
context "Defining and running callbacks on documents" do
|
5
36
|
setup do
|
6
|
-
@document = Doc
|
7
|
-
key :name, String
|
8
|
-
|
9
|
-
[ :before_validation_on_create, :before_validation_on_update,
|
10
|
-
:before_validation, :after_validation,
|
11
|
-
:before_create, :after_create,
|
12
|
-
:before_update, :after_update,
|
13
|
-
:before_save, :after_save,
|
14
|
-
:before_destroy, :after_destroy].each do |callback|
|
15
|
-
callback_method = "#{callback}_callback"
|
16
|
-
send(callback, callback_method)
|
17
|
-
define_method(callback_method) do
|
18
|
-
history << callback.to_sym
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def history
|
23
|
-
@history ||= []
|
24
|
-
end
|
25
|
-
|
26
|
-
def clear_history
|
27
|
-
@history = nil
|
28
|
-
end
|
29
|
-
end
|
37
|
+
@document = Doc { include CallbacksSupport }
|
30
38
|
end
|
31
|
-
|
39
|
+
|
32
40
|
should "get the order right for creating documents" do
|
33
41
|
doc = @document.create(:name => 'John Nunemaker')
|
34
|
-
doc.history.should ==
|
42
|
+
doc.history.should == CreateCallbackOrder
|
35
43
|
end
|
36
|
-
|
44
|
+
|
37
45
|
should "get the order right for updating documents" do
|
38
46
|
doc = @document.create(:name => 'John Nunemaker')
|
39
47
|
doc.clear_history
|
40
48
|
doc.name = 'John'
|
41
49
|
doc.save
|
42
|
-
doc.history.should ==
|
50
|
+
doc.history.should == UpdateCallbackOrder
|
43
51
|
end
|
44
|
-
|
52
|
+
|
45
53
|
should "work for before and after validation" do
|
46
54
|
doc = @document.new(:name => 'John Nunemaker')
|
47
55
|
doc.valid?
|
48
56
|
doc.history.should include(:before_validation)
|
49
57
|
doc.history.should include(:after_validation)
|
50
58
|
end
|
51
|
-
|
59
|
+
|
52
60
|
should "work for before and after create" do
|
53
61
|
doc = @document.create(:name => 'John Nunemaker')
|
54
62
|
doc.history.should include(:before_create)
|
55
63
|
doc.history.should include(:after_create)
|
56
64
|
end
|
57
|
-
|
65
|
+
|
58
66
|
should "work for before and after update" do
|
59
67
|
doc = @document.create(:name => 'John Nunemaker')
|
60
68
|
doc.name = 'John Doe'
|
@@ -62,7 +70,7 @@ class CallbacksTest < Test::Unit::TestCase
|
|
62
70
|
doc.history.should include(:before_update)
|
63
71
|
doc.history.should include(:after_update)
|
64
72
|
end
|
65
|
-
|
73
|
+
|
66
74
|
should "work for before and after save" do
|
67
75
|
doc = @document.new
|
68
76
|
doc.name = 'John Doe'
|
@@ -70,7 +78,7 @@ class CallbacksTest < Test::Unit::TestCase
|
|
70
78
|
doc.history.should include(:before_save)
|
71
79
|
doc.history.should include(:after_save)
|
72
80
|
end
|
73
|
-
|
81
|
+
|
74
82
|
should "work for before and after destroy" do
|
75
83
|
doc = @document.create(:name => 'John Nunemaker')
|
76
84
|
doc.destroy
|
@@ -78,4 +86,66 @@ class CallbacksTest < Test::Unit::TestCase
|
|
78
86
|
doc.history.should include(:after_destroy)
|
79
87
|
end
|
80
88
|
end
|
89
|
+
|
90
|
+
context "Defining and running callbacks on many embedded documents" do
|
91
|
+
setup do
|
92
|
+
@root_class = Doc { include CallbacksSupport }
|
93
|
+
@child_class = EDoc { include CallbacksSupport }
|
94
|
+
@grand_child_class = EDoc { include CallbacksSupport }
|
95
|
+
|
96
|
+
@root_class.many :children, :class => @child_class
|
97
|
+
@child_class.many :children, :class => @grand_child_class
|
98
|
+
end
|
99
|
+
|
100
|
+
should "get the order right based on root document creation" do
|
101
|
+
grand = @grand_child_class.new(:name => 'Grand Child')
|
102
|
+
child = @child_class.new(:name => 'Child', :children => [grand])
|
103
|
+
root = @root_class.create(:name => 'Parent', :children => [child])
|
104
|
+
|
105
|
+
child = root.children.first
|
106
|
+
child.history.should == CreateCallbackOrder
|
107
|
+
|
108
|
+
grand = root.children.first.children.first
|
109
|
+
grand.history.should == CreateCallbackOrder
|
110
|
+
end
|
111
|
+
|
112
|
+
should "get the order right based on root document updating" do
|
113
|
+
grand = @grand_child_class.new(:name => 'Grand Child')
|
114
|
+
child = @child_class.new(:name => 'Child', :children => [grand])
|
115
|
+
root = @root_class.create(:name => 'Parent', :children => [child])
|
116
|
+
root.clear_history
|
117
|
+
root.update_attributes(:name => 'Updated Parent')
|
118
|
+
|
119
|
+
child = root.children.first
|
120
|
+
child.history.should == UpdateCallbackOrder
|
121
|
+
|
122
|
+
grand = root.children.first.children.first
|
123
|
+
grand.history.should == UpdateCallbackOrder
|
124
|
+
end
|
125
|
+
|
126
|
+
should "work for before and after destroy" do
|
127
|
+
grand = @grand_child_class.new(:name => 'Grand Child')
|
128
|
+
child = @child_class.new(:name => 'Child', :children => [grand])
|
129
|
+
root = @root_class.create(:name => 'Parent', :children => [child])
|
130
|
+
root.destroy
|
131
|
+
child = root.children.first
|
132
|
+
child.history.should include(:before_destroy)
|
133
|
+
child.history.should include(:after_destroy)
|
134
|
+
|
135
|
+
grand = root.children.first.children.first
|
136
|
+
grand.history.should include(:before_destroy)
|
137
|
+
grand.history.should include(:after_destroy)
|
138
|
+
end
|
139
|
+
|
140
|
+
should "not attempt to run callback defined on root that is not defined on embedded association" do
|
141
|
+
@root_class.define_callbacks :after_publish
|
142
|
+
@root_class.after_save { |d| d.run_callbacks(:after_publish) }
|
143
|
+
|
144
|
+
assert_nothing_raised do
|
145
|
+
child = @child_class.new(:name => 'Child')
|
146
|
+
root = @root_class.create(:name => 'Parent', :children => [child])
|
147
|
+
child.history.should_not include(:after_publish)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
81
151
|
end
|
@@ -12,7 +12,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
12
12
|
key :date, Date
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "array key" do
|
17
17
|
setup do
|
18
18
|
@document.key :tags, Array
|
@@ -115,6 +115,23 @@ class DocumentTest < Test::Unit::TestCase
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
context "key with proc default value" do
|
119
|
+
setup do
|
120
|
+
@document.key :proc_default, String, :default => lambda { return 'string' }
|
121
|
+
end
|
122
|
+
|
123
|
+
should "detect and run proc default" do
|
124
|
+
doc = @document.new
|
125
|
+
doc.proc_default.should == 'string'
|
126
|
+
end
|
127
|
+
|
128
|
+
should "save and load from mongo" do
|
129
|
+
doc = @document.create
|
130
|
+
doc = doc.reload
|
131
|
+
doc.proc_default.should == 'string'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
118
135
|
context "ClassMethods#create (single document)" do
|
119
136
|
setup do
|
120
137
|
@doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
@@ -139,7 +156,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
139
156
|
@doc_instance.last_name.should == 'Nunemaker'
|
140
157
|
@doc_instance.age.should == 27
|
141
158
|
end
|
142
|
-
|
159
|
+
|
143
160
|
should "not fail if no attributes provided" do
|
144
161
|
document = Doc()
|
145
162
|
lambda { document.create }.should change { document.count }.by(1)
|
@@ -183,12 +200,12 @@ class DocumentTest < Test::Unit::TestCase
|
|
183
200
|
should "not create new document" do
|
184
201
|
@document.count.should == 1
|
185
202
|
end
|
186
|
-
|
203
|
+
|
187
204
|
should "raise error if not provided id" do
|
188
205
|
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
189
206
|
lambda { @document.update }.should raise_error(ArgumentError)
|
190
207
|
end
|
191
|
-
|
208
|
+
|
192
209
|
should "raise error if not provided attributes" do
|
193
210
|
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
194
211
|
lambda { @document.update(doc._id) }.should raise_error(ArgumentError)
|
@@ -221,7 +238,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
221
238
|
@document.find(@doc1._id).age.should == 30
|
222
239
|
@document.find(@doc2._id).age.should == 30
|
223
240
|
end
|
224
|
-
|
241
|
+
|
225
242
|
should "raise error if not a hash" do
|
226
243
|
lambda { @document.update([1, 2]) }.should raise_error(ArgumentError)
|
227
244
|
end
|
@@ -243,7 +260,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
243
260
|
@document.find!
|
244
261
|
end
|
245
262
|
end
|
246
|
-
|
263
|
+
|
247
264
|
should "raise error if trying to find with :all, :first, or :last" do
|
248
265
|
[:all, :first, :last].each do |m|
|
249
266
|
assert_raises(ArgumentError) { @document.find(m) }
|
@@ -525,7 +542,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
525
542
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
526
543
|
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
527
544
|
@document.destroy(@doc1._id, @doc2._id)
|
528
|
-
|
545
|
+
|
529
546
|
@document.count.should == 1
|
530
547
|
end
|
531
548
|
|
@@ -596,11 +613,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
596
613
|
@document.count(:age => [26, 27]).should == 2
|
597
614
|
end
|
598
615
|
end
|
599
|
-
|
616
|
+
|
600
617
|
should "have instance method for collection" do
|
601
618
|
@document.new.collection.name.should == @document.collection.name
|
602
619
|
end
|
603
|
-
|
620
|
+
|
604
621
|
should "have instance method for database" do
|
605
622
|
@document.new.database.should == @document.database
|
606
623
|
end
|
@@ -672,7 +689,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
672
689
|
@document.new.update_attributes({}).should be_false
|
673
690
|
end
|
674
691
|
end
|
675
|
-
|
692
|
+
|
676
693
|
context "#save (new document)" do
|
677
694
|
setup do
|
678
695
|
@doc = @document.new(:first_name => 'John', :age => '27')
|
@@ -710,12 +727,12 @@ class DocumentTest < Test::Unit::TestCase
|
|
710
727
|
should "allow to use custom methods to assign properties" do
|
711
728
|
klass = Doc do
|
712
729
|
key :name, String
|
713
|
-
|
730
|
+
|
714
731
|
def realname=(value)
|
715
732
|
self.name = value
|
716
733
|
end
|
717
734
|
end
|
718
|
-
|
735
|
+
|
719
736
|
person = klass.new(:realname => 'David')
|
720
737
|
person.save
|
721
738
|
person.reload.name.should == 'David'
|
@@ -723,7 +740,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
723
740
|
|
724
741
|
context "with key of type date" do
|
725
742
|
should "save the date value as a Time object" do
|
726
|
-
doc = @document.new(:first_name => 'John', :age => '27', :date => "12
|
743
|
+
doc = @document.new(:first_name => 'John', :age => '27', :date => "2009-12-01")
|
727
744
|
doc.save
|
728
745
|
doc.date.should == Date.new(2009, 12, 1)
|
729
746
|
end
|
@@ -775,7 +792,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
775
792
|
@document.count.should == 1
|
776
793
|
end
|
777
794
|
end
|
778
|
-
|
795
|
+
|
779
796
|
context "#save (with options)" do
|
780
797
|
setup do
|
781
798
|
@document = Doc do
|
@@ -799,7 +816,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
799
816
|
end
|
800
817
|
end
|
801
818
|
end
|
802
|
-
|
819
|
+
|
803
820
|
context "#save! (with options)" do
|
804
821
|
setup do
|
805
822
|
@document = Doc do
|
@@ -840,37 +857,57 @@ class DocumentTest < Test::Unit::TestCase
|
|
840
857
|
@document.count.should == 0
|
841
858
|
end
|
842
859
|
end
|
843
|
-
|
860
|
+
|
844
861
|
context "#delete" do
|
845
862
|
setup do
|
846
|
-
@doc1 = @document.create(
|
847
|
-
@doc2 = @document.create(
|
848
|
-
|
863
|
+
@doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
|
864
|
+
@doc2 = @document.create(:first_name => 'Steve', :last_name => 'Smith', :age => '28')
|
865
|
+
|
849
866
|
@document.class_eval do
|
850
867
|
before_destroy :before_destroy_callback
|
851
868
|
after_destroy :after_destroy_callback
|
852
|
-
|
869
|
+
|
853
870
|
def history; @history ||= [] end
|
854
871
|
def before_destroy_callback; history << :after_destroy end
|
855
872
|
def after_destroy_callback; history << :after_destroy end
|
856
873
|
end
|
857
|
-
|
874
|
+
|
858
875
|
@doc1.delete
|
859
876
|
end
|
860
877
|
|
861
878
|
should "remove document from collection" do
|
862
879
|
@document.count.should == 1
|
863
880
|
end
|
864
|
-
|
881
|
+
|
865
882
|
should "not remove other documents" do
|
866
883
|
@document.find(@doc2.id).should_not be(nil)
|
867
884
|
end
|
868
|
-
|
885
|
+
|
869
886
|
should "not call before/after destroy callbacks" do
|
870
887
|
@doc1.history.should == []
|
871
888
|
end
|
872
889
|
end
|
873
890
|
|
891
|
+
context "#destroyed?" do
|
892
|
+
setup do
|
893
|
+
@doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
|
894
|
+
end
|
895
|
+
|
896
|
+
should "be true if deleted" do
|
897
|
+
@doc1.delete
|
898
|
+
assert @doc1.destroyed?
|
899
|
+
end
|
900
|
+
|
901
|
+
should "be true if destroyed" do
|
902
|
+
@doc1.destroy
|
903
|
+
assert @doc1.destroyed?
|
904
|
+
end
|
905
|
+
|
906
|
+
should "be false if not deleted or destroyed" do
|
907
|
+
assert ! @doc1.destroyed?
|
908
|
+
end
|
909
|
+
end
|
910
|
+
|
874
911
|
context "Single collection inheritance" do
|
875
912
|
setup do
|
876
913
|
class ::DocParent
|
@@ -883,7 +920,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
883
920
|
class ::DocDaughter < ::DocParent; end
|
884
921
|
class ::DocSon < ::DocParent; end
|
885
922
|
class ::DocGrandSon < ::DocSon; end
|
886
|
-
|
923
|
+
|
887
924
|
DocSon.many :children, :class_name => 'DocGrandSon'
|
888
925
|
|
889
926
|
@parent = DocParent.new({:name => "Daddy Warbucks"})
|
@@ -939,7 +976,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
939
976
|
DocDaughter.all(:order => 'name').should == [carrie, steph]
|
940
977
|
DocParent.all(:order => 'name').should == [carrie, john, steph, steve]
|
941
978
|
end
|
942
|
-
|
979
|
+
|
943
980
|
should "work with nested hash conditions" do
|
944
981
|
john = DocSon.create(:name => 'John')
|
945
982
|
steve = DocSon.create(:name => 'Steve')
|
@@ -1023,7 +1060,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
1023
1060
|
DocParent.delete_all
|
1024
1061
|
}.should change { DocParent.count }.by(-2)
|
1025
1062
|
end
|
1026
|
-
|
1063
|
+
|
1027
1064
|
should "be able to reload parent inherited class" do
|
1028
1065
|
brian = DocParent.create(:name => 'Brian')
|
1029
1066
|
brian.name = 'B-Dawg'
|
@@ -1032,89 +1069,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
1032
1069
|
end
|
1033
1070
|
end
|
1034
1071
|
|
1035
|
-
context "timestamping" do
|
1036
|
-
setup do
|
1037
|
-
@klass = Doc do
|
1038
|
-
set_collection_name 'users'
|
1039
|
-
|
1040
|
-
key :first_name, String
|
1041
|
-
key :last_name, String
|
1042
|
-
key :age, Integer
|
1043
|
-
key :date, Date
|
1044
|
-
end
|
1045
|
-
@klass.timestamps!
|
1046
|
-
end
|
1047
|
-
|
1048
|
-
should "set created_at and updated_at on create" do
|
1049
|
-
doc = @klass.new(:first_name => 'John', :age => 27)
|
1050
|
-
doc.created_at.should be(nil)
|
1051
|
-
doc.updated_at.should be(nil)
|
1052
|
-
doc.save
|
1053
|
-
doc.created_at.should_not be(nil)
|
1054
|
-
doc.updated_at.should_not be(nil)
|
1055
|
-
end
|
1056
|
-
|
1057
|
-
should "not overwrite created_at if it already exists" do
|
1058
|
-
original_created_at = 1.month.ago
|
1059
|
-
doc = @klass.new(:first_name => 'John', :age => 27, :created_at => original_created_at)
|
1060
|
-
doc.created_at.to_i.should == original_created_at.to_i
|
1061
|
-
doc.updated_at.should be_nil
|
1062
|
-
doc.save
|
1063
|
-
doc.created_at.to_i.should == original_created_at.to_i
|
1064
|
-
doc.updated_at.should_not be_nil
|
1065
|
-
end
|
1066
|
-
|
1067
|
-
should "set updated_at on field update but leave created_at alone" do
|
1068
|
-
doc = @klass.create(:first_name => 'John', :age => 27)
|
1069
|
-
old_created_at = doc.created_at
|
1070
|
-
old_updated_at = doc.updated_at
|
1071
|
-
doc.first_name = 'Johnny'
|
1072
|
-
|
1073
|
-
Timecop.freeze(Time.now + 5.seconds) do
|
1074
|
-
doc.save
|
1075
|
-
end
|
1076
|
-
|
1077
|
-
doc.created_at.should == old_created_at
|
1078
|
-
doc.updated_at.should_not == old_updated_at
|
1079
|
-
end
|
1080
|
-
|
1081
|
-
should "set updated_at on document update but leave created_at alone" do
|
1082
|
-
doc = @klass.create(:first_name => 'John', :age => 27)
|
1083
|
-
old_created_at = doc.created_at
|
1084
|
-
old_updated_at = doc.updated_at
|
1085
|
-
|
1086
|
-
Timecop.freeze(Time.now + 5.seconds) do
|
1087
|
-
@klass.update(doc._id, { :first_name => 'Johnny' })
|
1088
|
-
end
|
1089
|
-
|
1090
|
-
doc = doc.reload
|
1091
|
-
doc.created_at.should == old_created_at
|
1092
|
-
doc.updated_at.should_not == old_updated_at
|
1093
|
-
end
|
1094
|
-
end
|
1095
|
-
|
1096
|
-
context "userstamping" do
|
1097
|
-
setup do
|
1098
|
-
@document.userstamps!
|
1099
|
-
end
|
1100
|
-
|
1101
|
-
should "add creator_id key" do
|
1102
|
-
@document.keys.keys.should include('creator_id')
|
1103
|
-
end
|
1104
|
-
|
1105
|
-
should "add updater_id key" do
|
1106
|
-
@document.keys.keys.should include('updater_id')
|
1107
|
-
end
|
1108
|
-
|
1109
|
-
should "add belongs_to creator" do
|
1110
|
-
@document.associations.keys.should include('creator')
|
1111
|
-
end
|
1112
|
-
|
1113
|
-
should "add belongs_to updater" do
|
1114
|
-
@document.associations.keys.should include('updater')
|
1115
|
-
end
|
1116
|
-
end
|
1117
|
-
|
1118
1072
|
context "#exists?" do
|
1119
1073
|
setup do
|
1120
1074
|
@doc = @document.create(:first_name => "James", :age => 27)
|
@@ -1143,14 +1097,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
1143
1097
|
@foo_class = Doc do
|
1144
1098
|
key :name
|
1145
1099
|
end
|
1146
|
-
|
1100
|
+
|
1147
1101
|
@bar_class = EDoc do
|
1148
1102
|
key :name
|
1149
1103
|
end
|
1150
|
-
|
1104
|
+
|
1151
1105
|
@document.many :foos, :class => @foo_class
|
1152
1106
|
@document.many :bars, :class => @bar_class
|
1153
|
-
|
1107
|
+
|
1154
1108
|
@instance = @document.create({
|
1155
1109
|
:age => 39,
|
1156
1110
|
:foos => [@foo_class.new(:name => '1')],
|
@@ -1164,22 +1118,22 @@ class DocumentTest < Test::Unit::TestCase
|
|
1164
1118
|
@instance.reload
|
1165
1119
|
@instance.age.should == 39
|
1166
1120
|
end
|
1167
|
-
|
1121
|
+
|
1168
1122
|
should "reset all associations" do
|
1169
1123
|
@instance.foos.expects(:reset).at_least_once
|
1170
1124
|
@instance.bars.expects(:reset).at_least_once
|
1171
1125
|
@instance.reload
|
1172
1126
|
end
|
1173
|
-
|
1127
|
+
|
1174
1128
|
should "reinstantiate embedded associations" do
|
1175
1129
|
@instance.reload
|
1176
1130
|
@instance.bars.first.name.should == '1'
|
1177
1131
|
end
|
1178
|
-
|
1132
|
+
|
1179
1133
|
should "return self" do
|
1180
1134
|
@instance.reload.object_id.should == @instance.object_id
|
1181
1135
|
end
|
1182
|
-
|
1136
|
+
|
1183
1137
|
should "raise DocumentNotFound if not found" do
|
1184
1138
|
@instance.destroy
|
1185
1139
|
assert_raises(MongoMapper::DocumentNotFound) { @instance.reload }
|
@@ -1208,37 +1162,4 @@ class DocumentTest < Test::Unit::TestCase
|
|
1208
1162
|
doc.skills.should == ['ruby', 'rails', 'javascript', 'xhtml', 'css']
|
1209
1163
|
end
|
1210
1164
|
end
|
1211
|
-
|
1212
|
-
context "Indexing" do
|
1213
|
-
setup do
|
1214
|
-
drop_indexes(@document)
|
1215
|
-
end
|
1216
|
-
|
1217
|
-
should "allow creating index for a key" do
|
1218
|
-
@document.ensure_index :first_name
|
1219
|
-
@document.should have_index('first_name_1')
|
1220
|
-
end
|
1221
|
-
|
1222
|
-
should "allow creating unique index for a key" do
|
1223
|
-
@document.ensure_index :first_name, :unique => true
|
1224
|
-
@document.should have_index('first_name_1')
|
1225
|
-
end
|
1226
|
-
|
1227
|
-
should "allow creating index on multiple keys" do
|
1228
|
-
@document.ensure_index [[:first_name, 1], [:last_name, -1]]
|
1229
|
-
|
1230
|
-
# order is different for different versions of ruby so instead of
|
1231
|
-
# just checking have_index('first_name_1_last_name_-1') I'm checking
|
1232
|
-
# the values of the indexes to make sure the index creation was successful
|
1233
|
-
@document.collection.index_information.detect do |index|
|
1234
|
-
keys = index[1]
|
1235
|
-
keys.include?(['first_name', 1]) && keys.include?(['last_name', -1])
|
1236
|
-
end.should_not be_nil
|
1237
|
-
end
|
1238
|
-
|
1239
|
-
should "work with :index shortcut when defining key" do
|
1240
|
-
@document.key :father, String, :index => true
|
1241
|
-
@document.should have_index('father_1')
|
1242
|
-
end
|
1243
|
-
end
|
1244
1165
|
end
|