summaryrefslogtreecommitdiff
path: root/test/csv/test_data_converters.rb
blob: 8b3163da18d17fd7a22c0489bb728e7283668b90 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env ruby -w
# encoding: UTF-8
# frozen_string_literal: false

# tc_data_converters.rb
#
# Created by James Edward Gray II on 2005-10-31.

require_relative "base"

class TestCSV::DataConverters < TestCSV
  extend DifferentOFS

  def setup
    super
    @data   = "Numbers,:integer,1,:float,3.015"
    @parser = CSV.new(@data)

    @custom = lambda { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }

    @win_safe_time_str = Time.now.strftime("%a %b %d %H:%M:%S %Y")
  end

  def test_builtin_integer_converter
    # does convert
    [-5, 1, 10000000000].each do |n|
      assert_equal(n, CSV::Converters[:integer][n.to_s])
    end

    # does not convert
    (%w{junk 1.0} + [""]).each do |str|
      assert_equal(str, CSV::Converters[:integer][str])
    end
  end

  def test_builtin_float_converter
    # does convert
    [-5.1234, 0, 2.3e-11].each do |n|
      assert_equal(n, CSV::Converters[:float][n.to_s])
    end

    # does not convert
    (%w{junk 1..0 .015F} + [""]).each do |str|
      assert_equal(str, CSV::Converters[:float][str])
    end
  end

  def test_builtin_date_converter
    # does convert
    assert_instance_of(
      Date,
      CSV::Converters[:date][@win_safe_time_str.sub(/\d+:\d+:\d+ /, "")]
    )

    # does not convert
    assert_instance_of(String, CSV::Converters[:date]["junk"])
  end

  def test_builtin_date_time_converter
    # does convert
    assert_instance_of( DateTime,
                        CSV::Converters[:date_time][@win_safe_time_str] )

    # does not convert
    assert_instance_of(String, CSV::Converters[:date_time]["junk"])
  end

  def test_builtin_date_time_converter_iso8601_date
    iso8601_string = "2018-01-14"
    datetime = DateTime.new(2018, 1, 14)
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_minute
    iso8601_string = "2018-01-14T22:25"
    datetime = DateTime.new(2018, 1, 14, 22, 25)
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_second
    iso8601_string = "2018-01-14T22:25:19"
    datetime = DateTime.new(2018, 1, 14, 22, 25, 19)
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_under_second
    iso8601_string = "2018-01-14T22:25:19.1"
    datetime = DateTime.new(2018, 1, 14, 22, 25, 19.1)
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_under_second_offset
    iso8601_string = "2018-01-14T22:25:19.1+09:00"
    datetime = DateTime.new(2018, 1, 14, 22, 25, 19.1, "+9")
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_offset
    iso8601_string = "2018-01-14T22:25:19+09:00"
    datetime = DateTime.new(2018, 1, 14, 22, 25, 19, "+9")
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_builtin_date_time_converter_iso8601_utc
    iso8601_string = "2018-01-14T22:25:19Z"
    datetime = DateTime.new(2018, 1, 14, 22, 25, 19)
    assert_equal(datetime,
                 CSV::Converters[:date_time][iso8601_string])
  end

  def test_convert_with_builtin_integer
    # setup parser...
    assert_respond_to(@parser, :convert)
    assert_nothing_raised(Exception) { @parser.convert(:integer) }

    # and use
    assert_equal(["Numbers", ":integer", 1, ":float", "3.015"], @parser.shift)
  end

  def test_convert_with_builtin_float
    # setup parser...
    assert_respond_to(@parser, :convert)
    assert_nothing_raised(Exception) { @parser.convert(:float) }

    # and use
    assert_equal(["Numbers", ":integer", 1.0, ":float", 3.015], @parser.shift)
  end

  def test_convert_order_float_integer
    # floats first, then integers...
    assert_nothing_raised(Exception) do
      @parser.convert(:float)
      @parser.convert(:integer)
    end

    # gets us nothing but floats
    assert_equal( [String, String, Float, String, Float],
                  @parser.shift.map { |field| field.class } )
  end

  def test_convert_order_integer_float
    # integers have precendance...
    assert_nothing_raised(Exception) do
      @parser.convert(:integer)
      @parser.convert(:float)
    end

    # gives us proper number conversion
    assert_equal( [String, String, 0.class, String, Float],
                  @parser.shift.map { |field| field.class } )
  end

  def test_builtin_numeric_combo_converter
    # setup parser...
    assert_nothing_raised(Exception) { @parser.convert(:numeric) }

    # and use
    assert_equal( [String, String, 0.class, String, Float],
                  @parser.shift.map { |field| field.class } )
  end

  def test_builtin_all_nested_combo_converter
    # setup parser...
    @data   << ",#{@win_safe_time_str}"        # add a DateTime field
    @parser =  CSV.new(@data)                  # reset parser
    assert_nothing_raised(Exception) { @parser.convert(:all) }

    # and use
    assert_equal( [String, String, 0.class, String, Float, DateTime],
                  @parser.shift.map { |field| field.class } )
  end

  def test_convert_with_custom_code
    # define custom converter...
    assert_nothing_raised(Exception) do
      @parser.convert { |field| field =~ /\A:(\S.*?)\s*\Z/ ? $1.to_sym : field }
    end

    # and use
    assert_equal(["Numbers", :integer, "1", :float, "3.015"], @parser.shift)
  end

  def test_convert_with_custom_code_mix
    # mix built-in and custom...
    assert_nothing_raised(Exception) { @parser.convert(:numeric) }
    assert_nothing_raised(Exception) { @parser.convert(&@custom) }

    # and use
    assert_equal(["Numbers", :integer, 1, :float, 3.015], @parser.shift)
  end

  def test_convert_with_custom_code_using_field_info
    # define custom converter that uses field information...
    assert_nothing_raised(Exception) do
      @parser.convert do |field, info|
        assert_equal(1, info.line)
        info.index == 4 ? Float(field).floor : field
      end
    end

    # and use
    assert_equal(["Numbers", ":integer", "1", ":float", 3], @parser.shift)
  end

  def test_convert_with_custom_code_using_field_info_header
    @parser = CSV.new(@data, headers: %w{one two three four five})

    # define custom converter that uses field header information...
    assert_nothing_raised(Exception) do
      @parser.convert do |field, info|
        info.header == "three" ? Integer(field) * 100 : field
      end
    end

    # and use
    assert_equal( ["Numbers", ":integer", 100, ":float", "3.015"],
                  @parser.shift.fields )
  end

  def test_custom_converter_with_blank_field
    converter = lambda { |field| field.nil? }
    row = nil
    assert_nothing_raised(Exception) do
      row = CSV.parse_line('nil,', converters: converter)
    end
    assert_equal([false, true], row);
  end

  def test_shortcut_interface
    assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
                  CSV.parse_line(@data, converters: :numeric) )

    assert_equal( ["Numbers", ":integer", 1, ":float", 3.015],
                  CSV.parse_line(@data, converters: [:integer, :float]) )

    assert_equal( ["Numbers", :integer, 1, :float, 3.015],
                  CSV.parse_line(@data, converters: [:numeric, @custom]) )
  end

  def test_unconverted_fields_number
    row = CSV.parse_line(@data,
                         converters:         [:numeric, @custom],
                         unconverted_fields: true)
    assert_equal([
                   ["Numbers", :integer, 1, :float, 3.015],
                   ["Numbers", ":integer", "1", ":float", "3.015"],
                 ],
                 [
                   row,
                   row.unconverted_fields,
                 ])
  end

  def test_unconverted_fields_empty_line
    row = CSV.parse_line("\n",
                         converters:         [:numeric, @custom],
                         unconverted_fields: true)
    assert_equal([
                   [],
                   [],
                 ],
                 [
                   row,
                   row.unconverted_fields,
                 ])
  end

  def test_unconverted_fields
    data = <<-CSV
