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