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