djsun-mongomapper 0.3.5.5 → 0.4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class EmbeddedDocumentTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::Document
|
8
|
+
set_collection_name 'users'
|
9
|
+
|
10
|
+
key :first_name, String
|
11
|
+
key :last_name, String
|
12
|
+
end
|
13
|
+
|
14
|
+
clear_all_collections
|
15
|
+
end
|
16
|
+
|
17
|
+
context "Saving a document with an embedded document" do
|
18
|
+
setup do
|
19
|
+
@document.class_eval do
|
20
|
+
key :foo, Address
|
21
|
+
end
|
22
|
+
|
23
|
+
@address = Address.new(:city => 'South Bend', :state => 'IN')
|
24
|
+
@doc = @document.new(:foo => @address)
|
25
|
+
end
|
26
|
+
|
27
|
+
should "embed embedded document" do
|
28
|
+
@doc.save
|
29
|
+
@doc.foo.city.should == 'South Bend'
|
30
|
+
@doc.foo.state.should == 'IN'
|
31
|
+
|
32
|
+
from_db = @document.find(@doc.id)
|
33
|
+
from_db.foo.city.should == 'South Bend'
|
34
|
+
from_db.foo.state.should == 'IN'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "new?" do
|
39
|
+
setup do
|
40
|
+
@document.class_eval do
|
41
|
+
key :foo, Address
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "be new until document is saved" do
|
46
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
47
|
+
doc = @document.new(:foo => address)
|
48
|
+
address.new?.should == true
|
49
|
+
end
|
50
|
+
|
51
|
+
should "not be new after document is saved" do
|
52
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
53
|
+
doc = @document.new(:foo => address)
|
54
|
+
doc.save
|
55
|
+
doc.foo.new?.should == false
|
56
|
+
end
|
57
|
+
|
58
|
+
should "not be new when document is read back" do
|
59
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
60
|
+
doc = @document.new(:foo => address)
|
61
|
+
doc.save
|
62
|
+
read_doc = @document.find(doc.id)
|
63
|
+
read_doc.foo.new?.should == false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "save" do
|
68
|
+
should "save the root document" do
|
69
|
+
person = RealPerson.create
|
70
|
+
|
71
|
+
pet = Pet.new :name => 'sparky'
|
72
|
+
person.pets << pet
|
73
|
+
pet.save
|
74
|
+
|
75
|
+
doc = RealPerson.find(person.id)
|
76
|
+
doc.pets.first.should == pet
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "update_attributes" do
|
81
|
+
should "save the root document" do
|
82
|
+
person = RealPerson.create
|
83
|
+
|
84
|
+
pet = Pet.new(:name => 'sparky')
|
85
|
+
person.pets << pet
|
86
|
+
pet.save
|
87
|
+
|
88
|
+
doc = RealPerson.find(person.id)
|
89
|
+
pet = doc.pets.first
|
90
|
+
pet.update_attributes :name => 'koda'
|
91
|
+
|
92
|
+
doc = RealPerson.find(person.id)
|
93
|
+
embedded = doc.pets.first
|
94
|
+
embedded.id.should == pet.id
|
95
|
+
embedded.name.should == 'koda'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -1,81 +1,88 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class PaginationTest < Test::Unit::TestCase
|
4
|
-
context "Paginating" do
|
5
|
-
setup do
|
6
|
-
@document = Class.new do
|
7
|
-
include MongoMapper::Document
|
8
|
-
|
9
|
-
|
10
|
-
key :first_name, String
|
11
|
-
key :last_name, String
|
12
|
-
key :age, Integer
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
result.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
result
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
result
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
:
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
result
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
:
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
result
|
78
|
-
|
79
|
-
|
80
|
-
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PaginationTest < Test::Unit::TestCase
|
4
|
+
context "Paginating" do
|
5
|
+
setup do
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::Document
|
8
|
+
set_collection_name 'users'
|
9
|
+
|
10
|
+
key :first_name, String
|
11
|
+
key :last_name, String
|
12
|
+
key :age, Integer
|
13
|
+
|
14
|
+
def self.per_page; 1 end
|
15
|
+
end
|
16
|
+
|
17
|
+
clear_all_collections
|
18
|
+
|
19
|
+
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
20
|
+
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
21
|
+
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
22
|
+
end
|
23
|
+
|
24
|
+
should "return the total pages" do
|
25
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
26
|
+
result.total_pages.should == 2
|
27
|
+
end
|
28
|
+
|
29
|
+
should "return the total pages when defaulting to the document class per_page" do
|
30
|
+
result = @document.paginate(:page => 1)
|
31
|
+
result.total_pages.should == 3
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return the total of records" do
|
35
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
36
|
+
result.total_entries.should == 3
|
37
|
+
end
|
38
|
+
|
39
|
+
should "return the items" do
|
40
|
+
result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
|
41
|
+
result.size.should == 2
|
42
|
+
result.should == [@doc1, @doc3]
|
43
|
+
end
|
44
|
+
|
45
|
+
should "accept conditions" do
|
46
|
+
result = @document.paginate({
|
47
|
+
:conditions => {:last_name => 'Nunemaker'},
|
48
|
+
:order => "age DESC",
|
49
|
+
:per_page => 2,
|
50
|
+
:page => 1,
|
51
|
+
})
|
52
|
+
result.should == [@doc1, @doc3]
|
53
|
+
result.first.age.should == 27
|
54
|
+
end
|
55
|
+
|
56
|
+
should "withstand rigor" do
|
57
|
+
result = @document.paginate({
|
58
|
+
:per_page => 1,
|
59
|
+
:page => 1,
|
60
|
+
:order => 'age desc',
|
61
|
+
:conditions => {:last_name => 'Nunemaker'}
|
62
|
+
})
|
63
|
+
result.should == [@doc1]
|
64
|
+
result.total_entries.should == 2
|
65
|
+
result.total_pages.should == 2
|
66
|
+
|
67
|
+
result = @document.paginate({
|
68
|
+
:per_page => 1,
|
69
|
+
:page => 2,
|
70
|
+
:order => 'age desc',
|
71
|
+
:conditions => {:last_name => 'Nunemaker'}
|
72
|
+
})
|
73
|
+
result.should == [@doc3]
|
74
|
+
result.total_entries.should == 2
|
75
|
+
result.total_pages.should == 2
|
76
|
+
|
77
|
+
result = @document.paginate({
|
78
|
+
:per_page => 2,
|
79
|
+
:page => 1,
|
80
|
+
:order => 'age desc',
|
81
|
+
:conditions => {:last_name => 'Nunemaker'}
|
82
|
+
})
|
83
|
+
result.should == [@doc1, @doc3]
|
84
|
+
result.total_entries.should == 2
|
85
|
+
result.total_pages.should == 1
|
86
|
+
end
|
87
|
+
end
|
81
88
|
end
|
@@ -1,30 +1,30 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestRailsCompatibility < Test::Unit::TestCase
|
4
|
-
class Item
|
5
|
-
include MongoMapper::EmbeddedDocument
|
6
|
-
key :for_all, String
|
7
|
-
end
|
8
|
-
|
9
|
-
class Order
|
10
|
-
include MongoMapper::Document
|
11
|
-
many :items, :class_name => 'TestRailsCompatibility::Item'
|
12
|
-
key :order_only, String
|
13
|
-
end
|
14
|
-
|
15
|
-
context "Document" do
|
16
|
-
setup do
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
should "have to_param that returns id" do
|
21
|
-
instance = Order.create('_id' => 1234)
|
22
|
-
instance.to_param.should == '1234'
|
23
|
-
end
|
24
|
-
|
25
|
-
should "alias new to new_record?" do
|
26
|
-
instance = Order.new
|
27
|
-
instance.new_record?.should == instance.new?
|
28
|
-
end
|
29
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRailsCompatibility < Test::Unit::TestCase
|
4
|
+
class Item
|
5
|
+
include MongoMapper::EmbeddedDocument
|
6
|
+
key :for_all, String
|
7
|
+
end
|
8
|
+
|
9
|
+
class Order
|
10
|
+
include MongoMapper::Document
|
11
|
+
many :items, :class_name => 'TestRailsCompatibility::Item'
|
12
|
+
key :order_only, String
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Document" do
|
16
|
+
setup do
|
17
|
+
clear_all_collections
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have to_param that returns id" do
|
21
|
+
instance = Order.create('_id' => 1234)
|
22
|
+
instance.to_param.should == '1234'
|
23
|
+
end
|
24
|
+
|
25
|
+
should "alias new to new_record?" do
|
26
|
+
instance = Order.new
|
27
|
+
instance.new_record?.should == instance.new?
|
28
|
+
end
|
29
|
+
end
|
30
30
|
end
|
@@ -1,172 +1,262 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ValidationsTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@document.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
doc
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
@doc.
|
56
|
-
@
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
@doc.
|
62
|
-
@doc.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
should "work with validate_on_create callback" do
|
79
|
-
@document.validate_on_create :action_present
|
80
|
-
|
81
|
-
doc = @document.new
|
82
|
-
doc.action = nil
|
83
|
-
doc.should have_error_on(:action)
|
84
|
-
|
85
|
-
doc.action = 'kick'
|
86
|
-
doc.should_not have_error_on(:action)
|
87
|
-
doc.save
|
88
|
-
|
89
|
-
doc.action = nil
|
90
|
-
doc.should_not have_error_on(:action)
|
91
|
-
end
|
92
|
-
|
93
|
-
should "work with validate_on_update callback" do
|
94
|
-
@document.validate_on_update :action_present
|
95
|
-
|
96
|
-
doc = @document.new
|
97
|
-
doc.action = nil
|
98
|
-
doc.should_not have_error_on(:action)
|
99
|
-
doc.save
|
100
|
-
|
101
|
-
doc.action = nil
|
102
|
-
doc.should have_error_on(:action)
|
103
|
-
|
104
|
-
doc.action = 'kick'
|
105
|
-
doc.should_not have_error_on(:action)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context "validating uniqueness of" do
|
110
|
-
setup do
|
111
|
-
@document = Class.new do
|
112
|
-
include MongoMapper::Document
|
113
|
-
key :name, String
|
114
|
-
validates_uniqueness_of :name
|
115
|
-
end
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
doc
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
should "
|
139
|
-
doc = @document.new("name" => "joe")
|
140
|
-
doc.save.should be_true
|
141
|
-
|
142
|
-
@document \
|
143
|
-
.stubs(:find) \
|
144
|
-
.with(:first, :conditions => {:name => 'joe'}, :limit => 1) \
|
145
|
-
.returns(doc)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationsTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
clear_all_collections
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Saving a new document that is invalid" do
|
9
|
+
setup do
|
10
|
+
@document = Class.new do
|
11
|
+
include MongoMapper::Document
|
12
|
+
key :name, String, :required => true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
should "not insert document" do
|
17
|
+
doc = @document.new
|
18
|
+
doc.save
|
19
|
+
@document.count.should == 0
|
20
|
+
end
|
21
|
+
|
22
|
+
should "populate document's errors" do
|
23
|
+
doc = @document.new
|
24
|
+
doc.errors.size.should == 0
|
25
|
+
doc.save
|
26
|
+
doc.errors.full_messages.should == ["Name can't be empty"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "Saving a document that is invalid (destructive)" do
|
31
|
+
setup do
|
32
|
+
@document = Class.new do
|
33
|
+
include MongoMapper::Document
|
34
|
+
key :name, String, :required => true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
should "raise error" do
|
39
|
+
doc = @document.new
|
40
|
+
lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "Saving an existing document that is invalid" do
|
45
|
+
setup do
|
46
|
+
@document = Class.new do
|
47
|
+
include MongoMapper::Document
|
48
|
+
key :name, String, :required => true
|
49
|
+
end
|
50
|
+
|
51
|
+
@doc = @document.create(:name => 'John Nunemaker')
|
52
|
+
end
|
53
|
+
|
54
|
+
should "not update document" do
|
55
|
+
@doc.name = nil
|
56
|
+
@doc.save
|
57
|
+
@document.find(@doc.id).name.should == 'John Nunemaker'
|
58
|
+
end
|
59
|
+
|
60
|
+
should "populate document's errors" do
|
61
|
+
@doc.name = nil
|
62
|
+
@doc.save
|
63
|
+
@doc.errors.full_messages.should == ["Name can't be empty"]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "Adding validation errors" do
|
68
|
+
setup do
|
69
|
+
@document = Class.new do
|
70
|
+
include MongoMapper::Document
|
71
|
+
key :action, String
|
72
|
+
def action_present
|
73
|
+
errors.add(:action, 'is invalid') if action.blank?
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
should "work with validate_on_create callback" do
|
79
|
+
@document.validate_on_create :action_present
|
80
|
+
|
81
|
+
doc = @document.new
|
82
|
+
doc.action = nil
|
83
|
+
doc.should have_error_on(:action)
|
84
|
+
|
85
|
+
doc.action = 'kick'
|
86
|
+
doc.should_not have_error_on(:action)
|
87
|
+
doc.save
|
88
|
+
|
89
|
+
doc.action = nil
|
90
|
+
doc.should_not have_error_on(:action)
|
91
|
+
end
|
92
|
+
|
93
|
+
should "work with validate_on_update callback" do
|
94
|
+
@document.validate_on_update :action_present
|
95
|
+
|
96
|
+
doc = @document.new
|
97
|
+
doc.action = nil
|
98
|
+
doc.should_not have_error_on(:action)
|
99
|
+
doc.save
|
100
|
+
|
101
|
+
doc.action = nil
|
102
|
+
doc.should have_error_on(:action)
|
103
|
+
|
104
|
+
doc.action = 'kick'
|
105
|
+
doc.should_not have_error_on(:action)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "validating uniqueness of" do
|
110
|
+
setup do
|
111
|
+
@document = Class.new do
|
112
|
+
include MongoMapper::Document
|
113
|
+
key :name, String
|
114
|
+
validates_uniqueness_of :name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
should "not fail if object is new" do
|
119
|
+
doc = @document.new
|
120
|
+
doc.should_not have_error_on(:name)
|
121
|
+
end
|
122
|
+
|
123
|
+
should "not fail when new object is out of scope" do
|
124
|
+
document = Class.new do
|
125
|
+
include MongoMapper::Document
|
126
|
+
key :name
|
127
|
+
key :adult
|
128
|
+
validates_uniqueness_of :name, :scope => :adult
|
129
|
+
end
|
130
|
+
doc = document.new("name" => "joe", :adult => true)
|
131
|
+
doc.save.should be_true
|
132
|
+
|
133
|
+
doc2 = document.new("name" => "joe", :adult => false)
|
134
|
+
doc2.should be_valid
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
should "allow to update an object" do
|
139
|
+
doc = @document.new("name" => "joe")
|
140
|
+
doc.save.should be_true
|
141
|
+
|
142
|
+
@document \
|
143
|
+
.stubs(:find) \
|
144
|
+
.with(:first, :conditions => {:name => 'joe'}, :limit => 1) \
|
145
|
+
.returns(doc)
|
146
|
+
|
147
|
+
doc.name = "joe"
|
148
|
+
doc.valid?.should be_true
|
149
|
+
doc.should_not have_error_on(:name)
|
150
|
+
end
|
151
|
+
|
152
|
+
should "fail if object name is not unique" do
|
153
|
+
doc = @document.new("name" => "joe")
|
154
|
+
doc.save.should be_true
|
155
|
+
|
156
|
+
@document \
|
157
|
+
.stubs(:find) \
|
158
|
+
.with(:first, :conditions => {:name => 'joe'}, :limit => 1) \
|
159
|
+
.returns(doc)
|
160
|
+
|
161
|
+
doc2 = @document.new("name" => "joe")
|
162
|
+
doc2.should have_error_on(:name)
|
163
|
+
end
|
164
|
+
|
165
|
+
context "scoped by a single attribute" do
|
166
|
+
setup do
|
167
|
+
@document = Class.new do
|
168
|
+
include MongoMapper::Document
|
169
|
+
key :name, String
|
170
|
+
key :scope, String
|
171
|
+
validates_uniqueness_of :name, :scope => :scope
|
172
|
+
end
|
173
|
+
@document.collection.clear
|
174
|
+
end
|
175
|
+
|
176
|
+
should "fail if the same name exists in the scope" do
|
177
|
+
doc = @document.new("name" => "joe", "scope" => "one")
|
178
|
+
doc.save.should be_true
|
179
|
+
|
180
|
+
@document \
|
181
|
+
.stubs(:find) \
|
182
|
+
.with(:first, :conditions => {:name => 'joe', :scope => "one"}, :limit => 1) \
|
183
|
+
.returns(doc)
|
184
|
+
|
185
|
+
doc2 = @document.new("name" => "joe", "scope" => "one")
|
186
|
+
doc2.should have_error_on(:name)
|
187
|
+
end
|
188
|
+
|
189
|
+
should "pass if the same name exists in a different scope" do
|
190
|
+
doc = @document.new("name" => "joe", "scope" => "one")
|
191
|
+
doc.save.should be_true
|
192
|
+
|
193
|
+
@document \
|
194
|
+
.stubs(:find) \
|
195
|
+
.with(:first, :conditions => {:name => 'joe', :scope => "two"}, :limit => 1) \
|
196
|
+
.returns(nil)
|
197
|
+
|
198
|
+
doc2 = @document.new("name" => "joe", "scope" => "two")
|
199
|
+
doc2.should_not have_error_on(:name)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "scoped by a multiple attributes" do
|
204
|
+
setup do
|
205
|
+
@document = Class.new do
|
206
|
+
include MongoMapper::Document
|
207
|
+
key :name, String
|
208
|
+
key :first_scope, String
|
209
|
+
key :second_scope, String
|
210
|
+
validates_uniqueness_of :name, :scope => [:first_scope, :second_scope]
|
211
|
+
end
|
212
|
+
@document.collection.clear
|
213
|
+
end
|
214
|
+
|
215
|
+
should "fail if the same name exists in the scope" do
|
216
|
+
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
217
|
+
doc.save.should be_true
|
218
|
+
|
219
|
+
@document \
|
220
|
+
.stubs(:find) \
|
221
|
+
.with(:first, :conditions => {:name => 'joe', :first_scope => "one", :second_scope => "two"}, :limit => 1) \
|
222
|
+
.returns(doc)
|
223
|
+
|
224
|
+
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
225
|
+
doc2.should have_error_on(:name)
|
226
|
+
end
|
227
|
+
|
228
|
+
should "pass if the same name exists in a different scope" do
|
229
|
+
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
230
|
+
doc.save.should be_true
|
231
|
+
|
232
|
+
@document \
|
233
|
+
.stubs(:find) \
|
234
|
+
.with(:first, :conditions => {:name => 'joe', :first_scope => "one", :second_scope => "one"}, :limit => 1) \
|
235
|
+
.returns(nil)
|
236
|
+
|
237
|
+
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "one")
|
238
|
+
doc2.should_not have_error_on(:name)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "validates uniqueness of with :unique shortcut" do
|
244
|
+
should "work" do
|
245
|
+
@document = Class.new do
|
246
|
+
include MongoMapper::Document
|
247
|
+
key :name, String, :unique => true
|
248
|
+
end
|
249
|
+
|
250
|
+
doc = @document.create(:name => 'John')
|
251
|
+
doc.should_not have_error_on(:name)
|
252
|
+
|
253
|
+
@document \
|
254
|
+
.stubs(:find) \
|
255
|
+
.with(:first, :conditions => {:name => 'John'}, :limit => 1) \
|
256
|
+
.returns(doc)
|
257
|
+
|
258
|
+
second_john = @document.create(:name => 'John')
|
259
|
+
second_john.should have_error_on(:name, 'has already been taken')
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|