jnunemaker-mongomapper 0.3.1 → 0.3.2

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.
Files changed (37) hide show
  1. data/History +11 -0
  2. data/VERSION +1 -1
  3. data/lib/mongomapper/associations/base.rb +5 -2
  4. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +1 -2
  5. data/lib/mongomapper/associations/belongs_to_proxy.rb +3 -3
  6. data/lib/mongomapper/associations/many_documents_proxy.rb +85 -0
  7. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +2 -3
  8. data/lib/mongomapper/associations/many_embedded_proxy.rb +2 -4
  9. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  10. data/lib/mongomapper/associations/many_proxy.rb +1 -50
  11. data/lib/mongomapper/associations/proxy.rb +1 -1
  12. data/lib/mongomapper/document.rb +5 -12
  13. data/lib/mongomapper/embedded_document.rb +21 -2
  14. data/lib/mongomapper/key.rb +2 -1
  15. data/lib/mongomapper/serializers/json_serializer.rb +15 -0
  16. data/lib/mongomapper.rb +17 -10
  17. data/mongomapper.gemspec +17 -4
  18. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +39 -0
  19. data/test/functional/associations/test_belongs_to_proxy.rb +35 -0
  20. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  21. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  22. data/test/functional/associations/test_many_polymorphic_proxy.rb +259 -0
  23. data/test/functional/associations/test_many_proxy.rb +236 -0
  24. data/test/functional/test_associations.rb +22 -467
  25. data/test/functional/test_document.rb +76 -19
  26. data/test/functional/test_pagination.rb +2 -3
  27. data/test/models.rb +16 -0
  28. data/test/test_helper.rb +1 -1
  29. data/test/unit/serializers/test_json_serializer.rb +69 -16
  30. data/test/unit/test_association_base.rb +5 -0
  31. data/test/unit/test_document.rb +14 -4
  32. data/test/unit/test_embedded_document.rb +46 -21
  33. data/test/unit/test_key.rb +5 -0
  34. data/test/unit/test_mongo_id.rb +2 -2
  35. data/test/unit/test_rails_compatibility.rb +3 -3
  36. metadata +16 -3
  37. data/lib/mongomapper/associations/array_proxy.rb +0 -6
data/test/models.rb CHANGED
@@ -6,6 +6,22 @@ class Address
6
6
  key :zip, Integer
7
7
  end
8
8
 
9
+ class Message
10
+ include MongoMapper::Document
11
+ key :body, String
12
+ belongs_to :room
13
+ end
14
+
15
+ class Enter < Message; end
16
+ class Exit < Message; end
17
+ class Chat < Message; end
18
+
19
+ class Room
20
+ include MongoMapper::Document
21
+ key :name, String
22
+ many :messages, :polymorphic => true
23
+ end
24
+
9
25
  class Project
10
26
  include MongoMapper::Document
11
27
  key :name, String
data/test/test_helper.rb CHANGED
@@ -14,7 +14,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
14
  dir = (Pathname(__FILE__).dirname + '..' + 'lib').expand_path
15
15
  require dir + 'mongomapper'
16
16
 
17
- class Test::Unit::TestCase
17
+ class Test::Unit::TestCase
18
18
  def clear_all_collections
19
19
  MongoMapper::Document.descendants.map(&:delete_all)
20
20
  end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class JsonSerializationTest < Test::Unit::TestCase
4
4
  class Contact
5
- include MongoMapper::EmbeddedDocument
5
+ include MongoMapper::Document
6
6
  key :name, String
7
7
  key :age, Integer
8
8
  key :created_at, Time
@@ -29,18 +29,20 @@ class JsonSerializationTest < Test::Unit::TestCase
29
29
  should "encode all encodable attributes" do
30
30
  json = @contact.to_json
31
31
 
