]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/csharp/test/Apache.Arrow.Tests/SchemaBuilderTests.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.Tests / SchemaBuilderTests.cs
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15
16 using Apache.Arrow.Types;
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using Xunit;
21 using Xunit.Sdk;
22
23 namespace Apache.Arrow.Tests
24 {
25 public class SchemaBuilderTests
26 {
27 public class Build
28 {
29 [Fact]
30 public void FieldsAreNullableByDefault()
31 {
32 var b = new Schema.Builder();
33
34 var schema = new Schema.Builder()
35 .Field(f => f.Name("f0").DataType(Int32Type.Default))
36 .Build();
37
38 Assert.True(schema.Fields["f0"].IsNullable);
39 }
40
41 [Fact]
42 public void FieldsHaveNullTypeByDefault()
43 {
44 var schema = new Schema.Builder()
45 .Field(f => f.Name("f0"))
46 .Build();
47
48 Assert.True(schema.Fields["f0"].DataType.GetType() == typeof(NullType));
49 }
50
51 [Fact]
52 public void FieldNameIsRequired()
53 {
54 Assert.Throws<ArgumentNullException>(() =>
55 {
56 var schema = new Schema.Builder()
57 .Field(f => f.DataType(Int32Type.Default))
58 .Build();
59 });
60 }
61
62 [Fact]
63 public void GetFieldIndex()
64 {
65 var schema = new Schema.Builder()
66 .Field(f => f.Name("f0").DataType(Int32Type.Default))
67 .Field(f => f.Name("f1").DataType(Int8Type.Default))
68 .Build();
69 Assert.True(schema.GetFieldIndex("f0") == 0 && schema.GetFieldIndex("f1") == 1);
70 }
71
72
73 [Fact]
74 public void GetFieldByName()
75 {
76 Field f0 = new Field.Builder().Name("f0").DataType(Int32Type.Default).Build();
77 Field f1 = new Field.Builder().Name("f1").DataType(Int8Type.Default).Build();
78
79 var schema = new Schema.Builder()
80 .Field(f0)
81 .Field(f1)
82 .Build();
83 Assert.True(schema.GetFieldByName("f0") == f0 && schema.GetFieldByName("f1") == f1);
84 }
85
86 [Fact]
87 public void MetadataConstruction()
88 {
89
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();
98
99 var schema0 = new Schema.Builder()
100 .Field(f0)
101 .Field(f1)
102 .Field(f2)
103 .Metadata(metadata0)
104 .Build();
105 var schema1 = new Schema.Builder()
106 .Field(f0)
107 .Field(f1)
108 .Field(f2)
109 .Metadata(metadata1)
110 .Build();
111 var schema2 = new Schema.Builder()
112 .Field(f0)
113 .Field(f1)
114 .Field(f2)
115 .Metadata(metadata0Copy)
116 .Build();
117 var schema3 = new Schema.Builder()
118 .Field(f0)
119 .Field(f1)
120 .Field(f3)
121 .Metadata(metadata0Copy)
122 .Build();
123
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));
131 }
132
133 [Theory]
134 [MemberData(nameof(SampleSchema1))]
135 public void FieldsHaveExpectedValues(string name, IArrowType type, bool nullable)
136 {
137 var schema = new Schema.Builder()
138 .Field(f => f.Name(name).DataType(type).Nullable(nullable))
139 .Build();
140
141 var field = schema.Fields[name];
142
143 Assert.Equal(name, field.Name);
144 Assert.Equal(type.Name, field.DataType.Name);
145 Assert.Equal(nullable, field.IsNullable);
146 }
147
148 public static IEnumerable<object[]> SampleSchema1()
149 {
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};
153 }
154 }
155 }
156 }