validates_serialized 0.0.1.pre2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ActiveModel::Validations::ArrayValidator do
5
+ context "#validates_array_values" do
6
+ class ValidatorArrayTestOne
7
+ include ActiveModel::Validations
8
+ validates_array_values :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }
9
+
10
+ def initialize(h={})
11
+ h.each {|k,v| send("#{k}=",v)}
12
+ end
13
+
14
+ def my_attr
15
+ @my_attr ||= []
16
+ end
17
+
18
+ def my_attr=(val)
19
+ @my_attr = val
20
+ end
21
+ end
22
+
23
+ describe "#validate" do
24
+ it "does not raise error for valid value" do
25
+ record = ValidatorArrayTestOne.new(my_attr: [2])
26
+ record.valid?
27
+ record.errors[:my_attr].should eq([])
28
+ end
29
+
30
+ it "adds error for invalid value" do
31
+ record = ValidatorArrayTestOne.new(my_attr: [1, 5])
32
+ record.valid?
33
+ record.errors[:my_attr].should eq(["is not included in the list"])
34
+ end
35
+
36
+ it "adds multiple errors for invalid value" do
37
+ record = ValidatorArrayTestOne.new(my_attr: [nil, 1, 7])
38
+ record.valid?
39
+ record.errors[:my_attr].should eq(["can't be blank", "is not included in the list", "is not included in the list"])
40
+ end
41
+
42
+ it "raises error for non-array" do
43
+ record = ValidatorArrayTestOne.new(my_attr: 4)
44
+ expect { record.valid? }.to raise_error(TypeError, '4 is not an Array')
45
+ end
46
+ end
47
+ end
48
+
49
+ context "#validates_array_values!" do
50
+ class ValidatorArrayTestStrict
51
+ include ActiveModel::Validations
52
+ validates_array_values! :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }
53
+
54
+ def initialize(h={})
55
+ h.each {|k,v| send("#{k}=",v)}
56
+ end
57
+
58
+ def my_attr
59
+ @my_attr ||= []
60
+ end
61
+
62
+ def my_attr=(val)
63
+ @my_attr = val
64
+ end
65
+ end
66
+
67
+ describe "#validate" do
68
+ it "does not raise error for valid value" do
69
+ record = ValidatorArrayTestStrict.new(my_attr: [2])
70
+ record.valid?
71
+ record.errors[:my_attr].should eq([])
72
+ end
73
+
74
+ it "raises error for invalid value" do
75
+ record = ValidatorArrayTestStrict.new(my_attr: [1, 5])
76
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'My attr is not included in the list')
77
+ end
78
+
79
+ it "raises error for multiple invalid value" do
80
+ record = ValidatorArrayTestStrict.new(my_attr: [nil, 1, 7])
81
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "My attr can't be blank")
82
+ end
83
+
84
+ it "raises error for non-array" do
85
+ record = ValidatorArrayTestStrict.new(my_attr: 4)
86
+ expect { record.valid? }.to raise_error(TypeError, '4 is not an Array')
87
+ end
88
+ end
89
+ end
90
+
91
+ context "with class #validates_array_values_with" do
92
+ class ValidatorClassTest
93
+ include ActiveModel::Validations
94
+ validates_array_values_with ::ActiveModel::Validations::PresenceValidator, attributes: [:my_attr]
95
+
96
+ def initialize(h={})
97
+ h.each {|k,v| send("#{k}=",v)}
98
+ end
99
+
100
+ def my_attr
101
+ @my_attr ||= []
102
+ end
103
+
104
+ def my_attr=(val)
105
+ @my_attr = val
106
+ end
107
+ end
108
+
109
+ describe "#validate" do
110
+ it "does not raise error for valid value" do
111
+ record = ValidatorClassTest.new(my_attr: [2])
112
+ record.valid?
113
+ record.errors[:my_attr].should eq([])
114
+ end
115
+
116
+ it "adds error for invalid value" do
117
+ record = ValidatorClassTest.new(my_attr: [1, nil])
118
+ record.valid?
119
+ record.errors[:my_attr].should eq(["can't be blank"])
120
+ end
121
+
122
+ it "adds multiple errors for invalid value" do
123
+ record = ValidatorClassTest.new(my_attr: [nil, 1, nil])
124
+ record.valid?
125
+ record.errors[:my_attr].should eq(["can't be blank", "can't be blank"])
126
+ end
127
+
128
+ it "raises error for non-array" do
129
+ record = ValidatorClassTest.new(my_attr: 4)
130
+ expect { record.valid? }.to raise_error(TypeError, '4 is not an Array')
131
+ end
132
+ end
133
+ end
134
+
135
+ context "with instance #validates_array_values_with" do
136
+ class ValidatorInstanceTest
137
+ include ActiveModel::Validations
138
+
139
+ validate :instance_validations
140
+ def initialize(h={})
141
+ h.each {|k,v| send("#{k}=",v)}
142
+ end
143
+
144
+ def my_attr
145
+ @my_attr ||= []
146
+ end
147
+
148
+ def my_attr=(val)
149
+ @my_attr = val
150
+ end
151
+
152
+ def instance_validations
153
+ validates_array_values_with ::ActiveModel::Validations::PresenceValidator, attributes: [:my_attr]
154
+ end
155
+ end
156
+
157
+ describe "#validate", failing: true do
158
+ it "does not raise error for valid value" do
159
+ record = ValidatorInstanceTest.new(my_attr: [2])
160
+ record.valid?
161
+ record.errors[:my_attr].should eq([])
162
+ end
163
+
164
+ it "adds error for invalid value" do
165
+ record = ValidatorInstanceTest.new(my_attr: [1, nil])
166
+ record.valid?
167
+ record.errors[:my_attr].should eq(["can't be blank"])
168
+ end
169
+
170
+ it "adds multiple errors for invalid value" do
171
+ record = ValidatorInstanceTest.new(my_attr: [nil, 1, nil])
172
+ record.valid?
173
+ record.errors[:my_attr].should eq(["can't be blank", "can't be blank"])
174
+ end
175
+
176
+ it "raises error for non-array" do
177
+ record = ValidatorInstanceTest.new(my_attr: 4)
178
+ expect { record.valid? }.to raise_error(TypeError, '4 is not an Array')
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ActiveModel::Validations::HashBlockValidator do
5
+ context "#validates_hash_values" do
6
+ class ValidatorBlockHashTestOne
7
+ include ActiveModel::Validations
8
+
9
+ def initialize(h={})
10
+ h.each {|k,v| send("#{k}=",v)}
11
+ end
12
+
13
+ def my_attr
14
+ @my_attr ||= {}
15
+ end
16
+
17
+ def my_attr=(val)
18
+ @my_attr = val
19
+ end
20
+
21
+ validates_hash_keys :my_attr do
22
+ validates :first_key, presence: true
23
+ validates :second_key, inclusion: { in: [1, 2, 3, 4] }
24
+ end
25
+ end
26
+
27
+ describe "#validate" do
28
+ it "does not raise error for valid value" do
29
+ record = ValidatorBlockHashTestOne.new(my_attr: { first_key: 2, second_key: 4 })
30
+ record.valid?
31
+ record.errors[:my_attr].should eq([])
32
+ end
33
+
34
+ it "adds error for invalid value" do
35
+ record = ValidatorBlockHashTestOne.new(my_attr: { first_key: nil, second_key: 4 })
36
+ record.valid?
37
+ record.errors[:my_attr].should eq(["first_key can't be blank"])
38
+ end
39
+
40
+ it "adds multiple errors for multiple invalid value" do
41
+ record = ValidatorBlockHashTestOne.new(my_attr: { first_key: nil, second_key: 6 })
42
+ record.valid?
43
+ record.errors[:my_attr].should eq(["first_key can't be blank", "second_key is not included in the list"])
44
+ end
45
+
46
+ it "raises error for non-array" do
47
+ record = ValidatorBlockHashTestOne.new(my_attr: 4)
48
+ expect { record.valid? }.to raise_error(TypeError, 'my_attr is not a Hash')
49
+ end
50
+ end
51
+ end
52
+
53
+ context "#validates_hash_keys!" do
54
+ class ValidatorBlockHashTestStrict
55
+ include ActiveModel::Validations
56
+
57
+ def initialize(h={})
58
+ h.each {|k,v| send("#{k}=",v)}
59
+ end
60
+
61
+ def my_attr
62
+ @my_attr ||= {}
63
+ end
64
+
65
+ def my_attr=(val)
66
+ @my_attr = val
67
+ end
68
+
69
+ validates_hash_keys! :my_attr do
70
+ validates :first_key, presence: true
71
+ validates :second_key, inclusion: { in: [1, 2, 3, 4] }
72
+ end
73
+ end
74
+
75
+ describe "#validate" do
76
+ it "does not raise error for valid value" do
77
+ record = ValidatorBlockHashTestStrict.new(my_attr: {first_key: 2, second_key: 3})
78
+ record.valid?
79
+ record.errors[:my_attr].should eq([])
80
+ end
81
+
82
+ it "raises error for invalid value" do
83
+ record = ValidatorBlockHashTestStrict.new(my_attr: {first_key: 2, second_key: 5})
84
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'second_key is not included in the list')
85
+ end
86
+
87
+ it "raises error for multiple invalid value" do
88
+ record = ValidatorBlockHashTestStrict.new(my_attr: {first_key: nil, second_key: 9})
89
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "first_key can't be blank")
90
+ end
91
+
92
+ it "raises error for non-array" do
93
+ record = ValidatorBlockHashTestStrict.new(my_attr: 4)
94
+ expect { record.valid? }.to raise_error(TypeError, 'my_attr is not a Hash')
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ActiveModel::Validations::HashValidator do
5
+ context "#validates_hash_values" do
6
+ class ValidatorHashTestOne
7
+ include ActiveModel::Validations
8
+
9
+ def initialize(h={})
10
+ h.each {|k,v| send("#{k}=",v)}
11
+ end
12
+
13
+ def my_attr
14
+ @my_attr ||= []
15
+ end
16
+
17
+ def my_attr=(val)
18
+ @my_attr = val
19
+ end
20
+
21
+ validates_hash_values :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }
22
+ end
23
+
24
+ describe "#validate" do
25
+ it "does not raise error for valid value" do
26
+ record = ValidatorHashTestOne.new(my_attr: { a: 2 })
27
+ record.valid?
28
+ record.errors[:my_attr].should eq([])
29
+ end
30
+
31
+ it "adds error for invalid value" do
32
+ record = ValidatorHashTestOne.new(my_attr: { a: 1, b: 5 })
33
+ record.valid?
34
+ record.errors[:my_attr].should eq(["is not included in the list"])
35
+ end
36
+
37
+ it "adds multiple errors for invalid value" do
38
+ record = ValidatorHashTestOne.new(my_attr: { a: nil, b: 1, c: 7 })
39
+ record.valid?
40
+ record.errors[:my_attr].should eq(["can't be blank", "is not included in the list", "is not included in the list"])
41
+ end
42
+
43
+ it "raises error for non-array" do
44
+ record = ValidatorHashTestOne.new(my_attr: 4)
45
+ expect { record.valid? }.to raise_error(TypeError, '4 is not a Hash')
46
+ end
47
+ end
48
+ end
49
+
50
+ context "#validates_hash_values!" do
51
+ class ValidatorHashTestStrict
52
+ include ActiveModel::Validations
53
+ validates_hash_values! :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }
54
+
55
+ def initialize(h={})
56
+ h.each {|k,v| send("#{k}=",v)}
57
+ end
58
+
59
+ def my_attr
60
+ @my_attr ||= []
61
+ end
62
+
63
+ def my_attr=(val)
64
+ @my_attr = val
65
+ end
66
+ end
67
+
68
+ describe "#validate" do
69
+ it "does not raise error for valid value" do
70
+ record = ValidatorHashTestStrict.new(my_attr: {a: 2})
71
+ record.valid?
72
+ record.errors[:my_attr].should eq([])
73
+ end
74
+
75
+ it "raises error for invalid value" do
76
+ record = ValidatorHashTestStrict.new(my_attr: {a: 1, b: 5})
77
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'My attr is not included in the list')
78
+ end
79
+
80
+ it "raises error for multiple invalid value" do
81
+ record = ValidatorHashTestStrict.new(my_attr: {a: nil, b: 1, c: 7})
82
+ expect { record.valid? }.to raise_error(ActiveModel::StrictValidationFailed, "My attr can't be blank")
83
+ end
84
+
85
+ it "raises error for non-array" do
86
+ record = ValidatorHashTestStrict.new(my_attr: 4)
87
+ expect { record.valid? }.to raise_error(TypeError, '4 is not a Hash')
88
+ end
89
+ end
90
+ end
91
+
92
+ context "with class #validates_hash_values_with" do
93
+ class ValidatorHashClassTest
94
+ include ActiveModel::Validations
95
+ validates_hash_values_with ::ActiveModel::Validations::PresenceValidator, attributes: [:my_attr]
96
+
97
+ def initialize(h={})
98
+ h.each {|k,v| send("#{k}=",v)}
99
+ end
100
+
101
+ def my_attr
102
+ @my_attr ||= []
103
+ end
104
+
105
+ def my_attr=(val)
106
+ @my_attr = val
107
+ end
108
+ end
109
+
110
+ describe "#validate" do
111
+ it "does not raise error for valid value" do
112
+ record = ValidatorHashClassTest.new(my_attr: {a: 2})
113
+ record.valid?
114
+ record.errors[:my_attr].should eq([])
115
+ end
116
+
117
+ it "adds error for invalid value" do
118
+ record = ValidatorHashClassTest.new(my_attr: {a: 1, b: nil})
119
+ record.valid?
120
+ record.errors[:my_attr].should eq(["can't be blank"])
121
+ end
122
+
123
+ it "adds multiple errors for invalid value" do
124
+ record = ValidatorHashClassTest.new(my_attr: {a: nil, b: 1, c: nil})
125
+ record.valid?
126
+ record.errors[:my_attr].should eq(["can't be blank", "can't be blank"])
127
+ end
128
+
129
+ it "raises error for non-array" do
130
+ record = ValidatorHashClassTest.new(my_attr: 4)
131
+ expect { record.valid? }.to raise_error(TypeError, '4 is not a Hash')
132
+ end
133
+ end
134
+ end
135
+
136
+ context "with instance #validates_hash_values_with" do
137
+ class ValidatorHashInstanceTest
138
+ include ActiveModel::Validations
139
+
140
+ validate :instance_validations
141
+ def initialize(h={})
142
+ h.each {|k,v| send("#{k}=",v)}
143
+ end
144
+
145
+ def my_attr
146
+ @my_attr ||= []
147
+ end
148
+
149
+ def my_attr=(val)
150
+ @my_attr = val
151
+ end
152
+
153
+ def instance_validations
154
+ validates_hash_values_with ::ActiveModel::Validations::PresenceValidator, attributes: [:my_attr]
155
+ end
156
+ end
157
+
158
+ describe "#validate" do
159
+ it "does not raise error for valid value" do
160
+ record = ValidatorHashInstanceTest.new(my_attr: {a: 2})
161
+ record.valid?
162
+ record.errors[:my_attr].should eq([])
163
+ end
164
+
165
+ it "adds error for invalid value" do
166
+ record = ValidatorHashInstanceTest.new(my_attr: {a: 1, b: nil})
167
+ record.valid?
168
+ record.errors[:my_attr].should eq(["can't be blank"])
169
+ end
170
+
171
+ it "adds multiple errors for invalid value" do
172
+ record = ValidatorHashInstanceTest.new(my_attr: {a: nil, b: 1, c: nil})
173
+ record.valid?
174
+ record.errors[:my_attr].should eq(["can't be blank", "can't be blank"])
175
+ end
176
+
177
+ it "raises error for non-array" do
178
+ record = ValidatorHashInstanceTest.new(my_attr: 4)
179
+ expect { record.valid? }.to raise_error(TypeError, '4 is not a Hash')
180
+ end
181
+ end
182
+ end
183
+ end