jnunemaker-mongomapper 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,9 +18,8 @@ class TestRailsCompatibility < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  should "have to_param that returns id" do
21
- id = MongoID.new
22
- instance = Order.create('_id' => id.to_s)
23
- instance.to_param.should == id.to_s
21
+ instance = Order.create('_id' => 1234)
22
+ instance.to_param.should == '1234'
24
23
  end
25
24
 
26
25
  should "alias new to new_record?" do
data/test/models.rb CHANGED
@@ -38,8 +38,12 @@ end
38
38
 
39
39
  class RealPerson
40
40
  include MongoMapper::Document
41
- many :pets
41
+ many :pets
42
42
  key :name, String
43
+
44
+ def realname=(n)
45
+ self.name = n
46
+ end
43
47
  end
44
48
 
45
49
  class Person
@@ -83,26 +87,26 @@ module TrModels
83
87
  include MongoMapper::EmbeddedDocument
84
88
  key :license_plate, String
85
89
  end
86
-
90
+
87
91
  class Car < TrModels::Transport
88
92
  include MongoMapper::EmbeddedDocument
89
93
  key :model, String
90
94
  key :year, Integer
91
95
  end
92
-
96
+
93
97
  class Bus < TrModels::Transport
94
98
  include MongoMapper::EmbeddedDocument
95
99
  key :max_passengers, Integer
96
100
  end
97
-
101
+
98
102
  class Ambulance < TrModels::Transport
99
103
  include MongoMapper::EmbeddedDocument
100
104
  key :icu, Boolean
101
105
  end
102
-
106
+
103
107
  class Fleet
104
108
  include MongoMapper::Document
105
109
  many :transports, :polymorphic => true, :class_name => "TrModels::Transport"
106
- key :name, String
110
+ key :name, String
107
111
  end
108
112
  end
@@ -141,7 +141,7 @@ class JsonSerializationTest < Test::Unit::TestCase
141
141
 
142
142
  should "allow attribute filtering with except" do
143
143
  json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at, :updated_at])
144
- assert_equal %([{"id":"","age":39},{"id":"","age":14}]), json
144
+ assert_equal %([{"id":null,"age":39},{"id":null,"age":14}]), json
145
145
  end
146
146
  end
147
147
 
@@ -100,7 +100,13 @@ class DocumentTest < Test::Unit::TestCase
100
100
 
101
101
  context "new?" do
102
102
  should "be true if no id" do
103
- @document.new.new?.should be(true)
103
+ @document.new.new?.should be_true
104
+ end
105
+
106
+ should "be true if id but using custom id and not saved yet" do
107
+ doc = @document.new
108
+ doc.id = '1234'
109
+ doc.new?.should be_true
104
110
  end
105
111
  end
106
112
 
@@ -55,16 +55,28 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
55
55
  end
56
56
  end
57
57
 
58
- should "work" do
58
+ should "work with name" do
59
+ key = @document.key(:name)
60
+ key.name.should == 'name'
61
+ end
62
+
63
+ should "work with name and type" do
59
64
  key = @document.key(:name, String)
60
65
  key.name.should == 'name'
61
66
  key.type.should == String
62
- key.should be_instance_of(MongoMapper::Key)
63
67
  end
64
68
 
65
- should "work with options" do
69
+ should "work with name, type and options" do
66
70
  key = @document.key(:name, String, :required => true)
67
- key.options[:required].should be(true)
71
+ key.name.should == 'name'
72
+ key.type.should == String
73
+ key.options[:required].should be_true
74
+ end
75
+
76
+ should "work with name and options" do
77
+ key = @document.key(:name, :required => true)
78
+ key.name.should == 'name'
79
+ key.options[:required].should be_true
68
80
  end
69
81
 
70
82
  should "be tracked per document" do
@@ -76,11 +88,11 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
76
88
  @document.keys['age'].type.should == Integer
77
89
  end
78
90
 
79
- should "be redefinable" do
91
+ should "not be redefinable" do
80
92
  @document.key(:foo, String)
81
93
  @document.keys['foo'].type.should == String
82
94
  @document.key(:foo, Integer)
83
- @document.keys['foo'].type.should == Integer
95
+ @document.keys['foo'].type.should == String
84
96
  end