32
- assert_match %r{"name": "Konata Izumi"}, json
33
- assert_match %r{"age": 16}, json
34
- assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
35
- assert_match %r{"awesome": true}, json
36
- assert_match %r{"preferences": \{"shows": "anime"\}}, json
32
+ assert_no_match %r{"_id"}, json
33
+ assert_match %r{"name":"Konata Izumi"}, json
34
+ assert_match %r{"age":16}, json
35
+ assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
36
+ assert_match %r{"awesome":true}, json
37
+ assert_match %r{"preferences":\{"shows":"anime"\}}, json
37
38
  end
38
39
 
39
40
  should "allow attribute filtering with only" do
40
41
  json = @contact.to_json(:only => [:name, :age])
41
42
 
42
- assert_match %r{"name": "Konata Izumi"}, json
43
- assert_match %r{"age": 16}, json
43
+ assert_no_match %r{"_id"}, json
44
+ assert_match %r{"name":"Konata Izumi"}, json
45
+ assert_match %r{"age":16}, json
44
46
  assert_no_match %r{"awesome"}, json
45
47
  assert_no_match %r{"created_at"}, json
46
48
  assert_no_match %r{"preferences"}, json
@@ -49,6 +51,7 @@ class JsonSerializationTest < Test::Unit::TestCase
49
51
  should "allow attribute filtering with except" do
50
52
  json = @contact.to_json(:except => [:name, :age])
51
53
 
54
+ assert_no_match %r{"_id"}, json
52
55
  assert_no_match %r{"name"}, json
53
56
  assert_no_match %r{"age"}, json
54
57
  assert_match %r{"awesome"}, json
@@ -56,6 +59,51 @@ class JsonSerializationTest < Test::Unit::TestCase
56
59
  assert_match %r{"preferences"}, json
57
60
  end
58
61
 
62
+ context "_id key" do
63
+ should "not be included by default" do
64
+ json = @contact.to_json
65
+ assert_no_match %r{"_id":}, json
66
+ end
67
+
68
+ should "not be included even if :except is used" do
69
+ json = @contact.to_json(:except => :name)
70
+ assert_no_match %r{"_id":}, json
71
+ end
72
+ end
73
+
74
+ context "id method" do
75
+ setup do
76
+ def @contact.label; "Has cheezburger"; end
77
+ def @contact.favorite_quote; "Constraints are liberating"; end
78
+ end
79
+
80
+ should "be included by default" do
81
+ json = @contact.to_json
82
+ assert_match %r{"id"}, json
83
+ end
84
+
85
+ should "be included when single method included" do
86
+ json = @contact.to_json(:methods => :label)
87
+ assert_match %r{"id"}, json
88
+ assert_match %r{"label":"Has cheezburger"}, json
89
+ assert_match %r{"name":"Konata Izumi"}, json
90
+ assert_no_match %r{"favorite_quote":"Constraints are liberating"}, json
91
+ end
92
+
93
+ should "be included when multiple methods included" do
94
+ json = @contact.to_json(:methods => [:label, :favorite_quote])
95
+ assert_match %r{"id"}, json
96
+ assert_match %r{"label":"Has cheezburger"}, json
97
+ assert_match %r{"favorite_quote":"Constraints are liberating"}, json
98
+ assert_match %r{"name":"Konata Izumi"}, json
99
+ end
100
+
101
+ should "not be included if :only is present" do
102
+ json = @contact.to_json(:only => :name)
103
+ assert_no_match %r{"id":}, json
104
+ end
105
+ end
106
+
59
107
  context "including methods" do
60
108
  setup do
61
109
  def @contact.label; "Has cheezburger"; end
@@ -63,14 +111,19 @@ class JsonSerializationTest < Test::Unit::TestCase
63
111
  end
64
112
 
65
113
  should "include single method" do
