Skip to content

Commit bbc18d5

Browse files
committed
add field/schema tests
1 parent e609d5d commit bbc18d5

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

test/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# under the License.
1717

1818
add_executable(core_unittest)
19-
target_sources(core_unittest PRIVATE core_unittest.cc type_test.cc)
19+
target_sources(core_unittest PRIVATE core_unittest.cc schema_test.cc schema_field_test.cc
20+
type_test.cc)
2021
target_link_libraries(core_unittest PRIVATE iceberg_static GTest::gtest_main GTest::gmock)
2122
target_include_directories(core_unittest PRIVATE "${ICEBERG_INCLUDES}")
2223
add_test(NAME core_unittest COMMAND core_unittest)

test/core/schema_field_test.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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_field.h"
21+
22+
#include <format>
23+
#include <memory>
24+
25+
#include <gtest/gtest.h>
26+
27+
#include "iceberg/type.h"
28+
#include "iceberg/util/formatter.h"
29+
30+
TEST(SchemaFieldTest, Basics) {
31+
{
32+
iceberg::SchemaField field(1, "foo", std::make_shared<iceberg::Int32Type>(), false);
33+
EXPECT_EQ(1, field.field_id());
34+
EXPECT_EQ("foo", field.name());
35+
EXPECT_EQ(iceberg::TypeId::kInt32, field.type()->type_id());
36+
EXPECT_FALSE(field.optional());
37+
EXPECT_EQ("foo (1): int32 (required)", field.ToString());
38+
EXPECT_EQ("foo (1): int32 (required)", std::format("{}", field));
39+
}
40+
{
41+
iceberg::SchemaField field = iceberg::SchemaField::MakeOptional(
42+
2, "foo bar", std::make_shared<iceberg::FixedType>(10));
43+
EXPECT_EQ(2, field.field_id());
44+
EXPECT_EQ("foo bar", field.name());
45+
EXPECT_EQ(iceberg::FixedType(10), *field.type());
46+
EXPECT_TRUE(field.optional());
47+
EXPECT_EQ("foo bar (2): fixed(10)", field.ToString());
48+
EXPECT_EQ("foo bar (2): fixed(10)", std::format("{}", field));
49+
}
50+
{
51+
iceberg::SchemaField field = iceberg::SchemaField::MakeRequired(
52+
2, "foo bar", std::make_shared<iceberg::FixedType>(10));
53+
EXPECT_EQ(2, field.field_id());
54+
EXPECT_EQ("foo bar", field.name());
55+
EXPECT_EQ(iceberg::FixedType(10), *field.type());
56+
EXPECT_FALSE(field.optional());
57+
EXPECT_EQ("foo bar (2): fixed(10) (required)", field.ToString());
58+
EXPECT_EQ("foo bar (2): fixed(10) (required)", std::format("{}", field));
59+
}
60+
}
61+
62+
TEST(SchemaFieldTest, Equality) {
63+
iceberg::SchemaField field1(1, "foo", std::make_shared<iceberg::Int32Type>(), false);
64+
iceberg::SchemaField field2(2, "foo", std::make_shared<iceberg::Int32Type>(), false);
65+
iceberg::SchemaField field3(1, "bar", std::make_shared<iceberg::Int32Type>(), false);
66+
iceberg::SchemaField field4(1, "foo", std::make_shared<iceberg::Int64Type>(), false);
67+
iceberg::SchemaField field5(1, "foo", std::make_shared<iceberg::Int32Type>(), true);
68+
iceberg::SchemaField field6(1, "foo", std::make_shared<iceberg::Int32Type>(), false);
69+
70+
ASSERT_EQ(field1, field1);
71+
ASSERT_NE(field1, field2);
72+
ASSERT_NE(field2, field1);
73+
ASSERT_NE(field1, field3);
74+
ASSERT_NE(field3, field2);
75+
ASSERT_NE(field1, field4);
76+
ASSERT_NE(field4, field1);
77+
ASSERT_NE(field1, field5);
78+
ASSERT_NE(field5, field1);
79+
ASSERT_EQ(field1, field6);
80+
ASSERT_EQ(field6, field1);
81+
}

test/core/schema_test.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)