85
97
 
86
98
  should "create reader method" do
@@ -134,6 +146,53 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
134
146
  Parent.subclasses.should == [Child, OtherChild]
135
147
  end
136
148
  end
149
+
150
+ context "Applying default values for keys" do
151
+ setup do
152
+ @document = Class.new do
153
+ include MongoMapper::EmbeddedDocument
154
+
155
+ key :name, String, :default => 'foo'
156
+ key :age, Integer, :default => 20
157
+ key :net_worth, Float, :default => 100.00
158
+ key :active, Boolean, :default => true
159
+ key :smart, Boolean, :default => false
160
+ key :skills, Array, :default => [1]
161
+ key :options, Hash, :default => {'foo' => 'bar'}
162
+ end
163
+
164
+ @doc = @document.new
165
+ end
166
+
167
+ should "work for strings" do
168
+ @doc.name.should == 'foo'
169
+ end
170
+
171
+ should "work for integers" do
172
+ @doc.age.should == 20
173
+ end
174
+
175
+ should "work for floats" do
176
+ @doc.net_worth.should == 100.00
177
+ end
178
+
179
+ should "work for booleans" do
180
+ @doc.active.should == true
181
+ @doc.smart.should == false
182
+ end
183
+
184
+ should "work for arrays" do
185
+ @doc.skills.should == [1]
186
+ @doc.skills << 2
187
+ @doc.skills.should == [1, 2]
188
+ end
189
+
190
+ should "work for hashes" do
191
+ @doc.options['foo'].should == 'bar'
192
+ @doc.options['baz'] = 'wick'
193
+ @doc.options['baz'].should == 'wick'
194
+ end
195
+ end
137
196
 
138
197
  context "An instance of an embedded document" do
139
198
  setup do
@@ -149,16 +208,23 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
149
208
  @document.keys.keys.should include('_id')
150
209
  end
151
210
 
152
- should "have id method that is string representation of _id" do
211
+ should "have id method that sets _id" do
153
212
  doc = @document.new
154
213
  doc.id.should == doc._id.to_s
155
214
  end
156
215
 
157
- should "be able to set _id using id=" do
158
- id = MongoID.new
159
- doc = @document.new(:id => id.to_s)
160
- doc._id.should == id
161
- doc.id.should == id.to_s
216
+ context "setting custom id" do
217
+ should "set _id" do
218
+ doc = @document.new(:id => '1234')
219
+ doc._id.should == '1234'
220
+ end
221
+
222
+ should "know that custom id is set" do
223
+ doc = @document.new
224
+ doc.using_custom_id?.should be_false
225
+ doc.id = '1234'
226
+ doc.using_custom_id?.should be_true
227
+ end
162
228
  end
163
229
 
164
230
  context "being initialized" do
@@ -168,6 +234,12 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
168
234
  doc.attributes['name'].should == 'John'
169
235
  doc.attributes['age'].should == 23
170
236
  end
237
+
238
+ should "be able to assign keys dynamically" do
239
+ doc = @document.new(:name => 'John', :skills => ['ruby', 'rails'])
240
+ doc.name.should == 'John'
241
+ doc.skills.should == ['ruby', 'rails']
242
+ end
171
243
 
172
244
  should "not throw error if initialized with nil" do
