Skip to content

Commit 85f1ade

Browse files
committed
add more unit tests
1 parent 91cd927 commit 85f1ade

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/iceberg/type.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ std::optional<std::reference_wrapper<const SchemaField>> MapType::GetFieldByInde
233233
int32_t index) const {
234234
if (index == 0) {
235235
return key();
236-
} else if (index == 0) {
236+
} else if (index == 1) {
237237
return value();
238238
}
239239
return std::nullopt;

test/core/type_test.cc

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,77 @@ TEST(TypeTest, List) {
342342
::testing::HasSubstr("child field name should be 'element', was 'wrongname'")));
343343
}
344344

345-
TEST(TypeTest, Map) {}
345+
TEST(TypeTest, Map) {
346+
{
347+
iceberg::SchemaField key(5, "key", std::make_shared<iceberg::Int32Type>(), true);
348+
iceberg::SchemaField value(7, "value", std::make_shared<iceberg::StringType>(), true);
349+
iceberg::MapType map(key, value);
350+
std::span<const iceberg::SchemaField> fields = map.fields();
351+
ASSERT_EQ(2, fields.size());
352+
ASSERT_EQ(key, fields[0]);
353+
ASSERT_EQ(value, fields[1]);
354+
ASSERT_THAT(map.GetFieldById(5), ::testing::Optional(key));
355+
ASSERT_THAT(map.GetFieldById(7), ::testing::Optional(value));
356+
ASSERT_THAT(map.GetFieldByIndex(0), ::testing::Optional(key));
357+
ASSERT_THAT(map.GetFieldByIndex(1), ::testing::Optional(value));
358+
ASSERT_THAT(map.GetFieldByName("key"), ::testing::Optional(key));
359+
ASSERT_THAT(map.GetFieldByName("value"), ::testing::Optional(value));
360+
361+
ASSERT_EQ(std::nullopt, map.GetFieldById(0));
362+
ASSERT_EQ(std::nullopt, map.GetFieldByIndex(2));
363+
ASSERT_EQ(std::nullopt, map.GetFieldByIndex(-1));
364+
ASSERT_EQ(std::nullopt, map.GetFieldByName("element"));
365+
}
366+
ASSERT_THAT(
367+
[]() {
368+
iceberg::SchemaField key(5, "notkey", std::make_shared<iceberg::Int32Type>(),
369+
true);
370+
iceberg::SchemaField value(7, "value", std::make_shared<iceberg::StringType>(),
371+
true);
372+
iceberg::MapType map(key, value);
373+
},
374+
::testing::ThrowsMessage<std::runtime_error>(
375+
::testing::HasSubstr("key field name should be 'key', was 'notkey'")));
376+
ASSERT_THAT(
377+
[]() {
378+
iceberg::SchemaField key(5, "key", std::make_shared<iceberg::Int32Type>(), true);
379+
iceberg::SchemaField value(7, "notvalue", std::make_shared<iceberg::StringType>(),
380+
true);
381+
iceberg::MapType map(key, value);
382+
},
383+
::testing::ThrowsMessage<std::runtime_error>(
384+
::testing::HasSubstr("value field name should be 'value', was 'notvalue'")));
385+
}
346386

347-
TEST(TypeTest, Struct) {}
387+
TEST(TypeTest, Struct) {
388+
{
389+
iceberg::SchemaField field1(5, "foo", std::make_shared<iceberg::Int32Type>(), true);
390+
iceberg::SchemaField field2(7, "bar", std::make_shared<iceberg::StringType>(), true);
391+
iceberg::StructType struct_({field1, field2});
392+
std::span<const iceberg::SchemaField> fields = struct_.fields();
393+
ASSERT_EQ(2, fields.size());
394+
ASSERT_EQ(field1, fields[0]);
395+
ASSERT_EQ(field2, fields[1]);
396+
ASSERT_THAT(struct_.GetFieldById(5), ::testing::Optional(field1));
397+
ASSERT_THAT(struct_.GetFieldById(7), ::testing::Optional(field2));
398+
ASSERT_THAT(struct_.GetFieldByIndex(0), ::testing::Optional(field1));
399+
ASSERT_THAT(struct_.GetFieldByIndex(1), ::testing::Optional(field2));
400+
ASSERT_THAT(struct_.GetFieldByName("foo"), ::testing::Optional(field1));
401+
ASSERT_THAT(struct_.GetFieldByName("bar"), ::testing::Optional(field2));
402+
403+
ASSERT_EQ(std::nullopt, struct_.GetFieldById(0));
404+
ASSERT_EQ(std::nullopt, struct_.GetFieldByIndex(2));
405+
ASSERT_EQ(std::nullopt, struct_.GetFieldByIndex(-1));
406+
ASSERT_EQ(std::nullopt, struct_.GetFieldByName("element"));
407+
}
408+
ASSERT_THAT(
409+
[]() {
410+
iceberg::SchemaField field1(5, "foo", std::make_shared<iceberg::Int32Type>(),
411+
true);
412+
iceberg::SchemaField field2(5, "bar", std::make_shared<iceberg::StringType>(),
413+
true);
414+
iceberg::StructType struct_({field1, field2});
415+
},
416+
::testing::ThrowsMessage<std::runtime_error>(
417+
::testing::HasSubstr("duplicate field ID 5")));
418+
}

0 commit comments

Comments
 (0)