Search in sources :

Example 16 with PutIfExists

use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.

the class PutStatementHandlerTest method handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException.

@Test
public void handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    CosmosException toThrow = mock(CosmosException.class);
    doThrow(toThrow).when(storedProcedure).execute(anyList(), any(CosmosStoredProcedureRequestOptions.class));
    when(toThrow.getSubStatusCode()).thenReturn(CosmosErrorCode.PRECONDITION_FAILED.get());
    Put put = preparePut().withCondition(new PutIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(put)).isInstanceOf(NoMutationException.class);
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 17 with PutIfExists

use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.

the class ConditionExpressionBuilderTest method visit_PutIfExistsAcceptCalled_ShouldReturnEmpty.

@Test
public void visit_PutIfExistsAcceptCalled_ShouldReturnEmpty() {
    // Arrange
    PutIfExists condition = new PutIfExists();
    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("");
}
Also used : PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 18 with PutIfExists

use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.

the class DynamoMutationTest method getValueBindMap_PutWithPutIfExistsGiven_ShouldReturnBindMap.

@Test
public void getValueBindMap_PutWithPutIfExistsGiven_ShouldReturnBindMap() {
    // Arrange
    Put put = preparePut().withCondition(new PutIfExists());
    Map<String, AttributeValue> expected = new HashMap<>();
    expected.put(DynamoOperation.VALUE_ALIAS + "0", AttributeValue.builder().n(String.valueOf(ANY_INT_1)).build());
    expected.put(DynamoOperation.VALUE_ALIAS + "1", AttributeValue.builder().n(String.valueOf(ANY_INT_2)).build());
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    // Act
    Map<String, AttributeValue> actual = dynamoMutation.getValueBindMap();
    // Assert
    assertThat(actual).isEqualTo(expected);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 19 with PutIfExists

use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.

the class DynamoMutationTest method getUpdateExpression_PutWithIfExistsGiven_ShouldReturnExpression.

@Test
public void getUpdateExpression_PutWithIfExistsGiven_ShouldReturnExpression() {
    // Arrange
    Put put = preparePut().withCondition(new PutIfExists());
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    // Act
    String actual = dynamoMutation.getUpdateExpression();
    // Assert
    assertThat(actual).isEqualTo("SET " + DynamoOperation.COLUMN_NAME_ALIAS + "0 = " + DynamoOperation.VALUE_ALIAS + "0, " + DynamoOperation.COLUMN_NAME_ALIAS + "1 = " + DynamoOperation.VALUE_ALIAS + "1");
}
Also used : Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 20 with PutIfExists

use of com.scalar.db.api.PutIfExists in project scalardb by scalar-labs.

the class UpdateStatementHandlerTest method prepare_PutOperationWithIfExistsGiven_ShouldPrepareProperQuery.

@Test
public void prepare_PutOperationWithIfExistsGiven_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "UPDATE", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "SET", ANY_NAME_3 + "=?", "WHERE", ANY_NAME_1 + "=?", "AND", ANY_NAME_2 + "=?", "IF EXISTS;" });
    configureBehavior(expected);
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfExists());
    // Act
    handler.prepare(put);
    // Assert
    verify(session).prepare(expected);
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

PutIfExists (com.scalar.db.api.PutIfExists)21 Test (org.junit.jupiter.api.Test)18 Put (com.scalar.db.api.Put)15 Key (com.scalar.db.io.Key)6 MutationCondition (com.scalar.db.api.MutationCondition)5 IntValue (com.scalar.db.io.IntValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 BooleanValue (com.scalar.db.io.BooleanValue)3 DoubleValue (com.scalar.db.io.DoubleValue)3 TextValue (com.scalar.db.io.TextValue)3 Value (com.scalar.db.io.Value)3 CosmosException (com.azure.cosmos.CosmosException)2 Get (com.scalar.db.api.Get)2 Result (com.scalar.db.api.Result)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 BoundStatement (com.datastax.driver.core.BoundStatement)1