active_interaction 5.1.0 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -0
- data/README.md +11 -12
- data/lib/active_interaction/base.rb +2 -0
- data/lib/active_interaction/errors.rb +16 -16
- data/lib/active_interaction/filter.rb +9 -12
- data/lib/active_interaction/filters/array_filter.rb +1 -1
- data/lib/active_interaction/filters/hash_filter.rb +5 -1
- data/lib/active_interaction/grouped_input.rb +23 -3
- data/lib/active_interaction/locale/es.yml +23 -0
- data/lib/active_interaction/version.rb +1 -1
- metadata +13 -115
- data/spec/active_interaction/array_input_spec.rb +0 -166
- data/spec/active_interaction/base_spec.rb +0 -537
- data/spec/active_interaction/concerns/active_modelable_spec.rb +0 -45
- data/spec/active_interaction/concerns/active_recordable_spec.rb +0 -49
- data/spec/active_interaction/concerns/hashable_spec.rb +0 -46
- data/spec/active_interaction/concerns/missable_spec.rb +0 -99
- data/spec/active_interaction/concerns/runnable_spec.rb +0 -397
- data/spec/active_interaction/errors_spec.rb +0 -196
- data/spec/active_interaction/filter/column_spec.rb +0 -87
- data/spec/active_interaction/filter_spec.rb +0 -39
- data/spec/active_interaction/filters/abstract_date_time_filter_spec.rb +0 -13
- data/spec/active_interaction/filters/abstract_numeric_filter_spec.rb +0 -13
- data/spec/active_interaction/filters/array_filter_spec.rb +0 -245
- data/spec/active_interaction/filters/boolean_filter_spec.rb +0 -87
- data/spec/active_interaction/filters/date_filter_spec.rb +0 -178
- data/spec/active_interaction/filters/date_time_filter_spec.rb +0 -189
- data/spec/active_interaction/filters/decimal_filter_spec.rb +0 -126
- data/spec/active_interaction/filters/file_filter_spec.rb +0 -40
- data/spec/active_interaction/filters/float_filter_spec.rb +0 -110
- data/spec/active_interaction/filters/hash_filter_spec.rb +0 -129
- data/spec/active_interaction/filters/integer_filter_spec.rb +0 -104
- data/spec/active_interaction/filters/interface_filter_spec.rb +0 -461
- data/spec/active_interaction/filters/object_filter_spec.rb +0 -237
- data/spec/active_interaction/filters/record_filter_spec.rb +0 -206
- data/spec/active_interaction/filters/string_filter_spec.rb +0 -60
- data/spec/active_interaction/filters/symbol_filter_spec.rb +0 -46
- data/spec/active_interaction/filters/time_filter_spec.rb +0 -251
- data/spec/active_interaction/grouped_input_spec.rb +0 -17
- data/spec/active_interaction/hash_input_spec.rb +0 -58
- data/spec/active_interaction/i18n_spec.rb +0 -113
- data/spec/active_interaction/inputs_spec.rb +0 -266
- data/spec/active_interaction/integration/array_interaction_spec.rb +0 -88
- data/spec/active_interaction/integration/boolean_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/date_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/date_time_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/file_interaction_spec.rb +0 -18
- data/spec/active_interaction/integration/float_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/hash_interaction_spec.rb +0 -76
- data/spec/active_interaction/integration/integer_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/interface_interaction_spec.rb +0 -19
- data/spec/active_interaction/integration/object_interaction_spec.rb +0 -14
- data/spec/active_interaction/integration/record_integration_spec.rb +0 -5
- data/spec/active_interaction/integration/string_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/symbol_interaction_spec.rb +0 -5
- data/spec/active_interaction/integration/time_interaction_spec.rb +0 -88
- data/spec/active_interaction/modules/validation_spec.rb +0 -57
- data/spec/spec_helper.rb +0 -20
- data/spec/support/concerns.rb +0 -13
- data/spec/support/filters.rb +0 -227
- data/spec/support/interactions.rb +0 -124
@@ -1,178 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveInteraction::DateFilter, :filter do
|
4
|
-
include_context 'filters'
|
5
|
-
it_behaves_like 'a filter'
|
6
|
-
|
7
|
-
shared_context 'with format' do
|
8
|
-
let(:format) { '%d/%m/%Y' }
|
9
|
-
|
10
|
-
before do
|
11
|
-
options[:format] = format
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#process' do
|
16
|
-
let(:result) { filter.process(value, nil) }
|
17
|
-
|
18
|
-
context 'with a Date' do
|
19
|
-
let(:value) { Date.new }
|
20
|
-
|
21
|
-
it 'returns the Date' do
|
22
|
-
expect(result.value).to eql value
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with a String' do
|
27
|
-
let(:value) { '2011-12-13' }
|
28
|
-
|
29
|
-
it 'returns a Date' do
|
30
|
-
expect(result.value).to eql Date.parse(value)
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'with format' do
|
34
|
-
include_context 'with format'
|
35
|
-
|
36
|
-
let(:value) { '13/12/2011' }
|
37
|
-
|
38
|
-
it 'returns a Date' do
|
39
|
-
expect(result.value).to eql Date.strptime(value, format)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with an invalid String' do
|
45
|
-
let(:value) { 'invalid' }
|
46
|
-
|
47
|
-
it 'indicates an error' do
|
48
|
-
error = result.errors.first
|
49
|
-
|
50
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
51
|
-
expect(error.type).to be :invalid_type
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'with format' do
|
55
|
-
include_context 'with format'
|
56
|
-
|
57
|
-
it 'raises an error' do
|
58
|
-
error = result.errors.first
|
59
|
-
|
60
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
61
|
-
expect(error.type).to be :invalid_type
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'with an implicit String' do
|
67
|
-
let(:value) do
|
68
|
-
Class.new do
|
69
|
-
def to_str
|
70
|
-
'2011-12-13'
|
71
|
-
end
|
72
|
-
end.new
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'returns a Date' do
|
76
|
-
expect(result.value).to eql Date.parse(value)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'with a blank String' do
|
81
|
-
let(:value) do
|
82
|
-
Class.new do
|
83
|
-
def to_str
|
84
|
-
' '
|
85
|
-
end
|
86
|
-
end.new
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'optional' do
|
90
|
-
include_context 'optional'
|
91
|
-
|
92
|
-
it 'returns the default' do
|
93
|
-
expect(result.value).to eql options[:default]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'required' do
|
98
|
-
include_context 'required'
|
99
|
-
|
100
|
-
it 'indicates an error' do
|
101
|
-
error = result.errors.first
|
102
|
-
|
103
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
104
|
-
expect(error.type).to be :missing
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context 'with a GroupedInput' do
|
110
|
-
let(:year) { 2012 }
|
111
|
-
let(:month) { 1 }
|
112
|
-
let(:day) { 2 }
|
113
|
-
let(:value) do
|
114
|
-
ActiveInteraction::GroupedInput.new(
|
115
|
-
'1' => year.to_s,
|
116
|
-
'2' => month.to_s,
|
117
|
-
'3' => day.to_s
|
118
|
-
)
|
119
|
-
end
|
120
|
-
|
121
|
-
it 'returns a Date' do
|
122
|
-
expect(result.value).to eql Date.new(year, month, day)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
context 'with an invalid GroupedInput' do
|
127
|
-
context 'empty' do
|
128
|
-
let(:value) { ActiveInteraction::GroupedInput.new }
|
129
|
-
|
130
|
-
it 'indicates an error' do
|
131
|
-
error = result.errors.first
|
132
|
-
|
133
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
134
|
-
expect(error.type).to be :invalid_type
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
context 'partial inputs' do
|
139
|
-
let(:value) do
|
140
|
-
ActiveInteraction::GroupedInput.new(
|
141
|
-
'2' => '1'
|
142
|
-
)
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'raises an error' do
|
146
|
-
error = result.errors.first
|
147
|
-
|
148
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
149
|
-
expect(error.type).to be :invalid_type
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
describe '#database_column_type' do
|
156
|
-
it 'returns :date' do
|
157
|
-
expect(filter.database_column_type).to be :date
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
describe '#default' do
|
162
|
-
context 'with a GroupedInput' do
|
163
|
-
before do
|
164
|
-
options[:default] = ActiveInteraction::GroupedInput.new(
|
165
|
-
'1' => '2012',
|
166
|
-
'2' => '1',
|
167
|
-
'3' => '2'
|
168
|
-
)
|
169
|
-
end
|
170
|
-
|
171
|
-
it 'raises an error' do
|
172
|
-
expect do
|
173
|
-
filter.default(nil)
|
174
|
-
end.to raise_error ActiveInteraction::InvalidDefaultError
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
end
|
@@ -1,189 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveInteraction::DateTimeFilter, :filter do
|
4
|
-
include_context 'filters'
|
5
|
-
it_behaves_like 'a filter'
|
6
|
-
|
7
|
-
shared_context 'with format' do
|
8
|
-
let(:format) { '%d/%m/%Y %H:%M:%S %:z' }
|
9
|
-
|
10
|
-
before do
|
11
|
-
options[:format] = format
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#process' do
|
16
|
-
let(:result) { filter.process(value, nil) }
|
17
|
-
|
18
|
-
context 'with a Datetime' do
|
19
|
-
let(:value) { DateTime.new }
|
20
|
-
|
21
|
-
it 'returns the DateTime' do
|
22
|
-
expect(result.value).to eql value
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with a String' do
|
27
|
-
let(:value) { '2011-12-13T14:15:16+17:18' }
|
28
|
-
|
29
|
-
it 'returns a DateTime' do
|
30
|
-
expect(result.value).to eql DateTime.parse(value)
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'with format' do
|
34
|
-
include_context 'with format'
|
35
|
-
|
36
|
-
let(:value) { '13/12/2011 14:15:16 +17:18' }
|
37
|
-
|
38
|
-
it 'returns a DateTime' do
|
39
|
-
expect(result.value).to eql DateTime.strptime(value, format)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with an invalid String' do
|
45
|
-
let(:value) { 'invalid' }
|
46
|
-
|
47
|
-
it 'indicates an error' do
|
48
|
-
error = result.errors.first
|
49
|
-
|
50
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
51
|
-
expect(error.type).to be :invalid_type
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'with format' do
|
55
|
-
include_context 'with format'
|
56
|
-
|
57
|
-
it 'indicates an error' do
|
58
|
-
error = result.errors.first
|
59
|
-
|
60
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
61
|
-
expect(error.type).to be :invalid_type
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'with an implicit String' do
|
67
|
-
let(:value) do
|
68
|
-
Class.new do
|
69
|
-
def to_str
|
70
|
-
'2011-12-13T14:15:16+17:18'
|
71
|
-
end
|
72
|
-
end.new
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'returns a DateTime' do
|
76
|
-
expect(result.value).to eql DateTime.parse(value)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'with a blank String' do
|
81
|
-
let(:value) do
|
82
|
-
Class.new do
|
83
|
-
def to_str
|
84
|
-
' '
|
85
|
-
end
|
86
|
-
end.new
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'optional' do
|
90
|
-
include_context 'optional'
|
91
|
-
|
92
|
-
it 'returns the default' do
|
93
|
-
expect(result.value).to eql options[:default]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'required' do
|
98
|
-
include_context 'required'
|
99
|
-
|
100
|
-
it 'indicates an error' do
|
101
|
-
error = result.errors.first
|
102
|
-
|
103
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
104
|
-
expect(error.type).to be :missing
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
context 'with a GroupedInput' do
|
110
|
-
let(:year) { 2012 }
|
111
|
-
let(:month) { 1 }
|
112
|
-
let(:day) { 2 }
|
113
|
-
let(:hour) { 3 }
|
114
|
-
let(:min) { 4 }
|
115
|
-
let(:sec) { 5 }
|
116
|
-
let(:value) do
|
117
|
-
ActiveInteraction::GroupedInput.new(
|
118
|
-
'1' => year.to_s,
|
119
|
-
'2' => month.to_s,
|
120
|
-
'3' => day.to_s,
|
121
|
-
'4' => hour.to_s,
|
122
|
-
'5' => min.to_s,
|
123
|
-
'6' => sec.to_s
|
124
|
-
)
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'returns a DateTime' do
|
128
|
-
expect(
|
129
|
-
result.value
|
130
|
-
).to eql DateTime.new(year, month, day, hour, min, sec)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
context 'with an invalid GroupedInput' do
|
135
|
-
context 'empty' do
|
136
|
-
let(:value) { ActiveInteraction::GroupedInput.new }
|
137
|
-
|
138
|
-
it 'indicates an error' do
|
139
|
-
error = result.errors.first
|
140
|
-
|
141
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
142
|
-
expect(error.type).to be :invalid_type
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
context 'partial inputs' do
|
147
|
-
let(:value) do
|
148
|
-
ActiveInteraction::GroupedInput.new(
|
149
|
-
'2' => '1'
|
150
|
-
)
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'indicates an error' do
|
154
|
-
error = result.errors.first
|
155
|
-
|
156
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
157
|
-
expect(error.type).to be :invalid_type
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
describe '#database_column_type' do
|
164
|
-
it 'returns :datetime' do
|
165
|
-
expect(filter.database_column_type).to be :datetime
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
describe '#default' do
|
170
|
-
context 'with a GroupedInput' do
|
171
|
-
before do
|
172
|
-
options[:default] = ActiveInteraction::GroupedInput.new(
|
173
|
-
'1' => '2012',
|
174
|
-
'2' => '1',
|
175
|
-
'3' => '2',
|
176
|
-
'4' => '3',
|
177
|
-
'5' => '4',
|
178
|
-
'6' => '5'
|
179
|
-
)
|
180
|
-
end
|
181
|
-
|
182
|
-
it 'raises an error' do
|
183
|
-
expect do
|
184
|
-
filter.default(nil)
|
185
|
-
end.to raise_error ActiveInteraction::InvalidDefaultError
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
@@ -1,126 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveInteraction::DecimalFilter, :filter do
|
4
|
-
include_context 'filters'
|
5
|
-
it_behaves_like 'a filter'
|
6
|
-
|
7
|
-
shared_context 'with digits' do
|
8
|
-
let(:digits) { 4 }
|
9
|
-
|
10
|
-
before do
|
11
|
-
options[:digits] = digits
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#process' do
|
16
|
-
let(:result) { filter.process(value, nil) }
|
17
|
-
|
18
|
-
context 'with a Float' do
|
19
|
-
let(:value) { rand }
|
20
|
-
|
21
|
-
it 'returns the BigDecimal' do
|
22
|
-
expect(result.value).to eql BigDecimal(value, 0)
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'with :digits option' do
|
26
|
-
include_context 'with digits'
|
27
|
-
|
28
|
-
let(:value) { 1.23456789 }
|
29
|
-
|
30
|
-
it 'returns BigDecimal with given digits' do
|
31
|
-
expect(result.value).to eql BigDecimal('1.235')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'with an implicit Integer' do
|
37
|
-
let(:value) do
|
38
|
-
Class.new do
|
39
|
-
def to_int
|
40
|
-
@to_int ||= rand(1 << 16)
|
41
|
-
end
|
42
|
-
end.new
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'returns a BigDecimal' do
|
46
|
-
expect(result.value).to eql BigDecimal(value.to_int)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with a Numeric' do
|
51
|
-
let(:value) { rand(1 << 16) }
|
52
|
-
|
53
|
-
it 'returns a BigDecimal' do
|
54
|
-
expect(result.value).to eql BigDecimal(value)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'with a String' do
|
59
|
-
let(:value) { rand.to_s }
|
60
|
-
|
61
|
-
it 'returns a BigDecimal' do
|
62
|
-
expect(result.value).to eql BigDecimal(value)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'with an invalid String' do
|
67
|
-
let(:value) { 'invalid' }
|
68
|
-
|
69
|
-
it 'indicates an error' do
|
70
|
-
error = result.errors.first
|
71
|
-
|
72
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
73
|
-
expect(error.type).to be :invalid_type
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context 'with an implicit String' do
|
78
|
-
let(:value) do
|
79
|
-
Class.new do
|
80
|
-
def to_str
|
81
|
-
'1.1'
|
82
|
-
end
|
83
|
-
end.new
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'returns a BigDecimal' do
|
87
|
-
expect(result.value).to eql BigDecimal(value)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
context 'with a blank String' do
|
92
|
-
let(:value) do
|
93
|
-
Class.new do
|
94
|
-
def to_str
|
95
|
-
' '
|
96
|
-
end
|
97
|
-
end.new
|
98
|
-
end
|
99
|
-
|
100
|
-
context 'optional' do
|
101
|
-
include_context 'optional'
|
102
|
-
|
103
|
-
it 'returns the default' do
|
104
|
-
expect(result.value).to eql options[:default]
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
context 'required' do
|
109
|
-
include_context 'required'
|
110
|
-
|
111
|
-
it 'indicates an error' do
|
112
|
-
error = result.errors.first
|
113
|
-
|
114
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
115
|
-
expect(error.type).to be :missing
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe '#database_column_type' do
|
122
|
-
it 'returns :decimal' do
|
123
|
-
expect(filter.database_column_type).to be :decimal
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveInteraction::FileFilter, :filter do
|
4
|
-
include_context 'filters'
|
5
|
-
it_behaves_like 'a filter'
|
6
|
-
|
7
|
-
describe '#process' do
|
8
|
-
let(:result) { filter.process(value, nil) }
|
9
|
-
|
10
|
-
context 'with a File' do
|
11
|
-
let(:value) { File.new(__FILE__) }
|
12
|
-
|
13
|
-
it 'returns the File' do
|
14
|
-
expect(result.value).to eql value
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with a Tempfile' do
|
19
|
-
let(:value) { Tempfile.new(SecureRandom.hex) }
|
20
|
-
|
21
|
-
it 'returns the Tempfile' do
|
22
|
-
expect(result.value).to eq value
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with an object that responds to #rewind' do
|
27
|
-
let(:value) { double(rewind: nil) } # rubocop:disable RSpec/VerifiedDoubles
|
28
|
-
|
29
|
-
it 'returns the object' do
|
30
|
-
expect(result.value).to eq value
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#database_column_type' do
|
36
|
-
it 'returns :file' do
|
37
|
-
expect(filter.database_column_type).to be :file
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'bigdecimal'
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
describe ActiveInteraction::FloatFilter, :filter do
|
5
|
-
include_context 'filters'
|
6
|
-
it_behaves_like 'a filter'
|
7
|
-
|
8
|
-
describe '#process' do
|
9
|
-
let(:result) { filter.process(value, nil) }
|
10
|
-
|
11
|
-
context 'with a Float' do
|
12
|
-
let(:value) { rand }
|
13
|
-
|
14
|
-
it 'returns the Float' do
|
15
|
-
expect(result.value).to eql value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'with an implicit Integer' do
|
20
|
-
let(:value) do
|
21
|
-
Class.new do
|
22
|
-
def to_int
|
23
|
-
@to_int ||= rand(1 << 16)
|
24
|
-
end
|
25
|
-
end.new
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'returns a Float' do
|
29
|
-
expect(result.value).to eql value.to_int.to_f
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'with a Numeric' do
|
34
|
-
let(:value) { BigDecimal('1.2') }
|
35
|
-
|
36
|
-
it 'returns a Float' do
|
37
|
-
expect(result.value).to eql value.to_f
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'with a String' do
|
42
|
-
let(:value) { rand.to_s }
|
43
|
-
|
44
|
-
it 'returns a Float' do
|
45
|
-
expect(result.value).to eql Float(value)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'with an invalid String' do
|
50
|
-
let(:value) { 'invalid' }
|
51
|
-
|
52
|
-
it 'indicates an error' do
|
53
|
-
error = result.errors.first
|
54
|
-
|
55
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
56
|
-
expect(error.type).to be :invalid_type
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context 'with an implicit String' do
|
61
|
-
let(:value) do
|
62
|
-
Class.new do
|
63
|
-
def to_str
|
64
|
-
'1.1'
|
65
|
-
end
|
66
|
-
end.new
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'returns a Float' do
|
70
|
-
# apparently `Float()` doesn't do this even though `Integer()` does
|
71
|
-
expect(result.value).to eql Float(value.to_str)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context 'with a blank String' do
|
76
|
-
let(:value) do
|
77
|
-
Class.new do
|
78
|
-
def to_str
|
79
|
-
' '
|
80
|
-
end
|
81
|
-
end.new
|
82
|
-
end
|
83
|
-
|
84
|
-
context 'optional' do
|
85
|
-
include_context 'optional'
|
86
|
-
|
87
|
-
it 'returns the default' do
|
88
|
-
expect(result.value).to eql options[:default]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context 'required' do
|
93
|
-
include_context 'required'
|
94
|
-
|
95
|
-
it 'indicates an error' do
|
96
|
-
error = result.errors.first
|
97
|
-
|
98
|
-
expect(error).to be_an_instance_of ActiveInteraction::Filter::Error
|
99
|
-
expect(error.type).to be :missing
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe '#database_column_type' do
|
106
|
-
it 'returns :float' do
|
107
|
-
expect(filter.database_column_type).to be :float
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|