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 SchemaTest < Test::Unit::TestCase
19  include Helper::Omittable
20
21  def setup
22    @count_field = Arrow::Field.new("count", :uint32)
23    @visible_field = Arrow::Field.new("visible", :boolean)
24  end
25
26  sub_test_case(".new") do
27    test("[Arrow::Field]") do
28      fields = [
29        @count_field,
30        @visible_field,
31      ]
32      assert_equal("count: uint32\n" +
33                   "visible: bool",
34                   Arrow::Schema.new(fields).to_s)
35    end
36
37    test("[Arrow::Field, Hash]") do
38      fields = [
39        @count_field,
40        {name: "visible", type: :boolean},
41      ]
42      assert_equal("count: uint32\n" +
43                   "visible: bool",
44                   Arrow::Schema.new(fields).to_s)
45    end
46
47    test("{String, Symbol => Arrow::DataType}") do
48      fields = {
49        "count" => Arrow::UInt32DataType.new,
50        :visible => :boolean,
51      }
52      assert_equal("count: uint32\n" +
53                   "visible: bool",
54                   Arrow::Schema.new(fields).to_s)
55    end
56
57    test("{String, Symbol => Hash}") do
58      fields = {
59        "count" => {type: :uint32},
60        :tags => {
61          type: :list,
62          field: {
63            name: "tag",
64            type: :string,
65          },
66        },
67      }
68      assert_equal("count: uint32\n" +
69                   "tags: list<tag: string>",
70                   Arrow::Schema.new(fields).to_s)
71    end
72  end
73
74  sub_test_case("instance methods") do
75    def setup
76      super
77      @schema = Arrow::Schema.new([@count_field, @visible_field])
78    end
79
80    sub_test_case("#[]") do
81      test("[String]") do
82        assert_equal([@count_field, @visible_field],
83                     [@schema["count"], @schema["visible"]])
84      end
85
86      test("[Symbol]") do
87        assert_equal([@count_field, @visible_field],
88                     [@schema[:count], @schema[:visible]])
89      end
90
91      test("[Integer]") do
92        assert_equal([@count_field, @visible_field],
93                     [@schema[0], @schema[1]])
94      end
95
96      test("[invalid]") do
97        invalid = []
98        message = "field name or index must be String, Symbol or Integer"
99        message << ": <#{invalid.inspect}>"
100        assert_raise(ArgumentError.new(message)) do
101          @schema[invalid]
102        end
103      end
104    end
105
106    sub_test_case("#==") do
107      test("Arrow::Schema") do
108        assert do
109          @schema == @schema
110        end
111      end
112
113      test("not Arrow::Schema") do
114        assert do
115          not (@schema == 29)
116        end
117      end
118    end
119
120    sub_test_case("#to_s") do
121      test("show_metadata") do
122        require_gi_bindings(3, 4, 2)
123
124        schema = @schema.with_metadata("key" => "value")
125        assert_equal(<<-SCHEMA.chomp, schema.to_s(show_metadata: true))
126count: uint32
127visible: bool
128-- metadata --
129key: value
130        SCHEMA
131      end
132    end
133  end
134end
135