To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 3c / 3cdf10eb8093cfc847c369901c8cec7bae62db8a.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (3.62 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require File.expand_path('../../test_helper', __FILE__)
19

    
20
class CustomValueTest < ActiveSupport::TestCase
21
  fixtures :custom_fields, :custom_values, :users
22

    
23
  def test_string_field_validation_with_blank_value
24
    f = CustomField.new(:field_format => 'string')
25
    v = CustomValue.new(:custom_field => f)
26

    
27
    v.value = nil
28
    assert v.valid?
29
    v.value = ''
30
    assert v.valid?
31

    
32
    f.is_required = true
33
    v.value = nil
34
    assert !v.valid?
35
    v.value = ''
36
    assert !v.valid?
37
  end
38

    
39
  def test_string_field_validation_with_min_and_max_lengths
40
    f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5)
41
    v = CustomValue.new(:custom_field => f, :value => '')
42
    assert v.valid?
43
    v.value = 'a'
44
    assert !v.valid?
45
    v.value = 'a' * 2
46
    assert v.valid?
47
    v.value = 'a' * 6
48
    assert !v.valid?
49
  end
50

    
51
  def test_string_field_validation_with_regexp
52
    f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$')
53
    v = CustomValue.new(:custom_field => f, :value => '')
54
    assert v.valid?
55
    v.value = 'abc'
56
    assert !v.valid?
57
    v.value = 'ABC'
58
    assert v.valid?
59
  end
60

    
61
  def test_date_field_validation
62
    f = CustomField.new(:field_format => 'date')
63
    v = CustomValue.new(:custom_field => f, :value => '')
64
    assert v.valid?
65
    v.value = 'abc'
66
    assert !v.valid?
67
    v.value = '1975-07-33'
68
    assert !v.valid?
69
    v.value = '1975-07-14'
70
    assert v.valid?
71
  end
72

    
73
  def test_list_field_validation
74
    f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2'])
75
    v = CustomValue.new(:custom_field => f, :value => '')
76
    assert v.valid?
77
    v.value = 'abc'
78
    assert !v.valid?
79
    v.value = 'value2'
80
    assert v.valid?
81
  end
82

    
83
  def test_int_field_validation
84
    f = CustomField.new(:field_format => 'int')
85
    v = CustomValue.new(:custom_field => f, :value => '')
86
    assert v.valid?
87
    v.value = 'abc'
88
    assert !v.valid?
89
    v.value = '123'
90
    assert v.valid?
91
    v.value = '+123'
92
    assert v.valid?
93
    v.value = '-123'
94
    assert v.valid?
95
  end
96

    
97
  def test_float_field_validation
98
    v = CustomValue.new(:customized => User.find(:first), :custom_field => UserCustomField.find_by_name('Money'))
99
    v.value = '11.2'
100
    assert v.save
101
    v.value = ''
102
    assert v.save
103
    v.value = '-6.250'
104
    assert v.save
105
    v.value = '6a'
106
    assert !v.save
107
  end
108

    
109
  def test_default_value
110
    field = CustomField.find_by_default_value('Default string')
111
    assert_not_nil field
112

    
113
    v = CustomValue.new(:custom_field => field)
114
    assert_equal 'Default string', v.value
115

    
116
    v = CustomValue.new(:custom_field => field, :value => 'Not empty')
117
    assert_equal 'Not empty', v.value
118
  end
119

    
120
  def test_sti_polymorphic_association
121
    # Rails uses top level sti class for polymorphic association. See #3978.
122
    assert !User.find(4).custom_values.empty?
123
    assert !CustomValue.find(2).customized.nil?
124
  end
125
end