1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18class TestStructDataType < Test::Unit::TestCase
19  def setup
20    @enabled_field_data_type = Arrow::BooleanDataType.new
21    @message_field_data_type = Arrow::StringDataType.new
22    @field_data_types = [
23      @enabled_field_data_type,
24      @message_field_data_type,
25    ]
26    @enabled_field = Arrow::Field.new("enabled", @enabled_field_data_type)
27    @message_field = Arrow::Field.new("message", @message_field_data_type)
28    @fields = [@enabled_field, @message_field]
29    @data_type = Arrow::StructDataType.new(@fields)
30  end
31
32  def test_type
33    assert_equal(Arrow::Type::STRUCT, @data_type.id)
34  end
35
36  def test_to_s
37    assert_equal("struct<enabled: bool, message: string>",
38                 @data_type.to_s)
39  end
40
41  def test_n_fields
42    assert_equal(2, @data_type.n_fields)
43  end
44
45  def test_fields
46    assert_equal(@fields.zip(@field_data_types),
47                 @data_type.fields.collect {|field| [field, field.data_type]})
48  end
49
50  sub_test_case("#get_field") do
51    def test_found
52      assert_equal(@fields[1], @data_type.get_field(1))
53    end
54
55    def test_negative
56      assert_equal(@fields[-1], @data_type.get_field(-1))
57    end
58
59    def test_over
60      assert_equal(nil, @data_type.get_field(2))
61    end
62
63    def test_data_type
64      field = @data_type.get_field(0)
65      assert_equal([
66                     @fields[0],
67                     @field_data_types[0],
68                   ],
69                   [
70                     field,
71                     field.data_type,
72                   ])
73    end
74  end
75
76  sub_test_case("#get_field_by_name") do
77    def test_found
78      assert_equal(@enabled_field,
79                   @data_type.get_field_by_name("enabled"))
80    end
81
82    def test_not_found
83      assert_equal(nil,
84                   @data_type.get_field_by_name("nonexistent"))
85    end
86
87    def test_data_type
88      field = @data_type.get_field_by_name("enabled")
89      assert_equal([
90                     @enabled_field,
91                     @enabled_field_data_type,
92                   ],
93                   [
94                     field,
95                     field.data_type,
96                   ])
97    end
98  end
99
100  sub_test_case("#get_field_index") do
101    def test_found
102      assert_equal(@fields.index(@enabled_field),
103                   @data_type.get_field_index("enabled"))
104    end
105
106    def test_not_found
107      assert_equal(-1,
108                   @data_type.get_field_index("nonexistent"))
109    end
110  end
111end
112