66
- # Single method.
67
- assert_match %r{"label": "Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
114
+ json = @contact.to_json(:methods => :label)
115
+ assert_match %r{"label":"Has cheezburger"}, json
68
116
  end
69
117
 
70
118
  should "include multiple methods" do
71
119
  json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
72
- assert_match %r{"label": "Has cheezburger"}, json
73
- assert_match %r{"favorite_quote": "Constraints are liberating"}, json
120
+ assert_match %r{"label":"Has cheezburger"}, json
121
+ assert_match %r{"favorite_quote":"Constraints are liberating"}, json
122
+ assert_match %r{"name":"Konata Izumi"}, json
123
+ assert_no_match %r{"age":16}, json
124
+ assert_no_match %r{"awesome"}, json
125
+ assert_no_match %r{"created_at"}, json
126
+ assert_no_match %r{"preferences"}, json
74
127
  end
75
128
  end
76
129
 
@@ -83,12 +136,12 @@ class JsonSerializationTest < Test::Unit::TestCase
83
136
  end
84
137
 
85
138
  should "allow attribute filtering with only" do
86
- assert_equal %([{"name": "David"}, {"name": "Mary"}]), @contacts.to_json(:only => :name)
139
+ assert_equal %([{"name":"David"},{"name":"Mary"}]), @contacts.to_json(:only => :name)
87
140
  end
88
141
 
89
142
  should "allow attribute filtering with except" do
90
- json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at])
91
- assert_equal %([{"age": 39}, {"age": 14}]), json
143
+ json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at, :updated_at])
144
+ assert_equal %([{"id":"","age":39},{"id":"","age":14}]), json
92
145
  end
93
146
  end
94
147
 
@@ -98,7 +151,7 @@ class JsonSerializationTest < Test::Unit::TestCase
98
151
  2 => Contact.new(:name => 'Mary', :age => 14)
99
152
  }
100
153
 
101
- assert_equal %({"1": {"name": "David"}}), contacts.to_json(:only => [1, :name])
154
+ assert_equal %({"1":{"name":"David"}}), contacts.to_json(:only => [1, :name])
102
155
  end
103
156
 
104
157
  end
@@ -107,6 +107,11 @@ class AssociationBaseTest < Test::Unit::TestCase
107
107
  base.proxy_class.should == ManyProxy
108
108
  end
109
109
 
110
+ should "be ManyPolymorphicProxy for polymorphic many" do
111
+ base = Base.new(:many, :messages, :polymorphic => true)
112
+ base.proxy_class.should == ManyPolymorphicProxy
113
+ end
114
+
110
115
  should "be ManyEmbeddedProxy for many embedded" do
111
116
  base = Base.new(:many, :medias)
112
117
  base.proxy_class.should == ManyEmbeddedProxy
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'models'
2
3
 
3
4
  class DocumentTest < Test::Unit::TestCase
4
5
  context "The Document Class" do
@@ -52,6 +53,19 @@ class DocumentTest < Test::Unit::TestCase
52
53
  @document.collection.name.should == 'foobar'
53
54
  end
54
55
  end # Document class
56
+
57
+ context "Documents that inherit from other documents" do
58
+ should "default collection to inherited class" do
59
+ Message.collection.name.should == 'messages'
60
+ Enter.collection.name.should == 'messages'
61
+ Exit.collection.name.should == 'messages'
62
+ Chat.collection.name.should == 'messages'
63
+ end
64
+
65
+ should "track subclasses" do
66
+ Message.subclasses.should == [Enter, Exit, Chat]
67
+ end
68
+ end
55
69
 
56
70
  context "An instance of a document" do
57
71
  setup do
@@ -69,10 +83,6 @@ class DocumentTest < Test::Unit::TestCase
69
83
  doc.collection.should == @document.collection
70
84
  end
71
85
 
72
- should "automatically have an _id key" do
73
- @document.keys.keys.should include('_id')
74
- end
75
-
76
86
  should "automatically have a created_at key" do
77
87
  @document.keys.keys.should include('created_at')
78
88
  end
@@ -28,8 +28,8 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
28
28
  end
29
29
  end
30
30
 
31
- should "clear out document default keys" do
32
- @klass.keys.size.should == 0
31
+ should "add _id key" do
32
+ @klass.keys['_id'].should_not be_nil
33
33
  end
34
34
  end
35
35
 
@@ -110,17 +110,17 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
110
110
 
111
111
  context "keys" do
112
112
  should "be inherited" do
