Search in sources :

Example 16 with DeleteIfExists

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

the class JdbcDatabaseTest method whenMutateOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException.

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

Example 17 with DeleteIfExists

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

the class JdbcServiceTest method whenDeleteOperationWithDeleteIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenDeleteOperationWithDeleteIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder() 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(0);
    // 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).isFalse();
    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 18 with DeleteIfExists

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

the class DeleteStatementHandlerTest method handle_DeleteWithConditionsGiven_ShouldCallStoredProcedure.

@Test
public void handle_DeleteWithConditionsGiven_ShouldCallStoredProcedure() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    when(storedProcedure.execute(anyList(), any(CosmosStoredProcedureRequestOptions.class))).thenReturn(spResponse);
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    CosmosMutation cosmosMutation = new CosmosMutation(delete, metadata);
    String query = cosmosMutation.makeConditionalQuery();
    // Act Assert
    assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException();
    // Assert
    verify(cosmosScripts).getStoredProcedure("mutate.js");
    verify(storedProcedure).execute(captor.capture(), any(CosmosStoredProcedureRequestOptions.class));
    assertThat(captor.getValue().get(0)).isEqualTo(1);
    assertThat(captor.getValue().get(1)).isEqualTo(CosmosMutation.MutationType.DELETE_IF.ordinal());
    assertThat(captor.getValue().get(2)).isEqualTo(new Record());
    assertThat(captor.getValue().get(3)).isEqualTo(query);
}
Also used : Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 19 with DeleteIfExists

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

the class DeleteStatementHandlerTest method handle_DeleteWithConditionDynamoDbExceptionThrown_ShouldThrowExecutionException.

@Test
public void handle_DeleteWithConditionDynamoDbExceptionThrown_ShouldThrowExecutionException() {
    // Arrange
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_2)));
    when(client.deleteItem(any(DeleteItemRequest.class))).thenReturn(response);
    DynamoDbException toThrow = mock(DynamoDbException.class);
    doThrow(toThrow).when(client).deleteItem(any(DeleteItemRequest.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) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 20 with DeleteIfExists

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingDeleteOperationWithInvalidPartitionKey_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, "p3", "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    MutationCondition condition = new DeleteIfExists();
    Delete delete = new Delete(partitionKey, clusteringKey).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatThrownBy(() -> operationChecker.check(delete)).isInstanceOf(IllegalArgumentException.class);
}
Also used : Delete(com.scalar.db.api.Delete) MutationCondition(com.scalar.db.api.MutationCondition) 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