| /* |
| * 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 "iceberg/transaction.h" |
| |
| #include "iceberg/expression/expressions.h" |
| #include "iceberg/expression/term.h" |
| #include "iceberg/sort_order.h" |
| #include "iceberg/test/matchers.h" |
| #include "iceberg/test/update_test_base.h" |
| #include "iceberg/transform.h" |
| #include "iceberg/update/update_properties.h" |
| #include "iceberg/update/update_sort_order.h" |
| |
| namespace iceberg { |
| |
| class TransactionTest : public UpdateTestBase {}; |
| |
| TEST_F(TransactionTest, CreateTransaction) { |
| ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| EXPECT_NE(txn, nullptr); |
| EXPECT_EQ(txn->table(), table_); |
| } |
| |
| TEST_F(TransactionTest, CommitEmptyTransaction) { |
| ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| EXPECT_THAT(txn->Commit(), IsOk()); |
| } |
| |
| TEST_F(TransactionTest, CommitTransactionWithPropertyUpdate) { |
| ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| ICEBERG_UNWRAP_OR_FAIL(auto update, txn->NewUpdateProperties()); |
| |
| update->Set("txn.property", "txn.value"); |
| EXPECT_THAT(update->Commit(), IsOk()); |
| |
| ICEBERG_UNWRAP_OR_FAIL(auto updated_table, txn->Commit()); |
| EXPECT_NE(updated_table, nullptr); |
| |
| // Reload table and verify the property was set |
| ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| const auto& props = reloaded->properties().configs(); |
| EXPECT_EQ(props.at("txn.property"), "txn.value"); |
| } |
| |
| TEST_F(TransactionTest, MultipleUpdatesInTransaction) { |
| ICEBERG_UNWRAP_OR_FAIL(auto txn, table_->NewTransaction()); |
| |
| // First update: set property |
| ICEBERG_UNWRAP_OR_FAIL(auto update1, txn->NewUpdateProperties()); |
| update1->Set("key1", "value1").Set("key2", "value2"); |
| EXPECT_THAT(update1->Commit(), IsOk()); |
| |
| // Second update: update sort order |
| ICEBERG_UNWRAP_OR_FAIL(auto update2, txn->NewUpdateSortOrder()); |
| auto term = |
| UnboundTransform::Make(Expressions::Ref("x"), Transform::Identity()).value(); |
| update2->AddSortField(std::move(term), SortDirection::kAscending, NullOrder::kFirst); |
| EXPECT_THAT(update2->Commit(), IsOk()); |
| |
| // Commit transaction |
| ICEBERG_UNWRAP_OR_FAIL(auto updated_table, txn->Commit()); |
| |
| // Verify properties were set |
| ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); |
| const auto& props = reloaded->properties().configs(); |
| EXPECT_EQ(props.at("key1"), "value1"); |
| EXPECT_EQ(props.at("key2"), "value2"); |
| |
| // Verify sort order was updated |
| ICEBERG_UNWRAP_OR_FAIL(auto sort_order, reloaded->sort_order()); |
| std::vector<SortField> expected_fields; |
| expected_fields.emplace_back(1, Transform::Identity(), SortDirection::kAscending, |
| NullOrder::kFirst); |
| ICEBERG_UNWRAP_OR_FAIL( |
| auto expected_sort_order, |
| SortOrder::Make(sort_order->order_id(), std::move(expected_fields))); |
| EXPECT_EQ(*sort_order, *expected_sort_order); |
| } |
| |
| } // namespace iceberg |