173
245
  lambda {
@@ -191,13 +263,6 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
191
263
  doc.attributes[:age].should == 10
192
264
  end
193
265
 
194
- should "raise undefined method if no key exists" do
195
- doc = @document.new(:name => 'foobar', :age => 10)
196
- lambda {
197
- doc.attributes = {:name => 'new value', :foobar => 'baz'}
198
- }.should raise_error(NoMethodError)
199
- end
200
-
201
266
  should "not ignore keys that have methods defined" do
202
267
  @document.class_eval do
203
268
  attr_writer :password
@@ -237,10 +302,19 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
237
302
  doc[:name].should == 'string'
238
303
  end
239
304
 
240
- should "be able to write key value with []=" do
241
- doc = @document.new
242
- doc[:name] = 'string'
243
- doc[:name].should == 'string'
305
+ context "[]=" do
306
+ should "write key value for existing key" do
307
+ doc = @document.new
308
+ doc[:name] = 'string'
309
+ doc[:name].should == 'string'
310
+ end
311
+
312
+ should "create key and write value for missing key" do
313
+ doc = @document.new
314
+ doc[:foo] = 'string'
315
+ @document.keys.keys.include?('foo').should be_true
316
+ doc[:foo].should == 'string'
317
+ end
244
318
  end
245
319
  end
246
320
 
@@ -273,6 +347,23 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
273
347
  doc = @document.new(:name => 'John', :age => 27)
274
348
  doc.name_and_age.should == 'John (27)'
275
349
  end
350
+
351
+ should "set instance variable" do
352
+ @document.key :foo, Array
353
+ doc = @document.new
354
+ doc.instance_variable_get("@foo").should be_nil
355
+ doc.foo
356
+ doc.instance_variable_get("@foo").should == []
357
+ end
358
+
359
+ should "not set instance variable if frozen" do
360
+ @document.key :foo, Array
361
+ doc = @document.new
362
+ doc.instance_variable_get("@foo").should be_nil
363
+ doc.freeze
364
+ doc.foo
365
+ doc.instance_variable_get("@foo").should be_nil
366
+ end
276
367
  end
277
368
 
278
369
  context "reading an attribute before typcasting" do
@@ -367,4 +458,4 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
367
458
  end
368
459
  end
369
460
  end # instance of a embedded document
370
- end
461
+ end
@@ -28,7 +28,6 @@ class FinderOptionsTest < Test::Unit::TestCase
28
28
  end
29
29
  end
30
30
 
31
-
32
31
  context "Converting conditions to criteria" do
33
32
  should "work with simple criteria" do
34
33
  FinderOptions.to_mongo_criteria(:foo => 'bar').should == {
@@ -47,53 +46,23 @@ class FinderOptionsTest < Test::Unit::TestCase
47
46
  }
48
47
  end
49
48
 
50
- should "not use $in for arrays if already using array modifier" do
49
+ should "not use $in for arrays if already using array operator" do
51
50
  FinderOptions.to_mongo_criteria(:foo => {'$all' => [1,2,3]}).should == {
52
51
  :foo => {'$all' => [1,2,3]}
53
52
  }
53
+
54
+ FinderOptions.to_mongo_criteria(:foo => {'$any' => [1,2,3]}).should == {
55
+ :foo => {'$any' => [1,2,3]}
56
+ }
54
57
  end
55
58
 
56
59
  should "work arbitrarily deep" do
57
60
  FinderOptions.to_mongo_criteria(:foo => {:bar => [1,2,3]}).should == {
58
61
  :foo => {:bar => {'$in' => [1,2,3]}}
59
62
  }
60
- end
61
-
62
- should "convert string _ids to objectid automatically" do
63
- id = XGen::Mongo::Driver::ObjectID.new
64
-
65
- FinderOptions.to_mongo_criteria(:_id => id.to_s).should == {
66
- :_id => id
67
- }
68
- end
69
-
70
- should "leave objectid _ids alone" do
71
- id = XGen::Mongo::Driver::ObjectID.new
72
-
73
- FinderOptions.to_mongo_criteria(:_id => id).should == {
74
- :_id => id
75
- }
76
- end
77
-
78
- should "convert array of string _ids to object ids" do
79
- id1 = XGen::Mongo::Driver::ObjectID.new
80
- id2 = XGen::Mongo::Driver::ObjectID.new
81
-
82
- FinderOptions.to_mongo_criteria(:_id => [id1.to_s, id2.to_s]).should == {
83
- :_id => {'$in' => [id1, id2]}
84
- }
85
- end
86
-
87
- should "convert array of string _ids when using mongo array stuff" do
88
- id1 = XGen::Mongo::Driver::ObjectID.new
89
- id2 = XGen::Mongo::Driver::ObjectID.new
90
-
91
- FinderOptions.to_mongo_criteria(:_id => {'$all' => [id1.to_s, id2.to_s]}).should == {
92
- :_id => {'$all' => [id1, id2]}
93
- }
94
63
 
95
- FinderOptions.to_mongo_criteria(:_id => {'$any' => [id1.to_s, id2.to_s]}).should == {
96
- :_id => {'$any' => [id1, id2]}
64
+ FinderOptions.to_mongo_criteria(:foo => {:bar => {'$any' => [1,2,3]}}).should == {
65
+ :foo => {:bar => {'$any' => [1,2,3]}}
97
66
  }
98
67
  end
99
68
  end
@@ -14,7 +14,7 @@ class KeyTest < Test::Unit::TestCase
14
14
 
15
15
  context "The Key Class" do
16
16
  should "have the native types defined" do
17
- Key::NativeTypes.should == [String, Float, Time, Integer, Boolean, Array, Hash, MongoID]
17
+ Key::NativeTypes.should == [String, Float, Time, Integer, Boolean, Array, Hash]
18
18
  end
19
19
  end
20
20
 
@@ -30,7 +30,7 @@ class KeyTest < Test::Unit::TestCase
30
30
  should "allow setting options" do
31
31
  Key.new(:foo, Integer, :required => true).options[:required].should be(true)
32
32
  end
33
-
33
+
34
34
  should "default options to {}" do
35
35
  Key.new(:foo, Integer, nil).options.should == {}
36
36
  end
@@ -38,6 +38,30 @@ class KeyTest < Test::Unit::TestCase
38
38
  should "symbolize option keys" do
39
39
  Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
40
40
  end
41
+
42
+ should "work with just name" do
43
+ key = Key.new(:foo)
44
+ key.name.should == 'foo'
45
+ end
46
+
47
+ should "work with name and type" do
48
+ key = Key.new(:foo, String)
49
+ key.name.should == 'foo'
50
+ key.type.should == String
51
+ end
52
+
53
+ should "work with name, type, and options" do
54
+ key = Key.new(:foo, String, :required => true)
55
+ key.name.should == 'foo'
56
+ key.type.should == String
57
+ key.options[:required].should be_true
58
+ end
59
+
60
+ should "work with name and options" do
61
+ key = Key.new(:foo, :required => true)
62
+ key.name.should == 'foo'
63
+ key.options[:required].should be_true
64
+ end
41
65
  end
42
66
 
43
67
  context "A key" do
@@ -52,14 +76,19 @@ class KeyTest < Test::Unit::TestCase
52
76
  should "not be equal to another key with different type" do
53
77
  Key.new(:name, String).should_not == Key.new(:name, Integer)
54
78
  end
55
-
56
- should "know if it is native" do
57
- Key.new(:name, String).native?.should be_true
58
- end
59
-
60
- should "know if it is not native" do
61
- klass = Class.new
62
- Key.new(:name, klass).native?.should be_false
79
+
80
+ context "native?" do
81
+ should "be true if native type" do
82
+ Key.new(:name, String).native?.should be_true
83
+ end
84
+
85
+ should "be true if no type" do
86
+ Key.new(:name).native?.should be_true
87
+ end
88
+
89
+ should "be false if not native" do
90
+ Key.new(:name, Class.new).native?.should be_false
91
+ end
63
92
  end
64
93
 
65
94
  should "know if it is a embedded_document" do
@@ -75,14 +104,6 @@ class KeyTest < Test::Unit::TestCase
75
104
  end
76
105
 
77
106
  context "setting a value" do
78
- should "correctly typecast MongoIDs" do
79
- key = Key.new(:_id, MongoID)
80
- id = MongoID.new
81
- [id, id.to_s].each do |a|
82
- key.set(a).should == id
83
- end
84
- end
85
-
86
107
  should "correctly typecast Strings" do
87
108
  key = Key.new(:foo, String)
88
109
  [21, '21'].each do |a|
@@ -114,11 +135,6 @@ class KeyTest < Test::Unit::TestCase
114
135
  key.set('2000-01-01 01:01:01.123456').zone.should == "UTC"
115
136
  end
116
137
 
117
- should_eventually "correctly typecast Dates" do
118
- key = Key.new(:foo, Date)
119
- key.set('2000-01-01').should == Date.new(2000, 1, 1)
120
- end
121
-
122
138
  should "correctly typecast Boolean" do
123
139
  key = Key.new(:foo, Boolean)
124
140
  ['false', false, 'f', '0', 0].each do |b|
@@ -151,6 +167,13 @@ class KeyTest < Test::Unit::TestCase
151
167
  key = Key.new(:foo, String)
152
168
  key.get('bar').should == 'bar'
153
169
  end
170
+
171
+ should "work without type" do
172
+ key = Key.new(:foo)
173
+ key.get([1,"2"]).should == [1, "2"]
174
+ key.get(false).should == false
175
+ key.get({}).should == {}
176
+ end
154
177
 
155
178
  context "for a key with a default value set" do
156
179
  setup do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnunemaker-mongomapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-06 00:00:00 -07:00
12
+ date: 2009-08-16 00:00:00 -07:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - "="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.10.1
33
+ version: 0.11.1
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: jnunemaker-validatable
@@ -92,6 +92,7 @@ files:
92
92
  - lib/mongomapper/associations/proxy.rb
93
93
  - lib/mongomapper/callbacks.rb
94
94
  - lib/mongomapper/document.rb
95
+ - lib/mongomapper/dynamic_finder.rb
95
96
  - lib/mongomapper/embedded_document.rb
96
97
  - lib/mongomapper/finder_options.rb
97
98
  - lib/mongomapper/key.rb
@@ -102,6 +103,7 @@ files:
102
103
  - lib/mongomapper/save_with_validation.rb
103
104
  - lib/mongomapper/serialization.rb
104
105
  - lib/mongomapper/serializers/json_serializer.rb
106
+ - lib/mongomapper/support.rb
105
107
  - lib/mongomapper/validations.rb
106
108
  - mongomapper.gemspec
107
109
  - test/NOTE_ON_TESTING
@@ -125,14 +127,13 @@ files:
125
127
  - test/unit/test_embedded_document.rb
126
128
  - test/unit/test_finder_options.rb
127
129
  - test/unit/test_key.rb
128
- - test/unit/test_mongo_id.rb
129
130
  - test/unit/test_mongomapper.rb
130
131
  - test/unit/test_observing.rb
131
132
  - test/unit/test_pagination.rb
132
133
  - test/unit/test_rails_compatibility.rb
133
134
  - test/unit/test_serializations.rb
134
135
  - test/unit/test_validations.rb
135
- has_rdoc: true
136
+ has_rdoc: false
136
137
  homepage: http://github.com/jnunemaker/mongomapper
137
138
  licenses:
138
139
  post_install_message:
@@ -157,7 +158,7 @@ requirements: []
157
158
  rubyforge_project: mongomapper
158
159
  rubygems_version: 1.3.5
159
160
  signing_key:
160
- specification_version: 2
161
+ specification_version: 3
161
162
  summary: Awesome gem for modeling your domain and storing it in mongo
162
163
  test_files:
163
164
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
@@ -180,7 +181,6 @@ test_files:
180
181
  - test/unit/test_embedded_document.rb
181
182
  - test/unit/test_finder_options.rb
182
183
  - test/unit/test_key.rb
183
- - test/unit/test_mongo_id.rb
184
184
  - test/unit/test_mongomapper.rb
185
185
  - test/unit/test_observing.rb
186
186
  - test/unit/test_pagination.rb
@@ -1,35 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MongoIDTest < Test::Unit::TestCase
4
- PhonyError = Class.new(StandardError)
5
-
6
- class Phony
7
- def to_s
8
- raise PhonyError
9
- end
10
- end
11
-
12
- context "mm_typecast" do
13
- should "return value if object id" do
14
- id = XGen::Mongo::Driver::ObjectID.new
15
- MongoID.mm_typecast(id).should == id
16
- end
17
-
18
- should "return object id if string" do
19
- id = XGen::Mongo::Driver::ObjectID.new
20
- MongoID.mm_typecast(id.to_s).should == id
21
- end
22
-
23
- should "raise MongoMapper::IllegalID if invalid id" do
24
- lambda {
25
- MongoID.mm_typecast(1234)
26
- }.should raise_error(MongoMapper::IllegalID)
27
- end
28
-
29
- should "raise exception if message does not match illegal object id" do
30
- lambda {
31
- MongoID.mm_typecast(Phony.new)
32
- }.should raise_error(PhonyError)
33
- end
34
- end
35
- end