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();
}
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());
}
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);
}
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);
}
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);
}
Aggregations