jnunemaker-mongomapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class SerializationTest < Test::Unit::TestCase
4
+ def setup
5
+ @document = Class.new do
6
+ include MongoMapper::EmbeddedDocument
7
+ key :name, String
8
+ key :age, Integer
9
+ key :awesome, Boolean
10
+ key :preferences, Hash
11
+ key :created_at, Time
12
+ end
13
+
14
+ @instance = @document.new(
15
+ :name => 'John Doe',
16
+ :age => 25,
17
+ :awesome => true,
18
+ :preferences => {:language => 'Ruby'},
19
+ :created_at => Time.now.change(:usec => 0)
20
+ )
21
+ end
22
+
23
+ # [:xml, :json].each do |format|
24
+ [:json].each do |format|
25
+ context format do
26
+ should "be reversable" do
27
+ serialized = @instance.send("to_#{format}")
28
+ unserialized = @document.new.send("from_#{format}", serialized)
29
+
30
+ assert_equal @instance, unserialized
31
+ end
32
+
33
+ should "allow attribute only filtering" do
34
+ serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
35
+ unserialized = @document.new.send("from_#{format}", serialized)
36
+
37
+ assert_equal @instance.name, unserialized.name
38
+ assert_equal @instance.age, unserialized.age
39
+ assert_nil unserialized.awesome
40
+ assert_nil unserialized.created_at
41
+ end
42
+
43
+ should "allow attribute except filtering" do
44
+ serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
45
+ unserialized = @document.new.send("from_#{format}", serialized)
46
+
47
+ assert_nil unserialized.name
48
+ assert_nil unserialized.age
49
+ assert_equal @instance.awesome, unserialized.awesome
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,222 @@
1
+ require 'test_helper'
2
+
3
+ class ValidationsTest < Test::Unit::TestCase
4
+ context "Validations" do
5
+ setup do
6
+ @document = Class.new do
7
+ include MongoMapper::Document
8
+ end
9
+ end
10
+
11
+ context "Validating acceptance of" do
12
+ should "work with validates_acceptance_of macro" do
13
+ @document.key :terms, String
14
+ @document.validates_acceptance_of :terms
15
+ doc = @document.new(:terms => '')
16
+ doc.should have_error_on(:terms)
17
+ doc.terms = '1'
18
+ doc.should_not have_error_on(:terms)
19
+ end
20
+ end
21
+
22
+ context "validating confirmation of" do
23
+ should "work with validates_confirmation_of macro" do
24
+ @document.key :password, String
25
+ @document.validates_confirmation_of :password
26
+ doc = @document.new
27
+ doc.password = 'foobar'
28
+ doc.should have_error_on(:password)
29
+ doc.password_confirmation = 'foobar'
30
+ doc.should_not have_error_on(:password)
31
+ end
32
+ end
33
+
34
+ context "validating format of" do
35
+ should "work with validates_format_of macro" do
36
+ @document.key :name, String
37
+ @document.validates_format_of :name, :with => /.+/
38
+ doc = @document.new
39
+ doc.should have_error_on(:name)
40
+ doc.name = 'John'
41
+ doc.should_not have_error_on(:name)
42
+ end
43
+
44
+ should "work with :format shorcut key" do
45
+ @document.key :name, String, :format => /.+/
46
+ doc = @document.new
47
+ doc.should have_error_on(:name)
48
+ doc.name = 'John'
49
+ doc.should_not have_error_on(:name)
50
+ end
51
+ end
52
+
53
+ context "validating length of" do
54
+ should "work with validates_length_of macro" do
55
+ @document.key :name, String
56
+ @document.validates_length_of :name, :minimum => 5
57
+ doc = @document.new
58
+ doc.should have_error_on(:name)
59
+ end
60
+
61
+ context "with :length => integer shortcut" do
62
+ should "set maximum of integer provided" do
63
+ @document.key :name, String, :length => 5
64
+ doc = @document.new
65
+ doc.name = '123456'
66
+ doc.should have_error_on(:name)
67
+ doc.name = '12345'
68
+ doc.should_not have_error_on(:name)
69
+ end
70
+ end
71
+
72
+ context "with :length => range shortcut" do
73
+ setup do
74
+ @document.key :name, String, :length => 5..7
75
+ end
76
+
77
+ should "set minimum of range min" do
78
+ doc = @document.new
79
+ doc.should have_error_on(:name)
80
+ doc.name = '123456'
81
+ doc.should_not have_error_on(:name)
82
+ end
83
+
84
+ should "set maximum of range max" do
85
+ doc = @document.new
86
+ doc.should have_error_on(:name)
87
+ doc.name = '12345678'
88
+ doc.should have_error_on(:name)
89
+ doc.name = '123456'
90
+ doc.should_not have_error_on(:name)
91
+ end
92
+ end
93
+
94
+ context "with :length => hash shortcut" do
95
+ should "pass options through" do
96
+ @document.key :name, String, :length => {:minimum => 2}
97
+ doc = @document.new
98
+ doc.should have_error_on(:name)
99
+ doc.name = '12'
100
+ doc.should_not have_error_on(:name)
101
+ end
102
+ end
103
+ end # validates_length_of
104
+
105
+ context "Validating numericality of" do
106
+ should "work with validates_numericality_of macro" do
107
+ @document.key :age, Integer
108
+ @document.validates_numericality_of :age
109
+ doc = @document.new
110
+ doc.age = 'String'
111
+ doc.should have_error_on(:age)
112
+ doc.age = 23
113
+ doc.should_not have_error_on(:age)
114
+ end
115
+
116
+ context "with :numeric shortcut" do
117
+ should "work with integer or float" do
118
+ @document.key :weight, Float, :numeric => true
119
+ doc = @document.new
120
+ doc.weight = 'String'
121
+ doc.should have_error_on(:weight)
122
+ doc.weight = 23.0
123
+ doc.should_not have_error_on(:weight)
124
+ doc.weight = 23
125
+ doc.should_not have_error_on(:weight)
126
+ end
127
+ end
128
+
129
+ context "with :numeric shortcut on Integer key" do
130
+ should "only work with integers" do
131
+ @document.key :age, Integer, :numeric => true
132
+ doc = @document.new
133
+ doc.age = 'String'
134
+ doc.should have_error_on(:age)
135
+ doc.age = 23.1
136
+ doc.should have_error_on(:age)
137
+ doc.age = 23
138
+ doc.should_not have_error_on(:age)
139
+ end
140
+ end
141
+ end # numericality of
142
+
143
+ context "validating presence of" do
144
+ should "work with validates_presence_of macro" do
145
+ @document.key :name, String
146
+ @document.validates_presence_of :name
147
+ doc = @document.new
148
+ doc.should have_error_on(:name)
149
+ end
150
+
151
+ should "work with :required shortcut on key definition" do
152
+ @document.key :name, String, :required => true
153
+ doc = @document.new
154
+ doc.should have_error_on(:name)
155
+ end
156
+ end
157
+ end # Validations
158
+
159
+ context "Saving a new document that is invalid" do
160
+ setup do
161
+ @document = Class.new do
162
+ include MongoMapper::Document
163
+ key :name, String, :required => true
164
+ end
165
+
166
+ @document.collection.clear
167
+ end
168
+
169
+ should "not insert document" do
170
+ doc = @document.new
171
+ doc.save
172
+ @document.count.should == 0
173
+ end
174
+
175
+ should "populate document's errors" do
176
+ doc = @document.new
177
+ doc.errors.size.should == 0
178
+ doc.save
179
+ doc.errors.full_messages.should == ["Name can't be empty"]
180
+ end
181
+ end
182
+
183
+ context "Saving a document that is invalid (destructive)" do
184
+ setup do
185
+ @document = Class.new do
186
+ include MongoMapper::Document
187
+ key :name, String, :required => true
188
+ end
189
+
190
+ @document.collection.clear
191
+ end
192
+
193
+ should "raise error" do
194
+ doc = @document.new
195
+ lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
196
+ end
197
+ end
198
+
199
+ context "Saving an existing document that is invalid" do
200
+ setup do
201
+ @document = Class.new do
202
+ include MongoMapper::Document
203
+ key :name, String, :required => true
204
+ end
205
+
206
+ @document.collection.clear
207
+ @doc = @document.create(:name => 'John Nunemaker')
208
+ end
209
+
210
+ should "not update document" do
211
+ @doc.name = nil
212
+ @doc.save
213
+ @document.find(@doc.id).name.should == 'John Nunemaker'
214
+ end
215
+
216
+ should "populate document's errors" do
217
+ @doc.name = nil
218
+ @doc.save
219
+ @doc.errors.full_messages.should == ["Name can't be empty"]
220
+ end
221
+ end
222
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jnunemaker-mongomapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongodb-mongo
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: jnunemaker-validatable
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: mocha
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: jnunemaker-matchy
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "="
62
+ - !ruby/object:Gem::Version
63
+ version: 0.4.0
64
+ version:
65
+ - !ruby/object:Gem::Dependency
66
+ name: thoughtbot-quietbacktrace
67
+ type: :development
68
+ version_requirement:
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ description:
76
+ email: nunemaker@gmail.com
77
+ executables: []
78
+
79
+ extensions: []
80
+
81
+ extra_rdoc_files:
82
+ - LICENSE
83
+ - README.rdoc
84
+ files:
85
+ - .gitignore
86
+ - History
87
+ - LICENSE
88
+ - README.rdoc
89
+ - Rakefile
90
+ - VERSION
91
+ - lib/mongomapper.rb
92
+ - lib/mongomapper/document.rb
93
+ - lib/mongomapper/embedded_document.rb
94
+ - lib/mongomapper/finder_options.rb
95
+ - lib/mongomapper/key.rb
96
+ - lib/mongomapper/rails_compatibility.rb
97
+ - lib/mongomapper/save_with_validation.rb
98
+ - lib/mongomapper/serialization.rb
99
+ - lib/mongomapper/serializers/json_serializer.rb
100
+ - test/serializers/test_json_serializer.rb
101
+ - test/test_associations.rb
102
+ - test/test_callbacks.rb
103
+ - test/test_document.rb
104
+ - test/test_embedded_document.rb
105
+ - test/test_finder_options.rb
106
+ - test/test_helper.rb
107
+ - test/test_key.rb
108
+ - test/test_mongo_mapper.rb
109
+ - test/test_rails_compatibility.rb
110
+ - test/test_serializations.rb
111
+ - test/test_validations.rb
112
+ has_rdoc: true
113
+ homepage: http://github.com/jnunemaker/mongomapper
114
+ post_install_message:
115
+ rdoc_options:
116
+ - --charset=UTF-8
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: "0"
124
+ version:
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: "0"
130
+ version:
131
+ requirements: []
132
+
133
+ rubyforge_project: mongomapper
134
+ rubygems_version: 1.2.0
135
+ signing_key:
136
+ specification_version: 2
137
+ summary: Awesome gem for modeling your domain and storing it in mongo
138
+ test_files:
139
+ - test/serializers/test_json_serializer.rb
140
+ - test/test_associations.rb
141
+ - test/test_callbacks.rb
142
+ - test/test_document.rb
143
+ - test/test_embedded_document.rb
144
+ - test/test_finder_options.rb
145
+ - test/test_helper.rb
146
+ - test/test_key.rb
147
+ - test/test_mongo_mapper.rb
148
+ - test/test_rails_compatibility.rb
149
+ - test/test_serializations.rb
150
+ - test/test_validations.rb