Search in sources :

Example 11 with DeleteIf

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

the class RollbackMutationComposerTest method add_ScanAndPreparedResultByThisGivenAndBeforeResultNotGiven_ShouldComposeDelete.

@Test
public void add_ScanAndPreparedResultByThisGivenAndBeforeResultNotGiven_ShouldComposeDelete() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareInitialResult(ANY_ID_2, TransactionState.PREPARED);
    Scan scan = prepareScan();
    // Act
    composer.add(scan, result);
    // Assert
    Delete actual = (Delete) mutations.get(0);
    Delete expected = new Delete(scan.getPartitionKey(), result.getClusteringKey().orElse(null)).forNamespace(scan.forNamespace().get()).forTable(scan.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new DeleteIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 12 with DeleteIf

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

the class RollbackMutationComposerTest method add_GetAndPreparedResultGivenAndBeforeResultNotGiven_ShouldComposeDelete.

@Test
public void add_GetAndPreparedResultGivenAndBeforeResultNotGiven_ShouldComposeDelete() throws ExecutionException {
    // Arrange
    composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
    TransactionResult result = prepareInitialResult(ANY_ID_2, TransactionState.PREPARED);
    Get get = prepareGet();
    // Act
    composer.add(get, result);
    // Assert
    Delete actual = (Delete) mutations.get(0);
    Delete expected = new Delete(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new DeleteIf(new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 13 with DeleteIf

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

the class JdbcServiceTest method whenDeleteOperationWithDeleteIfConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenDeleteOperationWithDeleteIfConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.deleteFrom(any(), any(), any())).thenReturn(deleteQueryBuilder);
    when(deleteQueryBuilder.where(any(), 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 DeleteIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).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) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Key(com.scalar.db.io.Key) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 14 with DeleteIf

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

the class CommitMutationComposerTest method add_DeleteGiven_ShouldComposeDeleteWithDeleteIfCondition.

@Test
public void add_DeleteGiven_ShouldComposeDeleteWithDeleteIfCondition() {
    // Arrange
    Delete delete = prepareDelete();
    // Act
    // result is not used, so it's set null
    composer.add(delete, null);
    // Assert
    Delete actual = (Delete) mutations.get(0);
    delete.withConsistency(Consistency.LINEARIZABLE);
    delete.withCondition(new DeleteIf(new ConditionalExpression(ID, toIdValue(ANY_ID), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
    assertThat(actual).isEqualTo(delete);
}
Also used : Delete(com.scalar.db.api.Delete) ConditionalExpression(com.scalar.db.api.ConditionalExpression) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 15 with DeleteIf

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

the class CommitMutationComposerTest method add_SelectionAndDeletedResultGiven_ShouldComposePutForRollforward.

@Test
public void add_SelectionAndDeletedResultGiven_ShouldComposePutForRollforward() {
    // Arrange
    Get get = prepareGet();
    TransactionResult result = prepareResult(TransactionState.DELETED);
    // Act
    composer.add(get, result);
    // Assert
    Delete actual = (Delete) mutations.get(0);
    Delete expected = new Delete(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new DeleteIf(new ConditionalExpression(ID, toIdValue(ANY_ID), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteIf (com.scalar.db.api.DeleteIf)20 ConditionalExpression (com.scalar.db.api.ConditionalExpression)18 Test (org.junit.jupiter.api.Test)15 Delete (com.scalar.db.api.Delete)13 TextValue (com.scalar.db.io.TextValue)12 Key (com.scalar.db.io.Key)10 IntValue (com.scalar.db.io.IntValue)6 Get (com.scalar.db.api.Get)5 Result (com.scalar.db.api.Result)5 Test (org.junit.Test)5 MutationCondition (com.scalar.db.api.MutationCondition)3 Put (com.scalar.db.api.Put)3 Scan (com.scalar.db.api.Scan)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 BooleanValue (com.scalar.db.io.BooleanValue)1 DoubleValue (com.scalar.db.io.DoubleValue)1 Value (com.scalar.db.io.Value)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1