use of com.scalar.db.api.ConditionalExpression 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.ConditionalExpression 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);
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class DynamoMutationTest method getConditionBindMap_PutWithPutIfGiven_ShouldReturnBindMap.
@Test
public void getConditionBindMap_PutWithPutIfGiven_ShouldReturnBindMap() {
// 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, AttributeValue> expected = new HashMap<>();
expected.put(DynamoOperation.CONDITION_VALUE_ALIAS + "0", AttributeValue.builder().n(String.valueOf(ANY_INT_3)).build());
expected.put(DynamoOperation.CONDITION_VALUE_ALIAS + "1", AttributeValue.builder().n(String.valueOf(ANY_INT_3)).build());
DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
// Act
Map<String, AttributeValue> actual = dynamoMutation.getConditionBindMap();
// Assert
assertThat(actual).isEqualTo(expected);
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class ConditionalQueryBuilderTest method visit_PutIfAcceptCalled_ShouldCallWhere.
@Test
public void visit_PutIfAcceptCalled_ShouldCallWhere() {
// Arrange
PutIf condition = new PutIf(new ConditionalExpression(ANY_NAME_1, ANY_INT_VALUE, Operator.EQ), new ConditionalExpression(ANY_NAME_2, ANY_INT_VALUE, Operator.GT));
ConditionalQueryBuilder builder = new ConditionalQueryBuilder(select);
// Act
condition.accept(builder);
// Assert
verify(select).and(DSL.field("r.values[\"" + ANY_NAME_1 + "\"]").equal(ANY_INT));
verify(select).and(DSL.field("r.values[\"" + ANY_NAME_2 + "\"]").greaterThan(ANY_INT));
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class CosmosMutationTest method makeConditionalQuery_MutationWithConditionsGiven_ShouldReturnQuery.
@Test
public void makeConditionalQuery_MutationWithConditionsGiven_ShouldReturnQuery() {
// 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);
CosmosMutation cosmosMutation = new CosmosMutation(put, metadata);
String id = cosmosMutation.getId();
// Act
String actual = cosmosMutation.makeConditionalQuery();
// Assert
assertThat(actual).isEqualTo("select * from Record r where (r.id = '" + id + "' and r.values[\"" + ANY_NAME_3 + "\"]" + " = " + ANY_INT_3 + " and r.values[\"" + ANY_NAME_4 + "\"]" + " > " + ANY_INT_3 + ")");
}
Aggregations