Search in sources :

Example 6 with DeleteIfExists

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

the class BatchHandlerTest method handle_MultipleMutationsGiven_ShouldCallStoredProcedure.

@Test
public void handle_MultipleMutationsGiven_ShouldCallStoredProcedure() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    when(storedProcedure.execute(anyList(), any(CosmosStoredProcedureRequestOptions.class))).thenReturn(spResponse);
    Put put1 = preparePut();
    Put put2 = preparePut().withCondition(new PutIfNotExists());
    Delete delete1 = prepareDelete();
    Delete delete2 = prepareDelete().withCondition(new DeleteIfExists());
    CosmosMutation cosmosMutation1 = new CosmosMutation(put1, metadata);
    CosmosMutation cosmosMutation2 = new CosmosMutation(put2, metadata);
    CosmosMutation cosmosMutation3 = new CosmosMutation(delete1, metadata);
    CosmosMutation cosmosMutation4 = new CosmosMutation(delete2, metadata);
    Record record1 = cosmosMutation1.makeRecord();
    Record record2 = cosmosMutation2.makeRecord();
    Record emptyRecord = new Record();
    String query1 = cosmosMutation1.makeConditionalQuery();
    String query2 = cosmosMutation2.makeConditionalQuery();
    String query3 = cosmosMutation3.makeConditionalQuery();
    String query4 = cosmosMutation4.makeConditionalQuery();
    // Act Assert
    assertThatCode(() -> handler.handle(Arrays.asList(put1, put2, delete1, delete2))).doesNotThrowAnyException();
    // Assert
    verify(cosmosScripts).getStoredProcedure("mutate.js");
    verify(storedProcedure).execute(captor.capture(), any(CosmosStoredProcedureRequestOptions.class));
    assertThat(captor.getValue().get(0)).isEqualTo(4);
    assertThat(captor.getValue().get(1)).isEqualTo(CosmosMutation.MutationType.PUT.ordinal());
    assertThat(captor.getValue().get(2)).isEqualTo(CosmosMutation.MutationType.PUT_IF_NOT_EXISTS.ordinal());
    assertThat(captor.getValue().get(3)).isEqualTo(CosmosMutation.MutationType.DELETE_IF.ordinal());
    assertThat(captor.getValue().get(4)).isEqualTo(CosmosMutation.MutationType.DELETE_IF.ordinal());
    assertThat(captor.getValue().get(5)).isEqualTo(record1);
    assertThat(captor.getValue().get(6)).isEqualTo(record2);
    assertThat(captor.getValue().get(7)).isEqualTo(emptyRecord);
    assertThat(captor.getValue().get(8)).isEqualTo(emptyRecord);
    assertThat(captor.getValue().get(9)).isEqualTo(query1);
    assertThat(captor.getValue().get(10)).isEqualTo(query2);
    assertThat(captor.getValue().get(11)).isEqualTo(query3);
    assertThat(captor.getValue().get(12)).isEqualTo(query4);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 7 with DeleteIfExists

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

the class BatchHandlerTest method handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException.

@Test
public void handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException() {
    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());
    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) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 8 with DeleteIfExists

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

the class JdbcServiceTest method whenDeleteOperationWithDeleteIfExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder.

@Test
public void whenDeleteOperationWithDeleteIfExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.deleteFrom(any(), any(), any())).thenReturn(deleteQueryBuilder);
    when(deleteQueryBuilder.where(any(), any())).thenReturn(deleteQueryBuilder);
    when(deleteQueryBuilder.build()).thenReturn(deleteQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenReturn(1);
    // Act
    Delete delete = new Delete(new Key("p1", "val1")).withCondition(new DeleteIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.delete(delete, connection);
    // Assert
    assertThat(ret).isTrue();
    verify(operationChecker).check(any(Delete.class));
    verify(queryBuilder).deleteFrom(any(), any(), any());
}
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)

Example 9 with DeleteIfExists

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

the class DeleteStatementHandlerTest method handle_DeleteWithConditionsGiven_ShouldCallDeleteItem.

@Test
public void handle_DeleteWithConditionsGiven_ShouldCallDeleteItem() {
    // Arrange
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_2)));
    when(client.deleteItem(any(DeleteItemRequest.class))).thenReturn(response);
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    DynamoMutation dynamoMutation = new DynamoMutation(delete, metadata);
    // Act Assert
    assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<DeleteItemRequest> captor = ArgumentCaptor.forClass(DeleteItemRequest.class);
    verify(client).deleteItem(captor.capture());
    DeleteItemRequest actualRequest = captor.getValue();
    assertThat(actualRequest.key()).isEqualTo(dynamoMutation.getKeyMap());
    assertThat(actualRequest.conditionExpression()).isEqualTo(dynamoMutation.getIfExistsCondition());
}
Also used : Delete(com.scalar.db.api.Delete) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 10 with DeleteIfExists

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

the class DeleteStatementHandlerTest method handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException() {
    // Arrange
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_2)));
    when(client.deleteItem(any(DeleteItemRequest.class))).thenReturn(response);
    ConditionalCheckFailedException toThrow = mock(ConditionalCheckFailedException.class);
    doThrow(toThrow).when(client).deleteItem(any(DeleteItemRequest.class));
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(delete)).isInstanceOf(NoMutationException.class);
}
Also used : Delete(com.scalar.db.api.Delete) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) 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