mrkurt-mongo_mapper 0.6.9 → 0.6.10
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.
- data/VERSION +1 -1
- data/lib/mongo_mapper/associations.rb +6 -0
- data/lib/mongo_mapper/associations/one_proxy.rb +13 -10
- data/lib/mongo_mapper/associations/proxy.rb +7 -2
- data/lib/mongo_mapper/document.rb +2 -1
- data/mongo_mapper.gemspec +2 -2
- data/mrkurt-mongo_mapper.gemspec +2 -2
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +8 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +11 -9
- data/test/functional/associations/test_many_embedded_proxy.rb +4 -14
- data/test/functional/associations/test_one_proxy.rb +29 -11
- data/test/functional/test_binary.rb +2 -8
- data/test/functional/test_callbacks.rb +1 -5
- data/test/functional/test_dirty.rb +1 -4
- data/test/functional/test_document.rb +16 -33
- data/test/functional/test_embedded_document.rb +7 -20
- data/test/functional/test_modifiers.rb +1 -5
- data/test/functional/test_pagination.rb +1 -3
- data/test/functional/test_string_id_compatibility.rb +3 -8
- data/test/functional/test_validations.rb +13 -58
- data/test/test_helper.rb +28 -0
- data/test/unit/associations/test_proxy.rb +12 -0
- data/test/unit/test_document.rb +12 -34
- data/test/unit/test_embedded_document.rb +14 -42
- data/test/unit/test_key.rb +1 -4
- data/test/unit/test_serializations.rb +1 -2
- data/test/unit/test_time_zones.rb +1 -2
- data/test/unit/test_validations.rb +3 -14
- metadata +2 -2
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.6.
|
|
1
|
+
0.6.10
|
|
@@ -40,6 +40,12 @@ module MongoMapper
|
|
|
40
40
|
value
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
+
if association.one? || association.belongs_to?
|
|
44
|
+
define_method("#{association.name}?") do
|
|
45
|
+
get_proxy(association).present?
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
43
49
|
if association.options[:dependent] && association.many? && !association.embeddable?
|
|
44
50
|
after_destroy do |doc|
|
|
45
51
|
case association.options[:dependent]
|
|
@@ -16,31 +16,34 @@ module MongoMapper
|
|
|
16
16
|
def replace(doc)
|
|
17
17
|
load_target
|
|
18
18
|
|
|
19
|
-
if
|
|
20
|
-
if options[:dependent] &&
|
|
19
|
+
if !target.nil? && target != doc
|
|
20
|
+
if options[:dependent] && !target.new?
|
|
21
21
|
case options[:dependent]
|
|
22
22
|
when :delete
|
|
23
|
-
|
|
23
|
+
target.delete
|
|
24
24
|
when :destroy
|
|
25
|
-
|
|
25
|
+
target.destroy
|
|
26
26
|
when :nullify
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
target[foreign_key] = nil
|
|
28
|
+
target.save
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
reset
|
|
34
|
+
|
|
35
|
+
unless doc.nil?
|
|
34
36
|
owner.save if owner.new?
|
|
35
37
|
doc[foreign_key] = owner.id
|
|
36
38
|
doc.save if doc.new?
|
|
37
|
-
|
|
39
|
+
loaded
|
|
40
|
+
@target = doc
|
|
38
41
|
end
|
|
39
42
|
end
|
|
40
43
|
|
|
41
44
|
protected
|
|
42
45
|
def find_target
|
|
43
|
-
target_class.first(foreign_key => owner.id)
|
|
46
|
+
target_class.first(reflection.finder_options.merge(foreign_key => owner.id))
|
|
44
47
|
end
|
|
45
48
|
|
|
46
49
|
def instantiate_target(instantiator, attrs={})
|
|
@@ -50,7 +53,7 @@ module MongoMapper
|
|
|
50
53
|
end
|
|
51
54
|
|
|
52
55
|
def target_class
|
|
53
|
-
@target_class ||= options[:class] || (options[:class_name] ||
|
|
56
|
+
@target_class ||= options[:class] || (options[:class_name] || reflection.name.to_s.camelize).constantize
|
|
54
57
|
end
|
|
55
58
|
|
|
56
59
|
def foreign_key
|
|
@@ -17,7 +17,7 @@ module MongoMapper
|
|
|
17
17
|
delegate :collection, :to => :klass
|
|
18
18
|
|
|
19
19
|
def initialize(owner, reflection)
|
|
20
|
-
@owner, @reflection = owner, reflection
|
|
20
|
+
@owner, @reflection, @loaded = owner, reflection, false
|
|
21
21
|
Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
|
|
22
22
|
reset
|
|
23
23
|
end
|
|
@@ -45,6 +45,11 @@ module MongoMapper
|
|
|
45
45
|
target.blank?
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
+
def present?
|
|
49
|
+
load_target
|
|
50
|
+
target.present?
|
|
51
|
+
end
|
|
52
|
+
|
|
48
53
|
def reload
|
|
49
54
|
reset
|
|
50
55
|
load_target
|
|
@@ -57,7 +62,7 @@ module MongoMapper
|
|
|
57
62
|
|
|
58
63
|
def reset
|
|
59
64
|
@loaded = false
|
|
60
|
-
target = nil
|
|
65
|
+
@target = nil
|
|
61
66
|
end
|
|
62
67
|
|
|
63
68
|
def respond_to?(*args)
|
|
@@ -437,7 +437,7 @@ module MongoMapper
|
|
|
437
437
|
def prep_lock_version
|
|
438
438
|
if lock_version_field
|
|
439
439
|
old = read_attribute(lock_version_field)
|
|
440
|
-
v = Time.now.to_f
|
|
440
|
+
v = (Time.now.to_f * 1000).ceil.to_s
|
|
441
441
|
write_attribute lock_version_field, v
|
|
442
442
|
old
|
|
443
443
|
else
|
|
@@ -472,6 +472,7 @@ module MongoMapper
|
|
|
472
472
|
|
|
473
473
|
if result.is_a? Array
|
|
474
474
|
if current_version && result[0][0]['updatedExisting'] == false
|
|
475
|
+
write_attribute lock_version_field, current_version
|
|
475
476
|
raise StaleDocumentError.new
|
|
476
477
|
end
|
|
477
478
|
elsif lock_version_field
|
data/mongo_mapper.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{mongo_mapper}
|
|
8
|
-
s.version = "0.6.
|
|
8
|
+
s.version = "0.6.10"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["John Nunemaker"]
|
|
12
|
-
s.date = %q{2010-01-
|
|
12
|
+
s.date = %q{2010-01-02}
|
|
13
13
|
s.default_executable = %q{mmconsole}
|
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
|
15
15
|
s.executables = ["mmconsole"]
|
data/mrkurt-mongo_mapper.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{mrkurt-mongo_mapper}
|
|
8
|
-
s.version = "0.6.
|
|
8
|
+
s.version = "0.6.10"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["John Nunemaker", "Kurt Mackey"]
|
|
12
|
-
s.date = %q{2010-01-
|
|
12
|
+
s.date = %q{2010-01-09}
|
|
13
13
|
s.default_executable = %q{mmconsole}
|
|
14
14
|
s.email = %q{mrkurt@gmail.com}
|
|
15
15
|
s.executables = ["mmconsole"]
|
|
@@ -12,6 +12,14 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
|
|
|
12
12
|
status.target.nil?.should be_true
|
|
13
13
|
status.target.inspect.should == "nil"
|
|
14
14
|
end
|
|
15
|
+
|
|
16
|
+
should "have boolean presence method" do
|
|
17
|
+
status = Status.new
|
|
18
|
+
status.target?.should be_false
|
|
19
|
+
|
|
20
|
+
status.target = Project.new(:name => 'mongomapper')
|
|
21
|
+
status.target?.should be_true
|
|
22
|
+
end
|
|
15
23
|
|
|
16
24
|
should "be able to replace the association" do
|
|
17
25
|
status = Status.new(:name => 'Foo!')
|
|
@@ -3,24 +3,26 @@ require 'models'
|
|
|
3
3
|
|
|
4
4
|
class BelongsToProxyTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
|
6
|
-
@post_class =
|
|
7
|
-
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
@comment_class = Class.new do
|
|
11
|
-
include MongoMapper::Document
|
|
6
|
+
@post_class = Doc()
|
|
7
|
+
@comment_class = Doc do
|
|
12
8
|
key :post_id, String
|
|
13
9
|
end
|
|
14
|
-
@comment_class.belongs_to :post, :class => @post_class
|
|
15
10
|
|
|
16
|
-
@post_class
|
|
17
|
-
@comment_class.collection.remove
|
|
11
|
+
@comment_class.belongs_to :post, :class => @post_class
|
|
18
12
|
end
|
|
19
13
|
|
|
20
14
|
should "default to nil" do
|
|
21
15
|
@comment_class.new.post.nil?.should be_true
|
|
22
16
|
end
|
|
23
17
|
|
|
18
|
+
should "have boolean presence method" do
|
|
19
|
+
comment = @comment_class.new(:name => 'Foo!')
|
|
20
|
+
comment.post?.should be_false
|
|
21
|
+
|
|
22
|
+
comment.post = @post_class.new(:name => 'mongomapper')
|
|
23
|
+
comment.post?.should be_true
|
|
24
|
+
end
|
|
25
|
+
|
|
24
26
|
should "be able to replace the association" do
|
|
25
27
|
post = @post_class.new(:name => 'mongomapper')
|
|
26
28
|
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
|
@@ -33,12 +33,9 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
should "allow embedding arbitrarily deep" do
|
|
36
|
-
@document =
|
|
37
|
-
include MongoMapper::Document
|
|
38
|
-
set_collection_name 'test'
|
|
36
|
+
@document = Doc do
|
|
39
37
|
key :person, Person
|
|
40
38
|
end
|
|
41
|
-
@document.collection.remove
|
|
42
39
|
|
|
43
40
|
meg = Person.new(:name => "Meg")
|
|
44
41
|
meg.child = Person.new(:name => "Steve")
|
|
@@ -80,12 +77,9 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
|
80
77
|
|
|
81
78
|
context "embedding many embedded documents" do
|
|
82
79
|
setup do
|
|
83
|
-
@document =
|
|
84
|
-
include MongoMapper::Document
|
|
85
|
-
set_collection_name 'test'
|
|
80
|
+
@document = Doc do
|
|
86
81
|
many :people
|
|
87
82
|
end
|
|
88
|
-
@document.collection.remove
|
|
89
83
|
end
|
|
90
84
|
|
|
91
85
|
should "persist all embedded documents" do
|
|
@@ -142,20 +136,16 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
|
|
|
142
136
|
|
|
143
137
|
context "extending the association" do
|
|
144
138
|
setup do
|
|
145
|
-
@address_class =
|
|
146
|
-
include MongoMapper::EmbeddedDocument
|
|
139
|
+
@address_class = EDoc do
|
|
147
140
|
key :address, String
|
|
148
141
|
key :city, String
|
|
149
142
|
key :state, String
|
|
150
143
|
key :zip, Integer
|
|
151
144
|
end
|
|
152
145
|
|
|
153
|
-
@project_class =
|
|
154
|
-
include MongoMapper::Document
|
|
146
|
+
@project_class = Doc do
|
|
155
147
|
key :name, String
|
|
156
148
|
end
|
|
157
|
-
|
|
158
|
-
@project_class.collection.remove
|
|
159
149
|
end
|
|
160
150
|
|
|
161
151
|
should "work using a block passed to many" do
|
|
@@ -2,18 +2,10 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
class OneProxyTest < Test::Unit::TestCase
|
|
4
4
|
def setup
|
|
5
|
-
@post_class =
|
|
6
|
-
|
|
7
|
-
def self.name; 'Post' end
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
@author_class = Class.new do
|
|
11
|
-
include MongoMapper::Document
|
|
5
|
+
@post_class = Doc('Post')
|
|
6
|
+
@author_class = Doc do
|
|
12
7
|
key :post_id, ObjectId
|
|
13
8
|
end
|
|
14
|
-
|
|
15
|
-
@post_class.collection.remove
|
|
16
|
-
@author_class.collection.remove
|
|
17
9
|
end
|
|
18
10
|
|
|
19
11
|
should "default to nil" do
|
|
@@ -23,13 +15,39 @@ class OneProxyTest < Test::Unit::TestCase
|
|
|
23
15
|
|
|
24
16
|
should "be able to replace the association" do
|
|
25
17
|
@post_class.one :author, :class => @author_class
|
|
18
|
+
|
|
26
19
|
post = @post_class.new
|
|
27
|
-
author = @author_class.new
|
|
20
|
+
author = @author_class.new(:name => 'Frank')
|
|
28
21
|
post.author = author
|
|
29
22
|
post.reload
|
|
30
23
|
|
|
31
24
|
post.author.should == author
|
|
32
25
|
post.author.nil?.should be_false
|
|
26
|
+
|
|
27
|
+
new_author = @author_class.new(:name => 'Emily')
|
|
28
|
+
post.author = new_author
|
|
29
|
+
post.author.should == new_author
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
should "have boolean method for testing presence" do
|
|
33
|
+
@post_class.one :author, :class => @author_class
|
|
34
|
+
|
|
35
|
+
post = @post_class.new
|
|
36
|
+
post.author?.should be_false
|
|
37
|
+
|
|
38
|
+
post.author = @author_class.new(:name => 'Frank')
|
|
39
|
+
post.author?.should be_true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
should "work with criteria" do
|
|
43
|
+
@post_class.one :primary_author, :class => @author_class, :primary => true
|
|
44
|
+
@post_class.one :author, :class => @author_class
|
|
45
|
+
|
|
46
|
+
post = @post_class.create
|
|
47
|
+
author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
|
|
48
|
+
primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
|
|
49
|
+
post.author.should == author
|
|
50
|
+
post.primary_author.should == primary
|
|
33
51
|
end
|
|
34
52
|
|
|
35
53
|
should "unset the association" do
|
|
@@ -2,12 +2,9 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
class BinaryTest < Test::Unit::TestCase
|
|
4
4
|
should "serialize and deserialize correctly" do
|
|
5
|
-
klass =
|
|
6
|
-
include MongoMapper::Document
|
|
7
|
-
set_collection_name 'test'
|
|
5
|
+
klass = Doc do
|
|
8
6
|
key :contents, Binary
|
|
9
7
|
end
|
|
10
|
-
klass.collection.remove
|
|
11
8
|
|
|
12
9
|
doc = klass.new(:contents => '010101')
|
|
13
10
|
doc.save
|
|
@@ -18,12 +15,9 @@ class BinaryTest < Test::Unit::TestCase
|
|
|
18
15
|
|
|
19
16
|
context "Saving a document with a blank binary value" do
|
|
20
17
|
setup do
|
|
21
|
-
@document =
|
|
22
|
-
include MongoMapper::Document
|
|
23
|
-
set_collection_name 'test'
|
|
18
|
+
@document = Doc do
|
|
24
19
|
key :file, Binary
|
|
25
20
|
end
|
|
26
|
-
@document.collection.remove
|
|
27
21
|
end
|
|
28
22
|
|
|
29
23
|
should "not fail" do
|
|
@@ -3,10 +3,7 @@ require 'test_helper'
|
|
|
3
3
|
class CallbacksTest < Test::Unit::TestCase
|
|
4
4
|
context "Defining and running callbacks" do
|
|
5
5
|
setup do
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
8
|
-
set_collection_name 'test'
|
|
9
|
-
|
|
6
|
+
@document = Doc do
|
|
10
7
|
key :name, String
|
|
11
8
|
|
|
12
9
|
[ :before_validation_on_create, :before_validation_on_update,
|
|
@@ -30,7 +27,6 @@ class CallbacksTest < Test::Unit::TestCase
|
|
|
30
27
|
@history = nil
|
|
31
28
|
end
|
|
32
29
|
end
|
|
33
|
-
@document.collection.remove
|
|
34
30
|
end
|
|
35
31
|
|
|
36
32
|
should "get the order right for creating documents" do
|
|
@@ -3,12 +3,9 @@ require 'models'
|
|
|
3
3
|
|
|
4
4
|
class DirtyTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
8
|
-
set_collection_name 'test'
|
|
6
|
+
@document = Doc do
|
|
9
7
|
key :phrase, String
|
|
10
8
|
end
|
|
11
|
-
@document.collection.remove
|
|
12
9
|
|
|
13
10
|
Status.collection.remove
|
|
14
11
|
Project.collection.remove
|
|
@@ -3,8 +3,7 @@ require 'models'
|
|
|
3
3
|
|
|
4
4
|
class DocumentTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
6
|
+
@document = Doc do
|
|
8
7
|
set_collection_name 'users'
|
|
9
8
|
|
|
10
9
|
key :first_name, String
|
|
@@ -12,7 +11,6 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
12
11
|
key :age, Integer
|
|
13
12
|
key :date, Date
|
|
14
13
|
end
|
|
15
|
-
@document.collection.remove
|
|
16
14
|
end
|
|
17
15
|
|
|
18
16
|
context "Using key with type Array" do
|
|
@@ -143,12 +141,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
143
141
|
end
|
|
144
142
|
|
|
145
143
|
should "not fail if no attributes provided" do
|
|
146
|
-
document =
|
|
147
|
-
include MongoMapper::Document
|
|
148
|
-
set_collection_name 'test'
|
|
149
|
-
end
|
|
150
|
-
document.collection.remove
|
|
151
|
-
|
|
144
|
+
document = Doc()
|
|
152
145
|
lambda { document.create }.should change { document.count }.by(1)
|
|
153
146
|
end
|
|
154
147
|
end
|
|
@@ -541,11 +534,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
541
534
|
end
|
|
542
535
|
|
|
543
536
|
should "return 0 if the collection does not exist" do
|
|
544
|
-
klass =
|
|
545
|
-
include MongoMapper::Document
|
|
537
|
+
klass = Doc do
|
|
546
538
|
set_collection_name 'foobarbazwickdoesnotexist'
|
|
547
539
|
end
|
|
548
|
-
@document.collection.remove
|
|
549
540
|
|
|
550
541
|
klass.count.should == 0
|
|
551
542
|
end
|
|
@@ -649,15 +640,12 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
649
640
|
should "fail if wrong version (and locking enabled)" do
|
|
650
641
|
@document.locking!
|
|
651
642
|
doc = @document.create(:first_name => 'Blah')
|
|
652
|
-
|
|
653
|
-
doc.
|
|
654
|
-
doc.save
|
|
655
|
-
|
|
656
|
-
doc2.first_name = 'Blip'
|
|
657
|
-
|
|
658
|
-
lambda{ doc2.save }.should raise_error(MongoMapper::StaleDocumentError)
|
|
659
|
-
doc.reload.first_name.should == 'Bloop'
|
|
643
|
+
attrs = {:_rev => "012345", :first_name => 'Blip'}
|
|
644
|
+
lambda{ doc.update_attributes(attrs) }.should raise_error(MongoMapper::StaleDocumentError)
|
|
645
|
+
doc._rev.should == attrs[:_rev] #ensures that the _rev is reset on a failed save
|
|
646
|
+
doc.reload.first_name.should == 'Blah'
|
|
660
647
|
end
|
|
648
|
+
|
|
661
649
|
end
|
|
662
650
|
|
|
663
651
|
context "#update_attributes (new document)" do
|
|
@@ -730,12 +718,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
730
718
|
|
|
731
719
|
context "#save (with validations off)" do
|
|
732
720
|
setup do
|
|
733
|
-
@document =
|
|
734
|
-
include MongoMapper::Document
|
|
735
|
-
set_collection_name 'test'
|
|
721
|
+
@document = Doc do
|
|
736
722
|
key :name, String, :required => true
|
|
737
723
|
end
|
|
738
|
-
@document.collection.remove
|
|
739
724
|
end
|
|
740
725
|
|
|
741
726
|
should "insert document" do
|
|
@@ -755,13 +740,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
755
740
|
setup do
|
|
756
741
|
MongoMapper.ensured_indexes = []
|
|
757
742
|
|
|
758
|
-
@document =
|
|
759
|
-
include MongoMapper::Document
|
|
760
|
-
set_collection_name 'test'
|
|
743
|
+
@document = Doc do
|
|
761
744
|
key :name, String
|
|
745
|
+
set_collection_name 'test_indexes'
|
|
762
746
|
ensure_index :name, :unique => true
|
|
763
747
|
end
|
|
764
|
-
@document.collection.
|
|
748
|
+
if @document.database.collection_names.include?(@document.collection.name)
|
|
749
|
+
@document.collection.drop_indexes
|
|
750
|
+
end
|
|
765
751
|
|
|
766
752
|
MongoMapper.ensure_indexes!
|
|
767
753
|
end
|
|
@@ -1078,14 +1064,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
1078
1064
|
|
|
1079
1065
|
context "#reload" do
|
|
1080
1066
|
setup do
|
|
1081
|
-
@foo_class =
|
|
1082
|
-
include MongoMapper::Document
|
|
1067
|
+
@foo_class = Doc do
|
|
1083
1068
|
key :name
|
|
1084
1069
|
end
|
|
1085
|
-
@foo_class.collection.remove
|
|
1086
1070
|
|
|
1087
|
-
@bar_class =
|
|
1088
|
-
include MongoMapper::EmbeddedDocument
|
|
1071
|
+
@bar_class = EDoc do
|
|
1089
1072
|
key :name
|
|
1090
1073
|
end
|
|
1091
1074
|
|
|
@@ -3,14 +3,11 @@ require 'models'
|
|
|
3
3
|
|
|
4
4
|
class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
6
|
+
@document = Doc do
|
|
8
7
|
set_collection_name 'users'
|
|
9
|
-
|
|
10
8
|
key :first_name, String
|
|
11
9
|
key :last_name, String
|
|
12
10
|
end
|
|
13
|
-
@document.collection.remove
|
|
14
11
|
end
|
|
15
12
|
|
|
16
13
|
context "Saving a document with an embedded document" do
|
|
@@ -34,23 +31,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
34
31
|
end
|
|
35
32
|
end
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
include MongoMapper::Document
|
|
41
|
-
key :message, Message
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
should "work" do
|
|
46
|
-
doc1 = @document.create(:message => Enter.new)
|
|
47
|
-
doc2 = @document.create(:message => Exit.new)
|
|
48
|
-
doc3 = @document.create(:message => Chat.new)
|
|
49
|
-
|
|
50
|
-
doc1.reload.message.class.should be(Enter)
|
|
51
|
-
doc2.reload.message.class.should be(Exit)
|
|
52
|
-
doc3.reload.message.class.should be(Chat)
|
|
34
|
+
should "correctly instantiate single collection inherited embedded documents" do
|
|
35
|
+
document = Doc('Foo') do
|
|
36
|
+
key :message, Message
|
|
53
37
|
end
|
|
38
|
+
|
|
39
|
+
doc1 = document.create(:message => Enter.new)
|
|
40
|
+
doc1.reload.message.class.should be(Enter)
|
|
54
41
|
end
|
|
55
42
|
|
|
56
43
|
context "new?" do
|
|
@@ -2,17 +2,13 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
class ModifierTest < Test::Unit::TestCase
|
|
4
4
|
def setup
|
|
5
|
-
@page_class =
|
|
6
|
-
include MongoMapper::Document
|
|
7
|
-
|
|
5
|
+
@page_class = Doc do
|
|
8
6
|
key :title, String
|
|
9
7
|
key :day_count, Integer, :default => 0
|
|
10
8
|
key :week_count, Integer, :default => 0
|
|
11
9
|
key :month_count, Integer, :default => 0
|
|
12
10
|
key :tags, Array
|
|
13
11
|
end
|
|
14
|
-
|
|
15
|
-
@page_class.collection.remove
|
|
16
12
|
end
|
|
17
13
|
|
|
18
14
|
def assert_page_counts(page, day_count, week_count, month_count)
|
|
@@ -3,8 +3,7 @@ require 'test_helper'
|
|
|
3
3
|
class PaginationTest < Test::Unit::TestCase
|
|
4
4
|
context "Paginating" do
|
|
5
5
|
setup do
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
6
|
+
@document = Doc do
|
|
8
7
|
set_collection_name 'users'
|
|
9
8
|
|
|
10
9
|
key :first_name, String
|
|
@@ -13,7 +12,6 @@ class PaginationTest < Test::Unit::TestCase
|
|
|
13
12
|
|
|
14
13
|
def self.per_page; 1 end
|
|
15
14
|
end
|
|
16
|
-
@document.collection.remove
|
|
17
15
|
|
|
18
16
|
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
|
19
17
|
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
|
@@ -2,19 +2,17 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
class StringIdCompatibilityTest < Test::Unit::TestCase
|
|
4
4
|
def setup
|
|
5
|
-
@note_class =
|
|
6
|
-
include MongoMapper::EmbeddedDocument
|
|
5
|
+
@note_class = EDoc do
|
|
7
6
|
key :_id, String
|
|
8
7
|
end
|
|
9
8
|
|
|
10
|
-
@task_class =
|
|
11
|
-
include MongoMapper::Document
|
|
9
|
+
@task_class = Doc do
|
|
12
10
|
key :_id, String
|
|
13
11
|
key :project_id, String
|
|
14
12
|
belongs_to :project
|
|
15
13
|
end
|
|
16
14
|
|
|
17
|
-
@project_class =
|
|
15
|
+
@project_class = Doc do
|
|
18
16
|
include MongoMapper::Document
|
|
19
17
|
key :_id, String
|
|
20
18
|
end
|
|
@@ -22,9 +20,6 @@ class StringIdCompatibilityTest < Test::Unit::TestCase
|
|
|
22
20
|
@task_class.belongs_to :project, :class => @project_class
|
|
23
21
|
@project_class.many :notes, :class => @note_class
|
|
24
22
|
@project_class.many :tasks, :class => @task_class, :foreign_key => 'project_id'
|
|
25
|
-
|
|
26
|
-
@project_class.collection.remove
|
|
27
|
-
@task_class.collection.remove
|
|
28
23
|
end
|
|
29
24
|
|
|
30
25
|
should "assign correct _id for documents" do
|
|
@@ -3,12 +3,9 @@ require 'test_helper'
|
|
|
3
3
|
class ValidationsTest < Test::Unit::TestCase
|
|
4
4
|
context "Saving a new document that is invalid" do
|
|
5
5
|
setup do
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::Document
|
|
8
|
-
set_collection_name 'test'
|
|
6
|
+
@document = Doc do
|
|
9
7
|
key :name, String, :required => true
|
|
10
8
|
end
|
|
11
|
-
@document.collection.remove
|
|
12
9
|
end
|
|
13
10
|
|
|
14
11
|
should "not insert document" do
|
|
@@ -27,12 +24,9 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
27
24
|
|
|
28
25
|
context "Saving a document that is invalid (destructive)" do
|
|
29
26
|
setup do
|
|
30
|
-
@document =
|
|
31
|
-
include MongoMapper::Document
|
|
32
|
-
set_collection_name 'test'
|
|
27
|
+
@document = Doc do
|
|
33
28
|
key :name, String, :required => true
|
|
34
29
|
end
|
|
35
|
-
@document.collection.remove
|
|
36
30
|
end
|
|
37
31
|
|
|
38
32
|
should "raise error" do
|
|
@@ -43,12 +37,9 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
43
37
|
|
|
44
38
|
context "Creating a document that is invalid (destructive)" do
|
|
45
39
|
setup do
|
|
46
|
-
@document =
|
|
47
|
-
include MongoMapper::Document
|
|
48
|
-
set_collection_name 'test'
|
|
40
|
+
@document = Doc do
|
|
49
41
|
key :name, String, :required => true
|
|
50
42
|
end
|
|
51
|
-
@document.collection.remove
|
|
52
43
|
end
|
|
53
44
|
|
|
54
45
|
should "raise error" do
|
|
@@ -63,12 +54,9 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
63
54
|
|
|
64
55
|
context "Saving an existing document that is invalid" do
|
|
65
56
|
setup do
|
|
66
|
-
@document =
|
|
67
|
-
include MongoMapper::Document
|
|
68
|
-
set_collection_name 'test'
|
|
57
|
+
@document = Doc do
|
|
69
58
|
key :name, String, :required => true
|
|
70
59
|
end
|
|
71
|
-
@document.collection.remove
|
|
72
60
|
|
|
73
61
|
@doc = @document.create(:name => 'John Nunemaker')
|
|
74
62
|
end
|
|
@@ -88,16 +76,12 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
88
76
|
|
|
89
77
|
context "Adding validation errors" do
|
|
90
78
|
setup do
|
|
91
|
-
@document =
|
|
92
|
-
include MongoMapper::Document
|
|
93
|
-
set_collection_name 'test'
|
|
94
|
-
|
|
79
|
+
@document = Doc do
|
|
95
80
|
key :action, String
|
|
96
81
|
def action_present
|
|
97
82
|
errors.add(:action, 'is invalid') if action.blank?
|
|
98
83
|
end
|
|
99
84
|
end
|
|
100
|
-
@document.collection.remove
|
|
101
85
|
end
|
|
102
86
|
|
|
103
87
|
should "work with validate_on_create callback" do
|
|
@@ -133,14 +117,10 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
133
117
|
|
|
134
118
|
context "validating uniqueness of" do
|
|
135
119
|
setup do
|
|
136
|
-
@document =
|
|
137
|
-
include MongoMapper::Document
|
|
138
|
-
set_collection_name 'test'
|
|
139
|
-
|
|
120
|
+
@document = Doc do
|
|
140
121
|
key :name, String
|
|
141
122
|
validates_uniqueness_of :name
|
|
142
123
|
end
|
|
143
|
-
@document.collection.remove
|
|
144
124
|
end
|
|
145
125
|
|
|
146
126
|
should "not fail if object is new" do
|
|
@@ -149,10 +129,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
149
129
|
end
|
|
150
130
|
|
|
151
131
|
should "not fail when new object is out of scope" do
|
|
152
|
-
document =
|
|
153
|
-
include MongoMapper::Document
|
|
154
|
-
set_collection_name 'test'
|
|
155
|
-
|
|
132
|
+
document = Doc do
|
|
156
133
|
key :name
|
|
157
134
|
key :adult
|
|
158
135
|
validates_uniqueness_of :name, :scope => :adult
|
|
@@ -162,7 +139,6 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
162
139
|
|
|
163
140
|
doc2 = document.new("name" => "joe", :adult => false)
|
|
164
141
|
doc2.should be_valid
|
|
165
|
-
|
|
166
142
|
end
|
|
167
143
|
|
|
168
144
|
should "allow to update an object" do
|
|
@@ -193,10 +169,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
193
169
|
end
|
|
194
170
|
|
|
195
171
|
should "allow multiple blank entries if :allow_blank => true" do
|
|
196
|
-
document =
|
|
197
|
-
include MongoMapper::Document
|
|
198
|
-
set_collection_name 'test'
|
|
199
|
-
|
|
172
|
+
document = Doc do
|
|
200
173
|
key :name
|
|
201
174
|
validates_uniqueness_of :name, :allow_blank => :true
|
|
202
175
|
end
|
|
@@ -214,10 +187,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
214
187
|
end
|
|
215
188
|
|
|
216
189
|
should "allow entries that differ only in case by default" do
|
|
217
|
-
document =
|
|
218
|
-
include MongoMapper::Document
|
|
219
|
-
set_collection_name 'test'
|
|
220
|
-
|
|
190
|
+
document = Doc do
|
|
221
191
|
key :name
|
|
222
192
|
validates_uniqueness_of :name
|
|
223
193
|
end
|
|
@@ -231,10 +201,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
231
201
|
|
|
232
202
|
context "with :case_sensitive => false" do
|
|
233
203
|
setup do
|
|
234
|
-
@document =
|
|
235
|
-
include MongoMapper::Document
|
|
236
|
-
set_collection_name 'test'
|
|
237
|
-
|
|
204
|
+
@document = Doc do
|
|
238
205
|
key :name
|
|
239
206
|
validates_uniqueness_of :name, :case_sensitive => false
|
|
240
207
|
end
|
|
@@ -256,15 +223,11 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
256
223
|
|
|
257
224
|
context "scoped by a single attribute" do
|
|
258
225
|
setup do
|
|
259
|
-
@document =
|
|
260
|
-
include MongoMapper::Document
|
|
261
|
-
set_collection_name 'test'
|
|
262
|
-
|
|
226
|
+
@document = Doc do
|
|
263
227
|
key :name, String
|
|
264
228
|
key :scope, String
|
|
265
229
|
validates_uniqueness_of :name, :scope => :scope
|
|
266
230
|
end
|
|
267
|
-
@document.collection.remove
|
|
268
231
|
end
|
|
269
232
|
|
|
270
233
|
should "fail if the same name exists in the scope" do
|
|
@@ -296,16 +259,12 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
296
259
|
|
|
297
260
|
context "scoped by a multiple attributes" do
|
|
298
261
|
setup do
|
|
299
|
-
@document =
|
|
300
|
-
include MongoMapper::Document
|
|
301
|
-
set_collection_name 'test'
|
|
302
|
-
|
|
262
|
+
@document = Doc do
|
|
303
263
|
key :name, String
|
|
304
264
|
key :first_scope, String
|
|
305
265
|
key :second_scope, String
|
|
306
266
|
validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
|
|
307
267
|
end
|
|
308
|
-
@document.collection.remove
|
|
309
268
|
end
|
|
310
269
|
|
|
311
270
|
should "fail if the same name exists in the scope" do
|
|
@@ -338,13 +297,9 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
338
297
|
|
|
339
298
|
context "validates uniqueness of with :unique shortcut" do
|
|
340
299
|
should "work" do
|
|
341
|
-
@document =
|
|
342
|
-
include MongoMapper::Document
|
|
343
|
-
set_collection_name 'test'
|
|
344
|
-
|
|
300
|
+
@document = Doc do
|
|
345
301
|
key :name, String, :unique => true
|
|
346
302
|
end
|
|
347
|
-
@document.collection.remove
|
|
348
303
|
|
|
349
304
|
doc = @document.create(:name => 'John')
|
|
350
305
|
doc.should_not have_error_on(:name)
|
data/test/test_helper.rb
CHANGED
|
@@ -16,6 +16,34 @@ require 'support/timing'
|
|
|
16
16
|
|
|
17
17
|
class Test::Unit::TestCase
|
|
18
18
|
include CustomMatchers
|
|
19
|
+
|
|
20
|
+
cattr_accessor :mm_document_count
|
|
21
|
+
self.mm_document_count = 0
|
|
22
|
+
|
|
23
|
+
def Doc(name=nil, &block)
|
|
24
|
+
Test::Unit::TestCase.mm_document_count += 1
|
|
25
|
+
|
|
26
|
+
klass = Class.new do
|
|
27
|
+
include MongoMapper::Document
|
|
28
|
+
set_collection_name "test#{rand(20)}"
|
|
29
|
+
|
|
30
|
+
if name
|
|
31
|
+
class_eval "def self.name; '#{name}' end"
|
|
32
|
+
class_eval "def self.to_s; '#{name}' end"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class_eval(&block) if block_given?
|
|
36
|
+
end
|
|
37
|
+
klass.collection.remove
|
|
38
|
+
klass
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def EDoc(&block)
|
|
42
|
+
Class.new do
|
|
43
|
+
include MongoMapper::EmbeddedDocument
|
|
44
|
+
instance_eval(&block) if block_given?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
19
47
|
end
|
|
20
48
|
|
|
21
49
|
test_dir = File.expand_path(File.dirname(__FILE__) + '/../tmp')
|
|
@@ -53,6 +53,7 @@ class ProxyTest < Test::Unit::TestCase
|
|
|
53
53
|
context "blank?" do
|
|
54
54
|
should "be true if blank" do
|
|
55
55
|
@blank_proxy.blank?.should be_true
|
|
56
|
+
@nil_proxy.blank?.should be_true
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
should "be false if not blank" do
|
|
@@ -60,6 +61,17 @@ class ProxyTest < Test::Unit::TestCase
|
|
|
60
61
|
end
|
|
61
62
|
end
|
|
62
63
|
|
|
64
|
+
context "present?" do
|
|
65
|
+
should "be true if present" do
|
|
66
|
+
@proxy.present?.should be_true
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
should "be false if not present" do
|
|
70
|
+
@blank_proxy.present?.should be_false
|
|
71
|
+
@nil_proxy.present?.should be_false
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
63
75
|
should "delegate respond_to? to target" do
|
|
64
76
|
@proxy.respond_to?(:each).should be_true
|
|
65
77
|
@proxy.respond_to?(:size).should be_true
|
data/test/unit/test_document.rb
CHANGED
|
@@ -4,11 +4,7 @@ require 'models'
|
|
|
4
4
|
class DocumentTest < Test::Unit::TestCase
|
|
5
5
|
context "The Document Class" do
|
|
6
6
|
setup do
|
|
7
|
-
@document =
|
|
8
|
-
include MongoMapper::Document
|
|
9
|
-
set_collection_name 'test'
|
|
10
|
-
end
|
|
11
|
-
@document.collection.remove
|
|
7
|
+
@document = Doc()
|
|
12
8
|
end
|
|
13
9
|
|
|
14
10
|
should "have logger method" do
|
|
@@ -40,10 +36,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
40
36
|
@document.database_name.should == 'test2'
|
|
41
37
|
@document.database.name.should == 'test2'
|
|
42
38
|
|
|
43
|
-
another_document =
|
|
44
|
-
include MongoMapper::Document
|
|
45
|
-
set_collection_name 'test'
|
|
46
|
-
end
|
|
39
|
+
another_document = Doc()
|
|
47
40
|
another_document.database.should == MongoMapper.database
|
|
48
41
|
end
|
|
49
42
|
|
|
@@ -76,32 +69,28 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
76
69
|
should 'allow extensions to Document to be appended' do
|
|
77
70
|
module Extension; def test_this_extension; end end
|
|
78
71
|
MongoMapper::Document.append_extensions(Extension)
|
|
79
|
-
article =
|
|
80
|
-
|
|
72
|
+
article = Doc()
|
|
81
73
|
article.should respond_to(:test_this_extension)
|
|
82
74
|
end
|
|
83
75
|
|
|
84
76
|
should 'add appended extensions to classes that include Document before they are added' do
|
|
85
77
|
module Extension; def test_this_extension; end end
|
|
86
|
-
article =
|
|
78
|
+
article = Doc()
|
|
87
79
|
MongoMapper::Document.append_extensions(Extension)
|
|
88
|
-
|
|
89
80
|
article.should respond_to(:test_this_extension)
|
|
90
81
|
end
|
|
91
82
|
|
|
92
83
|
should 'allow inclusions to Document to be appended' do
|
|
93
84
|
module Inclusion; def test_this_inclusion; end end
|
|
94
85
|
MongoMapper::Document.append_inclusions(Inclusion)
|
|
95
|
-
article =
|
|
96
|
-
|
|
86
|
+
article = Doc()
|
|
97
87
|
article.new.should respond_to(:test_this_inclusion)
|
|
98
88
|
end
|
|
99
89
|
|
|
100
90
|
should 'add appended inclusions to classes that include Document before they are added' do
|
|
101
91
|
module Inclusion; def test_this_inclusion; end end
|
|
102
|
-
article =
|
|
92
|
+
article = Doc()
|
|
103
93
|
MongoMapper::Document.append_inclusions(Inclusion)
|
|
104
|
-
|
|
105
94
|
article.new.should respond_to(:test_this_inclusion)
|
|
106
95
|
end
|
|
107
96
|
end # Document class
|
|
@@ -128,14 +117,10 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
128
117
|
|
|
129
118
|
context "An instance of a document" do
|
|
130
119
|
setup do
|
|
131
|
-
@document =
|
|
132
|
-
include MongoMapper::Document
|
|
133
|
-
set_collection_name 'test'
|
|
134
|
-
|
|
120
|
+
@document = Doc do
|
|
135
121
|
key :name, String
|
|
136
122
|
key :age, Integer
|
|
137
123
|
end
|
|
138
|
-
@document.collection.remove
|
|
139
124
|
end
|
|
140
125
|
|
|
141
126
|
should "have access to logger" do
|
|
@@ -169,13 +154,9 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
169
154
|
end
|
|
170
155
|
|
|
171
156
|
should "set self to the root document on embedded documents" do
|
|
172
|
-
klass =
|
|
173
|
-
|
|
174
|
-
end
|
|
157
|
+
klass = Doc()
|
|
158
|
+
pets = EDoc()
|
|
175
159
|
|
|
176
|
-
pets = Class.new do
|
|
177
|
-
include MongoMapper::EmbeddedDocument
|
|
178
|
-
end
|
|
179
160
|
klass.many :pets, :class => pets
|
|
180
161
|
|
|
181
162
|
doc = klass.new(:pets => [{}])
|
|
@@ -215,6 +196,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
215
196
|
setup do
|
|
216
197
|
@oid = Mongo::ObjectID.new
|
|
217
198
|
end
|
|
199
|
+
|
|
218
200
|
should "be equal if id and class are the same" do
|
|
219
201
|
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be(true)
|
|
220
202
|
end
|
|
@@ -224,12 +206,8 @@ class DocumentTest < Test::Unit::TestCase
|
|
|
224
206
|
end
|
|
225
207
|
|
|
226
208
|
should "not be equal if id same but class different" do
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
set_collection_name 'test'
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
(@document.new('_id' => @oid) == @another_document.new('_id' => @oid)).should be(false)
|
|
209
|
+
another_document = Doc()
|
|
210
|
+
(@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be(false)
|
|
233
211
|
end
|
|
234
212
|
end
|
|
235
213
|
end # instance of a document
|
|
@@ -35,9 +35,7 @@ end
|
|
|
35
35
|
class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
36
36
|
context "Including MongoMapper::EmbeddedDocument in a class" do
|
|
37
37
|
setup do
|
|
38
|
-
@klass =
|
|
39
|
-
include MongoMapper::EmbeddedDocument
|
|
40
|
-
end
|
|
38
|
+
@klass = EDoc()
|
|
41
39
|
end
|
|
42
40
|
|
|
43
41
|
should "give class access to logger" do
|
|
@@ -89,29 +87,16 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
89
87
|
end
|
|
90
88
|
end
|
|
91
89
|
|
|
92
|
-
context "looking up type constants" do
|
|
93
|
-
should "not raise an error" do
|
|
94
|
-
klass = Class.new do
|
|
95
|
-
include MongoMapper::EmbeddedDocument
|
|
96
|
-
key :file, Binary
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
90
|
context "parent_model" do
|
|
102
91
|
should "be nil if none of parents ancestors include EmbeddedDocument" do
|
|
103
92
|
parent = Class.new
|
|
104
|
-
document = Class.new(parent)
|
|
105
|
-
include MongoMapper::EmbeddedDocument
|
|
106
|
-
end
|
|
93
|
+
document = Class.new(parent) { include MongoMapper::EmbeddedDocument }
|
|
107
94
|
document.parent_model.should be_nil
|
|
108
95
|
end
|
|
109
96
|
|
|
110
97
|
should "work when other modules have been included" do
|
|
111
98
|
grandparent = Class.new
|
|
112
|
-
parent = Class.new
|
|
113
|
-
include MongoMapper::EmbeddedDocument
|
|
114
|
-
end
|
|
99
|
+
parent = Class.new(grandparent) { include MongoMapper::EmbeddedDocument }
|
|
115
100
|
|
|
116
101
|
example_module = Module.new
|
|
117
102
|
document = Class.new(parent) do
|
|
@@ -130,9 +115,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
130
115
|
|
|
131
116
|
context "defining a key" do
|
|
132
117
|
setup do
|
|
133
|
-
@document =
|
|
134
|
-
include MongoMapper::EmbeddedDocument
|
|
135
|
-
end
|
|
118
|
+
@document = EDoc()
|
|
136
119
|
end
|
|
137
120
|
|
|
138
121
|
should "work with name" do
|
|
@@ -226,8 +209,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
226
209
|
|
|
227
210
|
context "#inspect" do
|
|
228
211
|
setup do
|
|
229
|
-
@document =
|
|
230
|
-
include MongoMapper::Document
|
|
212
|
+
@document = Doc do
|
|
231
213
|
key :animals
|
|
232
214
|
end
|
|
233
215
|
end
|
|
@@ -252,9 +234,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
252
234
|
|
|
253
235
|
context "Applying default values for keys" do
|
|
254
236
|
setup do
|
|
255
|
-
@document =
|
|
256
|
-
include MongoMapper::EmbeddedDocument
|
|
257
|
-
|
|
237
|
+
@document = EDoc do
|
|
258
238
|
key :name, String, :default => 'foo'
|
|
259
239
|
key :age, Integer, :default => 20
|
|
260
240
|
key :net_worth, Float, :default => 100.00
|
|
@@ -299,9 +279,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
299
279
|
|
|
300
280
|
context "An instance of an embedded document" do
|
|
301
281
|
setup do
|
|
302
|
-
@document =
|
|
303
|
-
include MongoMapper::EmbeddedDocument
|
|
304
|
-
|
|
282
|
+
@document = EDoc do
|
|
305
283
|
key :name, String
|
|
306
284
|
key :age, Integer
|
|
307
285
|
end
|
|
@@ -398,8 +376,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
398
376
|
|
|
399
377
|
context "initialized when _type key present" do
|
|
400
378
|
setup do
|
|
401
|
-
::FooBar =
|
|
402
|
-
include MongoMapper::EmbeddedDocument
|
|
379
|
+
::FooBar = EDoc do
|
|
403
380
|
key :_type, String
|
|
404
381
|
end
|
|
405
382
|
end
|
|
@@ -581,8 +558,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
581
558
|
end
|
|
582
559
|
|
|
583
560
|
should "be overrideable by modules" do
|
|
584
|
-
@document =
|
|
585
|
-
include MongoMapper::Document
|
|
561
|
+
@document = Doc do
|
|
586
562
|
key :other_child, String
|
|
587
563
|
end
|
|
588
564
|
|
|
@@ -656,8 +632,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
656
632
|
end
|
|
657
633
|
|
|
658
634
|
should "be overrideable by modules" do
|
|
659
|
-
@document =
|
|
660
|
-
include MongoMapper::Document
|
|
635
|
+
@document = Doc do
|
|
661
636
|
key :other_child, String
|
|
662
637
|
end
|
|
663
638
|
|
|
@@ -690,19 +665,16 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
|
690
665
|
@oid = Mongo::ObjectID.new
|
|
691
666
|
end
|
|
692
667
|
should "be equal if id and class are the same" do
|
|
693
|
-
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should
|
|
668
|
+
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be_true
|
|
694
669
|
end
|
|
695
670
|
|
|
696
671
|
should "not be equal if class same but id different" do
|
|
697
|
-
(@document.new('_id' => @oid) == @document.new('_id' => Mongo::ObjectID.new)).should
|
|
672
|
+
(@document.new('_id' => @oid) == @document.new('_id' => Mongo::ObjectID.new)).should be_false
|
|
698
673
|
end
|
|
699
674
|
|
|
700
675
|
should "not be equal if id same but class different" do
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
end
|
|
704
|
-
|
|
705
|
-
(@document.new('_id' => @oid) == @another_document.new('_id' => @oid)).should be(false)
|
|
676
|
+
another_document = Doc()
|
|
677
|
+
(@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be_false
|
|
706
678
|
end
|
|
707
679
|
end
|
|
708
680
|
end # instance of a embedded document
|
data/test/unit/test_key.rb
CHANGED
|
@@ -82,10 +82,7 @@ class KeyTest < Test::Unit::TestCase
|
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
should "know if it is a embedded_document" do
|
|
85
|
-
|
|
86
|
-
include MongoMapper::EmbeddedDocument
|
|
87
|
-
end
|
|
88
|
-
Key.new(:name, klass).embeddable?.should be_true
|
|
85
|
+
Key.new(:name, EDoc()).embeddable?.should be_true
|
|
89
86
|
end
|
|
90
87
|
|
|
91
88
|
should "know if it is not a embedded_document" do
|
|
@@ -3,8 +3,7 @@ require 'test_helper'
|
|
|
3
3
|
class TimeZonesTest < Test::Unit::TestCase
|
|
4
4
|
context "An instance of an embedded document" do
|
|
5
5
|
setup do
|
|
6
|
-
@document =
|
|
7
|
-
include MongoMapper::EmbeddedDocument
|
|
6
|
+
@document = EDoc do
|
|
8
7
|
key :name, String
|
|
9
8
|
key :created_at, Time
|
|
10
9
|
end
|
|
@@ -2,14 +2,9 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
class ValidationsTest < Test::Unit::TestCase
|
|
4
4
|
context "Validations" do
|
|
5
|
-
|
|
6
5
|
context "on a Document" do
|
|
7
|
-
|
|
8
6
|
setup do
|
|
9
|
-
@document =
|
|
10
|
-
include MongoMapper::Document
|
|
11
|
-
set_collection_name 'test'
|
|
12
|
-
end
|
|
7
|
+
@document = Doc()
|
|
13
8
|
end
|
|
14
9
|
|
|
15
10
|
context "Validating acceptance of" do
|
|
@@ -240,11 +235,8 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
240
235
|
end # End on a Document
|
|
241
236
|
|
|
242
237
|
context "On an EmbeddedDocument" do
|
|
243
|
-
|
|
244
238
|
setup do
|
|
245
|
-
@embedded_doc =
|
|
246
|
-
include MongoMapper::EmbeddedDocument
|
|
247
|
-
end
|
|
239
|
+
@embedded_doc = EDoc()
|
|
248
240
|
end
|
|
249
241
|
|
|
250
242
|
context "Validating acceptance of" do
|
|
@@ -478,10 +470,7 @@ class ValidationsTest < Test::Unit::TestCase
|
|
|
478
470
|
|
|
479
471
|
context "Adding validation errors" do
|
|
480
472
|
setup do
|
|
481
|
-
@document =
|
|
482
|
-
include MongoMapper::Document
|
|
483
|
-
set_collection_name 'test'
|
|
484
|
-
|
|
473
|
+
@document = Doc do
|
|
485
474
|
key :action, String
|
|
486
475
|
def action_present
|
|
487
476
|
errors.add(:action, 'is invalid') if action.blank?
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mrkurt-mongo_mapper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Nunemaker
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2010-01-
|
|
13
|
+
date: 2010-01-09 00:00:00 -06:00
|
|
14
14
|
default_executable: mmconsole
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|