first,second,third
1,2,3
    CSV
    row = nil
    assert_nothing_raised(Exception) do
      row = CSV.parse_line( data,
                            converters:         :numeric,
                            unconverted_fields: true,
                            headers:            :first_row )
    end
    assert_not_nil(row)
    assert_equal([["first", 1], ["second", 2], ["third", 3]], row.to_a)
    assert_respond_to(row, :unconverted_fields)
    assert_equal(%w{1 2 3}, row.unconverted_fields)

    assert_nothing_raised(Exception) do
      row = CSV.parse_line( data,
                            converters:         :numeric,
                            unconverted_fields: true,
                            headers:            :first_row,
                            return_headers:     true )
    end
    assert_not_nil(row)
    assert_equal( [%w{first first}, %w{second second}, %w{third third}],
                  row.to_a )
    assert_respond_to(row, :unconverted_fields)
    assert_equal(%w{first second third}, row.unconverted_fields)

    assert_nothing_raised(Exception) do
      row = CSV.parse_line( data,
                            converters:         :numeric,
                            unconverted_fields: true,
                            headers:            :first_row,
                            return_headers:     true,
                            header_converters:  :symbol )
    end
    assert_not_nil(row)
    assert_equal( [[:first, "first"], [:second, "second"], [:third, "third"]],
                  row.to_a )
    assert_respond_to(row, :unconverted_fields)
    assert_equal(%w{first second third}, row.unconverted_fields)

    assert_nothing_raised(Exception) do
      row = CSV.parse_line( data,
                            converters:         :numeric,
                            unconverted_fields: true,
                            headers:            %w{my new headers},
                            return_headers:     true,
                            header_converters:  :symbol )
    end
    assert_not_nil(row)
    assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
                  row.to_a )
    assert_respond_to(row, :unconverted_fields)
    assert_equal(Array.new, row.unconverted_fields)
  end

  def test_nil_value
    assert_equal(["nil", "", "a"],
                 CSV.parse_line(',"",a', nil_value: "nil"))
  end

  def test_empty_value
    assert_equal([nil, "empty", "a"],
                 CSV.parse_line(',"",a', empty_value: "empty"))
  end
end