ohm 1.4.0 → 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
data/test/validations.rb DELETED
@@ -1,195 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require File.expand_path("./helper", File.dirname(__FILE__))
4
-
5
- class Event < Ohm::Model
6
- attribute :name
7
- attribute :place
8
- attribute :capacity
9
-
10
- index :name
11
- index :place
12
-
13
- def validate
14
- assert_format(:name, /^\w+$/)
15
- end
16
- end
17
-
18
- class Validatable
19
- attr_accessor :name
20
-
21
- include Scrivener::Validations
22
- end
23
-
24
- # A new model with validations
25
- scope do
26
- setup do
27
- Event.new
28
- end
29
-
30
- # That must have a present name
31
- scope do
32
- test "not be created if the name is never assigned" do |event|
33
- event.save
34
- assert event.new?
35
- end
36
-
37
- test "not be created if the name assigned is empty" do |event|
38
- event.name = ""
39
- event.save
40
- assert event.new?
41
- end
42
-
43
- test "be created if the name assigned is not empty" do |event|
44
- event.name = "hello"
45
- event.save
46
- assert event.id
47
- end
48
-
49
- # And must have a name with only \w+
50
- scope do
51
- test "not be created if the name doesn't match /^\w+$/" do |event|
52
- event.name = "hello-world"
53
- event.save
54
- assert event.new?
55
- end
56
- end
57
- end
58
-
59
- # That must have a numeric attribute :capacity
60
- scope do
61
- test "fail when the value is nil" do |event|
62
- def event.validate
63
- assert_numeric :capacity
64
- end
65
-
66
- event.name = "foo"
67
- event.place = "bar"
68
- event.save
69
-
70
- assert event.new?
71
- assert_equal({:capacity => [:not_numeric]}, event.errors)
72
- end
73
-
74
- test "fail when the value is not numeric" do |event|
75
- def event.validate
76
- assert_numeric :capacity
77
- end
78
-
79
- event.name = "foo"
80
- event.place = "bar"
81
- event.capacity = "baz"
82
- event.save
83
-
84
- assert event.new?
85
- assert_equal({:capacity => [:not_numeric]}, event.errors)
86
- end
87
-
88
- test "succeed when the value is numeric" do |event|
89
- def event.validate
90
- assert_numeric :capacity
91
- end
92
-
93
- event.name = "foo"
94
- event.place = "bar"
95
- event.capacity = 42
96
- event.save
97
-
98
- assert event.id
99
- end
100
- end
101
- end
102
-
103
- # An existing model with a valid name
104
- scope do
105
- setup do
106
- Event.create(:name => "original")
107
- end
108
-
109
- # That has the name changed
110
- scope do
111
- test "not be saved if the new name is nil" do |event|
112
- event.name = nil
113
- event.save
114
- assert false == event.valid?
115
- assert "original" == Event[event.id].name
116
- end
117
-
118
- test "not be saved if the name assigned is empty" do |event|
119
- event.name = ""
120
- event.save
121
- assert false == event.valid?
122
- assert "original" == Event[event.id].name
123
- end
124
-
125
- test "be saved if the name assigned is not empty" do |event|
126
- event.name = "hello"
127
- event.save
128
- assert event.valid?
129
- assert "hello" == Event[event.id].name
130
- end
131
- end
132
- end
133
-
134
- # Validations module
135
- scope do
136
- setup do
137
- Validatable.new
138
- end
139
-
140
- # assert
141
- scope do
142
- test "add errors to a collection" do |target|
143
- def target.validate
144
- assert(false, ["Attribute", "Something bad"])
145
- end
146
-
147
- target.validate
148
-
149
- assert_equal({ "Attribute" => ["Something bad"] }, target.errors)
150
- end
151
-
152
- test "allow for nested validations" do |target|
153
- def target.validate
154
- if assert(true, ["Attribute", "No error"])
155
- assert(false, ["Attribute", "Chained error"])
156
- end
157
-
158
- if assert(false, ["Attribute", "Parent error"])
159
- assert(false, ["Attribute", "No chained error"])
160
- end
161
- end
162
-
163
- target.validate
164
-
165
- expected = {"Attribute"=>["Chained error", "Parent error"]}
166
- assert_equal expected, target.errors
167
- end
168
- end
169
-
170
- # assert_present
171
- scope do
172
- setup do
173
- target = Validatable.new
174
-
175
- def target.validate
176
- assert_present(:name)
177
- end
178
-
179
- target
180
- end
181
-
182
- test "fail when the attribute is nil" do |target|
183
- target.validate
184
-
185
- assert_equal({ :name => [:not_present] }, target.errors)
186
- end
187
-
188
- test "fail when the attribute is empty" do |target|
189
- target.name = ""
190
- target.validate
191
-
192
- assert_equal({ :name => [:not_present] }, target.errors)
193
- end
194
- end
195
- end