use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class RollbackMutationComposerTest method add_PutAndResultFromSnapshotGivenAndDeletedResultGivenFromStorage_ShouldComposePut.
@Test
public void add_PutAndResultFromSnapshotGivenAndDeletedResultGivenFromStorage_ShouldComposePut() throws ExecutionException {
// Arrange
composer = new RollbackMutationComposer(ANY_ID_2, storage, tableMetadataManager, mutations);
TransactionResult resultInSnapshot = prepareInitialResult(ANY_ID_1, TransactionState.COMMITTED);
TransactionResult result = prepareResult(TransactionState.DELETED);
when(storage.get(any(Get.class))).thenReturn(Optional.of(result));
Put put = preparePut();
// Act
composer.add(put, resultInSnapshot);
// 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_2), Operator.EQ), new ConditionalExpression(STATE, toStateValue(TransactionState.DELETED), Operator.EQ)));
expected.withValues(extractAfterValues(resultInSnapshot));
assertThat(actual).isEqualTo(expected);
verify(storage).get(any(Get.class));
}
use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class CommitMutationComposerTest method add_SelectionAndPreparedResultGiven_ShouldComposePutForRollforward.
@Test
public void add_SelectionAndPreparedResultGiven_ShouldComposePutForRollforward() {
// Arrange
Get get = prepareGet();
TransactionResult result = prepareResult(TransactionState.PREPARED);
// Act
composer.add(get, result);
// Assert
Put actual = (Put) mutations.get(0);
Put expected = new Put(get.getPartitionKey(), get.getClusteringKey().orElse(null)).forNamespace(get.forNamespace().get()).forTable(get.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.PutIf in project scalardb by scalar-labs.
the class PrepareMutationComposerTest method add_PutAndResultGiven_ShouldComposePutWithPutIfCondition.
@Test
public void add_PutAndResultGiven_ShouldComposePutWithPutIfCondition() {
// Arrange
Put put = preparePut();
TransactionResult result = prepareResult();
// Act
composer.add(put, result);
// Assert
Put actual = (Put) mutations.get(0);
put.withConsistency(Consistency.LINEARIZABLE);
put.withCondition(new PutIf(new ConditionalExpression(VERSION, toVersionValue(2), Operator.EQ), new ConditionalExpression(ID, toIdValue(ANY_ID_2), Operator.EQ)));
put.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
put.withValue(Attribute.toIdValue(ANY_ID_3));
put.withValue(Attribute.toStateValue(TransactionState.PREPARED));
put.withValue(Attribute.toVersionValue(3));
put.withValue(Attribute.toBeforePreparedAtValue(ANY_TIME_3));
put.withValue(Attribute.toBeforeCommittedAtValue(ANY_TIME_4));
put.withValue(Attribute.toBeforeIdValue(ANY_ID_2));
put.withValue(Attribute.toBeforeStateValue(TransactionState.COMMITTED));
put.withValue(Attribute.toBeforeVersionValue(2));
put.withValue(Attribute.BEFORE_PREFIX + ANY_NAME_3, ANY_INT_2);
assertThat(actual).isEqualTo(put);
}
use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class ConditionExpressionBuilderTest method build_PutIfAcceptCalled_ShouldReturnCondition.
@Test
public void build_PutIfAcceptCalled_ShouldReturnCondition() {
// Arrange
PutIf condition = ConditionBuilder.putIf(ConditionBuilder.column(ANY_NAME_1).isEqualToInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_2).isGreaterThanInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_3).isNullInt()).and(ConditionBuilder.column(ANY_NAME_4).isNotNullInt()).build();
ConditionExpressionBuilder builder = new ConditionExpressionBuilder(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS, DynamoOperation.CONDITION_VALUE_ALIAS);
// Act
condition.accept(builder);
String actual = builder.build();
// Assert
assertThat(actual).isEqualTo("#ccol0 = :cval0 AND #ccol1 > :cval1 " + "AND (attribute_not_exists(#ccol2) OR #ccol2 = :cval2) " + "AND (attribute_exists(#ccol3) AND NOT #ccol3 = :cval3)");
}
use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class DynamoMutationTest method getConditionColumnMap_PutGiven_ShouldReturnCondition.
@Test
public void getConditionColumnMap_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);
Map<String, String> expected = new HashMap<>();
expected.put(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "0", ANY_NAME_3);
expected.put(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS + "1", ANY_NAME_4);
DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
// Act
Map<String, String> conditionColumnMap = dynamoMutation.getConditionColumnMap();
// Assert
assertThat(conditionColumnMap).isEqualTo(expected);
}
Aggregations