##// END OF EJS Templates
Fixed broken test....
Jean-Philippe Lang -
r9892:8fa787719ab4
parent child
Show More
@@ -1,215 +1,215
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class CustomFieldTest < ActiveSupport::TestCase
20 class CustomFieldTest < ActiveSupport::TestCase
21 fixtures :custom_fields
21 fixtures :custom_fields
22
22
23 def test_create
23 def test_create
24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float')
24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float')
25 assert field.save
25 assert field.save
26 end
26 end
27
27
28 def test_before_validation
28 def test_before_validation
29 field = CustomField.new(:name => 'test_before_validation', :field_format => 'int')
29 field = CustomField.new(:name => 'test_before_validation', :field_format => 'int')
30 field.searchable = true
30 field.searchable = true
31 assert field.save
31 assert field.save
32 assert_equal false, field.searchable
32 assert_equal false, field.searchable
33 field.searchable = true
33 field.searchable = true
34 assert field.save
34 assert field.save
35 assert_equal false, field.searchable
35 assert_equal false, field.searchable
36 end
36 end
37
37
38 def test_regexp_validation
38 def test_regexp_validation
39 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
39 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9')
40 assert !field.save
40 assert !field.save
41 assert_include I18n.t('activerecord.errors.messages.invalid'),
41 assert_include I18n.t('activerecord.errors.messages.invalid'),
42 field.errors[:regexp]
42 field.errors[:regexp]
43 field.regexp = '[a-z0-9]'
43 field.regexp = '[a-z0-9]'
44 assert field.save
44 assert field.save
45 end
45 end
46
46
47 def test_default_value_should_be_validated
47 def test_default_value_should_be_validated
48 field = CustomField.new(:name => 'Test', :field_format => 'int')
48 field = CustomField.new(:name => 'Test', :field_format => 'int')
49 field.default_value = 'abc'
49 field.default_value = 'abc'
50 assert !field.valid?
50 assert !field.valid?
51 field.default_value = '6'
51 field.default_value = '6'
52 assert field.valid?
52 assert field.valid?
53 end
53 end
54
54
55 def test_default_value_should_not_be_validated_when_blank
55 def test_default_value_should_not_be_validated_when_blank
56 field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '')
56 field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '')
57 assert field.valid?
57 assert field.valid?
58 end
58 end
59
59
60 def test_possible_values_should_accept_an_array
60 def test_possible_values_should_accept_an_array
61 field = CustomField.new
61 field = CustomField.new
62 field.possible_values = ["One value", ""]
62 field.possible_values = ["One value", ""]
63 assert_equal ["One value"], field.possible_values
63 assert_equal ["One value"], field.possible_values
64 end
64 end
65
65
66 def test_possible_values_should_accept_a_string
66 def test_possible_values_should_accept_a_string
67 field = CustomField.new
67 field = CustomField.new
68 field.possible_values = "One value"
68 field.possible_values = "One value"
69 assert_equal ["One value"], field.possible_values
69 assert_equal ["One value"], field.possible_values
70 end
70 end
71
71
72 def test_possible_values_should_accept_a_multiline_string
72 def test_possible_values_should_accept_a_multiline_string
73 field = CustomField.new
73 field = CustomField.new
74 field.possible_values = "One value\nAnd another one \r\n \n"
74 field.possible_values = "One value\nAnd another one \r\n \n"
75 assert_equal ["One value", "And another one"], field.possible_values
75 assert_equal ["One value", "And another one"], field.possible_values
76 end
76 end
77
77
78 if "string".respond_to?(:encoding)
78 if "string".respond_to?(:encoding)
79 def test_possible_values_stored_as_binary_should_be_utf8_encoded
79 def test_possible_values_stored_as_binary_should_be_utf8_encoded
80 field = CustomField.find(11)
80 field = CustomField.find(11)
81 assert_kind_of Array, field.possible_values
81 assert_kind_of Array, field.possible_values
82 assert field.possible_values.size > 0
82 assert field.possible_values.size > 0
83 field.possible_values.each do |value|
83 field.possible_values.each do |value|
84 assert_equal "UTF-8", value.encoding.name
84 assert_equal "UTF-8", value.encoding.name
85 end
85 end
86 end
86 end
87 end
87 end
88
88
89 def test_destroy
89 def test_destroy
90 field = CustomField.find(1)
90 field = CustomField.find(1)
91 assert field.destroy
91 assert field.destroy
92 end
92 end
93
93
94 def test_new_subclass_instance_should_return_an_instance
94 def test_new_subclass_instance_should_return_an_instance
95 f = CustomField.new_subclass_instance('IssueCustomField')
95 f = CustomField.new_subclass_instance('IssueCustomField')
96 assert_kind_of IssueCustomField, f
96 assert_kind_of IssueCustomField, f
97 end
97 end
98
98
99 def test_new_subclass_instance_should_set_attributes
99 def test_new_subclass_instance_should_set_attributes
100 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test')
100 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test')
101 assert_kind_of IssueCustomField, f
101 assert_kind_of IssueCustomField, f
102 assert_equal 'Test', f.name
102 assert_equal 'Test', f.name
103 end
103 end
104
104
105 def test_new_subclass_instance_with_invalid_class_name_should_return_nil
105 def test_new_subclass_instance_with_invalid_class_name_should_return_nil
106 assert_nil CustomField.new_subclass_instance('WrongClassName')
106 assert_nil CustomField.new_subclass_instance('WrongClassName')
107 end
107 end
108
108
109 def test_new_subclass_instance_with_non_subclass_name_should_return_nil
109 def test_new_subclass_instance_with_non_subclass_name_should_return_nil
110 assert_nil CustomField.new_subclass_instance('Project')
110 assert_nil CustomField.new_subclass_instance('Project')
111 end
111 end
112
112
113 def test_string_field_validation_with_blank_value
113 def test_string_field_validation_with_blank_value
114 f = CustomField.new(:field_format => 'string')
114 f = CustomField.new(:field_format => 'string')
115
115
116 assert f.valid_field_value?(nil)
116 assert f.valid_field_value?(nil)
117 assert f.valid_field_value?('')
117 assert f.valid_field_value?('')
118
118
119 f.is_required = true
119 f.is_required = true
120 assert !f.valid_field_value?(nil)
120 assert !f.valid_field_value?(nil)
121 assert !f.valid_field_value?('')
121 assert !f.valid_field_value?('')
122 end
122 end
123
123
124 def test_string_field_validation_with_min_and_max_lengths
124 def test_string_field_validation_with_min_and_max_lengths
125 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
125 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
126
126
127 assert f.valid_field_value?(nil)
127 assert f.valid_field_value?(nil)
128 assert f.valid_field_value?('')
128 assert f.valid_field_value?('')
129 assert f.valid_field_value?('a' * 2)
129 assert f.valid_field_value?('a' * 2)
130 assert !f.valid_field_value?('a')
130 assert !f.valid_field_value?('a')
131 assert !f.valid_field_value?('a' * 6)
131 assert !f.valid_field_value?('a' * 6)
132 end
132 end
133
133
134 def test_string_field_validation_with_regexp
134 def test_string_field_validation_with_regexp
135 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
135 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
136
136
137 assert f.valid_field_value?(nil)
137 assert f.valid_field_value?(nil)
138 assert f.valid_field_value?('')
138 assert f.valid_field_value?('')
139 assert f.valid_field_value?('ABC')
139 assert f.valid_field_value?('ABC')
140 assert !f.valid_field_value?('abc')
140 assert !f.valid_field_value?('abc')
141 end
141 end
142
142
143 def test_date_field_validation
143 def test_date_field_validation
144 f = CustomField.new(:field_format => 'date')
144 f = CustomField.new(:field_format => 'date')
145
145
146 assert f.valid_field_value?(nil)
146 assert f.valid_field_value?(nil)
147 assert f.valid_field_value?('')
147 assert f.valid_field_value?('')
148 assert f.valid_field_value?('1975-07-14')
148 assert f.valid_field_value?('1975-07-14')
149 assert !f.valid_field_value?('1975-07-33')
149 assert !f.valid_field_value?('1975-07-33')
150 assert !f.valid_field_value?('abc')
150 assert !f.valid_field_value?('abc')
151 end
151 end
152
152
153 def test_list_field_validation
153 def test_list_field_validation
154 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
154 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
155
155
156 assert f.valid_field_value?(nil)
156 assert f.valid_field_value?(nil)
157 assert f.valid_field_value?('')
157 assert f.valid_field_value?('')
158 assert f.valid_field_value?('value2')
158 assert f.valid_field_value?('value2')
159 assert !f.valid_field_value?('abc')
159 assert !f.valid_field_value?('abc')
160 end
160 end
161
161
162 def test_int_field_validation
162 def test_int_field_validation
163 f = CustomField.new(:field_format => 'int')
163 f = CustomField.new(:field_format => 'int')
164
164
165 assert f.valid_field_value?(nil)
165 assert f.valid_field_value?(nil)
166 assert f.valid_field_value?('')
166 assert f.valid_field_value?('')
167 assert f.valid_field_value?('123')
167 assert f.valid_field_value?('123')
168 assert f.valid_field_value?('+123')
168 assert f.valid_field_value?('+123')
169 assert f.valid_field_value?('-123')
169 assert f.valid_field_value?('-123')
170 assert !f.valid_field_value?('6abc')
170 assert !f.valid_field_value?('6abc')
171 end
171 end
172
172
173 def test_float_field_validation
173 def test_float_field_validation
174 f = CustomField.new(:field_format => 'float')
174 f = CustomField.new(:field_format => 'float')
175
175
176 assert f.valid_field_value?(nil)
176 assert f.valid_field_value?(nil)
177 assert f.valid_field_value?('')
177 assert f.valid_field_value?('')
178 assert f.valid_field_value?('11.2')
178 assert f.valid_field_value?('11.2')
179 assert f.valid_field_value?('-6.250')
179 assert f.valid_field_value?('-6.250')
180 assert f.valid_field_value?('5')
180 assert f.valid_field_value?('5')
181 assert !f.valid_field_value?('6abc')
181 assert !f.valid_field_value?('6abc')
182 end
182 end
183
183
184 def test_multi_field_validation
184 def test_multi_field_validation
185 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2'])
185 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2'])
186
186
187 assert f.valid_field_value?(nil)
187 assert f.valid_field_value?(nil)
188 assert f.valid_field_value?('')
188 assert f.valid_field_value?('')
189 assert f.valid_field_value?([])
189 assert f.valid_field_value?([])
190 assert f.valid_field_value?([nil])
190 assert f.valid_field_value?([nil])
191 assert f.valid_field_value?([''])
191 assert f.valid_field_value?([''])
192
192
193 assert f.valid_field_value?('value2')
193 assert f.valid_field_value?('value2')
194 assert !f.valid_field_value?('abc')
194 assert !f.valid_field_value?('abc')
195
195
196 assert f.valid_field_value?(['value2'])
196 assert f.valid_field_value?(['value2'])
197 assert !f.valid_field_value?(['abc'])
197 assert !f.valid_field_value?(['abc'])
198
198
199 assert f.valid_field_value?(['', 'value2'])
199 assert f.valid_field_value?(['', 'value2'])
200 assert !f.valid_field_value?(['', 'abc'])
200 assert !f.valid_field_value?(['', 'abc'])
201
201
202 assert f.valid_field_value?(['value1', 'value2'])
202 assert f.valid_field_value?(['value1', 'value2'])
203 assert !f.valid_field_value?(['value1', 'abc'])
203 assert !f.valid_field_value?(['value1', 'abc'])
204 end
204 end
205
205
206 def test_value_class_should_return_the_class_used_for_fields_values
206 def test_value_class_should_return_the_class_used_for_fields_values
207 assert_equal User, CustomField.new(:field_format => 'user')
207 assert_equal User, CustomField.new(:field_format => 'user').value_class
208 assert_equal Version, CustomField.new(:field_format => 'version')
208 assert_equal Version, CustomField.new(:field_format => 'version').value_class
209 end
209 end
210
210
211 def test_value_class_should_return_nil_for_other_fields
211 def test_value_class_should_return_nil_for_other_fields
212 assert_nil CustomField.new(:field_format => 'text')
212 assert_nil CustomField.new(:field_format => 'text').value_class
213 assert_nil CustomField.new
213 assert_nil CustomField.new.value_class
214 end
214 end
215 end
215 end
General Comments 0
You need to be logged in to leave comments. Login now