jnunemaker-mongomapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,241 @@
1
+ require 'test_helper'
2
+
3
+ class EmbeddedDocumentTest < Test::Unit::TestCase
4
+ context "Including MongoMapper::EmbeddedDocument" do
5
+ setup do
6
+ @klass = Class.new do
7
+ include MongoMapper::EmbeddedDocument
8
+ end
9
+ end
10
+
11
+ should "clear out document default keys" do
12
+ @klass.keys.size.should == 0
13
+ end
14
+ end
15
+
16
+ context "An instance of an embedded document" do
17
+ setup do
18
+ @document = Class.new do
19
+ include MongoMapper::EmbeddedDocument
20
+
21
+ key :name, String
22
+ key :age, Integer
23
+ end
24
+ end
25
+
26
+ context "when initialized" do
27
+ should "accept a hash that sets keys and values" do
28
+ doc = @document.new(:name => 'John', :age => 23)
29
+ doc.attributes.should == {'name' => 'John', 'age' => 23}
30
+ end
31
+
32
+ should "silently reject keys that have not been defined" do
33
+ doc = @document.new(:foobar => 'baz')
34
+ doc.attributes.should == {}
35
+ end
36
+ end
37
+
38
+ context "mass assigning keys" do
39
+ should "update values for keys provided" do
40
+ doc = @document.new(:name => 'foobar', :age => 10)
41
+ doc.attributes = {:name => 'new value', :age => 5}
42
+ doc.attributes[:name].should == 'new value'
43
+ doc.attributes[:age].should == 5
44
+ end
45
+
46
+ should "not update values for keys that were not provided" do
47
+ doc = @document.new(:name => 'foobar', :age => 10)
48
+ doc.attributes = {:name => 'new value'}
49
+ doc.attributes[:name].should == 'new value'
50
+ doc.attributes[:age].should == 10
51
+ end
52
+
53
+ should "ignore keys that do not exist" do
54
+ doc = @document.new(:name => 'foobar', :age => 10)
55
+ doc.attributes = {:name => 'new value', :foobar => 'baz'}
56
+ doc.attributes[:name].should == 'new value'
57
+ doc.attributes[:foobar].should be(nil)
58
+ end
59
+
60
+ should "typecast key values" do
61
+ doc = @document.new(:name => 1234, :age => '21')
62
+ doc.name.should == '1234'
63
+ doc.age.should == 21
64
+ end
65
+ end
66
+
67
+ context "requesting keys" do
68
+ should "default to empty hash" do
69
+ doc = @document.new
70
+ doc.attributes.should == {}
71
+ end
72
+
73
+ should "return all keys that aren't nil" do
74
+ doc = @document.new(:name => 'string', :age => nil)
75
+ doc.attributes.should == {'name' => 'string'}
76
+ end
77
+ end
78
+
79
+ context "key shorcuts" do
80
+ should "be able to read key with []" do
81
+ doc = @document.new(:name => 'string')
82
+ doc[:name].should == 'string'
83
+ end
84
+
85
+ should "be able to write key value with []=" do
86
+ doc = @document.new
87
+ doc[:name] = 'string'
88
+ doc[:name].should == 'string'
89
+ end
90
+ end
91
+
92
+ context "indifferent access" do
93
+ should "be enabled for keys" do
94
+ doc = @document.new(:name => 'string')
95
+ doc.attributes[:name].should == 'string'
96
+ doc.attributes['name'].should == 'string'
97
+ end
98
+ end
99
+
100
+ context "reading an attribute" do
101
+ should "work for defined keys" do
102
+ doc = @document.new(:name => 'string')
103
+ doc.name.should == 'string'
104
+ end
105
+
106
+ should "raise no method error for undefined keys" do
107
+ doc = @document.new
108
+ lambda { doc.fart }.should raise_error(NoMethodError)
109
+ end
110
+
111
+ should "know if reader defined" do
112
+ doc = @document.new
113
+ doc.reader?('name').should be(true)
114
+ doc.reader?(:name).should be(true)
115
+ doc.reader?('age').should be(true)
116
+ doc.reader?(:age).should be(true)
117
+ doc.reader?('foobar').should be(false)
118
+ doc.reader?(:foobar).should be(false)
119
+ end
120
+
121
+ should "be accessible for use in the model" do
122
+ @document.class_eval do
123
+ def name_and_age
124
+ "#{read_attribute(:name)} (#{read_attribute(:age)})"
125
+ end
126
+ end
127
+
128
+ doc = @document.new(:name => 'John', :age => 27)
129
+ doc.name_and_age.should == 'John (27)'
130
+ end
131
+ end
132
+
133
+ context "reading an attribute before typcasting" do
134
+ should "work for defined keys" do
135
+ doc = @document.new(:name => 12)
136
+ doc.name_before_typecast.should == 12
137
+ end
138
+
139
+ should "raise no method error for undefined keys" do
140
+ doc = @document.new
141
+ lambda { doc.foo_before_typecast }.should raise_error(NoMethodError)
142
+ end
143
+
144
+ should "be accessible for use in a document" do
145
+ @document.class_eval do
146
+ def untypcasted_name
147
+ read_attribute_before_typecast(:name)
148
+ end
149
+ end
150
+
151
+ doc = @document.new(:name => 12)
152
+ doc.name.should == '12'
153
+ doc.untypcasted_name.should == 12
154
+ end
155
+ end
156
+
157
+ context "writing an attribute" do
158
+ should "work for defined keys" do
159
+ doc = @document.new
160
+ doc.name = 'John'
161
+ doc.name.should == 'John'
162
+ end
163
+
164
+ should "raise no method error for undefined keys" do
165
+ doc = @document.new
166
+ lambda { doc.fart = 'poof!' }.should raise_error(NoMethodError)
167
+ end
168
+
169
+ should "typecast value" do
170
+ doc = @document.new
171
+ doc.name = 1234
172
+ doc.name.should == '1234'
173
+ doc.age = '21'
174
+ doc.age.should == 21
175
+ end
176
+
177
+ should "know if writer defined" do
178
+ doc = @document.new
179
+ doc.writer?('name').should be(true)
180
+ doc.writer?('name=').should be(true)
181
+ doc.writer?(:name).should be(true)
182
+ doc.writer?('age').should be(true)
183
+ doc.writer?('age=').should be(true)
184
+ doc.writer?(:age).should be(true)
185
+ doc.writer?('foobar').should be(false)
186
+ doc.writer?('foobar=').should be(false)
187
+ doc.writer?(:foobar).should be(false)
188
+ end
189
+
190
+ should "be accessible for use in the model" do
191
+ @document.class_eval do
192
+ def name_and_age=(new_value)
193
+ new_value.match(/([^\(\s]+) \((.*)\)/)
194
+ write_attribute :name, $1
195
+ write_attribute :age, $2
196
+ end
197
+ end
198
+
199
+ doc = @document.new
200
+ doc.name_and_age = 'Frank (62)'
201
+ doc.name.should == 'Frank'
202
+ doc.age.should == 62
203
+ end
204
+ end # writing an attribute
205
+
206
+ context "respond_to?" do
207
+ setup do
208
+ @doc = @document.new
209
+ end
210
+
211
+ should "work for readers" do
212
+ @doc.respond_to?(:name).should be_true
213
+ @doc.respond_to?('name').should be_true
214
+ end
215
+
216
+ should "work for writers" do
217
+ @doc.respond_to?(:name=).should be_true
218
+ @doc.respond_to?('name=').should be_true
219
+ end
220
+
221
+ should "work for readers before typecast" do
222
+ @doc.respond_to?(:name_before_typecast).should be_true
223
+ @doc.respond_to?('name_before_typecast').should be_true
224
+ end
225
+ end
226
+
227
+ context "equality" do
228
+ should "be true if all keys and values are equal" do
229
+ doc1 = @document.new(:name => 'John', :age => 27)
230
+ doc2 = @document.new(:name => 'John', :age => 27)
231
+ doc1.should == doc2
232
+ end
233
+
234
+ should "be false if not all the keys and values are equal" do
235
+ doc1 = @document.new(:name => 'Steve', :age => 27)
236
+ doc2 = @document.new(:name => 'John', :age => 27)
237
+ doc1.should_not == doc2
238
+ end
239
+ end
240
+ end # instance of a embedded document
241
+ end
@@ -0,0 +1,133 @@
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('offset' => 1).options.keys.map do |key|
13
+ key.should be_instance_of(Symbol)
14
+ end
15
+ end
16
+
17
+ context "Converting conditions to criteria" do
18
+ should "work with simple criteria" do
19
+ FinderOptions.new(:conditions => {:foo => 'bar'}).criteria.should == {
20
+ :foo => 'bar'
21
+ }
22
+
23
+ FinderOptions.new(:conditions => {:foo => 'bar', :baz => 'wick'}).criteria.should == {
24
+ :foo => 'bar',
25
+ :baz => 'wick'
26
+ }
27
+ end
28
+
29
+ should "use $in for arrays" do
30
+ FinderOptions.new(:conditions => {:foo => [1,2,3]}).criteria.should == {
31
+ :foo => {'$in' => [1,2,3]}
32
+ }
33
+ end
34
+
35
+ should "work arbitrarily deep" do
36
+ FinderOptions.new(:conditions => {:foo => {:bar => [1,2,3]}}).criteria.should == {
37
+ :foo => {:bar => {'$in' => [1,2,3]}}
38
+ }
39
+ end
40
+ end
41
+
42
+ context "ordering" do
43
+ should "single field with ascending direction" do
44
+ hash = OrderedHash.new
45
+ hash[:foo] = 1
46
+ FinderOptions.new(:order => 'foo asc').options[:sort].should == hash
47
+ FinderOptions.new(:order => 'foo ASC').options[:sort].should == hash
48
+ end
49
+
50
+ should "single field with descending direction" do
51
+ hash = OrderedHash.new
52
+ hash[:foo] = -1
53
+ FinderOptions.new(:order => 'foo desc').options[:sort].should == hash
54
+ FinderOptions.new(:order => 'foo DESC').options[:sort].should == hash
55
+ end
56
+
57
+ should "convert field without direction to ascending" do
58
+ hash = OrderedHash.new
59
+ hash[:foo] = 1
60
+ FinderOptions.new(:order => 'foo').options[:sort].should == hash
61
+ end
62
+
63
+ should "convert multiple fields with directions" do
64
+ hash = OrderedHash.new
65
+ hash[:foo] = -1
66
+ hash[:bar] = 1
67
+ hash[:baz] = -1
68
+ options = FinderOptions.new(:order => 'foo desc, bar asc, baz desc').options[:sort].should == hash
69
+ end
70
+
71
+ should "convert multiple fields with some missing directions" do
72
+ hash = OrderedHash.new
73
+ hash[:foo] = -1
74
+ hash[:bar] = 1
75
+ hash[:baz] = 1
76
+ options = FinderOptions.new(:order => 'foo desc, bar, baz').options[:sort].should == hash
77
+ end
78
+ end
79
+
80
+ context "offset" do
81
+ should "default to 0" do
82
+ FinderOptions.new({}).options[:offset].should == 0
83
+ end
84
+
85
+ should "use offset provided" do
86
+ FinderOptions.new(:offset => 2).options[:offset].should == 2
87
+ end
88
+
89
+ should "covert string to integer" do
90
+ FinderOptions.new(:offset => '2').options[:offset].should == 2
91
+ end
92
+ end
93
+
94
+ context "limit" do
95
+ should "default to 0" do
96
+ FinderOptions.new({}).options[:limit].should == 0
97
+ end
98
+
99
+ should "use offset provided" do
100
+ FinderOptions.new(:limit => 2).options[:limit].should == 2
101
+ end
102
+
103
+ should "covert string to integer" do
104
+ FinderOptions.new(:limit => '2').options[:limit].should == 2
105
+ end
106
+ end
107
+
108
+ context "fields" do
109
+ should "default to nil" do
110
+ FinderOptions.new({}).options[:fields].should be(nil)
111
+ end
112
+
113
+ should "be converted to nil if empty string" do
114
+ FinderOptions.new(:fields => '').options[:fields].should be(nil)
115
+ end
116
+
117
+ should "be converted to nil if []" do
118
+ FinderOptions.new(:fields => []).options[:fields].should be(nil)
119
+ end
120
+
121
+ should "should work with array" do
122
+ FinderOptions.new({:fields => %w(a b)}).options[:fields].should == %w(a b)
123
+ end
124
+
125
+ should "convert comma separated list to array" do
126
+ FinderOptions.new({:fields => 'a, b'}).options[:fields].should == %w(a b)
127
+ end
128
+
129
+ should "also work as select" do
130
+ FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
131
+ end
132
+ end
133
+ end # FinderOptionsTest
@@ -0,0 +1,64 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+
6
+ gem 'jnunemaker-matchy', '0.4.0'
7
+ gem 'mocha'
8
+ gem 'thoughtbot-quietbacktrace'
9
+
10
+ require 'matchy'
11
+ require 'mocha'
12
+ require 'quietbacktrace'
13
+
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ dir = (Pathname(__FILE__).dirname + '..' + 'lib').expand_path
16
+ require dir + 'mongomapper'
17
+
18
+ class Test::Unit::TestCase
19
+ custom_matcher :be_nil do |receiver, matcher, args|
20
+ matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
21
+ matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
22
+ receiver.nil?
23
+ end
24
+
25
+ custom_matcher :be_true do |receiver, matcher, args|
26
+ matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
27
+ matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
28
+ receiver.eql?(true)
29
+ end
30
+
31
+ custom_matcher :be_false do |receiver, matcher, args|
32
+ matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
33
+ matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
34
+ receiver.eql?(false)
35
+ end
36
+
37
+ custom_matcher :be_valid do |receiver, matcher, args|
38
+ matcher.positive_failure_message = "Expected to be valid but it was invalid #{receiver.errors.inspect}"
39
+ matcher.negative_failure_message = "Expected to be invalid but it was valid #{receiver.errors.inspect}"
40
+ receiver.valid?
41
+ end
42
+
43
+ custom_matcher :have_error_on do |receiver, matcher, args|
44
+ receiver.valid?
45
+ attribute = args[0]
46
+ expected_message = args[1]
47
+
48
+ if expected_message.nil?
49
+ matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
50
+ matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
51
+ !receiver.errors.on(attribute).blank?
52
+ else
53
+ actual = receiver.errors.on(attribute)
54
+ matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
55
+ matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
56
+ actual == expected_message
57
+ end
58
+ end
59
+ end
60
+
61
+ DefaultDatabase = 'test' unless defined?(DefaultDatabase)
62
+ AlternateDatabase = 'test2' unless defined?(AlternateDatabase)
63
+
64
+ MongoMapper.database = DefaultDatabase
data/test/test_key.rb ADDED
@@ -0,0 +1,200 @@
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 KeyTest < Test::Unit::TestCase
13
+ include MongoMapper
14
+
15
+ context "The Key Class" do
16
+ should "have the native types defined" do
17
+ Key::NativeTypes.should == [String, Float, Time, Integer, Boolean, Array, Hash]
18
+ end
19
+ end
20
+
21
+ context "Initializing a new key" do
22
+ should "allow setting the name" do
23
+ Key.new(:foo, String).name.should == 'foo'
24
+ end
25
+
26
+ should "allow setting the type" do
27
+ Key.new(:foo, Integer).type.should be(Integer)
28
+ end
29
+
30
+ should "allow setting options" do
31
+ Key.new(:foo, Integer, :required => true).options[:required].should be(true)
32
+ end
33
+
34
+ should "symbolize option keys" do
35
+ Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
36
+ end
37
+ end
38
+
39
+ context "A key" do
40
+ should "be equal to another key with same name and type" do
41
+ Key.new(:name, String).should == Key.new(:name, String)
42
+ end
43
+
44
+ should "not be equal to another key with different name" do
45
+ Key.new(:name, String).should_not == Key.new(:foo, String)
46
+ end
47
+
48
+ should "not be equal to another key with different type" do
49
+ Key.new(:name, String).should_not == Key.new(:name, Integer)
50
+ end
51
+
52
+ should "know if it is native" do
53
+ Key.new(:name, String).native?.should be_true
54
+ end
55
+
56
+ should "know if it is not native" do
57
+ klass = Class.new
58
+ Key.new(:name, klass).native?.should be_false
59
+ end
60
+
61
+ should "know if it is a embedded_document" do
62
+ klass = Class.new do
63
+ include MongoMapper::EmbeddedDocument
64
+ end
65
+ Key.new(:name, klass).embedded_document?.should be_true
66
+ end
67
+
68
+ should "know if it is not a embedded_document" do
69
+ Key.new(:name, String).embedded_document?.should be_false
70
+ end
71
+ end
72
+
73
+ context "setting a value" do
74
+ should "correctly typecast Strings" do
75
+ key = Key.new(:foo, String)
76
+ [21, '21'].each do |a|
77
+ key.set(a).should == '21'
78
+ end
79
+ end
80
+
81
+ should "correctly typecast Integers" do
82
+ key = Key.new(:foo, Integer)
83
+ [21, 21.0, '21'].each do |a|
84
+ key.set(a).should == 21
85
+ end
86
+ end
87
+
88
+ should "correctly typecast Floats" do
89
+ key = Key.new(:foo, Float)
90
+ [21, 21.0, '21'].each do |a|
91
+ key.set(a).should == 21.0
92
+ end
93
+ end
94
+
95
+ should "correctly typecast Times" do
96
+ key = Key.new(:foo, Time)
97
+ key.set('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456)
98
+ end
99
+
100
+ should_eventually "correctly typecast Dates" do
101
+ key = Key.new(:foo, Date)
102
+ key.set('2000-01-01').should == Date.new(2000, 1, 1)
103
+ end
104
+
105
+ should "correctly typecast Boolean" do
106
+ key = Key.new(:foo, Boolean)
107
+ ['false', false, 'f', '0', 0].each do |b|
108
+ key.set(b).should == false
109
+ end
110
+
111
+ ['true', true, 't', '1', 1].each do |b|
112
+ key.set(b).should == true
113
+ end
114
+ end
115
+
116
+ should "correctly typecast Array" do
117
+ key = Key.new(:foo, Array)
118
+ key.set([1,2,3,4]).should == [1,2,3,4]
119
+ key.set({'1' => '2', '3' => '4'}).should == [['1', '2'], ['3', '4']]
120
+ key.set('1').should == ['1']
121
+ end
122
+
123
+ should "correctly typecast Hash using indifferent access" do
124
+ key = Key.new(:foo, Hash)
125
+ key.set(:foo => 'bar')[:foo].should == 'bar'
126
+ key.set(:foo => 'bar')['foo'].should == 'bar'
127
+ key.set(:foo => {:bar => 'baz'})[:foo][:bar].should == 'baz'
128
+ key.set(:foo => {:bar => 'baz'})['foo']['bar'].should == 'baz'
129
+ end
130
+ end
131
+
132
+ context "getting a value" do
133
+ should "work" do
134
+ key = Key.new(:foo, String)
135
+ key.get('bar').should == 'bar'
136
+ end
137
+
138
+ context "for a key with a default value set" do
139
+ setup do
140
+ @key = Key.new(:foo, String, :default => 'baz')
141
+ end
142
+
143
+ should "return default value if value nil" do
144
+ @key.get(nil).should == 'baz'
145
+ end
146
+
147
+ should "return value if not blank" do
148
+ @key.get('foobar').should == 'foobar'
149
+ end
150
+ end
151
+
152
+ context "for a boolean key" do
153
+ should "allow setting default to false" do
154
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
155
+ end
156
+
157
+ should "allow setting default to true" do
158
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
159
+ end
160
+ end
161
+
162
+ context "for an array" do
163
+ should "return array" do
164
+ key = Key.new(:foo, Array)
165
+ key.get([1,2]).should == [1,2]
166
+ end
167
+
168
+ should "default to empty array" do
169
+ key = Key.new(:foo, Array)
170
+ key.get(nil).should == []
171
+ end
172
+ end
173
+
174
+ context "for a hash" do
175
+ should "default to empty hash" do
176
+ key = Key.new(:foo, Hash)
177
+ key.get(nil).should == {}
178
+ end
179
+
180
+ should "use hash with indifferent access" do
181
+ key = Key.new(:foo, Hash)
182
+ key.get({:foo => 'bar'})['foo'].should == 'bar'
183
+ key.get({:foo => 'bar'})[:foo].should == 'bar'
184
+ end
185
+ end
186
+
187
+ context "for a embedded_document" do
188
+ should "default to nil" do
189
+ key = Key.new(:foo, Address)
190
+ key.get(nil).should be_nil
191
+ end
192
+
193
+ should "return instance if instance" do
194
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
195
+ key = Key.new(:foo, Address)
196
+ key.get(address).should == address
197
+ end
198
+ end
199
+ end
200
+ end # KeyTest
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class Address; end
4
+
5
+ class MongoMapperTest < Test::Unit::TestCase
6
+ should "be able to write and read connection" do
7
+ conn = XGen::Mongo::Driver::Mongo.new
8
+ MongoMapper.connection = conn
9
+ MongoMapper.connection.should == conn
10
+ end
11
+
12
+ should "default connection to new mongo ruby driver" do
13
+ MongoMapper.connection = nil
14
+ MongoMapper.connection.should be_instance_of(XGen::Mongo::Driver::Mongo)
15
+ end
16
+
17
+ should "be able to write and read default database" do
18
+ MongoMapper.database = DefaultDatabase
19
+ MongoMapper.database.should be_instance_of(XGen::Mongo::Driver::DB)
20
+ MongoMapper.database.name.should == DefaultDatabase
21
+ end
22
+
23
+ should "have document not found error" do
24
+ lambda {
25
+ MongoMapper::DocumentNotFound
26
+ }.should_not raise_error
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class TestRailsCompatibility < Test::Unit::TestCase
4
+ def setup
5
+ @document = Class.new do
6
+ include MongoMapper::Document
7
+ end
8
+ end
9
+
10
+ should "have to_param that returns id" do
11
+ instance = @document.create('_id' => '1234')
12
+ instance.to_param.should == '1234'
13
+ end
14
+
15
+ should "alias new to new_record?" do
16
+ instance = @document.new
17
+ instance.new_record?.should == instance.new?
18
+ end
19
+
20
+ should "have column names" do
21
+ @document.key :fname, String
22
+ @document.column_names.sort.should == ['_id', 'created_at', 'fname', 'updated_at']
23
+ end
24
+ end