summaryrefslogtreecommitdiffstats
path: root/src/arrow/ruby/red-arrow/test/test-csv-loader.rb
blob: 7f7f23498d0fdec59f0141b9f8f1bc36a350e055 (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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

class CSVLoaderTest < Test::Unit::TestCase
  include Helper::Fixture

  def load_csv(input)
    Arrow::CSVLoader.load(input, skip_lines: /^#/)
  end

  sub_test_case(".load") do
    test("String: data: with header") do
      data = fixture_path("with-header-float.csv").read
      assert_equal(<<-TABLE, load_csv(data).to_s)
	name	     score
0	alice	 10.100000
1	bob 	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("String: data: without header") do
      data = fixture_path("without-header-float.csv").read
      assert_equal(<<-TABLE, load_csv(data).to_s)
	0	         1
0	alice	 10.100000
1	bob	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("String: path: with header") do
      path = fixture_path("with-header-float.csv").to_s
      assert_equal(<<-TABLE, load_csv(path).to_s)
	name	     score
0	alice	 10.100000
1	bob 	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("String: path: without header") do
      path = fixture_path("without-header-float.csv").to_s
      assert_equal(<<-TABLE, load_csv(path).to_s)
	0	         1
0	alice	 10.100000
1	bob	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("Pathname: with header") do
      path = fixture_path("with-header-float.csv")
      assert_equal(<<-TABLE, load_csv(path).to_s)
	name	     score
0	alice	 10.100000
1	bob 	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("Pathname: without header") do
      path = fixture_path("without-header-float.csv")
      assert_equal(<<-TABLE, load_csv(path).to_s)
	0	         1
0	alice	 10.100000
1	bob	 29.200000
2	chris	 -1.300000
      TABLE
    end

    test("null: with double quote") do
      path = fixture_path("null-with-double-quote.csv").to_s
      assert_equal(<<-TABLE, load_csv(path).to_s)
	name	 score
0	alice	    10
1	bob 	(null)
2	chris	    -1
      TABLE
    end

    test("null: without double quote") do
      path = fixture_path("null-without-double-quote.csv").to_s
      assert_equal(<<-TABLE, load_csv(path).to_s)
	name	 score
0	alice	    10
1	bob 	(null)
2	chris	    -1
      TABLE
    end

    test("number: float, integer") do
      path = fixture_path("float-integer.csv").to_s
      assert_equal([2.9, 10, -1.1],
                   load_csv(path)[:score].to_a)
    end

    test("number: integer, float") do
      path = fixture_path("integer-float.csv").to_s
      assert_equal([10.0, 2.9, -1.1],
                   load_csv(path)[:score].to_a)
    end
  end

  sub_test_case("CSVReader") do
    def load_csv(data, **options)
      Arrow::CSVLoader.load(data, **options)
    end

    sub_test_case(":headers") do
      test("true") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(value: values),
                     load_csv(<<-CSV, headers: true))
value
a
b
c
                     CSV
      end

      test(":first_line") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(value: values),
                     load_csv(<<-CSV, headers: :first_line))
value
a
b
c
                     CSV
      end

      test("truthy") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(value: values),
                     load_csv(<<-CSV, headers: 0))
value
a
b
c
                     CSV
      end

      test("Array of column names") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(column: values),
                     load_csv(<<-CSV, headers: ["column"]))
a
b
c
                     CSV
      end

      test("false") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(f0: values),
                     load_csv(<<-CSV, headers: false))
a
b
c
                     CSV
      end

      test("nil") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(f0: values),
                     load_csv(<<-CSV, headers: nil))
a
b
c
                     CSV
      end

      test("string") do
        values = Arrow::StringArray.new(["a", "b", "c"])
        assert_equal(Arrow::Table.new(column: values),
                     load_csv(<<-CSV, headers: "column"))
a
b
c
                     CSV
      end
    end

    test(":column_types") do
      assert_equal(Arrow::Table.new(:count => Arrow::UInt16Array.new([1, 2, 4])),
                   load_csv(<<-CSV, column_types: {count: :uint16}))
count
1
2
4
                   CSV
    end

    test(":schema") do
      table = Arrow::Table.new(:count => Arrow::UInt16Array.new([1, 2, 4]))
      assert_equal(table,
                   load_csv(<<-CSV, schema: table.schema))
count
1
2
4
                   CSV
    end

    test(":encoding") do
      messages = [
        "\u3042", # U+3042 HIRAGANA LETTER A
        "\u3044", # U+3044 HIRAGANA LETTER I
        "\u3046", # U+3046 HIRAGANA LETTER U
      ]
      table = Arrow::Table.new(:message => Arrow::StringArray.new(messages))
      encoding = "cp932"
      assert_equal(table,
                   load_csv((["message"] + messages).join("\n").encode(encoding),
                            schema: table.schema,
                            encoding: encoding))
    end

    test(":encoding and :compression") do
      messages = [
        "\u3042", # U+3042 HIRAGANA LETTER A
        "\u3044", # U+3044 HIRAGANA LETTER I
        "\u3046", # U+3046 HIRAGANA LETTER U
      ]
      table = Arrow::Table.new(:message => Arrow::StringArray.new(messages))
      encoding = "cp932"
      csv = (["message"] + messages).join("\n").encode(encoding)
      assert_equal(table,
                   load_csv(Zlib::Deflate.deflate(csv),
                            schema: table.schema,
                            encoding: encoding,
                            compression: :gzip))
    end
  end
end