Search in sources :

Example 16 with ConditionalExpression

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

the class JdbcServiceTest method whenPutOperationWithPutIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.where(any(), any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.build()).thenReturn(updateQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenReturn(1);
    // Act
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.put(put, connection);
    // Assert
    assertThat(ret).isTrue();
    verify(operationChecker).check(any(Put.class));
    verify(queryBuilder).update(any(), any(), any());
}
Also used : PutIf(com.scalar.db.api.PutIf) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 17 with ConditionalExpression

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

the class JdbcServiceTest method whenDeleteOperationWithDeleteIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder.

@Test
public void whenDeleteOperationWithDeleteIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder() 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(1);
    // 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).isTrue();
    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 18 with ConditionalExpression

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

the class DynamoMutationTest method getCondition_PutGiven_ShouldReturnCondition.

@Test
public void getCondition_PutGiven_ShouldReturnCondition() {
    // Arrange
    PutIf conditions = new PutIf(new ConditionalExpression(ANY_NAME_3, ANY_INT_VALUE, Operator.EQ), new ConditionalExpression(ANY_NAME_4, ANY_INT_VALUE, Operator.GT));
    Put put = preparePut().withCondition(conditions);
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    // Act
    String actual = dynamoMutation.getCondition();
    // Assert
    assertThat(actual).isEqualTo(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "0 = " + DynamoOperation.CONDITION_VALUE_ALIAS + "0 AND " + DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "1 > " + DynamoOperation.CONDITION_VALUE_ALIAS + "1");
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 19 with ConditionalExpression

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

the class CommitMutationComposerTest method add_PutGiven_ShouldComposePutWithPutIfCondition.

@Test
public void add_PutGiven_ShouldComposePutWithPutIfCondition() {
    // Arrange
    Put put = preparePut();
    // Act
    // result is not used, so it's set null
    composer.add(put, null);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(put.getPartitionKey(), put.getClusteringKey().orElse(null)).forNamespace(put.forNamespace().get()).forTable(put.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(ID, toIdValue(ANY_ID), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.PREPARED), Operator.EQ)));
    expected.withValue(Attribute.toCommittedAtValue(ANY_TIME_2));
    expected.withValue(Attribute.toStateValue(TransactionState.COMMITTED));
    assertThat(actual).isEqualTo(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 20 with ConditionalExpression

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

the class PrepareMutationComposerTest method delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition.

@Test
public void delete_DeleteAndResultGiven_ShouldComposePutWithPutIfCondition() {
    // Arrange
    Delete delete = prepareDelete();
    TransactionResult result = prepareResult();
    // Act
    composer.add(delete, result);
    // Assert
    Put actual = (Put) mutations.get(0);
    Put expected = new Put(delete.getPartitionKey(), delete.getClusteringKey().orElse(null)).forNamespace(delete.forNamespace().get()).forTable(delete.forTable().get());
    expected.withConsistency(Consistency.LINEARIZABLE);
    expected.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(2), Operator.EQ), new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ)));
    expected.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
    expected.withValue(Attribute.toIdValue(ANY_ID_3));
    expected.withValue(Attribute.toStateValue(TransactionState.DELETED));
    expected.withValue(Attribute.toVersionValue(3));
    expected.withValue(Attribute.toBeforePreparedAtValue(ANY_TIME_3));
    expected.withValue(Attribute.toBeforeCommittedAtValue(ANY_TIME_4));
    expected.withValue(Attribute.toBeforeIdValue(ANY_ID_2));
    expected.withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED));
    expected.withValue(Attribute.toBeforeVersionValue(2));
    expected.withValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_2);
    assertThat(actual).isEqualTo(expected);
}
Also used : Delete(com.scalar.db.api.Delete) PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Aggregations

ConditionalExpression (com.scalar.db.api.ConditionalExpression)55 Test (org.junit.jupiter.api.Test)35 PutIf (com.scalar.db.api.PutIf)30 Put (com.scalar.db.api.Put)27 TextValue (com.scalar.db.io.TextValue)22 DeleteIf (com.scalar.db.api.DeleteIf)18 Key (com.scalar.db.io.Key)18 Delete (com.scalar.db.api.Delete)14 Get (com.scalar.db.api.Get)13 IntValue (com.scalar.db.io.IntValue)13 Test (org.junit.Test)12 Result (com.scalar.db.api.Result)10 Scan (com.scalar.db.api.Scan)7 HashMap (java.util.HashMap)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)4 PreparedStatement (java.sql.PreparedStatement)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2