More unit test fixes + throw error when transaction level is invalid.

This commit is contained in:
Vedanshu Jain 2023-07-04 16:22:07 +05:30
parent 3c59b8b4dc
commit f02b6c5b26
2 changed files with 26 additions and 32 deletions

View File

@ -101,7 +101,9 @@ class PostsToOrdersMigrationController {
return;
}
$this->commit_transaction();
if ( $using_transactions ) {
$this->commit_transaction();
}
}
@ -149,6 +151,8 @@ class PostsToOrdersMigrationController {
* Start a database transaction if the configuration mandates so.
*
* @return bool|null True if transaction started, false if transactions won't be used, null if transaction failed to start.
*
* @throws \Exception If the transaction isolation level is invalid.
*/
private function maybe_start_transaction(): ?bool {
@ -157,7 +161,12 @@ class PostsToOrdersMigrationController {
return null;
}
$transaction_isolation_level = get_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, CustomOrdersTableController::DEFAULT_DB_TRANSACTIONS_ISOLATION_LEVEL );
$transaction_isolation_level = get_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, CustomOrdersTableController::DEFAULT_DB_TRANSACTIONS_ISOLATION_LEVEL );
$valid_transaction_isolation_levels = array( 'READ UNCOMMITTED', 'READ COMMITTED', 'REPEATABLE READ', 'SERIALIZABLE' );
if ( ! in_array( $transaction_isolation_level, $valid_transaction_isolation_levels, true ) ) {
throw new \Exception( "Invalid database transaction isolation level name $transaction_isolation_level" );
}
$set_transaction_isolation_level_command = "SET TRANSACTION ISOLATION LEVEL $transaction_isolation_level";
// We suppress errors in transaction isolation level setting because it's not supported by all DB engines, additionally, this might be executing in context of another transaction with a different isolation level.

View File

@ -135,27 +135,6 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
// phpcs:enable
}
/**
* Test that when an order is partially migrated, it can still be resumed as expected.
*/
public function test_interrupted_migration() {
$this->markTestSkipped();
}
/**
* Test that invalid order data is not migrated but logged.
*/
public function test_migrating_invalid_order_data() {
$this->markTestSkipped();
}
/**
* Test when one order is invalid but other one is valid in a migration batch.
*/
public function test_migrating_invalid_valid_order_combo() {
$this->markTestSkipped();
}
/**
* Helper method to get order object from COT.
*
@ -576,22 +555,28 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
*/
public function test_db_transaction_is_rolled_back_on_db_error() {
update_option( CustomOrdersTableController::USE_DB_TRANSACTIONS_OPTION, 'yes' );
update_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, 'SERIALIZABLE' );
update_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, 'READ UNCOMMITTED' );
$wpdb_mock = $this->use_wpdb_mock();
$wpdb_mock->register_method_replacement(
'get_results',
function( ...$args ) {
$wpdb_decorator = $args[0];
$wpdb_decorator->decorated_object->last_error = 'Something failed!';
return false;
'query',
function( $wpdb_decorator, $query ) {
$result = $this->fake_query_transaction_logger( $wpdb_decorator, $query, false );
if ( str_contains( $query, 'INSERT INTO ' . OrdersTableDataStore::get_orders_table_name() ) ) {
$wpdb_decorator->decorated_object->last_error = 'Something failed!';
}
if ( str_contains( $query, 'SET TRANSACTION ISOLATION LEVEL' ) ) {
$wpdb_decorator->decorated_object->last_error = '';
return true;
}
return $result;
}
);
$this->create_and_migrate_order();
$expected = array(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
'START TRANSACTION',
'ROLLBACK',
);
@ -715,7 +700,7 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
*
* @return bool
*/
private function fake_query_transaction_logger( $wpdb_decorator, $query, $transaction_fails ) {
private function fake_query_transaction_logger( DynamicDecorator $wpdb_decorator, $query, $transaction_fails ) {
$is_transaction_related_query =
StringUtil::contains( $query, 'TRANSACTION' ) ||
StringUtil::contains( $query, 'COMMIT' ) ||
@ -732,7 +717,7 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
return true;
}
} else {
return $wpdb_decorator->call_original_method( 'query', $query );
return $wpdb_decorator->decorated_object->query( $query );
}
}