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,183 +1,183 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FinderOptionsTest < Test::Unit::TestCase
|
4
|
-
include MongoMapper
|
5
|
-
|
6
|
-
should "raise error if provided something other than a hash" do
|
7
|
-
lambda { FinderOptions.new }.should raise_error(ArgumentError)
|
8
|
-
lambda { FinderOptions.new(1) }.should raise_error(ArgumentError)
|
9
|
-
end
|
10
|
-
|
11
|
-
should "have symbolize the keys of the hash provided" do
|
12
|
-
FinderOptions.new('
|
13
|
-
key.should be_instance_of(Symbol)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context "#criteria" do
|
18
|
-
should "convert conditions to criteria" do
|
19
|
-
FinderOptions.expects(:to_mongo_criteria).with(:foo => 1).returns({})
|
20
|
-
FinderOptions.new(:conditions => {:foo => 1}).criteria
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "#options" do
|
25
|
-
should "convert options to mongo options" do
|
26
|
-
FinderOptions.expects(:to_mongo_options).with(:order => 'foo asc', :select => 'foo,bar').returns({})
|
27
|
-
FinderOptions.new(:order => 'foo asc', :select => 'foo,bar').options
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "Converting conditions to criteria" do
|
32
|
-
should "work with simple criteria" do
|
33
|
-
FinderOptions.to_mongo_criteria(:foo => 'bar').should == {
|
34
|
-
:foo => 'bar'
|
35
|
-
}
|
36
|
-
|
37
|
-
FinderOptions.to_mongo_criteria(:foo => 'bar', :baz => 'wick').should == {
|
38
|
-
:foo => 'bar',
|
39
|
-
:baz => 'wick'
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
should "use $in for arrays" do
|
44
|
-
FinderOptions.to_mongo_criteria(:foo => [1,2,3]).should == {
|
45
|
-
:foo => {'$in' => [1,2,3]}
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
should "not use $in for arrays if already using array operator" do
|
50
|
-
FinderOptions.to_mongo_criteria(:foo => {'$all' => [1,2,3]}).should == {
|
51
|
-
:foo => {'$all' => [1,2,3]}
|
52
|
-
}
|
53
|
-
|
54
|
-
FinderOptions.to_mongo_criteria(:foo => {'$any' => [1,2,3]}).should == {
|
55
|
-
:foo => {'$any' => [1,2,3]}
|
56
|
-
}
|
57
|
-
end
|
58
|
-
|
59
|
-
should "work arbitrarily deep" do
|
60
|
-
FinderOptions.to_mongo_criteria(:foo => {:bar => [1,2,3]}).should == {
|
61
|
-
:foo => {:bar => {'$in' => [1,2,3]}}
|
62
|
-
}
|
63
|
-
|
64
|
-
FinderOptions.to_mongo_criteria(:foo => {:bar => {'$any' => [1,2,3]}}).should == {
|
65
|
-
:foo => {:bar => {'$any' => [1,2,3]}}
|
66
|
-
}
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context "ordering" do
|
71
|
-
should "single field with ascending direction" do
|
72
|
-
hash = OrderedHash.new
|
73
|
-
hash[:foo] = 1
|
74
|
-
FinderOptions.to_mongo_options(:order => 'foo asc')[:sort].should == hash
|
75
|
-
FinderOptions.to_mongo_options(:order => 'foo ASC')[:sort].should == hash
|
76
|
-
end
|
77
|
-
|
78
|
-
should "single field with descending direction" do
|
79
|
-
hash = OrderedHash.new
|
80
|
-
hash[:foo] = -1
|
81
|
-
FinderOptions.to_mongo_options(:order => 'foo desc')[:sort].should == hash
|
82
|
-
FinderOptions.to_mongo_options(:order => 'foo DESC')[:sort].should == hash
|
83
|
-
end
|
84
|
-
|
85
|
-
should "convert field without direction to ascending" do
|
86
|
-
hash = OrderedHash.new
|
87
|
-
hash[:foo] = 1
|
88
|
-
FinderOptions.to_mongo_options(:order => 'foo')[:sort].should == hash
|
89
|
-
end
|
90
|
-
|
91
|
-
should "convert multiple fields with directions" do
|
92
|
-
hash = OrderedHash.new
|
93
|
-
hash[:foo] = -1
|
94
|
-
hash[:bar] = 1
|
95
|
-
hash[:baz] = -1
|
96
|
-
FinderOptions.to_mongo_options(:order => 'foo desc, bar asc, baz desc')[:sort].should == hash
|
97
|
-
end
|
98
|
-
|
99
|
-
should "convert multiple fields with some missing directions" do
|
100
|
-
hash = OrderedHash.new
|
101
|
-
hash[:foo] = -1
|
102
|
-
hash[:bar] = 1
|
103
|
-
hash[:baz] = 1
|
104
|
-
FinderOptions.to_mongo_options(:order => 'foo desc, bar, baz')[:sort].should == hash
|
105
|
-
end
|
106
|
-
|
107
|
-
should "just use sort if sort and order are present" do
|
108
|
-
FinderOptions.to_mongo_options(:sort => {'$natural' => 1}, :order => 'foo asc')[:sort].should == {
|
109
|
-
'$natural' => 1
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
should "convert natural in order to proper" do
|
114
|
-
hash = OrderedHash.new
|
115
|
-
hash[:'$natural'] = 1
|
116
|
-
FinderOptions.to_mongo_options(:order => '$natural asc')[:sort].should == hash
|
117
|
-
hash[:'$natural'] = -1
|
118
|
-
FinderOptions.to_mongo_options(:order => '$natural desc')[:sort].should == hash
|
119
|
-
end
|
120
|
-
|
121
|
-
should "work for natural order ascending" do
|
122
|
-
FinderOptions.to_mongo_options(:sort => {'$natural' => 1})[:sort]['$natural'].should == 1
|
123
|
-
end
|
124
|
-
|
125
|
-
should "work for natural order descending" do
|
126
|
-
FinderOptions.to_mongo_options(:sort => {'$natural' => -1})[:sort]['$natural'].should == -1
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
context "
|
131
|
-
should "default to 0" do
|
132
|
-
FinderOptions.to_mongo_options({})[:
|
133
|
-
end
|
134
|
-
|
135
|
-
should "use
|
136
|
-
FinderOptions.to_mongo_options(:
|
137
|
-
end
|
138
|
-
|
139
|
-
should "covert string to integer" do
|
140
|
-
FinderOptions.to_mongo_options(:
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
context "limit" do
|
145
|
-
should "default to 0" do
|
146
|
-
FinderOptions.to_mongo_options({})[:limit].should == 0
|
147
|
-
end
|
148
|
-
|
149
|
-
should "use
|
150
|
-
FinderOptions.to_mongo_options(:limit => 2)[:limit].should == 2
|
151
|
-
end
|
152
|
-
|
153
|
-
should "covert string to integer" do
|
154
|
-
FinderOptions.to_mongo_options(:limit => '2')[:limit].should == 2
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
context "fields" do
|
159
|
-
should "default to nil" do
|
160
|
-
FinderOptions.to_mongo_options({})[:fields].should be(nil)
|
161
|
-
end
|
162
|
-
|
163
|
-
should "be converted to nil if empty string" do
|
164
|
-
FinderOptions.to_mongo_options(:fields => '')[:fields].should be(nil)
|
165
|
-
end
|
166
|
-
|
167
|
-
should "be converted to nil if []" do
|
168
|
-
FinderOptions.to_mongo_options(:fields => [])[:fields].should be(nil)
|
169
|
-
end
|
170
|
-
|
171
|
-
should "should work with array" do
|
172
|
-
FinderOptions.to_mongo_options({:fields => %w(a b)})[:fields].should == %w(a b)
|
173
|
-
end
|
174
|
-
|
175
|
-
should "convert comma separated list to array" do
|
176
|
-
FinderOptions.to_mongo_options({:fields => 'a, b'})[:fields].should == %w(a b)
|
177
|
-
end
|
178
|
-
|
179
|
-
should "also work as select" do
|
180
|
-
FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end # FinderOptionsTest
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FinderOptionsTest < Test::Unit::TestCase
|
4
|
+
include MongoMapper
|
5
|
+
|
6
|
+
should "raise error if provided something other than a hash" do
|
7
|
+
lambda { FinderOptions.new }.should raise_error(ArgumentError)
|
8
|
+
lambda { FinderOptions.new(1) }.should raise_error(ArgumentError)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "have symbolize the keys of the hash provided" do
|
12
|
+
FinderOptions.new('skip' => 1).options.keys.map do |key|
|
13
|
+
key.should be_instance_of(Symbol)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "#criteria" do
|
18
|
+
should "convert conditions to criteria" do
|
19
|
+
FinderOptions.expects(:to_mongo_criteria).with(:foo => 1).returns({})
|
20
|
+
FinderOptions.new(:conditions => {:foo => 1}).criteria
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#options" do
|
25
|
+
should "convert options to mongo options" do
|
26
|
+
FinderOptions.expects(:to_mongo_options).with(:order => 'foo asc', :select => 'foo,bar').returns({})
|
27
|
+
FinderOptions.new(:order => 'foo asc', :select => 'foo,bar').options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "Converting conditions to criteria" do
|
32
|
+
should "work with simple criteria" do
|
33
|
+
FinderOptions.to_mongo_criteria(:foo => 'bar').should == {
|
34
|
+
:foo => 'bar'
|
35
|
+
}
|
36
|
+
|
37
|
+
FinderOptions.to_mongo_criteria(:foo => 'bar', :baz => 'wick').should == {
|
38
|
+
:foo => 'bar',
|
39
|
+
:baz => 'wick'
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
should "use $in for arrays" do
|
44
|
+
FinderOptions.to_mongo_criteria(:foo => [1,2,3]).should == {
|
45
|
+
:foo => {'$in' => [1,2,3]}
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
should "not use $in for arrays if already using array operator" do
|
50
|
+
FinderOptions.to_mongo_criteria(:foo => {'$all' => [1,2,3]}).should == {
|
51
|
+
:foo => {'$all' => [1,2,3]}
|
52
|
+
}
|
53
|
+
|
54
|
+
FinderOptions.to_mongo_criteria(:foo => {'$any' => [1,2,3]}).should == {
|
55
|
+
:foo => {'$any' => [1,2,3]}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
should "work arbitrarily deep" do
|
60
|
+
FinderOptions.to_mongo_criteria(:foo => {:bar => [1,2,3]}).should == {
|
61
|
+
:foo => {:bar => {'$in' => [1,2,3]}}
|
62
|
+
}
|
63
|
+
|
64
|
+
FinderOptions.to_mongo_criteria(:foo => {:bar => {'$any' => [1,2,3]}}).should == {
|
65
|
+
:foo => {:bar => {'$any' => [1,2,3]}}
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "ordering" do
|
71
|
+
should "single field with ascending direction" do
|
72
|
+
hash = OrderedHash.new
|
73
|
+
hash[:foo] = 1
|
74
|
+
FinderOptions.to_mongo_options(:order => 'foo asc')[:sort].should == hash
|
75
|
+
FinderOptions.to_mongo_options(:order => 'foo ASC')[:sort].should == hash
|
76
|
+
end
|
77
|
+
|
78
|
+
should "single field with descending direction" do
|
79
|
+
hash = OrderedHash.new
|
80
|
+
hash[:foo] = -1
|
81
|
+
FinderOptions.to_mongo_options(:order => 'foo desc')[:sort].should == hash
|
82
|
+
FinderOptions.to_mongo_options(:order => 'foo DESC')[:sort].should == hash
|
83
|
+
end
|
84
|
+
|
85
|
+
should "convert field without direction to ascending" do
|
86
|
+
hash = OrderedHash.new
|
87
|
+
hash[:foo] = 1
|
88
|
+
FinderOptions.to_mongo_options(:order => 'foo')[:sort].should == hash
|
89
|
+
end
|
90
|
+
|
91
|
+
should "convert multiple fields with directions" do
|
92
|
+
hash = OrderedHash.new
|
93
|
+
hash[:foo] = -1
|
94
|
+
hash[:bar] = 1
|
95
|
+
hash[:baz] = -1
|
96
|
+
FinderOptions.to_mongo_options(:order => 'foo desc, bar asc, baz desc')[:sort].should == hash
|
97
|
+
end
|
98
|
+
|
99
|
+
should "convert multiple fields with some missing directions" do
|
100
|
+
hash = OrderedHash.new
|
101
|
+
hash[:foo] = -1
|
102
|
+
hash[:bar] = 1
|
103
|
+
hash[:baz] = 1
|
104
|
+
FinderOptions.to_mongo_options(:order => 'foo desc, bar, baz')[:sort].should == hash
|
105
|
+
end
|
106
|
+
|
107
|
+
should "just use sort if sort and order are present" do
|
108
|
+
FinderOptions.to_mongo_options(:sort => {'$natural' => 1}, :order => 'foo asc')[:sort].should == {
|
109
|
+
'$natural' => 1
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
should "convert natural in order to proper" do
|
114
|
+
hash = OrderedHash.new
|
115
|
+
hash[:'$natural'] = 1
|
116
|
+
FinderOptions.to_mongo_options(:order => '$natural asc')[:sort].should == hash
|
117
|
+
hash[:'$natural'] = -1
|
118
|
+
FinderOptions.to_mongo_options(:order => '$natural desc')[:sort].should == hash
|
119
|
+
end
|
120
|
+
|
121
|
+
should "work for natural order ascending" do
|
122
|
+
FinderOptions.to_mongo_options(:sort => {'$natural' => 1})[:sort]['$natural'].should == 1
|
123
|
+
end
|
124
|
+
|
125
|
+
should "work for natural order descending" do
|
126
|
+
FinderOptions.to_mongo_options(:sort => {'$natural' => -1})[:sort]['$natural'].should == -1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "skip" do
|
131
|
+
should "default to 0" do
|
132
|
+
FinderOptions.to_mongo_options({})[:skip].should == 0
|
133
|
+
end
|
134
|
+
|
135
|
+
should "use skip provided" do
|
136
|
+
FinderOptions.to_mongo_options(:skip => 2)[:skip].should == 2
|
137
|
+
end
|
138
|
+
|
139
|
+
should "covert string to integer" do
|
140
|
+
FinderOptions.to_mongo_options(:skip => '2')[:skip].should == 2
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context "limit" do
|
145
|
+
should "default to 0" do
|
146
|
+
FinderOptions.to_mongo_options({})[:limit].should == 0
|
147
|
+
end
|
148
|
+
|
149
|
+
should "use skip provided" do
|
150
|
+
FinderOptions.to_mongo_options(:limit => 2)[:limit].should == 2
|
151
|
+
end
|
152
|
+
|
153
|
+
should "covert string to integer" do
|
154
|
+
FinderOptions.to_mongo_options(:limit => '2')[:limit].should == 2
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "fields" do
|
159
|
+
should "default to nil" do
|
160
|
+
FinderOptions.to_mongo_options({})[:fields].should be(nil)
|
161
|
+
end
|
162
|
+
|
163
|
+
should "be converted to nil if empty string" do
|
164
|
+
FinderOptions.to_mongo_options(:fields => '')[:fields].should be(nil)
|
165
|
+
end
|
166
|
+
|
167
|
+
should "be converted to nil if []" do
|
168
|
+
FinderOptions.to_mongo_options(:fields => [])[:fields].should be(nil)
|
169
|
+
end
|
170
|
+
|
171
|
+
should "should work with array" do
|
172
|
+
FinderOptions.to_mongo_options({:fields => %w(a b)})[:fields].should == %w(a b)
|
173
|
+
end
|
174
|
+
|
175
|
+
should "convert comma separated list to array" do
|
176
|
+
FinderOptions.to_mongo_options({:fields => 'a, b'})[:fields].should == %w(a b)
|
177
|
+
end
|
178
|
+
|
179
|
+
should "also work as select" do
|
180
|
+
FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end # FinderOptionsTest
|
data/test/unit/test_key.rb
CHANGED
@@ -1,247 +1,175 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class Address
|
4
|
-
include MongoMapper::EmbeddedDocument
|
5
|
-
|
6
|
-
key :address, String
|
7
|
-
key :city, String
|
8
|
-
key :state, String
|
9
|
-
key :zip, Integer
|
10
|
-
end
|
11
|
-
|
12
|
-
class
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
should "allow setting the
|
27
|
-
Key.new(:foo,
|
28
|
-
end
|
29
|
-
|
30
|
-
should "allow setting
|
31
|
-
Key.new(:foo, Integer
|
32
|
-
end
|
33
|
-
|
34
|
-
should "
|
35
|
-
Key.new(:foo, Integer,
|
36
|
-
end
|
37
|
-
|
38
|
-
should "
|
39
|
-
Key.new(:foo, Integer,
|
40
|
-
end
|
41
|
-
|
42
|
-
should "
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
key
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
key
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
key
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
should "
|
73
|
-
Key.new(:name, String).
|
74
|
-
end
|
75
|
-
|
76
|
-
should "not be equal to another key with different
|
77
|
-
Key.new(:name, String).should_not == Key.new(:
|
78
|
-
end
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
Key.new(:
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
should "work
|
122
|
-
key = Key.new(:foo
|
123
|
-
[
|
124
|
-
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
key.
|
132
|
-
end
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
key.
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
end
|
177
|
-
|
178
|
-
should "work without type" do
|
179
|
-
key = Key.new(:foo)
|
180
|
-
key.get([1,"2"]).should == [1, "2"]
|
181
|
-
key.get(false).should == false
|
182
|
-
key.get({}).should == {}
|
183
|
-
end
|
184
|
-
|
185
|
-
context "for a key with a default value set" do
|
186
|
-
setup do
|
187
|
-
@key = Key.new(:foo, String, :default => 'baz')
|
188
|
-
end
|
189
|
-
|
190
|
-
should "return default value if value nil" do
|
191
|
-
@key.get(nil).should == 'baz'
|
192
|
-
end
|
193
|
-
|
194
|
-
should "return value if not blank" do
|
195
|
-
@key.get('foobar').should == 'foobar'
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
context "for a boolean key" do
|
200
|
-
should "allow setting default to false" do
|
201
|
-
Key.new(:active, Boolean, :default => false).get(nil).should be_false
|
202
|
-
end
|
203
|
-
|
204
|
-
should "allow setting default to true" do
|
205
|
-
Key.new(:active, Boolean, :default => true).get(nil).should be_true
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
context "for an array" do
|
210
|
-
should "return array" do
|
211
|
-
key = Key.new(:foo, Array)
|
212
|
-
key.get([1,2]).should == [1,2]
|
213
|
-
end
|
214
|
-
|
215
|
-
should "default to empty array" do
|
216
|
-
key = Key.new(:foo, Array)
|
217
|
-
key.get(nil).should == []
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
context "for a hash" do
|
222
|
-
should "default to empty hash" do
|
223
|
-
key = Key.new(:foo, Hash)
|
224
|
-
key.get(nil).should == {}
|
225
|
-
end
|
226
|
-
|
227
|
-
should "use hash with indifferent access" do
|
228
|
-
key = Key.new(:foo, Hash)
|
229
|
-
key.get({:foo => 'bar'})['foo'].should == 'bar'
|
230
|
-
key.get({:foo => 'bar'})[:foo].should == 'bar'
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
context "for a embedded_document" do
|
235
|
-
should "default to nil" do
|
236
|
-
key = Key.new(:foo, Address)
|
237
|
-
key.get(nil).should be_nil
|
238
|
-
end
|
239
|
-
|
240
|
-
should "return instance if instance" do
|
241
|
-
address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
|
242
|
-
key = Key.new(:foo, Address)
|
243
|
-
key.get(address).should == address
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
end # KeyTest
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Address
|
4
|
+
include MongoMapper::EmbeddedDocument
|
5
|
+
|
6
|
+
key :address, String
|
7
|
+
key :city, String
|
8
|
+
key :state, String
|
9
|
+
key :zip, Integer
|
10
|
+
end
|
11
|
+
|
12
|
+
class FooType < Struct.new(:bar)
|
13
|
+
def self.to_mongo(value)
|
14
|
+
'to_mongo'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_mongo(value)
|
18
|
+
'from_mongo'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class KeyTest < Test::Unit::TestCase
|
23
|
+
include MongoMapper
|
24
|
+
|
25
|
+
context "Initializing a new key" do
|
26
|
+
should "allow setting the name" do
|
27
|
+
Key.new(:foo, String).name.should == 'foo'
|
28
|
+
end
|
29
|
+
|
30
|
+
should "allow setting the type" do
|
31
|
+
Key.new(:foo, Integer).type.should be(Integer)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "allow setting options" do
|
35
|
+
Key.new(:foo, Integer, :required => true).options[:required].should be(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
should "default options to {}" do
|
39
|
+
Key.new(:foo, Integer, nil).options.should == {}
|
40
|
+
end
|
41
|
+
|
42
|
+
should "symbolize option keys" do
|
43
|
+
Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "work with just name" do
|
47
|
+
key = Key.new(:foo)
|
48
|
+
key.name.should == 'foo'
|
49
|
+
end
|
50
|
+
|
51
|
+
should "work with name and type" do
|
52
|
+
key = Key.new(:foo, String)
|
53
|
+
key.name.should == 'foo'
|
54
|
+
key.type.should == String
|
55
|
+
end
|
56
|
+
|
57
|
+
should "work with name, type, and options" do
|
58
|
+
key = Key.new(:foo, String, :required => true)
|
59
|
+
key.name.should == 'foo'
|
60
|
+
key.type.should == String
|
61
|
+
key.options[:required].should be_true
|
62
|
+
end
|
63
|
+
|
64
|
+
should "work with name and options" do
|
65
|
+
key = Key.new(:foo, :required => true)
|
66
|
+
key.name.should == 'foo'
|
67
|
+
key.options[:required].should be_true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "A key" do
|
72
|
+
should "be equal to another key with same name and type" do
|
73
|
+
Key.new(:name, String).should == Key.new(:name, String)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "not be equal to another key with different name" do
|
77
|
+
Key.new(:name, String).should_not == Key.new(:foo, String)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "not be equal to another key with different type" do
|
81
|
+
Key.new(:name, String).should_not == Key.new(:name, Integer)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "know if it is a embedded_document" do
|
85
|
+
klass = Class.new do
|
86
|
+
include MongoMapper::EmbeddedDocument
|
87
|
+
end
|
88
|
+
Key.new(:name, klass).embeddable?.should be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
should "know if it is not a embedded_document" do
|
92
|
+
Key.new(:name, String).embeddable?.should be_false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "setting a value with a custom type" do
|
97
|
+
should "correctly typecast" do
|
98
|
+
key = Key.new(:foo, FooType)
|
99
|
+
key.set("something").should == 'to_mongo'
|
100
|
+
end
|
101
|
+
|
102
|
+
should "correctly typecast if object of that type is given" do
|
103
|
+
key = Key.new(:foo, FooType)
|
104
|
+
key.set(FooType.new('something')).should == 'to_mongo'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "getting a value with a custom type" do
|
109
|
+
should "use #from_mongo to convert back to custom type" do
|
110
|
+
key = Key.new(:foo, FooType)
|
111
|
+
key.get('something').should == 'from_mongo'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "getting a value" do
|
116
|
+
should "work with a type" do
|
117
|
+
key = Key.new(:foo, String)
|
118
|
+
key.get('bar').should == 'bar'
|
119
|
+
end
|
120
|
+
|
121
|
+
should "work without type" do
|
122
|
+
key = Key.new(:foo)
|
123
|
+
key.get([1, '2']).should == [1, '2']
|
124
|
+
key.get(false).should == false
|
125
|
+
key.get({}).should == {}
|
126
|
+
end
|
127
|
+
|
128
|
+
context "for a embedded_document" do
|
129
|
+
should "default to nil" do
|
130
|
+
key = Key.new(:foo, Address)
|
131
|
+
key.get(nil).should be_nil
|
132
|
+
end
|
133
|
+
|
134
|
+
should "return instance if instance" do
|
135
|
+
address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
|
136
|
+
key = Key.new(:foo, Address)
|
137
|
+
key.get(address).should == address
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "string key with 'baz' default" do
|
143
|
+
setup do
|
144
|
+
@key = Key.new(:foo, String, :default => 'baz')
|
145
|
+
end
|
146
|
+
|
147
|
+
should "return default value if value nil" do
|
148
|
+
@key.get(nil).should == 'baz'
|
149
|
+
end
|
150
|
+
|
151
|
+
should "return value if not nil" do
|
152
|
+
@key.get('foobar').should == 'foobar'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context "string key with '' default" do
|
157
|
+
setup do
|
158
|
+
@key = Key.new(:foo, String, :default => '')
|
159
|
+
end
|
160
|
+
|
161
|
+
should "return default value if value nil" do
|
162
|
+
@key.get(nil).should == ''
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
context "boolean key" do
|
167
|
+
should "with false default" do
|
168
|
+
Key.new(:active, Boolean, :default => false).get(nil).should be_false
|
169
|
+
end
|
170
|
+
|
171
|
+
should "with true default" do
|
172
|
+
Key.new(:active, Boolean, :default => true).get(nil).should be_true
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end # KeyTest
|