| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| #include "paimon/common/utils/options_utils.h" | |
| #include "gtest/gtest.h" | |
| #include "paimon/testing/utils/testharness.h" | |
| namespace paimon::test { | |
| TEST(OptionsUtilsTest, TestGetValueFromMap) { | |
| std::map<std::string, std::string> key_value_map; | |
| key_value_map["key_int"] = "10"; | |
| key_value_map["key_bool"] = "true"; | |
| key_value_map["key_int16"] = "100"; | |
| key_value_map["key_double"] = "4.5E10"; | |
| // invalid | |
| key_value_map["key_bool2"] = "true1"; | |
| key_value_map["key_int8"] = "500"; | |
| key_value_map["key_int64"] = "ab"; | |
| ASSERT_OK_AND_ASSIGN(auto int32_value, | |
| OptionsUtils::GetValueFromMap<int32_t>(key_value_map, "key_int")); | |
| ASSERT_EQ(10, int32_value); | |
| ASSERT_OK_AND_ASSIGN(auto bool_value, | |
| OptionsUtils::GetValueFromMap<bool>(key_value_map, "key_bool")); | |
| ASSERT_TRUE(bool_value); | |
| ASSERT_OK_AND_ASSIGN(auto int16_value, | |
| OptionsUtils::GetValueFromMap<int16_t>(key_value_map, "key_int16")); | |
| ASSERT_EQ(100, int16_value); | |
| ASSERT_OK_AND_ASSIGN(auto double_value, | |
| OptionsUtils::GetValueFromMap<double>(key_value_map, "key_double")); | |
| ASSERT_NEAR(4.5E10, double_value, 0.00001); | |
| ASSERT_NOK_WITH_MSG(OptionsUtils::GetValueFromMap<bool>(key_value_map, "key_bool2"), | |
| "convert key key_bool2, value true1 to bool failed"); | |
| ASSERT_NOK_WITH_MSG(OptionsUtils::GetValueFromMap<int8_t>(key_value_map, "key_int8"), | |
| "convert key key_int8, value 500 to signed char failed"); | |
| ASSERT_NOK_WITH_MSG(OptionsUtils::GetValueFromMap<int64_t>(key_value_map, "key_int64"), | |
| "convert key key_int64, value ab to long failed"); | |
| ASSERT_NOK_WITH_MSG(OptionsUtils::GetValueFromMap<int64_t>(key_value_map, "key_int64", 10), | |
| "convert key key_int64, value ab to long failed"); | |
| ASSERT_OK_AND_ASSIGN( | |
| auto nonexist, OptionsUtils::GetValueFromMap<int32_t>(key_value_map, "key_nonexist", 233)); | |
| ASSERT_EQ(233, nonexist); | |
| ASSERT_OK_AND_ASSIGN(auto empty, | |
| OptionsUtils::GetValueFromMap<int32_t>(key_value_map, "", 999)); | |
| ASSERT_EQ(999, empty); | |
| ASSERT_OK_AND_ASSIGN(auto present, | |
| OptionsUtils::GetValueFromMap<int32_t>(key_value_map, "key_int", 233)); | |
| ASSERT_EQ(10, present); | |
| ASSERT_TRUE( | |
| OptionsUtils::GetValueFromMap<int32_t>(key_value_map, "missing").status().IsNotExist()); | |
| key_value_map["empty_string"] = ""; | |
| ASSERT_OK_AND_ASSIGN(std::string empty_string, OptionsUtils::GetValueFromMap<std::string>( | |
| key_value_map, "empty_string", "default")); | |
| ASSERT_TRUE(empty_string.empty()); | |
| } | |
| TEST(OptionsUtilsTest, TestGetOptionalValueFromMap) { | |
| const std::map<std::string, std::string> key_value_map = { | |
| {"key_int", "10"}, {"key_empty", ""}, {"key_invalid", "ab"}}; | |
| ASSERT_OK_AND_ASSIGN(std::optional<int32_t> optional_value, | |
| OptionsUtils::GetOptionalValueFromMap<int32_t>(key_value_map, "key_int")); | |
| ASSERT_EQ(std::optional<int32_t>(10), optional_value); | |
| ASSERT_OK_AND_ASSIGN( | |
| std::optional<int32_t> optional_missing, | |
| OptionsUtils::GetOptionalValueFromMap<int32_t>(key_value_map, "key_nonexist")); | |
| ASSERT_EQ(std::nullopt, optional_missing); | |
| ASSERT_OK_AND_ASSIGN( | |
| std::optional<std::string> optional_empty, | |
| OptionsUtils::GetOptionalValueFromMap<std::string>(key_value_map, "key_empty")); | |
| ASSERT_EQ(std::optional<std::string>(""), optional_empty); | |
| ASSERT_TRUE(OptionsUtils::GetOptionalValueFromMap<int64_t>(key_value_map, "key_invalid") | |
| .status() | |
| .IsInvalid()); | |
| } | |
| TEST(OptionsUtilsTest, TestFetchOptionsWithPrefix) { | |
| std::map<std::string, std::string> options = { | |
| {"key1", "value1"}, {"test.", "empty-key"}, {"test.key2", "value2"}}; | |
| auto new_options = OptionsUtils::FetchOptionsWithPrefix("test.", options); | |
| std::map<std::string, std::string> expected = {{"key2", "value2"}}; | |
| ASSERT_EQ(expected, new_options); | |
| } | |
| TEST(OptionsUtilsTest, TestGetNonEmptyValueFromMap) { | |
| std::map<std::string, std::string> options = {{"present", "value"}, {"empty", ""}}; | |
| ASSERT_OK_AND_ASSIGN(std::string value, | |
| OptionsUtils::GetNonEmptyValueFromMap(options, "present")); | |
| ASSERT_EQ("value", value); | |
| ASSERT_TRUE(OptionsUtils::GetNonEmptyValueFromMap(options, "missing").status().IsNotExist()); | |
| ASSERT_TRUE(OptionsUtils::GetNonEmptyValueFromMap(options, "empty").status().IsInvalid()); | |
| } | |
| } // namespace paimon::test |