113
- Grandparent.keys.keys.should == ['grandparent']
114
- Parent.keys.keys.sort.should == ['grandparent', 'parent']
115
- Child.keys.keys.sort.should == ['child', 'grandparent', 'parent']
113
+ Grandparent.keys.keys.sort.should == ['_id', 'grandparent']
114
+ Parent.keys.keys.sort.should == ['_id', 'grandparent', 'parent']
115
+ Child.keys.keys.sort.should == ['_id', 'child', 'grandparent', 'parent']
116
116
  end
117
117
 
118
118
  should "propogate to subclasses if key added after class definition" do
119
119
  Grandparent.key :_type, String
120
120
 
121
- Grandparent.keys.keys.sort.should == ['_type', 'grandparent']
122
- Parent.keys.keys.sort.should == ['_type', 'grandparent', 'parent']
123
- Child.keys.keys.sort.should == ['_type', 'child', 'grandparent', 'parent']
121
+ Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
122
+ Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
123
+ Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
124
124
  end
125
125
  end
126
126
 
@@ -144,15 +144,35 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
144
144
  key :age, Integer
145
145
  end
146
146
  end
147
+
148
+ should "automatically have an _id key" do
149
+ @document.keys.keys.should include('_id')
150
+ end
151
+
152
+ should "have id method that is string representation of _id" do
153
+ doc = @document.new
154
+ doc.id.should == doc._id.to_s
155
+ end
156
+
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
162
+ end
147
163
 
148
164
  context "being initialized" do
149
165
  should "accept a hash that sets keys and values" do
150
166
  doc = @document.new(:name => 'John', :age => 23)
151
- doc.attributes.should == {'name' => 'John', 'age' => 23}
167
+ doc.attributes.keys.sort.should == ['_id', 'age', 'name']
168
+ doc.attributes['name'].should == 'John'
169
+ doc.attributes['age'].should == 23
152
170
  end
153
171
 
154
172
  should "not throw error if initialized with nil" do
155
- doc = @document.new(nil)
173
+ lambda {
174
+ @document.new(nil)
175
+ }.should_not raise_error
156
176
  end
157
177
  end
158
178
 
@@ -199,14 +219,15 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
199
219
  end
200
220
 
201
221
  context "attributes" do
202
- should "default to empty hash" do
222
+ should "default to hash with _id" do
203
223
  doc = @document.new
204
- doc.attributes.should == {}
224
+ doc.attributes.keys.should == ['_id']
205
225
  end
206
226
 
207
227
  should "return all keys that aren't nil" do
208
228
  doc = @document.new(:name => 'string', :age => nil)
209
- doc.attributes.should == {'name' => 'string'}
229
+ doc.attributes.keys.sort.should == ['_id', 'name']
230
+ doc.attributes.values.should include('string')
210
231
  end
211
232
  end
212
233
 
@@ -329,16 +350,20 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
329
350
  end
330
351
 
331
352
  context "equality" do
332
- should "be true if all keys and values are equal" do
333
- doc1 = @document.new(:name => 'John', :age => 27)
334
- doc2 = @document.new(:name => 'John', :age => 27)
335
- doc1.should == doc2
353
+ should "be equal if id and class are the same" do
354
+ (@document.new('_id' => 1) == @document.new('_id' => 1)).should be(true)
355
+ end
356
+
357
+ should "not be equal if class same but id different" do
358
+ (@document.new('_id' => 1) == @document.new('_id' => 2)).should be(false)
336
359
  end
337
360
 
338
- should "be false if not all the keys and values are equal" do
339
- doc1 = @document.new(:name => 'Steve', :age => 27)
340
- doc2 = @document.new(:name => 'John', :age => 27)
341
- doc1.should_not == doc2
361
+ should "not be equal if id same but class different" do
362
+ @another_document = Class.new do
363
+ include MongoMapper::Document
364
+ end
365
+
366
+ (@document.new('_id' => 1) == @another_document.new('_id' => 1)).should be(false)
342
367
  end
343
368
  end
344
369
  end # instance of a embedded document
