djsun-mongomapper 0.3.5.5 → 0.4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -1,34 +1,39 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestRailsCompatibility < Test::Unit::TestCase
|
4
|
-
class Item
|
5
|
-
include MongoMapper::EmbeddedDocument
|
6
|
-
key :for_all, String
|
7
|
-
end
|
8
|
-
|
9
|
-
class FirstItem < Item
|
10
|
-
key :first_only, String
|
11
|
-
many :second_items
|
12
|
-
end
|
13
|
-
|
14
|
-
class SecondItem < Item
|
15
|
-
key :second_only, String
|
16
|
-
end
|
17
|
-
|
18
|
-
context "EmbeddedDocument" do
|
19
|
-
should "raise error for to_param as embedded do not have id's" do
|
20
|
-
lambda { Item.new.to_param }.should raise_error
|
21
|
-
end
|
22
|
-
|
23
|
-
should "alias many to has_many" do
|
24
|
-
FirstItem.should respond_to(:has_many)
|
25
|
-
FirstItem.method(:has_many).should == FirstItem.method(:many)
|
26
|
-
end
|
27
|
-
|
28
|
-
should "have column names" do
|
29
|
-
Item.column_names.sort.should == ['_id', 'for_all']
|
30
|
-
FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
|
31
|
-
SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
|
32
|
-
end
|
33
|
-
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRailsCompatibility < Test::Unit::TestCase
|
4
|
+
class Item
|
5
|
+
include MongoMapper::EmbeddedDocument
|
6
|
+
key :for_all, String
|
7
|
+
end
|
8
|
+
|
9
|
+
class FirstItem < Item
|
10
|
+
key :first_only, String
|
11
|
+
many :second_items
|
12
|
+
end
|
13
|
+
|
14
|
+
class SecondItem < Item
|
15
|
+
key :second_only, String
|
16
|
+
end
|
17
|
+
|
18
|
+
context "EmbeddedDocument" do
|
19
|
+
should "raise error for to_param as embedded do not have id's" do
|
20
|
+
lambda { Item.new.to_param }.should raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
should "alias many to has_many" do
|
24
|
+
FirstItem.should respond_to(:has_many)
|
25
|
+
FirstItem.method(:has_many).should == FirstItem.method(:many)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "have column names" do
|
29
|
+
Item.column_names.sort.should == ['_id', 'for_all']
|
30
|
+
FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
|
31
|
+
SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
|
32
|
+
end
|
33
|
+
|
34
|
+
should "alias new to new_record?" do
|
35
|
+
instance = Item.new
|
36
|
+
instance.new_record?.should == instance.new?
|
37
|
+
end
|
38
|
+
end
|
34
39
|
end
|
@@ -1,52 +1,52 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class SerializationTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@document = Class.new do
|
6
|
-
include MongoMapper::EmbeddedDocument
|
7
|
-
key :name, String
|
8
|
-
key :age, Integer
|
9
|
-
key :awesome, Boolean
|
10
|
-
key :preferences, Hash
|
11
|
-
key :created_at, Time
|
12
|
-
end
|
13
|
-
|
14
|
-
@instance = @document.new(
|
15
|
-
:name => 'John Doe',
|
16
|
-
:age => 25,
|
17
|
-
:awesome => true,
|
18
|
-
:preferences => {:language => 'Ruby'},
|
19
|
-
:created_at => Time.now.change(:usec => 0)
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
[:json].each do |format|
|
24
|
-
context format do
|
25
|
-
should "be reversable" do
|
26
|
-
serialized = @instance.send("to_#{format}")
|
27
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
28
|
-
|
29
|
-
assert_equal @instance, unserialized
|
30
|
-
end
|
31
|
-
|
32
|
-
should "allow attribute only filtering" do
|
33
|
-
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
34
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
35
|
-
|
36
|
-
assert_equal @instance.name, unserialized.name
|
37
|
-
assert_equal @instance.age, unserialized.age
|
38
|
-
|
39
|
-
assert_nil unserialized.created_at
|
40
|
-
end
|
41
|
-
|
42
|
-
should "allow attribute except filtering" do
|
43
|
-
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
44
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
45
|
-
|
46
|
-
assert_nil unserialized.name
|
47
|
-
assert_nil unserialized.age
|
48
|
-
assert_equal @instance.awesome, unserialized.awesome
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SerializationTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@document = Class.new do
|
6
|
+
include MongoMapper::EmbeddedDocument
|
7
|
+
key :name, String
|
8
|
+
key :age, Integer
|
9
|
+
key :awesome, Boolean
|
10
|
+
key :preferences, Hash
|
11
|
+
key :created_at, Time
|
12
|
+
end
|
13
|
+
|
14
|
+
@instance = @document.new(
|
15
|
+
:name => 'John Doe',
|
16
|
+
:age => 25,
|
17
|
+
:awesome => true,
|
18
|
+
:preferences => {:language => 'Ruby'},
|
19
|
+
:created_at => Time.now.change(:usec => 0)
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
[:json].each do |format|
|
24
|
+
context format do
|
25
|
+
should "be reversable" do
|
26
|
+
serialized = @instance.send("to_#{format}")
|
27
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
28
|
+
|
29
|
+
assert_equal @instance, unserialized
|
30
|
+
end
|
31
|
+
|
32
|
+
should "allow attribute only filtering" do
|
33
|
+
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
34
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
35
|
+
|
36
|
+
assert_equal @instance.name, unserialized.name
|
37
|
+
assert_equal @instance.age, unserialized.age
|
38
|
+
assert ! unserialized.awesome
|
39
|
+
assert_nil unserialized.created_at
|
40
|
+
end
|
41
|
+
|
42
|
+
should "allow attribute except filtering" do
|
43
|
+
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
44
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
45
|
+
|
46
|
+
assert_nil unserialized.name
|
47
|
+
assert_nil unserialized.age
|
48
|
+
assert_equal @instance.awesome, unserialized.awesome
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,268 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SupportTest < Test::Unit::TestCase
|
4
|
+
context "Array#to_mongo" do
|
5
|
+
should "convert value to_a" do
|
6
|
+
Array.to_mongo([1, 2, 3, 4]).should == [1, 2, 3, 4]
|
7
|
+
Array.to_mongo('1').should == ['1']
|
8
|
+
Array.to_mongo({'1' => '2', '3' => '4'}).should == [['1', '2'], ['3', '4']]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "Array#from_mongo" do
|
13
|
+
should "be array if array" do
|
14
|
+
Array.from_mongo([1, 2]).should == [1, 2]
|
15
|
+
end
|
16
|
+
|
17
|
+
should "be empty array if nil" do
|
18
|
+
Array.from_mongo(nil).should == []
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "Binary#to_mongo" do
|
23
|
+
should "convert to byte buffer if not byte buffer" do
|
24
|
+
Binary.to_mongo('asdfsadasdfs').is_a?(ByteBuffer).should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
should "be byte buffer if byte buffer" do
|
28
|
+
Binary.to_mongo(ByteBuffer.new('asdfsadasdfs')).is_a?(ByteBuffer).should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Binary#from_mongo" do
|
33
|
+
should "return value" do
|
34
|
+
buffer = ByteBuffer.new('asdfasdfasdf')
|
35
|
+
Binary.from_mongo(buffer).to_s.should == buffer.to_s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "Boolean#to_mongo" do
|
40
|
+
should "be true for true" do
|
41
|
+
Boolean.to_mongo(true).should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
should "be false for false" do
|
45
|
+
Boolean.to_mongo(false).should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
should "handle odd assortment of other values" do
|
49
|
+
Boolean.to_mongo('true').should be_true
|
50
|
+
Boolean.to_mongo('t').should be_true
|
51
|
+
Boolean.to_mongo('1').should be_true
|
52
|
+
Boolean.to_mongo(1).should be_true
|
53
|
+
|
54
|
+
Boolean.to_mongo('false').should be_false
|
55
|
+
Boolean.to_mongo('f').should be_false
|
56
|
+
Boolean.to_mongo('0').should be_false
|
57
|
+
Boolean.to_mongo(0).should be_false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "Boolean#from_mongo" do
|
62
|
+
should "be true for true" do
|
63
|
+
Boolean.from_mongo(true).should be_true
|
64
|
+
end
|
65
|
+
|
66
|
+
should "be false for false" do
|
67
|
+
Boolean.from_mongo(false).should be_false
|
68
|
+
end
|
69
|
+
|
70
|
+
should "be false for nil" do
|
71
|
+
Boolean.from_mongo(nil).should be_false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "Date#to_mongo" do
|
76
|
+
should "be time if string" do
|
77
|
+
date = Date.to_mongo('10/1/2009')
|
78
|
+
date.should == Time.utc(2009, 10, 1)
|
79
|
+
date.should == date
|
80
|
+
date.month.should == 10
|
81
|
+
date.day.should == 1
|
82
|
+
date.year.should == 2009
|
83
|
+
end
|
84
|
+
|
85
|
+
should "be time if date" do
|
86
|
+
Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
|
87
|
+
end
|
88
|
+
|
89
|
+
should "be nil if bogus string" do
|
90
|
+
Date.to_mongo('jdsafop874').should be_nil
|
91
|
+
end
|
92
|
+
|
93
|
+
should "be nil if empty string" do
|
94
|
+
Date.to_mongo('').should be_nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "Date#from_mongo" do
|
99
|
+
should "be date if date" do
|
100
|
+
date = Date.new(2009, 10, 1)
|
101
|
+
from_date = Date.from_mongo(date)
|
102
|
+
from_date.should == date
|
103
|
+
from_date.month.should == 10
|
104
|
+
from_date.day.should == 1
|
105
|
+
from_date.year.should == 2009
|
106
|
+
end
|
107
|
+
|
108
|
+
should "be date if time" do
|
109
|
+
time = Time.now
|
110
|
+
Date.from_mongo(time).should == time.to_date
|
111
|
+
end
|
112
|
+
|
113
|
+
should "be nil if nil" do
|
114
|
+
Date.from_mongo(nil).should be_nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "Float#to_mongo" do
|
119
|
+
should "convert value to_f" do
|
120
|
+
[21, 21.0, '21'].each do |value|
|
121
|
+
Float.to_mongo(value).should == 21.0
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "Hash#from_mongo" do
|
127
|
+
should "convert hash to hash with indifferent access" do
|
128
|
+
hash = Hash.from_mongo(:foo => 'bar')
|
129
|
+
hash[:foo].should == 'bar'
|
130
|
+
hash['foo'].should == 'bar'
|
131
|
+
end
|
132
|
+
|
133
|
+
should "be hash if nil" do
|
134
|
+
hash = Hash.from_mongo(nil)
|
135
|
+
hash.should == {}
|
136
|
+
hash.is_a?(HashWithIndifferentAccess).should be_true
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "Hash#to_mongo instance method" do
|
141
|
+
should "have instance method that returns self" do
|
142
|
+
hash = HashWithIndifferentAccess.new('foo' => 'bar')
|
143
|
+
hash.to_mongo.should == {'foo' => 'bar'}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "Integer#to_mongo" do
|
148
|
+
should "convert value to integer" do
|
149
|
+
[21, 21.0, '21'].each do |value|
|
150
|
+
Integer.to_mongo(value).should == 21
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
should "work fine with big integers" do
|
155
|
+
[9223372036854775807, '9223372036854775807'].each do |value|
|
156
|
+
Integer.to_mongo(value).should == 9223372036854775807
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "NilClass#to_mongo" do
|
162
|
+
should "return nil" do
|
163
|
+
nil.to_mongo(nil).should be_nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "NilClass#from_mongo" do
|
168
|
+
should "return nil" do
|
169
|
+
nil.from_mongo(nil).should be_nil
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context "Object#to_mongo" do
|
174
|
+
should "return value" do
|
175
|
+
Object.to_mongo(21).should == 21
|
176
|
+
Object.to_mongo('21').should == '21'
|
177
|
+
Object.to_mongo(9223372036854775807).should == 9223372036854775807
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context "Object#from_mongo" do
|
182
|
+
should "return value" do
|
183
|
+
Object.from_mongo(21).should == 21
|
184
|
+
Object.from_mongo('21').should == '21'
|
185
|
+
Object.from_mongo(9223372036854775807).should == 9223372036854775807
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context "String#to_mongo" do
|
190
|
+
should "convert value to_s" do
|
191
|
+
[21, '21'].each do |value|
|
192
|
+
String.to_mongo(value).should == '21'
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
should "be nil if nil" do
|
197
|
+
String.to_mongo(nil).should be_nil
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context "String#from_mongo" do
|
202
|
+
should "be string if value present" do
|
203
|
+
String.from_mongo('Scotch! Scotch! Scotch!').should == 'Scotch! Scotch! Scotch!'
|
204
|
+
end
|
205
|
+
|
206
|
+
should "return nil if nil" do
|
207
|
+
String.from_mongo(nil).should be_nil
|
208
|
+
end
|
209
|
+
|
210
|
+
should "return nil if blank" do
|
211
|
+
String.from_mongo('').should be_nil
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context "Time#to_mongo without Time.zone" do
|
216
|
+
should "be time if string" do
|
217
|
+
Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456).utc
|
218
|
+
end
|
219
|
+
|
220
|
+
should "be time in utc if time" do
|
221
|
+
Time.to_mongo(Time.local(2009, 8, 15, 0, 0, 0)).zone.should == 'UTC'
|
222
|
+
end
|
223
|
+
|
224
|
+
should "not be nil if nil" do
|
225
|
+
# Time.parse actually returns like right now
|
226
|
+
Time.to_mongo(nil).should_not be_nil
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context "Time#to_mongo with Time.zone" do
|
231
|
+
should "be time if time" do
|
232
|
+
Time.zone = 'Hawaii'
|
233
|
+
Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0)).should == Time.utc(2009, 8, 16, 0, 0, 0)
|
234
|
+
Time.zone = nil
|
235
|
+
end
|
236
|
+
|
237
|
+
should "be time if string" do
|
238
|
+
Time.zone = 'Hawaii'
|
239
|
+
Time.to_mongo('2009-08-15 14:00:00').should == Time.utc(2009, 8, 16, 0, 0, 0)
|
240
|
+
Time.zone = nil
|
241
|
+
end
|
242
|
+
|
243
|
+
should "be nil if nil" do
|
244
|
+
Time.zone = 'Hawaii'
|
245
|
+
Time.to_mongo(nil).should be_nil
|
246
|
+
Time.zone = nil
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context "Time#from_mongo without Time.zone" do
|
251
|
+
should "be time" do
|
252
|
+
time = Time.now
|
253
|
+
Time.from_mongo(time).should == time
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
context "Time#from_mongo with Time.zone" do
|
258
|
+
should "be time in Time.zone" do
|
259
|
+
Time.zone = 'Hawaii'
|
260
|
+
|
261
|
+
time = Time.from_mongo(Time.utc(2009, 10, 1))
|
262
|
+
time.should == Time.zone.local(2009, 9, 30, 14)
|
263
|
+
time.is_a?(ActiveSupport::TimeWithZone).should be_true
|
264
|
+
|
265
|
+
Time.zone = nil
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TimeZonesTest < Test::Unit::TestCase
|
4
|
+
context "An instance of an embedded document" do
|
5
|
+
setup do
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::EmbeddedDocument
|
8
|
+
key :name, String
|
9
|
+
key :created_at, Time
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should "work without Time.zone" do
|
14
|
+
Time.zone = nil
|
15
|
+
|
16
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
17
|
+
doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
|
18
|
+
end
|
19
|
+
|
20
|
+
should "work with Time.zone set to the (default) UTC" do
|
21
|
+
Time.zone = 'UTC'
|
22
|
+
|
23
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
24
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
25
|
+
doc.created_at.should == Time.utc(2009, 8, 15, 14)
|
26
|
+
|
27
|
+
Time.zone = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
should_eventually "work with timezones that are not UTC" do
|
31
|
+
Time.zone = 'Hawaii'
|
32
|
+
|
33
|
+
doc = @document.new(:created_at => @original_time)
|
34
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
35
|
+
doc.created_at.should == Time.utc(2009, 8, 16)
|
36
|
+
|
37
|
+
Time.zone = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|