Search in sources :

Example 11 with DeleteIfExists

use of com.scalar.db.api.DeleteIfExists in project scalardb by scalar-labs.

the class DeleteStatementHandlerTest method handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException.

@Test
public void handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    CosmosException toThrow = mock(CosmosException.class);
    doThrow(toThrow).when(storedProcedure).execute(anyList(), any(CosmosStoredProcedureRequestOptions.class));
    when(toThrow.getSubStatusCode()).thenReturn(CosmosErrorCode.PRECONDITION_FAILED.get());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(delete)).isInstanceOf(NoMutationException.class);
}
Also used : Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 12 with DeleteIfExists

use of com.scalar.db.api.DeleteIfExists in project scalardb by scalar-labs.

the class DeleteStatementHandlerTest method handle_DeleteWithConditionCosmosExceptionThrown_ShouldThrowExecutionException.

@Test
public void handle_DeleteWithConditionCosmosExceptionThrown_ShouldThrowExecutionException() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    CosmosException toThrow = mock(CosmosException.class);
    doThrow(toThrow).when(storedProcedure).execute(anyList(), any(CosmosStoredProcedureRequestOptions.class));
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(delete)).isInstanceOf(ExecutionException.class).hasCause(toThrow);
}
Also used : Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 13 with DeleteIfExists

use of com.scalar.db.api.DeleteIfExists in project scalardb by scalar-labs.

the class BatchHandlerTest method handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_TransactionCanceledExceptionWithConditionCheckFailed_ShouldThrowNoMutationException() {
    TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("ConditionalCheckFailed").build()).build();
    doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
    Put put = preparePut().withCondition(new PutIfNotExists());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(NoMutationException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 14 with DeleteIfExists

use of com.scalar.db.api.DeleteIfExists in project scalardb by scalar-labs.

the class BatchHandlerTest method handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException.

@Test
public void handle_TransactionCanceledExceptionWithTransactionConflict_ShouldThrowRetriableExecutionException() {
    TransactionCanceledException toThrow = TransactionCanceledException.builder().cancellationReasons(CancellationReason.builder().code("TransactionConflict").build(), CancellationReason.builder().code("None").build()).build();
    doThrow(toThrow).when(client).transactWriteItems(any(TransactWriteItemsRequest.class));
    Put put = preparePut().withCondition(new PutIfNotExists());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(RetriableExecutionException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 15 with DeleteIfExists

use of com.scalar.db.api.DeleteIfExists in project scalardb by scalar-labs.

the class JdbcDatabaseTest method whenDeleteOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException.

@Test
public void whenDeleteOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException() throws Exception {
    // Arrange
    when(jdbcService.delete(any(), any())).thenReturn(false);
    // Act Assert
    assertThatThrownBy(() -> {
        Delete delete = new Delete(new Key("p1", "val1")).withCondition(new DeleteIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
        jdbcDatabase.delete(delete);
    }).isInstanceOf(NoMutationException.class);
    verify(connection).close();
}
Also used : Delete(com.scalar.db.api.Delete) Key(com.scalar.db.io.Key) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteIfExists (com.scalar.db.api.DeleteIfExists)25 Delete (com.scalar.db.api.Delete)22 Test (org.junit.jupiter.api.Test)20 Key (com.scalar.db.io.Key)11 Put (com.scalar.db.api.Put)9 PutIfNotExists (com.scalar.db.api.PutIfNotExists)6 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)5 Test (org.junit.Test)5 MutationCondition (com.scalar.db.api.MutationCondition)4 CosmosException (com.azure.cosmos.CosmosException)3 Result (com.scalar.db.api.Result)3 TextValue (com.scalar.db.io.TextValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 DeleteItemRequest (software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3 ConditionalExpression (com.scalar.db.api.ConditionalExpression)2 DeleteIf (com.scalar.db.api.DeleteIf)2 Scan (com.scalar.db.api.Scan)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 TransactionCanceledException (software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException)2