Search in sources :

Example 6 with PutIf

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithPutIfCondition_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingDeleteOperationWithPutIfCondition_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    MutationCondition condition = new PutIf();
    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) PutIf(com.scalar.db.api.PutIf) MutationCondition(com.scalar.db.api.MutationCondition) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 7 with PutIf

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

the class JdbcServiceTest method whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder() 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(0);
    // 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).isFalse();
    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 8 with PutIf

use of com.scalar.db.api.PutIf 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 9 with PutIf

use of com.scalar.db.api.PutIf 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 10 with PutIf

use of com.scalar.db.api.PutIf 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)

Aggregations

PutIf (com.scalar.db.api.PutIf)34 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 Put (com.scalar.db.api.Put)26 Test (org.junit.jupiter.api.Test)25 Get (com.scalar.db.api.Get)8 IntValue (com.scalar.db.io.IntValue)8 TextValue (com.scalar.db.io.TextValue)7 Key (com.scalar.db.io.Key)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)5 Result (com.scalar.db.api.Result)5 Test (org.junit.Test)5 Scan (com.scalar.db.api.Scan)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 HashMap (java.util.HashMap)3 Delete (com.scalar.db.api.Delete)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2 Mutation (com.scalar.db.api.Mutation)1 PutIfExists (com.scalar.db.api.PutIfExists)1 BooleanValue (com.scalar.db.io.BooleanValue)1