mongo_mapper 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_mapper.rb +3 -5
- data/lib/mongo_mapper/document.rb +23 -53
- data/lib/mongo_mapper/plugins/associations.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +1 -1
- data/lib/mongo_mapper/plugins/equality.rb +3 -3
- data/lib/mongo_mapper/plugins/identity_map.rb +8 -7
- data/lib/mongo_mapper/plugins/keys.rb +49 -73
- data/lib/mongo_mapper/plugins/keys/key.rb +44 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +9 -5
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +3 -3
- data/lib/mongo_mapper/plugins/serialization.rb +3 -3
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -1
- data/lib/mongo_mapper/plugins/validations.rb +2 -2
- data/lib/mongo_mapper/query.rb +9 -129
- data/lib/mongo_mapper/support.rb +17 -39
- data/lib/mongo_mapper/version.rb +1 -1
- metadata +54 -140
- data/.gitignore +0 -10
- data/Rakefile +0 -37
- data/mongo_mapper.gemspec +0 -214
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/NOTE_ON_TESTING +0 -1
- data/test/active_model_lint_test.rb +0 -13
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
- data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
- data/test/functional/associations/test_in_array_proxy.rb +0 -325
- data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
- data/test/functional/associations/test_many_documents_proxy.rb +0 -536
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
- data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
- data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
- data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
- data/test/functional/associations/test_one_proxy.rb +0 -196
- data/test/functional/test_associations.rb +0 -44
- data/test/functional/test_binary.rb +0 -27
- data/test/functional/test_callbacks.rb +0 -151
- data/test/functional/test_dirty.rb +0 -163
- data/test/functional/test_document.rb +0 -1219
- data/test/functional/test_embedded_document.rb +0 -210
- data/test/functional/test_identity_map.rb +0 -507
- data/test/functional/test_indexing.rb +0 -44
- data/test/functional/test_logger.rb +0 -20
- data/test/functional/test_modifiers.rb +0 -394
- data/test/functional/test_pagination.rb +0 -93
- data/test/functional/test_protected.rb +0 -163
- data/test/functional/test_string_id_compatibility.rb +0 -67
- data/test/functional/test_timestamps.rb +0 -64
- data/test/functional/test_userstamps.rb +0 -28
- data/test/functional/test_validations.rb +0 -342
- data/test/models.rb +0 -227
- data/test/support/custom_matchers.rb +0 -37
- data/test/support/timing.rb +0 -16
- data/test/test_helper.rb +0 -64
- data/test/unit/associations/test_base.rb +0 -212
- data/test/unit/associations/test_proxy.rb +0 -105
- data/test/unit/serializers/test_json_serializer.rb +0 -202
- data/test/unit/test_descendant_appends.rb +0 -71
- data/test/unit/test_document.rb +0 -225
- data/test/unit/test_dynamic_finder.rb +0 -123
- data/test/unit/test_embedded_document.rb +0 -657
- data/test/unit/test_keys.rb +0 -185
- data/test/unit/test_mongo_mapper.rb +0 -118
- data/test/unit/test_pagination.rb +0 -160
- data/test/unit/test_plugins.rb +0 -50
- data/test/unit/test_query.rb +0 -374
- data/test/unit/test_rails.rb +0 -181
- data/test/unit/test_rails_compatibility.rb +0 -52
- data/test/unit/test_serialization.rb +0 -51
- data/test/unit/test_support.rb +0 -382
- data/test/unit/test_time_zones.rb +0 -39
- data/test/unit/test_validations.rb +0 -544
@@ -1,105 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FakeNilProxy < MongoMapper::Plugins::Associations::Proxy
|
4
|
-
def find_target; nil end
|
5
|
-
end
|
6
|
-
|
7
|
-
class FakeBlankProxy < MongoMapper::Plugins::Associations::Proxy
|
8
|
-
def find_target; '' end
|
9
|
-
end
|
10
|
-
|
11
|
-
class FakeNumberProxy < MongoMapper::Plugins::Associations::Proxy
|
12
|
-
def find_target; 17 end
|
13
|
-
end
|
14
|
-
|
15
|
-
class FakeProxy < MongoMapper::Plugins::Associations::Proxy
|
16
|
-
def find_target; [1, 2] end
|
17
|
-
end
|
18
|
-
|
19
|
-
class ProxyTest < Test::Unit::TestCase
|
20
|
-
def setup
|
21
|
-
@owner = mock('owner')
|
22
|
-
@owner.stubs(:new?).returns(false)
|
23
|
-
@association = mock('association')
|
24
|
-
@association.stubs(:options).returns({:extend => []})
|
25
|
-
|
26
|
-
@proxy = FakeProxy.new(@owner, @association)
|
27
|
-
@nil_proxy = FakeNilProxy.new(@owner, @association)
|
28
|
-
@blank_proxy = FakeBlankProxy.new(@owner, @association)
|
29
|
-
end
|
30
|
-
|
31
|
-
should 'return true for === target' do
|
32
|
-
@proxy = FakeProxy.new(@owner, @association)
|
33
|
-
@proxy.should === Array
|
34
|
-
end
|
35
|
-
|
36
|
-
should "set target to nil when reset is called" do
|
37
|
-
@proxy.reset
|
38
|
-
@proxy.target.should be_nil
|
39
|
-
end
|
40
|
-
|
41
|
-
should "be able to inspect the proxy" do
|
42
|
-
@proxy.inspect.should == '[1, 2]'
|
43
|
-
end
|
44
|
-
|
45
|
-
context "nil?" do
|
46
|
-
should "be true if nil" do
|
47
|
-
@nil_proxy.nil?.should be_true
|
48
|
-
end
|
49
|
-
|
50
|
-
should "be false if not nil" do
|
51
|
-
@proxy.nil?.should be_false
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context "blank?" do
|
56
|
-
should "be true if blank" do
|
57
|
-
@blank_proxy.blank?.should be_true
|
58
|
-
@nil_proxy.blank?.should be_true
|
59
|
-
end
|
60
|
-
|
61
|
-
should "be false if not blank" do
|
62
|
-
@proxy.blank?.should be_false
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "present?" do
|
67
|
-
should "be true if present" do
|
68
|
-
@proxy.present?.should be_true
|
69
|
-
end
|
70
|
-
|
71
|
-
should "be false if not present" do
|
72
|
-
@blank_proxy.present?.should be_false
|
73
|
-
@nil_proxy.present?.should be_false
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
should "delegate respond_to? to target" do
|
78
|
-
@proxy.respond_to?(:each).should be_true
|
79
|
-
@proxy.respond_to?(:size).should be_true
|
80
|
-
@proxy.respond_to?(:gsub).should be_false
|
81
|
-
end
|
82
|
-
|
83
|
-
should "alias proxy owner to owner" do
|
84
|
-
@proxy.proxy_owner.should == @owner
|
85
|
-
end
|
86
|
-
|
87
|
-
should "alias proxy target to target" do
|
88
|
-
@proxy.proxy_target.should == @target
|
89
|
-
end
|
90
|
-
|
91
|
-
context "send" do
|
92
|
-
should "work if proxy responds to method" do
|
93
|
-
@proxy.send(:reset)
|
94
|
-
@proxy.target.should be_nil
|
95
|
-
end
|
96
|
-
|
97
|
-
should "work if the target responds to the method" do
|
98
|
-
@proxy.send(:size).should == 2
|
99
|
-
end
|
100
|
-
|
101
|
-
should "not work if neither the proxy or target respond to method" do
|
102
|
-
lambda { @proxy.send(:gsub) }.should raise_error
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
@@ -1,202 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_support/version'
|
3
|
-
|
4
|
-
class JsonSerializationTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
# Helper function in case things change in the future
|
7
|
-
# - replacing all those to_json calls was a nightmare
|
8
|
-
def convert_to_json object, options = {}
|
9
|
-
ActiveSupport::JSON.encode(object, options)
|
10
|
-
end
|
11
|
-
|
12
|
-
class Tag
|
13
|
-
include MongoMapper::EmbeddedDocument
|
14
|
-
key :name, String
|
15
|
-
end
|
16
|
-
|
17
|
-
class Contact
|
18
|
-
include MongoMapper::Document
|
19
|
-
key :name, String
|
20
|
-
key :age, Integer
|
21
|
-
key :created_at, Time
|
22
|
-
key :awesome, Boolean
|
23
|
-
key :preferences, Hash
|
24
|
-
|
25
|
-
many :tags, :class_name => 'JsonSerializationTest::Tag'
|
26
|
-
end
|
27
|
-
|
28
|
-
def setup
|
29
|
-
Contact.include_root_in_json = false
|
30
|
-
@contact = Contact.new(
|
31
|
-
:name => 'Konata Izumi',
|
32
|
-
:age => 16,
|
33
|
-
:created_at => Time.utc(2006, 8, 1),
|
34
|
-
:awesome => true,
|
35
|
-
:preferences => { :shows => 'anime' }
|
36
|
-
)
|
37
|
-
end
|
38
|
-
|
39
|
-
should "include demodulized root" do
|
40
|
-
Contact.include_root_in_json = true
|
41
|
-
assert_match %r{^\{"contact":\s?\{}, convert_to_json(@contact)
|
42
|
-
end
|
43
|
-
|
44
|
-
should "encode all encodable attributes" do
|
45
|
-
json = convert_to_json(@contact)
|
46
|
-
|
47
|
-
assert_no_match %r{"_id"}, json
|
48
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
49
|
-
assert_match %r{"age":16}, json
|
50
|
-
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
51
|
-
assert_match %r{"awesome":true}, json
|
52
|
-
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
53
|
-
end
|
54
|
-
|
55
|
-
should "allow attribute filtering with only" do
|
56
|
-
json = convert_to_json(@contact, :only => [:name, :age])
|
57
|
-
|
58
|
-
assert_no_match %r{"_id"}, json
|
59
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
60
|
-
assert_match %r{"age":16}, json
|
61
|
-
assert_no_match %r{"awesome"}, json
|
62
|
-
assert_no_match %r{"created_at"}, json
|
63
|
-
assert_no_match %r{"preferences"}, json
|
64
|
-
end
|
65
|
-
|
66
|
-
should "allow attribute filtering with except" do
|
67
|
-
json = convert_to_json(@contact, :except => [:name, :age])
|
68
|
-
|
69
|
-
assert_no_match %r{"_id"}, json
|
70
|
-
assert_no_match %r{"name"}, json
|
71
|
-
assert_no_match %r{"age"}, json
|
72
|
-
assert_match %r{"awesome"}, json
|
73
|
-
assert_match %r{"created_at"}, json
|
74
|
-
assert_match %r{"preferences"}, json
|
75
|
-
end
|
76
|
-
|
77
|
-
context "_id key" do
|
78
|
-
should "not be included by default" do
|
79
|
-
json = convert_to_json(@contact)
|
80
|
-
assert_no_match %r{"_id":}, json
|
81
|
-
end
|
82
|
-
|
83
|
-
should "not be included even if :except is used" do
|
84
|
-
json = convert_to_json(@contact, :except => :name)
|
85
|
-
assert_no_match %r{"_id":}, json
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context "id method" do
|
90
|
-
setup do
|
91
|
-
def @contact.label; "Has cheezburger"; end
|
92
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
93
|
-
end
|
94
|
-
|
95
|
-
should "be included by default" do
|
96
|
-
json = convert_to_json(@contact)
|
97
|
-
assert_match %r{"id"}, json
|
98
|
-
end
|
99
|
-
|
100
|
-
should "be included when single method included" do
|
101
|
-
json = convert_to_json(@contact, :methods => :label)
|
102
|
-
assert_match %r{"id"}, json
|
103
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
104
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
105
|
-
assert_no_match %r{"favorite_quote":"Constraints are liberating"}, json
|
106
|
-
end
|
107
|
-
|
108
|
-
should "be included when multiple methods included" do
|
109
|
-
json = convert_to_json(@contact, :methods => [:label, :favorite_quote])
|
110
|
-
assert_match %r{"id"}, json
|
111
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
112
|
-
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
113
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
114
|
-
end
|
115
|
-
|
116
|
-
should "not be included if :only is present" do
|
117
|
-
json = convert_to_json(@contact, :only => :name)
|
118
|
-
assert_no_match %r{"id":}, json
|
119
|
-
end
|
120
|
-
|
121
|
-
should "be represented by a string" do
|
122
|
-
json = convert_to_json(@contact)
|
123
|
-
assert_match %r{"id":"}, json
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "including methods" do
|
128
|
-
setup do
|
129
|
-
def @contact.label; "Has cheezburger"; end
|
130
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
131
|
-
end
|
132
|
-
|
133
|
-
should "include single method" do
|
134
|
-
json = convert_to_json(@contact, :methods => :label)
|
135
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
136
|
-
end
|
137
|
-
|
138
|
-
should "include multiple methods" do
|
139
|
-
json = convert_to_json(@contact, :only => :name, :methods => [:label, :favorite_quote])
|
140
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
141
|
-
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
142
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
143
|
-
assert_no_match %r{"age":16}, json
|
144
|
-
assert_no_match %r{"awesome"}, json
|
145
|
-
assert_no_match %r{"created_at"}, json
|
146
|
-
assert_no_match %r{"preferences"}, json
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
context "array of records" do
|
151
|
-
setup do
|
152
|
-
@contacts = [
|
153
|
-
Contact.new(:name => 'David', :age => 39),
|
154
|
-
Contact.new(:name => 'Mary', :age => 14)
|
155
|
-
]
|
156
|
-
end
|
157
|
-
|
158
|
-
should "allow attribute filtering with only" do
|
159
|
-
json =convert_to_json(@contacts, :only => :name)
|
160
|
-
assert_match %r{\{"name":"David"\}}, json
|
161
|
-
assert_match %r{\{"name":"Mary"\}}, json
|
162
|
-
end
|
163
|
-
|
164
|
-
should "allow attribute filtering with except" do
|
165
|
-
json = convert_to_json(@contacts, :except => [:name, :preferences, :awesome, :created_at, :updated_at])
|
166
|
-
assert_match %r{"age":39}, json
|
167
|
-
assert_match %r{"age":14}, json
|
168
|
-
assert_no_match %r{"name":}, json
|
169
|
-
assert_no_match %r{"preferences":}, json
|
170
|
-
assert_no_match %r{"awesome":}, json
|
171
|
-
assert_no_match %r{"created_at":}, json
|
172
|
-
assert_no_match %r{"updated_at":}, json
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
should "allow options for hash of records" do
|
177
|
-
contacts = {
|
178
|
-
1 => Contact.new(:name => 'David', :age => 39),
|
179
|
-
2 => Contact.new(:name => 'Mary', :age => 14)
|
180
|
-
}
|
181
|
-
json = convert_to_json(contacts, :only => [1, :name])
|
182
|
-
assert_match %r{"1":}, json
|
183
|
-
assert_match %r{\{"name":"David"\}}, json
|
184
|
-
assert_no_match %r{"2":}, json
|
185
|
-
end
|
186
|
-
|
187
|
-
should "include embedded attributes" do
|
188
|
-
contact = Contact.new(:name => 'John', :age => 27)
|
189
|
-
contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
|
190
|
-
json = convert_to_json(contact)
|
191
|
-
assert_match %r{"tags":}, json
|
192
|
-
assert_match %r{"name":"awesome"}, json
|
193
|
-
assert_match %r{"name":"ruby"}, json
|
194
|
-
end
|
195
|
-
|
196
|
-
should "include dynamic attributes" do
|
197
|
-
contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
|
198
|
-
contact['smell'] = 'stinky'
|
199
|
-
json = convert_to_json(contact)
|
200
|
-
assert_match %r{"smell":"stinky"}, json
|
201
|
-
end
|
202
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class DescendantAppendsTest < Test::Unit::TestCase
|
4
|
-
context "Document" do
|
5
|
-
should "default descendants to a new set" do
|
6
|
-
MongoMapper::Document.descendants.should be_instance_of(Set)
|
7
|
-
end
|
8
|
-
|
9
|
-
should 'allow extensions to Document to be appended' do
|
10
|
-
module Extension; def test_this_extension; end end
|
11
|
-
MongoMapper::Document.append_extensions(Extension)
|
12
|
-
article = Doc()
|
13
|
-
article.should respond_to(:test_this_extension)
|
14
|
-
end
|
15
|
-
|
16
|
-
should 'add appended extensions to classes that include Document before they are added' do
|
17
|
-
module Extension; def test_this_extension; end end
|
18
|
-
article = Doc()
|
19
|
-
MongoMapper::Document.append_extensions(Extension)
|
20
|
-
article.should respond_to(:test_this_extension)
|
21
|
-
end
|
22
|
-
|
23
|
-
should 'allow inclusions to Document to be appended' do
|
24
|
-
module Inclusion; def test_this_inclusion; end end
|
25
|
-
MongoMapper::Document.append_inclusions(Inclusion)
|
26
|
-
article = Doc()
|
27
|
-
article.new.should respond_to(:test_this_inclusion)
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'add appended inclusions to classes that include Document before they are added' do
|
31
|
-
module Inclusion; def test_this_inclusion; end end
|
32
|
-
article = Doc()
|
33
|
-
MongoMapper::Document.append_inclusions(Inclusion)
|
34
|
-
article.new.should respond_to(:test_this_inclusion)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "EmbeddedDocument" do
|
39
|
-
should "default descendants to a new set" do
|
40
|
-
MongoMapper::EmbeddedDocument.descendants.should be_instance_of(Set)
|
41
|
-
end
|
42
|
-
|
43
|
-
should 'allow extensions to Document to be appended' do
|
44
|
-
module Extension; def test_this_extension; end end
|
45
|
-
MongoMapper::EmbeddedDocument.append_extensions(Extension)
|
46
|
-
article = EDoc()
|
47
|
-
article.should respond_to(:test_this_extension)
|
48
|
-
end
|
49
|
-
|
50
|
-
should 'add appended extensions to classes that include Document before they are added' do
|
51
|
-
module Extension; def test_this_extension; end end
|
52
|
-
article = EDoc()
|
53
|
-
MongoMapper::EmbeddedDocument.append_extensions(Extension)
|
54
|
-
article.should respond_to(:test_this_extension)
|
55
|
-
end
|
56
|
-
|
57
|
-
should 'allow inclusions to Document to be appended' do
|
58
|
-
module Inclusion; def test_this_inclusion; end end
|
59
|
-
MongoMapper::EmbeddedDocument.append_inclusions(Inclusion)
|
60
|
-
article = EDoc()
|
61
|
-
article.new.should respond_to(:test_this_inclusion)
|
62
|
-
end
|
63
|
-
|
64
|
-
should 'add appended inclusions to classes that include Document before they are added' do
|
65
|
-
module Inclusion; def test_this_inclusion; end end
|
66
|
-
article = EDoc()
|
67
|
-
MongoMapper::EmbeddedDocument.append_inclusions(Inclusion)
|
68
|
-
article.new.should respond_to(:test_this_inclusion)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
data/test/unit/test_document.rb
DELETED
@@ -1,225 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class DocumentTest < Test::Unit::TestCase
|
5
|
-
context "The Document Class" do
|
6
|
-
setup do
|
7
|
-
@document = Doc()
|
8
|
-
end
|
9
|
-
|
10
|
-
should "return false for embeddable" do
|
11
|
-
Doc().embeddable?.should be_false
|
12
|
-
end
|
13
|
-
|
14
|
-
should "have logger method" do
|
15
|
-
@document.logger.should == MongoMapper.logger
|
16
|
-
@document.logger.should be_instance_of(Logger)
|
17
|
-
end
|
18
|
-
|
19
|
-
should "use default database by default" do
|
20
|
-
@document.database.should == MongoMapper.database
|
21
|
-
end
|
22
|
-
|
23
|
-
should "have a connection" do
|
24
|
-
@document.connection.should be_instance_of(Mongo::Connection)
|
25
|
-
end
|
26
|
-
|
27
|
-
should "allow setting different connection without affecting the default" do
|
28
|
-
conn = Mongo::Connection.new
|
29
|
-
@document.connection conn
|
30
|
-
@document.connection.should == conn
|
31
|
-
@document.connection.should_not == MongoMapper.connection
|
32
|
-
end
|
33
|
-
|
34
|
-
should "allow setting a different database without affecting the default" do
|
35
|
-
@document.set_database_name 'test2'
|
36
|
-
@document.database_name.should == 'test2'
|
37
|
-
@document.database.name.should == 'test2'
|
38
|
-
|
39
|
-
another_document = Doc()
|
40
|
-
another_document.database.should == MongoMapper.database
|
41
|
-
end
|
42
|
-
|
43
|
-
should "default collection name to class name tableized" do
|
44
|
-
class ::Item
|
45
|
-
include MongoMapper::Document
|
46
|
-
end
|
47
|
-
|
48
|
-
Item.collection.should be_instance_of(Mongo::Collection)
|
49
|
-
Item.collection.name.should == 'items'
|
50
|
-
end
|
51
|
-
|
52
|
-
should "default collection name of namespaced class to tableized with dot separation" do
|
53
|
-
module ::BloggyPoo
|
54
|
-
class Post
|
55
|
-
include MongoMapper::Document
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
BloggyPoo::Post.collection.should be_instance_of(Mongo::Collection)
|
60
|
-
BloggyPoo::Post.collection.name.should == 'bloggy_poo.posts'
|
61
|
-
end
|
62
|
-
|
63
|
-
should "allow setting the collection name" do
|
64
|
-
@document.set_collection_name('foobar')
|
65
|
-
@document.collection.should be_instance_of(Mongo::Collection)
|
66
|
-
@document.collection.name.should == 'foobar'
|
67
|
-
end
|
68
|
-
end # Document class
|
69
|
-
|
70
|
-
context "Documents that inherit from other documents" do
|
71
|
-
should "default collection name to inherited class" do
|
72
|
-
Message.collection_name.should == 'messages'
|
73
|
-
Enter.collection_name.should == 'messages'
|
74
|
-
Exit.collection_name.should == 'messages'
|
75
|
-
Chat.collection_name.should == 'messages'
|
76
|
-
end
|
77
|
-
|
78
|
-
should "default associations to inherited class" do
|
79
|
-
Message.associations.keys.should include("room")
|
80
|
-
Enter.associations.keys.should include("room")
|
81
|
-
Exit.associations.keys.should include("room")
|
82
|
-
Chat.associations.keys.should include("room")
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "descendants" do
|
87
|
-
should "default to an empty array" do
|
88
|
-
Enter.descendants.should == []
|
89
|
-
end
|
90
|
-
|
91
|
-
should "be recorded" do
|
92
|
-
Message.descendants.should == [Enter, Exit, Chat]
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "An instance of a document" do
|
97
|
-
setup do
|
98
|
-
@document = Doc do
|
99
|
-
key :name, String
|
100
|
-
key :age, Integer
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
should "create id during initialization" do
|
105
|
-
@document.new._id.should be_instance_of(BSON::ObjectID)
|
106
|
-
end
|
107
|
-
|
108
|
-
should "have access to logger" do
|
109
|
-
doc = @document.new
|
110
|
-
doc.logger.should == @document.logger
|
111
|
-
doc.logger.should be_instance_of(Logger)
|
112
|
-
end
|
113
|
-
|
114
|
-
should "have access to the class's collection" do
|
115
|
-
doc = @document.new
|
116
|
-
doc.collection.name.should == @document.collection.name
|
117
|
-
end
|
118
|
-
|
119
|
-
should "use default values if defined for keys" do
|
120
|
-
@document.key :active, Boolean, :default => true
|
121
|
-
|
122
|
-
@document.new.active.should be_true
|
123
|
-
@document.new(:active => false).active.should be_false
|
124
|
-
end
|
125
|
-
|
126
|
-
should "use default values if defined even when custom data type" do
|
127
|
-
@document.key :window, WindowSize, :default => WindowSize.new(600, 480)
|
128
|
-
|
129
|
-
doc = @document.new
|
130
|
-
doc.window.should == WindowSize.new(600, 480)
|
131
|
-
end
|
132
|
-
|
133
|
-
context "root document" do
|
134
|
-
should "set self to the root document on embedded documents" do
|
135
|
-
klass = Doc()
|
136
|
-
pets = EDoc()
|
137
|
-
|
138
|
-
klass.many :pets, :class => pets
|
139
|
-
|
140
|
-
doc = klass.new(:pets => [{}])
|
141
|
-
doc.pets.first._root_document.should == doc
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
context "new?" do
|
146
|
-
should "be true if no id" do
|
147
|
-
@document.new.new?.should be_true
|
148
|
-
end
|
149
|
-
|
150
|
-
should "be true if id but using custom id and not saved yet" do
|
151
|
-
@document.key :_id, String
|
152
|
-
doc = @document.new
|
153
|
-
doc.id = '1234'
|
154
|
-
doc.new?.should be_true
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
context "clone" do
|
159
|
-
should "be new" do
|
160
|
-
doc = @document.create(:name => "foo", :age => 27)
|
161
|
-
clone = doc.clone
|
162
|
-
clone.should be_new
|
163
|
-
end
|
164
|
-
|
165
|
-
should "copy the attributes" do
|
166
|
-
doc = @document.create(:name => "foo", :age => 27)
|
167
|
-
clone = doc.clone
|
168
|
-
clone.name.should == "foo"
|
169
|
-
clone.age.should == 27
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
should "call inspect on the document's attributes instead of to_s when inspecting the document" do
|
174
|
-
doc = @document.new(:animals => %w(dog cat))
|
175
|
-
doc.inspect.should include(%(animals: ["dog", "cat"]))
|
176
|
-
end
|
177
|
-
|
178
|
-
context "equality" do
|
179
|
-
setup do
|
180
|
-
@oid = BSON::ObjectID.new
|
181
|
-
end
|
182
|
-
|
183
|
-
should "delegate hash to _id" do
|
184
|
-
doc = @document.new
|
185
|
-
doc.hash.should == doc._id.hash
|
186
|
-
end
|
187
|
-
|
188
|
-
should "delegate eql to ==" do
|
189
|
-
doc = @document.new
|
190
|
-
other = @document.new
|
191
|
-
doc.eql?(other).should == (doc == other)
|
192
|
-
doc.eql?(doc).should == (doc == doc)
|
193
|
-
end
|
194
|
-
|
195
|
-
should "know if same object as another" do
|
196
|
-
doc = @document.new
|
197
|
-
doc.should equal(doc)
|
198
|
-
doc.should_not equal(@document.new)
|
199
|
-
end
|
200
|
-
|
201
|
-
should "allow set operations on array of documents" do
|
202
|
-
@document.key :parent_id, ObjectId
|
203
|
-
@document.belongs_to :parent, :class => @document
|
204
|
-
|
205
|
-
parent = @document.create
|
206
|
-
child = @document.create(:parent => parent)
|
207
|
-
|
208
|
-
([child.parent] & [parent]).should == [parent]
|
209
|
-
end
|
210
|
-
|
211
|
-
should "be equal if id and class are the same" do
|
212
|
-
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be(true)
|
213
|
-
end
|
214
|
-
|
215
|
-
should "not be equal if class same but id different" do
|
216
|
-
(@document.new('_id' => @oid) == @document.new('_id' => BSON::ObjectID.new)).should be(false)
|
217
|
-
end
|
218
|
-
|
219
|
-
should "not be equal if id same but class different" do
|
220
|
-
another_document = Doc()
|
221
|
-
(@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be(false)
|
222
|
-
end
|
223
|
-
end
|
224
|
-
end # instance of a document
|
225
|
-
end # DocumentTest
|