summaryrefslogtreecommitdiff
path: root/test/csv/test_data_converters.rb
blob: 1620e077be450d83ebf6540a36f965879279cb74 (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
# -*- coding: utf-8 -*-
# frozen_string_literal: false

require_relative "helper"

class TestCSVDataConverters < Test::Unit::TestCase
  extend DifferentOFS

  def setup
    super
    @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
end