mongo_mapper-unstable 2010.3.8 → 2010.06.23
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -8
- data/bin/mmconsole +1 -1
- data/examples/keys.rb +37 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +11 -329
- data/lib/mongo_mapper/embedded_document.rb +9 -38
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
- data/lib/mongo_mapper/plugins/associations.rb +14 -22
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
- data/lib/mongo_mapper/plugins/clone.rb +10 -4
- data/lib/mongo_mapper/plugins/descendants.rb +3 -2
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -10
- data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/keys.rb +85 -110
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
- data/lib/mongo_mapper/plugins/pagination.rb +5 -15
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +9 -1
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/rails.rb +6 -1
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +9 -5
- data/lib/mongo_mapper/plugins.rb +1 -20
- data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/mongo_mapper.rb +71 -128
- data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
- data/test/functional/associations/test_in_array_proxy.rb +7 -9
- data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
- data/test/functional/associations/test_many_documents_proxy.rb +186 -64
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
- data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
- data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +70 -49
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +11 -11
- data/test/functional/test_binary.rb +5 -5
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_dirty.rb +16 -16
- data/test/functional/test_document.rb +12 -924
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +88 -8
- data/test/functional/test_identity_map.rb +41 -43
- data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
- data/test/functional/test_logger.rb +1 -1
- data/test/functional/test_modifiers.rb +275 -181
- data/test/functional/test_pagination.rb +13 -15
- data/test/functional/test_protected.rb +25 -11
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +11 -11
- data/test/functional/test_timestamps.rb +0 -2
- data/test/functional/test_userstamps.rb +0 -1
- data/test/functional/test_validations.rb +44 -31
- data/test/models.rb +18 -17
- data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
- data/test/test_helper.rb +59 -16
- data/test/unit/associations/test_base.rb +47 -42
- data/test/unit/associations/test_proxy.rb +15 -15
- data/test/unit/serializers/test_json_serializer.rb +29 -29
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +3 -3
- data/test/unit/test_document.rb +49 -67
- data/test/unit/test_dynamic_finder.rb +53 -51
- data/test/unit/test_embedded_document.rb +19 -38
- data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +29 -147
- data/test/unit/test_mongo_mapper.rb +3 -48
- data/test/unit/test_pagination.rb +1 -150
- data/test/unit/test_rails.rb +77 -19
- data/test/unit/test_rails_compatibility.rb +12 -12
- data/test/unit/test_serialization.rb +5 -5
- data/test/unit/test_time_zones.rb +9 -9
- data/test/unit/test_validations.rb +46 -46
- metadata +157 -155
- data/.gitignore +0 -10
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
- data/lib/mongo_mapper/query.rb +0 -130
- data/lib/mongo_mapper/support.rb +0 -215
- data/mongo_mapper.gemspec +0 -196
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/support/custom_matchers.rb +0 -55
- data/test/support/timing.rb +0 -16
- data/test/unit/test_query.rb +0 -340
@@ -7,113 +7,134 @@ class OneProxyTest < Test::Unit::TestCase
|
|
7
7
|
key :post_id, ObjectId
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
should "default to nil" do
|
12
12
|
@post_class.one :author, :class => @author_class
|
13
13
|
@post_class.new.author.nil?.should be_true
|
14
14
|
end
|
15
|
-
|
16
|
-
should "
|
15
|
+
|
16
|
+
should "allow assignment of associated document using a hash" do
|
17
17
|
@post_class.one :author, :class => @author_class
|
18
|
-
|
19
|
-
post = @post_class.new
|
20
|
-
author
|
21
|
-
|
22
|
-
author.save.should be_true
|
18
|
+
|
19
|
+
post = @post_class.new('author' => { 'name' => 'Frank' })
|
20
|
+
post.author.name.should == 'Frank'
|
21
|
+
|
23
22
|
post.save.should be_true
|
24
|
-
|
25
|
-
post.author.object_id.should == post.author.target.object_id
|
26
|
-
end
|
27
|
-
|
28
|
-
should "be able to replace the association" do
|
29
|
-
@post_class.one :author, :class => @author_class
|
30
|
-
|
31
|
-
post = @post_class.new
|
32
|
-
author = @author_class.new(:name => 'Frank')
|
33
|
-
post.author = author
|
34
23
|
post.reload
|
35
|
-
|
36
|
-
post.author.should ==
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
24
|
+
|
25
|
+
post.author.name.should == 'Frank'
|
26
|
+
end
|
27
|
+
|
28
|
+
context "replacing the association" do
|
29
|
+
context "with an object of the class" do
|
30
|
+
should "work" do
|
31
|
+
@post_class.one :author, :class => @author_class
|
32
|
+
|
33
|
+
post = @post_class.new
|
34
|
+
author = @author_class.new(:name => 'Frank')
|
35
|
+
post.author = author
|
36
|
+
post.reload
|
37
|
+
|
38
|
+
post.author.should == author
|
39
|
+
post.author.nil?.should be_false
|
40
|
+
|
41
|
+
new_author = @author_class.new(:name => 'Emily')
|
42
|
+
post.author = new_author
|
43
|
+
post.author.should == new_author
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with a Hash" do
|
48
|
+
should "convert to an object of the class and work" do
|
49
|
+
@post_class.one :author, :class => @author_class
|
50
|
+
|
51
|
+
post = @post_class.new
|
52
|
+
post.author = {'name' => 'Frank'}
|
53
|
+
post.reload
|
54
|
+
|
55
|
+
post.author.name.should == 'Frank'
|
56
|
+
post.author.nil?.should be_false
|
57
|
+
|
58
|
+
post.author = {'name' => 'Emily'}
|
59
|
+
post.author.name.should == 'Emily'
|
60
|
+
end
|
61
|
+
end
|
42
62
|
end
|
43
|
-
|
63
|
+
|
44
64
|
should "have boolean method for testing presence" do
|
45
65
|
@post_class.one :author, :class => @author_class
|
46
|
-
|
66
|
+
|
47
67
|
post = @post_class.new
|
48
68
|
post.author?.should be_false
|
49
|
-
|
69
|
+
|
50
70
|
post.author = @author_class.new(:name => 'Frank')
|
51
71
|
post.author?.should be_true
|
52
72
|
end
|
53
|
-
|
73
|
+
|
54
74
|
should "work with criteria" do
|
55
75
|
@post_class.one :primary_author, :class => @author_class, :primary => true
|
56
|
-
@post_class.one :author, :class => @author_class
|
57
|
-
|
76
|
+
@post_class.one :author, :class => @author_class, :primary => false
|
77
|
+
|
58
78
|
post = @post_class.create
|
59
79
|
author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
|
60
80
|
primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
|
81
|
+
post.reload
|
61
82
|
post.author.should == author
|
62
83
|
post.primary_author.should == primary
|
63
84
|
end
|
64
|
-
|
85
|
+
|
65
86
|
should "unset the association" do
|
66
87
|
@post_class.one :author, :class => @author_class
|
67
88
|
post = @post_class.new
|
68
89
|
author = @author_class.new
|
69
90
|
post.author = author
|
70
91
|
post.reload
|
71
|
-
|
92
|
+
|
72
93
|
post.author = nil
|
73
94
|
post.author.nil?.should be_false
|
74
95
|
end
|
75
|
-
|
96
|
+
|
76
97
|
should "work with :dependent delete" do
|
77
98
|
@post_class.one :author, :class => @author_class, :dependent => :delete
|
78
|
-
|
99
|
+
|
79
100
|
post = @post_class.create
|
80
101
|
author = @author_class.new
|
81
102
|
post.author = author
|
82
103
|
post.reload
|
83
|
-
|
104
|
+
|
84
105
|
@author_class.any_instance.expects(:delete).once
|
85
106
|
post.author = @author_class.new
|
86
107
|
end
|
87
|
-
|
108
|
+
|
88
109
|
should "work with :dependent destroy" do
|
89
110
|
@post_class.one :author, :class => @author_class, :dependent => :destroy
|
90
|
-
|
111
|
+
|
91
112
|
post = @post_class.create
|
92
113
|
author = @author_class.new
|
93
114
|
post.author = author
|
94
115
|
post.reload
|
95
|
-
|
116
|
+
|
96
117
|
@author_class.any_instance.expects(:destroy).once
|
97
118
|
post.author = @author_class.new
|
98
119
|
end
|
99
|
-
|
120
|
+
|
100
121
|
should "work with :dependent nullify" do
|
101
122
|
@post_class.one :author, :class => @author_class, :dependent => :nullify
|
102
|
-
|
123
|
+
|
103
124
|
post = @post_class.create
|
104
125
|
author = @author_class.new
|
105
126
|
post.author = author
|
106
127
|
post.reload
|
107
|
-
|
128
|
+
|
108
129
|
post.author = @author_class.new
|
109
|
-
|
130
|
+
|
110
131
|
author.reload
|
111
132
|
author.post_id.should be_nil
|
112
133
|
end
|
113
134
|
|
114
135
|
should "be able to build" do
|
115
136
|
@post_class.one :author, :class => @author_class
|
116
|
-
|
137
|
+
|
117
138
|
post = @post_class.create
|
118
139
|
author = post.author.build(:name => 'John')
|
119
140
|
post.author.should be_instance_of(@author_class)
|
@@ -122,10 +143,10 @@ class OneProxyTest < Test::Unit::TestCase
|
|
122
143
|
post.author.should == author
|
123
144
|
post.author.post_id.should == post.id
|
124
145
|
end
|
125
|
-
|
146
|
+
|
126
147
|
should "be able to create" do
|
127
148
|
@post_class.one :author, :class => @author_class
|
128
|
-
|
149
|
+
|
129
150
|
post = @post_class.create
|
130
151
|
author = post.author.create(:name => 'John')
|
131
152
|
post.author.should be_instance_of(@author_class)
|
@@ -134,20 +155,20 @@ class OneProxyTest < Test::Unit::TestCase
|
|
134
155
|
post.author.should == author
|
135
156
|
post.author.post_id.should == post.id
|
136
157
|
end
|
137
|
-
|
158
|
+
|
138
159
|
context "#create!" do
|
139
160
|
setup do
|
140
161
|
@author_class.key :name, String, :required => true
|
141
162
|
@post_class.one :author, :class => @author_class
|
142
163
|
end
|
143
|
-
|
164
|
+
|
144
165
|
should "raise exception if invalid" do
|
145
166
|
post = @post_class.create
|
146
167
|
assert_raises(MongoMapper::DocumentNotValid) do
|
147
168
|
post.author.create!
|
148
169
|
end
|
149
170
|
end
|
150
|
-
|
171
|
+
|
151
172
|
should "work if valid" do
|
152
173
|
post = @post_class.create
|
153
174
|
author = post.author.create!(:name => 'John')
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AccessibleTest < Test::Unit::TestCase
|
4
|
+
context 'A document with accessible attributes' do
|
5
|
+
setup do
|
6
|
+
@doc_class = Doc do
|
7
|
+
key :name, String
|
8
|
+
key :admin, Boolean, :default => false
|
9
|
+
|
10
|
+
attr_accessible :name
|
11
|
+
end
|
12
|
+
|
13
|
+
@doc = @doc_class.create(:name => 'Steve Sloan')
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'have accessible attributes class method' do
|
17
|
+
@doc_class.accessible_attributes.should == [:name].to_set
|
18
|
+
end
|
19
|
+
|
20
|
+
should "default accessible attributes to nil" do
|
21
|
+
Doc().accessible_attributes.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
should "have accessible_attributes instance method" do
|
25
|
+
@doc.accessible_attributes.should equal(@doc_class.accessible_attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "raise error if there are protected attributes" do
|
29
|
+
doc = Doc('Post')
|
30
|
+
doc.attr_protected :admin
|
31
|
+
lambda { doc.attr_accessible :name }.
|
32
|
+
should raise_error(/Declare either attr_protected or attr_accessible for Post/)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "know if using accessible attributes" do
|
36
|
+
@doc_class.accessible_attributes?.should be(true)
|
37
|
+
Doc().accessible_attributes?.should be(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "assign inaccessible attribute through accessor" do
|
41
|
+
@doc.admin = true
|
42
|
+
@doc.admin.should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
should "ignore inaccessible attribute on #initialize" do
|
46
|
+
doc = @doc_class.new(:name => 'John', :admin => true)
|
47
|
+
doc.admin.should be_false
|
48
|
+
doc.name.should == 'John'
|
49
|
+
end
|
50
|
+
|
51
|
+
should "not ignore inaccessible attributes on #initialize from the database" do
|
52
|
+
doc = @doc_class.new(:name => 'John')
|
53
|
+
doc.admin = true
|
54
|
+
doc.save!
|
55
|
+
|
56
|
+
doc = @doc_class.first(:name => 'John')
|
57
|
+
doc.admin.should be_true
|
58
|
+
doc.name.should == 'John'
|
59
|
+
end
|
60
|
+
|
61
|
+
should "ignore inaccessible attribute on #update_attributes" do
|
62
|
+
@doc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
63
|
+
@doc.name.should == 'Ren Hoek'
|
64
|
+
@doc.admin.should be_false
|
65
|
+
end
|
66
|
+
|
67
|
+
should "ignore inaccessible attribute on #update_attributes!" do
|
68
|
+
@doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
69
|
+
@doc.name.should == 'Stimpson J. Cat'
|
70
|
+
@doc.admin.should be_false
|
71
|
+
end
|
72
|
+
|
73
|
+
should "be indifferent to whether the accessible keys are strings or symbols" do
|
74
|
+
@doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
|
75
|
+
@doc.name.should == 'Stimpson J. Cat'
|
76
|
+
@doc.admin.should be_false
|
77
|
+
end
|
78
|
+
|
79
|
+
should "accept nil as constructor's argument without raising exception" do
|
80
|
+
lambda { @doc_class.new(nil) }.should_not raise_error
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "Single collection inherited accessible attributes" do
|
85
|
+
setup do
|
86
|
+
class ::GrandParent
|
87
|
+
include MongoMapper::Document
|
88
|
+
attr_accessible :name
|
89
|
+
key :name, String
|
90
|
+
key :site_id, ObjectId
|
91
|
+
end
|
92
|
+
GrandParent.collection.remove
|
93
|
+
|
94
|
+
class ::Child < ::GrandParent
|
95
|
+
attr_accessible :position
|
96
|
+
key :position, Integer
|
97
|
+
end
|
98
|
+
|
99
|
+
class ::GrandChild < ::Child; end
|
100
|
+
|
101
|
+
class ::OtherChild < ::GrandParent
|
102
|
+
attr_accessible :favorite_color
|
103
|
+
key :favorite_color, String
|
104
|
+
key :blog_id, ObjectId
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
teardown do
|
109
|
+
Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
|
110
|
+
Object.send :remove_const, 'Child' if defined?(::Child)
|
111
|
+
Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
|
112
|
+
Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
|
113
|
+
end
|
114
|
+
|
115
|
+
should "share keys down the inheritance trail" do
|
116
|
+
GrandParent.accessible_attributes.should == [:name].to_set
|
117
|
+
Child.accessible_attributes.should == [:name, :position].to_set
|
118
|
+
GrandChild.accessible_attributes.should == [:name, :position].to_set
|
119
|
+
OtherChild.accessible_attributes.should == [:name, :favorite_color].to_set
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "An embedded document with accessible attributes" do
|
124
|
+
setup do
|
125
|
+
@doc_class = Doc('Project')
|
126
|
+
@edoc_class = EDoc('Person') do
|
127
|
+
key :name, String
|
128
|
+
key :admin, Boolean, :default => false
|
129
|
+
|
130
|
+
attr_accessible :name
|
131
|
+
end
|
132
|
+
@doc_class.many :people, :class => @edoc_class
|
133
|
+
|
134
|
+
@doc = @doc_class.create(:title => 'MongoMapper')
|
135
|
+
@edoc = @edoc_class.new(:name => 'Steve Sloan')
|
136
|
+
@doc.people << @edoc
|
137
|
+
end
|
138
|
+
|
139
|
+
should "have accessible attributes class method" do
|
140
|
+
@edoc_class.accessible_attributes.should == [:name].to_set
|
141
|
+
end
|
142
|
+
|
143
|
+
should "default accessible attributes to nil" do
|
144
|
+
EDoc().accessible_attributes.should be_nil
|
145
|
+
end
|
146
|
+
|
147
|
+
should "have accessible attributes instance method" do
|
148
|
+
@edoc.accessible_attributes.should equal(@edoc_class.accessible_attributes)
|
149
|
+
end
|
150
|
+
|
151
|
+
should "assign inaccessible attribute through accessor" do
|
152
|
+
@edoc.admin = true
|
153
|
+
@edoc.admin.should be_true
|
154
|
+
end
|
155
|
+
|
156
|
+
should "ignore inaccessible attribute on #update_attributes" do
|
157
|
+
@edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
158
|
+
@edoc.name.should == 'Ren Hoek'
|
159
|
+
@edoc.admin.should be_false
|
160
|
+
end
|
161
|
+
|
162
|
+
should "ignore inaccessible attribute on #update_attributes!" do
|
163
|
+
@edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
164
|
+
@edoc.name.should == 'Stimpson J. Cat'
|
165
|
+
@edoc.admin.should be_false
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -1,43 +1,43 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
require 'models'
|
3
3
|
|
4
|
-
class AssociationsTest < Test::Unit::TestCase
|
4
|
+
class AssociationsTest < Test::Unit::TestCase
|
5
5
|
should "allow changing class names" do
|
6
6
|
class AwesomeUser
|
7
7
|
include MongoMapper::Document
|
8
|
-
|
8
|
+
|
9
9
|
many :posts, :class_name => 'AssociationsTest::AwesomePost', :foreign_key => :creator_id
|
10
10
|
end
|
11
11
|
AwesomeUser.collection.remove
|
12
|
-
|
12
|
+
|
13
13
|
class AwesomeTag
|
14
14
|
include MongoMapper::EmbeddedDocument
|
15
|
-
|
15
|
+
|
16
16
|
key :name, String
|
17
17
|
key :post_id, ObjectId
|
18
|
-
|
18
|
+
|
19
19
|
belongs_to :post, :class_name => 'AssociationsTest::AwesomeUser'
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
class AwesomePost
|
23
23
|
include MongoMapper::Document
|
24
|
-
|
24
|
+
|
25
25
|
key :creator_id, ObjectId
|
26
|
-
|
26
|
+
|
27
27
|
belongs_to :creator, :class_name => 'AssociationsTest::AwesomeUser'
|
28
28
|
many :tags, :class_name => 'AssociationsTest::AwesomeTag', :foreign_key => :post_id
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
AwesomeUser.collection.remove
|
32
32
|
AwesomePost.collection.remove
|
33
|
-
|
33
|
+
|
34
34
|
user = AwesomeUser.create
|
35
35
|
tag1 = AwesomeTag.new(:name => 'awesome')
|
36
36
|
tag2 = AwesomeTag.new(:name => 'grand')
|
37
37
|
post1 = AwesomePost.create(:creator => user, :tags => [tag1])
|
38
38
|
post2 = AwesomePost.create(:creator => user, :tags => [tag2])
|
39
39
|
user.posts.should == [post1, post2]
|
40
|
-
|
40
|
+
|
41
41
|
post1 = post1.reload
|
42
42
|
post1.tags.should == [tag1]
|
43
43
|
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class BinaryTest < Test::Unit::TestCase
|
3
|
+
class BinaryTest < Test::Unit::TestCase
|
4
4
|
should "serialize and deserialize correctly" do
|
5
5
|
klass = Doc do
|
6
6
|
key :contents, Binary
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
doc = klass.new(:contents => '010101')
|
10
10
|
doc.save
|
11
|
-
|
11
|
+
|
12
12
|
doc = doc.reload
|
13
|
-
doc.contents.to_s.should == ByteBuffer.new('010101').to_s
|
13
|
+
doc.contents.to_s.should == BSON::ByteBuffer.new('010101').to_s
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
context "Saving a document with a blank binary value" do
|
17
17
|
setup do
|
18
18
|
@document = Doc do
|
@@ -0,0 +1,76 @@
|
|
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(:[]).returns(nil)
|
12
|
+
@klass.any_instance.stubs(:[]=).returns(nil)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "new" do
|
16
|
+
setup do
|
17
|
+
@doc = @klass.new
|
18
|
+
@doc.stubs(:new?).returns(true)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "be class/new" do
|
22
|
+
@doc.cache_key.should == 'Post/new'
|
23
|
+
end
|
24
|
+
|
25
|
+
should "work with suffix" do
|
26
|
+
@doc.cache_key(:foo).
|
27
|
+
should == 'Post/new/foo'
|
28
|
+
|
29
|
+
@doc.cache_key(:foo, :bar).
|
30
|
+
should == 'Post/new/foo/bar'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "not new" do
|
35
|
+
setup do
|
36
|
+
@object_id = BSON::ObjectID.new
|
37
|
+
@doc = @klass.new
|
38
|
+
@doc.stubs(:new?).returns(false)
|
39
|
+
@doc.stubs(:id).returns(@object_id)
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with updated_at" do
|
43
|
+
setup do
|
44
|
+
time = Time.utc(2010, 6, 20, 8, 10, 7)
|
45
|
+
@doc.stubs(:[]).with(:updated_at).returns(time)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "be class/id-timestamp" do
|
49
|
+
@doc.cache_key.should == "Post/#{@object_id}-20100620081007"
|
50
|
+
end
|
51
|
+
|
52
|
+
should "work with suffix" do
|
53
|
+
@doc.cache_key(:foo).
|
54
|
+
should == "Post/#{@object_id}-20100620081007/foo"
|
55
|
+
|
56
|
+
@doc.cache_key(:foo, :bar).
|
57
|
+
should == "Post/#{@object_id}-20100620081007/foo/bar"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "without updated_at" do
|
62
|
+
should "be class/id" do
|
63
|
+
@doc.cache_key.should == "Post/#{@object_id}"
|
64
|
+
end
|
65
|
+
|
66
|
+
should "work with suffix" do
|
67
|
+
@doc.cache_key(:foo).
|
68
|
+
should == "Post/#{@object_id}/foo"
|
69
|
+
|
70
|
+
@doc.cache_key(:foo, :bar, :baz).
|
71
|
+
should == "Post/#{@object_id}/foo/bar/baz"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|