jnunemaker-mongomapper 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,11 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class JsonSerializationTest < Test::Unit::TestCase
4
+ class Tag
5
+ include MongoMapper::EmbeddedDocument
6
+ key :name, String
7
+ end
8
+
4
9
  class Contact
5
10
  include MongoMapper::Document
6
11
  key :name, String
@@ -8,6 +13,8 @@ class JsonSerializationTest < Test::Unit::TestCase
8
13
  key :created_at, Time
9
14
  key :awesome, Boolean
10
15
  key :preferences, Hash
16
+
17
+ many :tags, :class_name => 'JsonSerializationTest::Tag'
11
18
  end
12
19
 
13
20
  def setup
@@ -136,12 +143,20 @@ class JsonSerializationTest < Test::Unit::TestCase
136
143
  end
137
144
 
138
145
  should "allow attribute filtering with only" do
139
- assert_equal %([{"name":"David"},{"name":"Mary"}]), @contacts.to_json(:only => :name)
146
+ json = @contacts.to_json(:only => :name)
147
+ assert_match %r{\{"name":"David"\}}, json
148
+ assert_match %r{\{"name":"Mary"\}}, json
140
149
  end
141
150
 
142
151
  should "allow attribute filtering with except" do
143
152
  json = @contacts.to_json(:except => [:name, :preferences, :awesome, :created_at, :updated_at])
144
- assert_equal %([{"id":null,"age":39},{"id":null,"age":14}]), json
153
+ assert_match %r{"age":39}, json
154
+ assert_match %r{"age":14}, json
155
+ assert_no_match %r{"name":}, json
156
+ assert_no_match %r{"preferences":}, json
157
+ assert_no_match %r{"awesome":}, json
158
+ assert_no_match %r{"created_at":}, json
159
+ assert_no_match %r{"updated_at":}, json
145
160
  end
146
161
  end
147
162
 
@@ -150,8 +165,25 @@ class JsonSerializationTest < Test::Unit::TestCase
150
165
  1 => Contact.new(:name => 'David', :age => 39),
151
166
  2 => Contact.new(:name => 'Mary', :age => 14)
152
167
  }
153
-
154
- assert_equal %({"1":{"name":"David"}}), contacts.to_json(:only => [1, :name])
168
+ json = contacts.to_json(:only => [1, :name])
169
+ assert_match %r{"1":}, json
170
+ assert_match %r{\{"name":"David"\}}, json
171
+ assert_no_match %r{"2":}, json
172
+ end
173
+
174
+ should "include embedded attributes" do
175
+ contact = Contact.new(:name => 'John', :age => 27)
176
+ contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
177
+ json = contact.to_json
178
+ assert_match %r{"tags":}, json
179
+ assert_match %r{"name":"awesome"}, json
180
+ assert_match %r{"name":"ruby"}, json
155
181
  end
156
182
 
183
+ should "include dynamic attributes" do
184
+ contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
185
+ contact['smell'] = 'stinky'
186
+ json = contact.to_json
187
+ assert_match %r{"smell":"stinky"}, json
188
+ end
157
189
  end
@@ -78,8 +78,16 @@ class AssociationBaseTest < Test::Unit::TestCase
78
78
  end
79
79
  end
80
80
 
81
- should "have belongs_to_key_name" do
82
- Base.new(:belongs_to, :foo).belongs_to_key_name.should == 'foo_id'
81
+ context "foreign_key" do
82
+ should "default to assocation_name_id" do
83
+ base = Base.new(:belongs_to, :foo)
84
+ base.foreign_key.should == 'foo_id'
85
+ end
86
+
87
+ should "be overridable with :foreign_key option" do
88
+ base = Base.new(:belongs_to, :foo, :foreign_key => 'foobar_id')
89
+ base.foreign_key.should == 'foobar_id'
90
+ end
83
91
  end
84
92
 
85
93
  should "have ivar that is association name" do
@@ -83,14 +83,6 @@ class DocumentTest < Test::Unit::TestCase
83
83
  doc.collection.should == @document.collection
84
84
  end
85
85
 
86
- should "automatically have a created_at key" do
87
- @document.keys.keys.should include('created_at')
88
- end
89
-
90
- should "automatically have an updated_at key" do
91
- @document.keys.keys.should include('updated_at')
92
- end
93
-
94
86
  should "use default values if defined for keys" do
95
87
  @document.key :active, Boolean, :default => true
96
88
 
@@ -15,8 +15,20 @@ class Child < Parent
15
15
  key :child, String
16
16
  end
17
17
 
18
+ module KeyOverride
19
+ def other_child
20
+ read_attribute(:other_child) || "special result"
21
+ end
22
+
23
+ def other_child=(value)
24
+ super(value + " modified")
25
+ end
26
+ end
27
+
18
28
  class OtherChild < Parent
19
29
  include MongoMapper::EmbeddedDocument
30
+ include KeyOverride
31
+
20
32
  key :other_child, String
21
33
  end
22
34
 
@@ -41,6 +53,21 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
41
53
  end
