Search in sources :

Example 41 with ConditionalExpression

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);
}
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 42 with ConditionalExpression

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);
}
Also used : PutIf(com.scalar.db.api.PutIf) HashMap(java.util.HashMap) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 43 with ConditionalExpression

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);
}
Also used : PutIf(com.scalar.db.api.PutIf) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 44 with ConditionalExpression

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));
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Test(org.junit.jupiter.api.Test)

Example 45 with ConditionalExpression

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

ConditionalExpression (com.scalar.db.api.ConditionalExpression)55 Test (org.junit.jupiter.api.Test)35 PutIf (com.scalar.db.api.PutIf)30 Put (com.scalar.db.api.Put)27 TextValue (com.scalar.db.io.TextValue)22 DeleteIf (com.scalar.db.api.DeleteIf)18 Key (com.scalar.db.io.Key)18 Delete (com.scalar.db.api.Delete)14 Get (com.scalar.db.api.Get)13 IntValue (com.scalar.db.io.IntValue)13 Test (org.junit.Test)12 Result (com.scalar.db.api.Result)10 Scan (com.scalar.db.api.Scan)7 HashMap (java.util.HashMap)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)4 PreparedStatement (java.sql.PreparedStatement)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2