|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/schema.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +#include <gmock/gmock.h> |
| 26 | +#include <gtest/gtest.h> |
| 27 | + |
| 28 | +#include "iceberg/schema_field.h" |
| 29 | +#include "iceberg/util/formatter.h" |
| 30 | + |
| 31 | +TEST(SchemaTest, Basics) { |
| 32 | + { |
| 33 | + iceberg::SchemaField field1(5, "foo", std::make_shared<iceberg::Int32Type>(), true); |
| 34 | + iceberg::SchemaField field2(7, "bar", std::make_shared<iceberg::StringType>(), true); |
| 35 | + iceberg::Schema schema(100, {field1, field2}); |
| 36 | + ASSERT_EQ(schema, schema); |
| 37 | + ASSERT_EQ(100, schema.schema_id()); |
| 38 | + std::span<const iceberg::SchemaField> fields = schema.fields(); |
| 39 | + ASSERT_EQ(2, fields.size()); |
| 40 | + ASSERT_EQ(field1, fields[0]); |
| 41 | + ASSERT_EQ(field2, fields[1]); |
| 42 | + ASSERT_THAT(schema.GetFieldById(5), ::testing::Optional(field1)); |
| 43 | + ASSERT_THAT(schema.GetFieldById(7), ::testing::Optional(field2)); |
| 44 | + ASSERT_THAT(schema.GetFieldByIndex(0), ::testing::Optional(field1)); |
| 45 | + ASSERT_THAT(schema.GetFieldByIndex(1), ::testing::Optional(field2)); |
| 46 | + ASSERT_THAT(schema.GetFieldByName("foo"), ::testing::Optional(field1)); |
| 47 | + ASSERT_THAT(schema.GetFieldByName("bar"), ::testing::Optional(field2)); |
| 48 | + |
| 49 | + ASSERT_EQ(std::nullopt, schema.GetFieldById(0)); |
| 50 | + ASSERT_EQ(std::nullopt, schema.GetFieldByIndex(2)); |
| 51 | + ASSERT_EQ(std::nullopt, schema.GetFieldByIndex(-1)); |
| 52 | + ASSERT_EQ(std::nullopt, schema.GetFieldByName("element")); |
| 53 | + } |
| 54 | + ASSERT_THAT( |
| 55 | + []() { |
| 56 | + iceberg::SchemaField field1(5, "foo", std::make_shared<iceberg::Int32Type>(), |
| 57 | + true); |
| 58 | + iceberg::SchemaField field2(5, "bar", std::make_shared<iceberg::StringType>(), |
| 59 | + true); |
| 60 | + iceberg::Schema schema(100, {field1, field2}); |
| 61 | + }, |
| 62 | + ::testing::ThrowsMessage<std::runtime_error>( |
| 63 | + ::testing::HasSubstr("duplicate field ID 5"))); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(SchemaTest, Equality) { |
| 67 | + iceberg::SchemaField field1(5, "foo", std::make_shared<iceberg::Int32Type>(), true); |
| 68 | + iceberg::SchemaField field2(7, "bar", std::make_shared<iceberg::StringType>(), true); |
| 69 | + iceberg::SchemaField field3(5, "foobar", std::make_shared<iceberg::Int32Type>(), true); |
| 70 | + iceberg::Schema schema1(100, {field1, field2}); |
| 71 | + iceberg::Schema schema2(101, {field1, field2}); |
| 72 | + iceberg::Schema schema3(101, {field1}); |
| 73 | + iceberg::Schema schema4(101, {field3, field2}); |
| 74 | + iceberg::Schema schema5(100, {field1, field2}); |
| 75 | + |
| 76 | + ASSERT_EQ(schema1, schema1); |
| 77 | + ASSERT_NE(schema1, schema2); |
| 78 | + ASSERT_NE(schema2, schema1); |
| 79 | + ASSERT_NE(schema1, schema3); |
| 80 | + ASSERT_NE(schema3, schema1); |
| 81 | + ASSERT_NE(schema1, schema4); |
| 82 | + ASSERT_NE(schema4, schema1); |
| 83 | + ASSERT_EQ(schema1, schema5); |
| 84 | + ASSERT_EQ(schema5, schema1); |
| 85 | +} |
0 commit comments