mongo_mapper-unstable 2010.3.8 → 2010.06.23
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 +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
@@ -5,203 +5,208 @@ class FooMonster; end
|
|
5
5
|
|
6
6
|
class AssociationBaseTest < Test::Unit::TestCase
|
7
7
|
include MongoMapper::Plugins::Associations
|
8
|
-
|
8
|
+
|
9
9
|
should "initialize with type and name" do
|
10
10
|
base = Base.new(:many, :foos)
|
11
11
|
base.type.should == :many
|
12
12
|
base.name.should == :foos
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
should "also allow options when initializing" do
|
16
16
|
base = Base.new(:many, :foos, :polymorphic => true)
|
17
17
|
base.options[:polymorphic].should be_true
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
context "class_name" do
|
21
21
|
should "work for belongs_to" do
|
22
22
|
Base.new(:belongs_to, :user).class_name.should == 'User'
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
should "work for many" do
|
26
26
|
Base.new(:many, :smart_people).class_name.should == 'SmartPerson'
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
should "be changeable using class_name option" do
|
30
30
|
base = Base.new(:many, :smart_people, :class_name => 'IntelligentPerson')
|
31
31
|
base.class_name.should == 'IntelligentPerson'
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
context "klass" do
|
36
36
|
should "default to class_name constantized" do
|
37
37
|
Base.new(:belongs_to, :foo_monster).klass.should == FooMonster
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
should "be the specified class" do
|
41
41
|
anonnymous_class = Class.new
|
42
42
|
Base.new(:belongs_to, :foo_monster, :class => anonnymous_class).klass.should == anonnymous_class
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
context "many?" do
|
47
47
|
should "be true if many" do
|
48
48
|
Base.new(:many, :foos).many?.should be_true
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
should "be false if not many" do
|
52
52
|
Base.new(:belongs_to, :foo).many?.should be_false
|
53
53
|
Base.new(:one, :foo).many?.should be_false
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
context "one?" do
|
58
58
|
should "be true if one" do
|
59
59
|
Base.new(:one, :foo).one?.should be_true
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
should "be false if not one" do
|
63
63
|
Base.new(:many, :foo).one?.should be_false
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
context "belongs_to?" do
|
68
68
|
should "be true if belongs_to" do
|
69
69
|
Base.new(:belongs_to, :foo).belongs_to?.should be_true
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
should "be false if not belongs_to" do
|
73
73
|
Base.new(:many, :foos).belongs_to?.should be_false
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
context "polymorphic?" do
|
78
78
|
should "be true if polymorphic" do
|
79
79
|
Base.new(:many, :foos, :polymorphic => true).polymorphic?.should be_true
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
should "be false if not polymorphic" do
|
83
83
|
Base.new(:many, :bars).polymorphic?.should be_false
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
context "as?" do
|
88
88
|
should "be true if one" do
|
89
89
|
Base.new(:one, :foo, :as => :commentable).as?.should be_true
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
should "be false if not one" do
|
93
93
|
Base.new(:many, :foo).as?.should be_false
|
94
94
|
end
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
context "in_array?" do
|
98
98
|
should "be true if one" do
|
99
99
|
Base.new(:one, :foo, :in => :list_ids).in_array?.should be_true
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
should "be false if not one" do
|
103
103
|
Base.new(:many, :foo).in_array?.should be_false
|
104
104
|
end
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
context "query_options" do
|
108
108
|
should "default to empty hash" do
|
109
109
|
base = Base.new(:many, :foos)
|
110
110
|
base.query_options.should == {}
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
should "work with order" do
|
114
114
|
base = Base.new(:many, :foos, :order => 'position')
|
115
115
|
base.query_options.should == {:order => 'position'}
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
should "correctly parse from options" do
|
119
119
|
base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
|
120
120
|
base.query_options.should == {:order => 'position', :somekey => 'somevalue'}
|
121
121
|
end
|
122
122
|
end
|
123
|
-
|
123
|
+
|
124
124
|
context "type_key_name" do
|
125
125
|
should "be _type for many" do
|
126
126
|
Base.new(:many, :foos).type_key_name.should == '_type'
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
should "be association name _ type for belongs_to" do
|
130
130
|
Base.new(:belongs_to, :foo).type_key_name.should == 'foo_type'
|
131
131
|
end
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
context "foreign_key" do
|
135
135
|
should "default to assocation name _id for belongs to" do
|
136
136
|
base = Base.new(:belongs_to, :foo)
|
137
137
|
base.foreign_key.should == 'foo_id'
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
should "be overridable with :foreign_key option" do
|
141
141
|
base = Base.new(:belongs_to, :foo, :foreign_key => 'foobar_id')
|
142
142
|
base.foreign_key.should == 'foobar_id'
|
143
143
|
end
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
should "have ivar that is association name" do
|
147
147
|
Base.new(:belongs_to, :foo).ivar.should == '@_foo'
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
context "embeddable?" do
|
151
151
|
should "be true if class is embeddable" do
|
152
152
|
base = Base.new(:many, :medias)
|
153
153
|
base.embeddable?.should be_true
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
should "be false if class is not embeddable" do
|
157
157
|
base = Base.new(:many, :statuses)
|
158
158
|
base.embeddable?.should be_false
|
159
|
-
|
159
|
+
|
160
160
|
base = Base.new(:belongs_to, :project)
|
161
161
|
base.embeddable?.should be_false
|
162
162
|
end
|
163
163
|
end
|
164
|
-
|
164
|
+
|
165
165
|
context "proxy_class" do
|
166
|
-
should "be ManyDocumentsProxy for many" do
|
166
|
+
should "be ManyDocumentsProxy for many" do
|
167
167
|
base = Base.new(:many, :statuses)
|
168
168
|
base.proxy_class.should == ManyDocumentsProxy
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
171
|
should "be ManyPolymorphicProxy for polymorphic many" do
|
172
172
|
base = Base.new(:many, :messages, :polymorphic => true)
|
173
173
|
base.proxy_class.should == ManyPolymorphicProxy
|
174
174
|
end
|
175
|
-
|
175
|
+
|
176
176
|
should "be ManyEmbeddedProxy for many embedded" do
|
177
177
|
base = Base.new(:many, :medias)
|
178
178
|
base.proxy_class.should == ManyEmbeddedProxy
|
179
179
|
end
|
180
|
-
|
180
|
+
|
181
181
|
should "be ManyEmbeddedPolymorphicProxy for polymorphic many embedded" do
|
182
182
|
base = Base.new(:many, :medias, :polymorphic => true)
|
183
183
|
base.proxy_class.should == ManyEmbeddedPolymorphicProxy
|
184
184
|
end
|
185
|
-
|
185
|
+
|
186
186
|
should "be BelongsToProxy for belongs_to" do
|
187
187
|
base = Base.new(:belongs_to, :project)
|
188
188
|
base.proxy_class.should == BelongsToProxy
|
189
189
|
end
|
190
|
-
|
190
|
+
|
191
191
|
should "be BelongsToPolymorphicProxy for polymorphic belongs_to" do
|
192
192
|
base = Base.new(:belongs_to, :target, :polymorphic => true)
|
193
193
|
base.proxy_class.should == BelongsToPolymorphicProxy
|
194
194
|
end
|
195
|
-
|
195
|
+
|
196
196
|
should "be OneProxy for one" do
|
197
|
-
base = Base.new(:one, :
|
197
|
+
base = Base.new(:one, :status, :polymorphic => true)
|
198
198
|
base.proxy_class.should == OneProxy
|
199
199
|
end
|
200
|
-
|
200
|
+
|
201
|
+
should "be OneEmbeddedProxy for one embedded" do
|
202
|
+
base = Base.new(:one, :media)
|
203
|
+
base.proxy_class.should == OneEmbeddedProxy
|
204
|
+
end
|
205
|
+
|
201
206
|
should "be InArrayProxy for many with :in option" do
|
202
207
|
base = Base.new(:many, :messages, :in => :message_ids)
|
203
208
|
base.proxy_class.should == InArrayProxy
|
204
209
|
end
|
205
210
|
end
|
206
|
-
|
207
|
-
end
|
211
|
+
|
212
|
+
end
|
@@ -27,77 +27,77 @@ class ProxyTest < Test::Unit::TestCase
|
|
27
27
|
@nil_proxy = FakeNilProxy.new(@owner, @association)
|
28
28
|
@blank_proxy = FakeBlankProxy.new(@owner, @association)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
should 'return true for === target' do
|
32
32
|
@proxy = FakeProxy.new(@owner, @association)
|
33
33
|
@proxy.should === Array
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
should "set target to nil when reset is called" do
|
37
37
|
@proxy.reset
|
38
38
|
@proxy.target.should be_nil
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
should "be able to inspect the proxy" do
|
42
42
|
@proxy.inspect.should == '[1, 2]'
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
context "nil?" do
|
46
46
|
should "be true if nil" do
|
47
47
|
@nil_proxy.nil?.should be_true
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
should "be false if not nil" do
|
51
51
|
@proxy.nil?.should be_false
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
context "blank?" do
|
56
56
|
should "be true if blank" do
|
57
57
|
@blank_proxy.blank?.should be_true
|
58
58
|
@nil_proxy.blank?.should be_true
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
should "be false if not blank" do
|
62
62
|
@proxy.blank?.should be_false
|
63
63
|
end
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
context "present?" do
|
67
67
|
should "be true if present" do
|
68
68
|
@proxy.present?.should be_true
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
should "be false if not present" do
|
72
72
|
@blank_proxy.present?.should be_false
|
73
73
|
@nil_proxy.present?.should be_false
|
74
74
|
end
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
should "delegate respond_to? to target" do
|
78
78
|
@proxy.respond_to?(:each).should be_true
|
79
79
|
@proxy.respond_to?(:size).should be_true
|
80
80
|
@proxy.respond_to?(:gsub).should be_false
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
should "alias proxy owner to owner" do
|
84
84
|
@proxy.proxy_owner.should == @owner
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
should "alias proxy target to target" do
|
88
88
|
@proxy.proxy_target.should == @target
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
context "send" do
|
92
92
|
should "work if proxy responds to method" do
|
93
93
|
@proxy.send(:reset)
|
94
94
|
@proxy.target.should be_nil
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
should "work if the target responds to the method" do
|
98
98
|
@proxy.send(:size).should == 2
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
should "not work if neither the proxy or target respond to method" do
|
102
102
|
lambda { @proxy.send(:gsub) }.should raise_error
|
103
103
|
end
|
@@ -2,18 +2,18 @@ require 'test_helper'
|
|
2
2
|
require 'active_support/version'
|
3
3
|
|
4
4
|
class JsonSerializationTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
# Helper function in case things change in the future
|
5
|
+
|
6
|
+
# Helper function in case things change in the future
|
7
7
|
# - replacing all those to_json calls was a nightmare
|
8
|
-
def convert_to_json
|
8
|
+
def convert_to_json(object, options={})
|
9
9
|
ActiveSupport::JSON.encode(object, options)
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
class Tag
|
13
13
|
include MongoMapper::EmbeddedDocument
|
14
14
|
key :name, String
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
class Contact
|
18
18
|
include MongoMapper::Document
|
19
19
|
key :name, String
|
@@ -21,10 +21,10 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
21
21
|
key :created_at, Time
|
22
22
|
key :awesome, Boolean
|
23
23
|
key :preferences, Hash
|
24
|
-
|
24
|
+
|
25
25
|
many :tags, :class_name => 'JsonSerializationTest::Tag'
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def setup
|
29
29
|
Contact.include_root_in_json = false
|
30
30
|
@contact = Contact.new(
|
@@ -35,12 +35,12 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
35
35
|
:preferences => { :shows => 'anime' }
|
36
36
|
)
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
should "include demodulized root" do
|
40
40
|
Contact.include_root_in_json = true
|
41
41
|
assert_match %r{^\{"contact":\s?\{}, convert_to_json(@contact)
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
should "encode all encodable attributes" do
|
45
45
|
json = convert_to_json(@contact)
|
46
46
|
|
@@ -51,7 +51,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
51
51
|
assert_match %r{"awesome":true}, json
|
52
52
|
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
should "allow attribute filtering with only" do
|
56
56
|
json = convert_to_json(@contact, :only => [:name, :age])
|
57
57
|
|
@@ -62,7 +62,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
62
62
|
assert_no_match %r{"created_at"}, json
|
63
63
|
assert_no_match %r{"preferences"}, json
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
should "allow attribute filtering with except" do
|
67
67
|
json = convert_to_json(@contact, :except => [:name, :age])
|
68
68
|
|
@@ -73,30 +73,30 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
73
73
|
assert_match %r{"created_at"}, json
|
74
74
|
assert_match %r{"preferences"}, json
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
context "_id key" do
|
78
78
|
should "not be included by default" do
|
79
79
|
json = convert_to_json(@contact)
|
80
80
|
assert_no_match %r{"_id":}, json
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
should "not be included even if :except is used" do
|
84
84
|
json = convert_to_json(@contact, :except => :name)
|
85
85
|
assert_no_match %r{"_id":}, json
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
context "id method" do
|
90
90
|
setup do
|
91
91
|
def @contact.label; "Has cheezburger"; end
|
92
92
|
def @contact.favorite_quote; "Constraints are liberating"; end
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
should "be included by default" do
|
96
96
|
json = convert_to_json(@contact)
|
97
97
|
assert_match %r{"id"}, json
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
should "be included when single method included" do
|
101
101
|
json = convert_to_json(@contact, :methods => :label)
|
102
102
|
assert_match %r{"id"}, json
|
@@ -104,7 +104,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
104
104
|
assert_match %r{"name":"Konata Izumi"}, json
|
105
105
|
assert_no_match %r{"favorite_quote":"Constraints are liberating"}, json
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
should "be included when multiple methods included" do
|
109
109
|
json = convert_to_json(@contact, :methods => [:label, :favorite_quote])
|
110
110
|
assert_match %r{"id"}, json
|
@@ -112,29 +112,29 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
112
112
|
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
113
113
|
assert_match %r{"name":"Konata Izumi"}, json
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
should "not be included if :only is present" do
|
117
117
|
json = convert_to_json(@contact, :only => :name)
|
118
118
|
assert_no_match %r{"id":}, json
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
should "be represented by a string" do
|
122
122
|
json = convert_to_json(@contact)
|
123
123
|
assert_match %r{"id":"}, json
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
127
|
context "including methods" do
|
128
128
|
setup do
|
129
129
|
def @contact.label; "Has cheezburger"; end
|
130
130
|
def @contact.favorite_quote; "Constraints are liberating"; end
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
should "include single method" do
|
134
134
|
json = convert_to_json(@contact, :methods => :label)
|
135
135
|
assert_match %r{"label":"Has cheezburger"}, json
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
should "include multiple methods" do
|
139
139
|
json = convert_to_json(@contact, :only => :name, :methods => [:label, :favorite_quote])
|
140
140
|
assert_match %r{"label":"Has cheezburger"}, json
|
@@ -146,7 +146,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
146
146
|
assert_no_match %r{"preferences"}, json
|
147
147
|
end
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
context "array of records" do
|
151
151
|
setup do
|
152
152
|
@contacts = [
|
@@ -160,7 +160,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
160
160
|
assert_match %r{\{"name":"David"\}}, json
|
161
161
|
assert_match %r{\{"name":"Mary"\}}, json
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
should "allow attribute filtering with except" do
|
165
165
|
json = convert_to_json(@contacts, :except => [:name, :preferences, :awesome, :created_at, :updated_at])
|
166
166
|
assert_match %r{"age":39}, json
|
@@ -172,7 +172,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
172
172
|
assert_no_match %r{"updated_at":}, json
|
173
173
|
end
|
174
174
|
end
|
175
|
-
|
175
|
+
|
176
176
|
should "allow options for hash of records" do
|
177
177
|
contacts = {
|
178
178
|
1 => Contact.new(:name => 'David', :age => 39),
|
@@ -183,7 +183,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
183
183
|
assert_match %r{\{"name":"David"\}}, json
|
184
184
|
assert_no_match %r{"2":}, json
|
185
185
|
end
|
186
|
-
|
186
|
+
|
187
187
|
should "include embedded attributes" do
|
188
188
|
contact = Contact.new(:name => 'John', :age => 27)
|
189
189
|
contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
|
@@ -192,7 +192,7 @@ class JsonSerializationTest < Test::Unit::TestCase
|
|
192
192
|
assert_match %r{"name":"awesome"}, json
|
193
193
|
assert_match %r{"name":"ruby"}, json
|
194
194
|
end
|
195
|
-
|
195
|
+
|
196
196
|
should "include dynamic attributes" do
|
197
197
|
contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
|
198
198
|
contact['smell'] = 'stinky'
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CloneTest < Test::Unit::TestCase
|
4
|
+
context "Document" do
|
5
|
+
setup do
|
6
|
+
@document = Doc()
|
7
|
+
@embedded = EDoc()
|
8
|
+
@document.many :widgets, :class => @embedded
|
9
|
+
@tags = ['red', 'green', 'blue']
|
10
|
+
@doc = @document.create({
|
11
|
+
:name => "foo",
|
12
|
+
:age => 27,
|
13
|
+
:tags => @tags,
|
14
|
+
:widgets => [@embedded.new, @embedded.new],
|
15
|
+
})
|
16
|
+
end
|
17
|
+
|
18
|
+
context "#clone" do
|
19
|
+
should "be new" do
|
20
|
+
@doc.clone.should be_new
|
21
|
+
end
|
22
|
+
|
23
|
+
should "copy the attributes" do
|
24
|
+
clone = @doc.clone
|
25
|
+
clone.name.should == "foo"
|
26
|
+
clone.age.should == 27
|
27
|
+
end
|
28
|
+
|
29
|
+
should "clone duplicable attributes" do
|
30
|
+
@doc.clone.tags.should_not equal(@tags)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "clone many embedded documents" do
|
34
|
+
@doc.clone.widgets.should_not equal(@doc.widgets)
|
35
|
+
end
|
36
|
+
|
37
|
+
should "not be destroyed" do
|
38
|
+
@doc.destroy
|
39
|
+
@doc.clone.should_not be_destroyed
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "EmbeddedDocument" do
|
45
|
+
setup do
|
46
|
+
@document = EDoc do
|
47
|
+
key :name, String
|
48
|
+
key :age, Integer
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "#clone" do
|
53
|
+
should "regenerate the id" do
|
54
|
+
doc = @document.new(:name => "foo", :age => 27)
|
55
|
+
doc_id = doc.id
|
56
|
+
clone = doc.clone
|
57
|
+
clone_id = clone.id
|
58
|
+
clone_id.should_not == doc_id
|
59
|
+
end
|
60
|
+
|
61
|
+
should "copy the attributes" do
|
62
|
+
doc = @document.new(:name => "foo", :age => 27)
|
63
|
+
clone = doc.clone
|
64
|
+
clone.name.should == "foo"
|
65
|
+
clone.age.should == 27
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -5,7 +5,7 @@ class DescendantAppendsTest < Test::Unit::TestCase
|
|
5
5
|
should "default descendants to a new set" do
|
6
6
|
MongoMapper::Document.descendants.should be_instance_of(Set)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
should 'allow extensions to Document to be appended' do
|
10
10
|
module Extension; def test_this_extension; end end
|
11
11
|
MongoMapper::Document.append_extensions(Extension)
|
@@ -34,12 +34,12 @@ class DescendantAppendsTest < Test::Unit::TestCase
|
|
34
34
|
article.new.should respond_to(:test_this_inclusion)
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
context "EmbeddedDocument" do
|
39
39
|
should "default descendants to a new set" do
|
40
40
|
MongoMapper::EmbeddedDocument.descendants.should be_instance_of(Set)
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
should 'allow extensions to Document to be appended' do
|
44
44
|
module Extension; def test_this_extension; end end
|
45
45
|
MongoMapper::EmbeddedDocument.append_extensions(Extension)
|