-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathnumericality_validator_spec.rb
More file actions
169 lines (146 loc) · 4.41 KB
/
numericality_validator_spec.rb
File metadata and controls
169 lines (146 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'spec_helper'
describe Volt::Model do
let(:buffer) { model.buffer }
let(:model) { test_model_class.new }
let(:test_model_class) do
Class.new(Volt::Model) do
validate :count, numericality: { gt: 5, lt: 10 }
end
end
it 'should return errors for all failed validations' do
model.validate!
expect(model.errors).to eq(
count: ['must be a number']
)
end
it 'should show all fields in marked errors once saved' do
buffer.save!
expect(buffer.marked_errors.keys).to eq(
[:count]
)
end
describe 'builtin validations' do
shared_examples_for 'a built in validation' do |field, message|
specify do
expect { model.mark_field! field }
.to change { model.marked_errors }
.from({}).to(field => [message])
end
end
describe 'numericality' do
message = 'must be a number'
it_should_behave_like 'a built in validation', :count, message
end
end
describe 'range conditions' do
describe 'gt: and lt:' do
let(:test_model_class) do
Class.new(Volt::Model) do
validate :count, numericality: { gt: 5, lt: 10 }
end
end
describe 'gt:' do
it 'fails for values less than or equal to specified' do
buffer._count = 5
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be greater than 5']
)
end
it 'passes for values greater than specified' do
buffer._count = 5.1
buffer.validate!
expect(buffer.errors).to eq({})
end
end
describe 'lt:' do
it 'fails for values greater than or equal to specified' do
buffer._count = 10
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be less than 10']
)
end
it 'passes for values less than specified' do
buffer._count = 9.9
buffer.validate!
expect(buffer.errors).to eq({})
end
end
end
describe 'gte: and lte:' do
let(:test_model_class) do
Class.new(Volt::Model) do
validate :count, numericality: { gte: 5, lte: 10 }
end
end
describe 'gte:' do
it 'fails for values less than specified' do
buffer._count = 4.9
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be greater than or equal to 5']
)
end
it 'passes for values equal to or greater than specified' do
buffer._count = 5
buffer.validate!
expect(buffer.errors).to eq({})
end
end
describe 'lte:' do
it 'fails for values greater than specified' do
buffer._count = 10.1
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be less than or equal to 10']
)
end
it 'passes for values equal to or less than specified' do
buffer._count = 10
buffer.validate!
expect(buffer.errors).to eq({})
end
end
end
describe 'deprecated conditons' do
let(:test_model_class) do
Class.new(Volt::Model) do
validate :count, numericality: { min: 5, max: 10 }
end
end
describe 'min:' do
it 'fails for values less than specified' do
expect(Volt.logger).to receive(:warn).at_least(:once)
buffer._count = 4.9
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be greater than 5']
)
end
it 'passes for values equal to or greater than specified' do
expect(Volt.logger).to receive(:warn).at_least(:once)
buffer._count = 5
buffer.validate!
expect(buffer.errors).to eq({})
end
end
describe 'max:' do
it 'fails for values greater than specified' do
expect(Volt.logger).to receive(:warn).at_least(:once)
buffer._count = 10.1
buffer.validate!
expect(buffer.errors).to eq(
count: ['number must be less than 10']
)
end
it 'passes for values equal to or less than specified' do
expect(Volt.logger).to receive(:warn).at_least(:once)
buffer._count = 10
buffer.validate!
expect(buffer.errors).to eq({})
end
end
end
end
end