1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 using Apache.Arrow.Types;
18 using System.Collections.Generic;
23 namespace Apache.Arrow.Tests
25 public class SchemaBuilderTests
30 public void FieldsAreNullableByDefault()
32 var b = new Schema.Builder();
34 var schema = new Schema.Builder()
35 .Field(f => f.Name("f0").DataType(Int32Type.Default))
38 Assert.True(schema.Fields["f0"].IsNullable);
42 public void FieldsHaveNullTypeByDefault()
44 var schema = new Schema.Builder()
45 .Field(f => f.Name("f0"))
48 Assert.True(schema.Fields["f0"].DataType.GetType() == typeof(NullType));
52 public void FieldNameIsRequired()
54 Assert.Throws<ArgumentNullException>(() =>
56 var schema = new Schema.Builder()
57 .Field(f => f.DataType(Int32Type.Default))
63 public void GetFieldIndex()
65 var schema = new Schema.Builder()
66 .Field(f => f.Name("f0").DataType(Int32Type.Default))
67 .Field(f => f.Name("f1").DataType(Int8Type.Default))
69 Assert.True(schema.GetFieldIndex("f0") == 0 && schema.GetFieldIndex("f1") == 1);
74 public void GetFieldByName()
76 Field f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
77 Field f1 = new Field.Builder().Name("f1").DataType(Int8Type.Default).Build();
79 var schema = new Schema.Builder()
83 Assert.True(schema.GetFieldByName("f0") == f0 && schema.GetFieldByName("f1") == f1);
87 public void MetadataConstruction()
90 var metadata0 = new Dictionary<string, string> { { "foo", "bar" }, { "bizz", "buzz" } };
91 var metadata1 = new Dictionary<string, string> { { "foo", "bar" } };
92 var metadata0Copy = new Dictionary<string, string>(metadata0);
93 var metadata1Copy = new Dictionary<string, string>(metadata1);
94 Field f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
95 Field f1 = new Field.Builder().Name("f1").DataType(UInt8Type.Default).Nullable(false).Build();
96 Field f2 = new Field.Builder().Name("f2").DataType(StringType.Default).Build();
97 Field f3 = new Field.Builder().Name("f2").DataType(StringType.Default).Metadata(metadata1Copy).Build();
99 var schema0 = new Schema.Builder()
105 var schema1 = new Schema.Builder()
111 var schema2 = new Schema.Builder()
115 .Metadata(metadata0Copy)
117 var schema3 = new Schema.Builder()
121 .Metadata(metadata0Copy)
124 Assert.True(metadata0.Keys.SequenceEqual(schema0.Metadata.Keys) && metadata0.Values.SequenceEqual(schema0.Metadata.Values));
125 Assert.True(metadata1.Keys.SequenceEqual(schema1.Metadata.Keys) && metadata1.Values.SequenceEqual(schema1.Metadata.Values));
126 Assert.True(metadata0.Keys.SequenceEqual(schema2.Metadata.Keys) && metadata0.Values.SequenceEqual(schema2.Metadata.Values));
127 SchemaComparer.Compare(schema0, schema2);
128 Assert.Throws<EqualException>(() => SchemaComparer.Compare(schema0, schema1));
129 Assert.Throws<EqualException>(() => SchemaComparer.Compare(schema2, schema1));
130 Assert.Throws<EqualException>(() => SchemaComparer.Compare(schema2, schema3));
134 [MemberData(nameof(SampleSchema1))]
135 public void FieldsHaveExpectedValues(string name, IArrowType type, bool nullable)
137 var schema = new Schema.Builder()
138 .Field(f => f.Name(name).DataType(type).Nullable(nullable))
141 var field = schema.Fields[name];
143 Assert.Equal(name, field.Name);
144 Assert.Equal(type.Name, field.DataType.Name);
145 Assert.Equal(nullable, field.IsNullable);
148 public static IEnumerable<object[]> SampleSchema1()
150 yield return new object[] {"f0", Int32Type.Default, true};
151 yield return new object[] {"f1", DoubleType.Default, true};
152 yield return new object[] {"f2", Int64Type.Default, false};