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
|
require "cases/helper"
require 'models/company'
module ActiveRecord
module ConnectionAdapters
class TypesTest < ActiveRecord::TestCase
def test_type_cast_boolean
type = Type::Boolean.new
assert type.type_cast_from_user('').nil?
assert type.type_cast_from_user(nil).nil?
assert type.type_cast_from_user(true)
assert type.type_cast_from_user(1)
assert type.type_cast_from_user('1')
assert type.type_cast_from_user('t')
assert type.type_cast_from_user('T')
assert type.type_cast_from_user('true')
assert type.type_cast_from_user('TRUE')
assert type.type_cast_from_user('on')
assert type.type_cast_from_user('ON')
# explicitly check for false vs nil
assert_equal false, type.type_cast_from_user(false)
assert_equal false, type.type_cast_from_user(0)
assert_equal false, type.type_cast_from_user('0')
assert_equal false, type.type_cast_from_user('f')
assert_equal false, type.type_cast_from_user('F')
assert_equal false, type.type_cast_from_user('false')
assert_equal false, type.type_cast_from_user('FALSE')
assert_equal false, type.type_cast_from_user('off')
assert_equal false, type.type_cast_from_user('OFF')
assert_equal false, type.type_cast_from_user(' ')
assert_equal false, type.type_cast_from_user("\u3000\r\n")
assert_equal false, type.type_cast_from_user("\u0000")
assert_equal false, type.type_cast_from_user('SOMETHING RANDOM')
end
def test_type_cast_integer
type = Type::Integer.new
assert_equal 1, type.type_cast_from_user(1)
assert_equal 1, type.type_cast_from_user('1')
assert_equal 1, type.type_cast_from_user('1ignore')
assert_equal 0, type.type_cast_from_user('bad1')
assert_equal 0, type.type_cast_from_user('bad')
assert_equal 1, type.type_cast_from_user(1.7)
assert_equal 0, type.type_cast_from_user(false)
assert_equal 1, type.type_cast_from_user(true)
assert_nil type.type_cast_from_user(nil)
end
def test_type_cast_non_integer_to_integer
type = Type::Integer.new
assert_nil type.type_cast_from_user([1,2])
assert_nil type.type_cast_from_user({1 => 2})
assert_nil type.type_cast_from_user((1..2))
end
def test_type_cast_activerecord_to_integer
type = Type::Integer.new
firm = Firm.create(:name => 'Apple')
assert_nil type.type_cast_from_user(firm)
end
def test_type_cast_object_without_to_i_to_integer
type = Type::Integer.new
assert_nil type.type_cast_from_user(Object.new)
end
def test_type_cast_nan_and_infinity_to_integer
type = Type::Integer.new
assert_nil type.type_cast_from_user(Float::NAN)
assert_nil type.type_cast_from_user(1.0/0.0)
end
def test_changing_integers
type = Type::Integer.new
assert type.changed?(5, 5, '5wibble')
assert_not type.changed?(5, 5, '5')
assert_not type.changed?(5, 5, '5.0')
assert_not type.changed?(nil, nil, nil)
end
def test_type_cast_float
type = Type::Float.new
assert_equal 1.0, type.type_cast_from_user("1")
end
def test_changing_float
type = Type::Float.new
assert type.changed?(5.0, 5.0, '5wibble')
assert_not type.changed?(5.0, 5.0, '5')
assert_not type.changed?(5.0, 5.0, '5.0')
assert_not type.changed?(nil, nil, nil)
end
def test_type_cast_binary
type = Type::Binary.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal "1", type.type_cast_from_user("1")
assert_equal 1, type.type_cast_from_user(1)
end
def test_type_cast_time
type = Type::Time.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user('ABC')
time_string = Time.now.utc.strftime("%T")
assert_equal time_string, type.type_cast_from_user(time_string).strftime("%T")
end
def test_type_cast_datetime_and_timestamp
type = Type::DateTime.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user(' ')
assert_equal nil, type.type_cast_from_user('ABC')
datetime_string = Time.now.utc.strftime("%FT%T")
assert_equal datetime_string, type.type_cast_from_user(datetime_string).strftime("%FT%T")
end
def test_type_cast_date
type = Type::Date.new
assert_equal nil, type.type_cast_from_user(nil)
assert_equal nil, type.type_cast_from_user('')
assert_equal nil, type.type_cast_from_user(' ')
assert_equal nil, type.type_cast_from_user('ABC')
date_string = Time.now.utc.strftime("%F")
assert_equal date_string, type.type_cast_from_user(date_string).strftime("%F")
end
def test_type_cast_duration_to_integer
type = Type::Integer.new
assert_equal 1800, type.type_cast_from_user(30.minutes)
assert_equal 7200, type.type_cast_from_user(2.hours)
end
def test_string_to_time_with_timezone
[:utc, :local].each do |zone|
with_timezone_config default: zone do
type = Type::DateTime.new
assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.type_cast_from_user("Wed, 04 Sep 2013 03:00:00 EAT")
end
end
end
if current_adapter?(:SQLite3Adapter)
def test_binary_encoding
type = SQLite3Binary.new
utf8_string = "a string".encode(Encoding::UTF_8)
type_cast = type.type_cast_from_user(utf8_string)
assert_equal Encoding::ASCII_8BIT, type_cast.encoding
end
end
end
end
end
|