42
54
  document.parent_model.should be_nil
43
55
  end
56
+
57
+ should "work when other modules have been included" do
58
+ grandparent = Class.new
59
+ parent = Class.new grandparent do
60
+ include MongoMapper::EmbeddedDocument
61
+ end
62
+
63
+ example_module = Module.new
64
+ document = Class.new(parent) do
65
+ include MongoMapper::EmbeddedDocument
66
+ include example_module
67
+ end
68
+
69
+ document.parent_model.should == parent
70
+ end
44
71
 
45
72
  should "find parent" do
46
73
  Parent.parent_model.should == Grandparent
@@ -134,6 +161,14 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
134
161
  Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
135
162
  Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
136
163
  end
164
+
165
+ should "not add anonymous objects to the ancestor tree" do
166
+ OtherChild.ancestors.any? { |a| a.name.blank? }.should be_false
167
+ end
168
+
169
+ should "not include descendant keys" do
170
+ lambda { Parent.new.other_child }.should raise_error
171
+ end
137
172
  end
138
173
 
139
174
  context "subclasses" do
@@ -364,6 +399,21 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
364
399
  doc.foo
365
400
  doc.instance_variable_get("@foo").should be_nil
366
401
  end
402
+
403
+ should "be overrideable by modules" do
404
+ @document = Class.new do
405
+ include MongoMapper::Document
406
+ key :other_child, String
407
+ end
408
+
409
+ child = @document.new
410
+ child.other_child.should be_nil
411
+
412
+ @document.send :include, KeyOverride
413
+
414
+ overriden_child = @document.new
415
+ overriden_child.other_child.should == 'special result'
416
+ end
367
417
  end
368
418
 
369
419
  context "reading an attribute before typcasting" do
@@ -424,6 +474,21 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
424
474
  doc.name.should == 'Frank'
425
475
  doc.age.should == 62
426
476
  end
477
+
478
+ should "be overrideable by modules" do
479
+ @document = Class.new do
480
+ include MongoMapper::Document
481
+ key :other_child, String
482
+ end
483
+
484
+ child = @document.new(:other_child => 'foo')
485
+ child.other_child.should == 'foo'
486
+
487
+ @document.send :include, KeyOverride
488
+
489
+ overriden_child = @document.new(:other_child => 'foo')
490
+ overriden_child.other_child.should == 'foo modified'
491
+ end
427
492
  end # writing an attribute
428
493
 
429
494
  context "checking if an attributes value is present" do
@@ -117,6 +117,13 @@ class KeyTest < Test::Unit::TestCase
117
117
  key.set(a).should == 21
118
118
  end
119
119
  end
120
+
121
+ should "work fine with long integers" do
122
+ key = Key.new(:foo, Integer)
123
+ [9223372036854775807, '9223372036854775807'].each do |value|
124
+ key.set(value).should == 9223372036854775807
125
+ end
126
+ end
120
127
 
121
128
  should "correctly typecast Floats" do
122
129
  key = Key.new(:foo, Float)
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.3
4
+ version: 0.3.4
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-16 00:00:00 -07:00
12
+ date: 2009-08-28 00:00:00 -07:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ files:
84
84
  - lib/mongomapper/associations/base.rb
85
85
  - lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb
86
86
  - lib/mongomapper/associations/belongs_to_proxy.rb
87
+ - lib/mongomapper/associations/many_documents_as_proxy.rb
87
88
  - lib/mongomapper/associations/many_documents_proxy.rb
88
89
  - lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb
89
90
  - lib/mongomapper/associations/many_embedded_proxy.rb
@@ -109,6 +110,7 @@ files:
109
110
  - test/NOTE_ON_TESTING
110
111
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
111
112
  - test/functional/associations/test_belongs_to_proxy.rb
113
+ - test/functional/associations/test_many_documents_as_proxy.rb
112
114
  - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
113
115
  - test/functional/associations/test_many_embedded_proxy.rb
114
116
  - test/functional/associations/test_many_polymorphic_proxy.rb
@@ -135,7 +137,6 @@ files:
135
137
  - test/unit/test_validations.rb
136
138
  has_rdoc: false
137
139
  homepage: http://github.com/jnunemaker/mongomapper
138
- licenses:
139
140
  post_install_message:
140
141
  rdoc_options:
141
142
  - --charset=UTF-8
@@ -156,13 +157,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
157
  requirements: []
157
158
 
158
159
  rubyforge_project: mongomapper
159
- rubygems_version: 1.3.5
160
+ rubygems_version: 1.2.0
160
161
  signing_key:
161
162
  specification_version: 3
162
163
  summary: Awesome gem for modeling your domain and storing it in mongo
163
164
  test_files:
164
165
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
165
166
  - test/functional/associations/test_belongs_to_proxy.rb
167
+ - test/functional/associations/test_many_documents_as_proxy.rb
166
168
  - test/functional/associations/test_many_embedded_polymorphic_proxy.rb
167
169
  - test/functional/associations/test_many_embedded_proxy.rb
168
170
  - test/functional/associations/test_many_polymorphic_proxy.rb