@@ -109,6 +109,11 @@ class KeyTest < Test::Unit::TestCase
109
109
  key.set('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456)
110
110
  end
111
111
 
112
+ should "correctly typecast Times into UTC time zone" do
113
+ key = Key.new(:foo, Time)
114
+ key.set('2000-01-01 01:01:01.123456').zone.should == "UTC"
115
+ end
116
+
112
117
  should_eventually "correctly typecast Dates" do
113
118
  key = Key.new(:foo, Date)
114
119
  key.set('2000-01-01').should == Date.new(2000, 1, 1)
@@ -20,10 +20,10 @@ class MongoIDTest < Test::Unit::TestCase
20
20
  MongoID.mm_typecast(id.to_s).should == id
21
21
  end
22
22
 
23
- should "raise DocumentNotFound if invalid id" do
23
+ should "raise MongoMapper::IllegalID if invalid id" do
24
24
  lambda {
25
25
  MongoID.mm_typecast(1234)
26
- }.should raise_error(MongoMapper::DocumentNotFound)
26
+ }.should raise_error(MongoMapper::IllegalID)
27
27
  end
28
28
 
29
29
  should "raise exception if message does not match illegal object id" do
@@ -26,9 +26,9 @@ class TestRailsCompatibility < Test::Unit::TestCase
26
26
  end
27
27
 
28
28
  should "have column names" do
29
- Item.column_names.sort.should == ['for_all']
30
- FirstItem.column_names.sort.should == ['first_only', 'for_all']
31
- SecondItem.column_names.sort.should == ['for_all', 'second_only']
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
32
  end
33
33
  end
34
34
  end
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.1
4
+ version: 0.3.2
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-07-28 00:00:00 -07:00
12
+ date: 2009-08-06 00:00:00 -07:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -81,12 +81,13 @@ files:
81
81
  - bin/mmconsole
82
82
  - lib/mongomapper.rb
83
83
  - lib/mongomapper/associations.rb
84
- - lib/mongomapper/associations/array_proxy.rb
85
84
  - lib/mongomapper/associations/base.rb
86
85
  - lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
87
86
  - lib/mongomapper/associations/belongs_to_proxy.rb
87
+ - lib/mongomapper/associations/many_documents_proxy.rb
88
88
  - lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb
89
89
  - lib/mongomapper/associations/many_embedded_proxy.rb
90
+ - lib/mongomapper/associations/many_polymorphic_proxy.rb
90
91
  - lib/mongomapper/associations/many_proxy.rb
91
92
  - lib/mongomapper/associations/proxy.rb
92
93
  - lib/mongomapper/callbacks.rb
@@ -104,6 +105,12 @@ files:
104
105
  - lib/mongomapper/validations.rb
105
106
  - mongomapper.gemspec
106
107
  - test/NOTE_ON_TESTING
108
+ - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
109
+ - test/functional/associations/test_belongs_to_proxy.rb
110
+ - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
111
+ - test/functional/associations/test_many_embedded_proxy.rb
112
+ - test/functional/associations/test_many_polymorphic_proxy.rb
113
+ - test/functional/associations/test_many_proxy.rb
107
114
  - test/functional/test_associations.rb
108
115
  - test/functional/test_callbacks.rb
109
116
  - test/functional/test_document.rb
@@ -153,6 +160,12 @@ signing_key:
153
160
  specification_version: 2
154
161
  summary: Awesome gem for modeling your domain and storing it in mongo
155
162
  test_files:
163
+ - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
164
+ - test/functional/associations/test_belongs_to_proxy.rb
165
+ - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
166
+ - test/functional/associations/test_many_embedded_proxy.rb
167
+ - test/functional/associations/test_many_polymorphic_proxy.rb
168
+ - test/functional/associations/test_many_proxy.rb
156
169
  - test/functional/test_associations.rb
157
170
  - test/functional/test_callbacks.rb
158
171
  - test/functional/test_document.rb
@@ -1,6 +0,0 @@
1
- module MongoMapper
2
- module Associations
3
- class ArrayProxy < Proxy
4
- end
5
- end
6
- end