Search in sources :

Example 26 with PutIfNotExists

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

the class ConditionExpressionBuilderTest method visit_PutIfNotExistsAcceptCalled_ShouldReturnEmpty.

@Test
public void visit_PutIfNotExistsAcceptCalled_ShouldReturnEmpty() {
    // Arrange
    PutIfNotExists condition = new PutIfNotExists();
    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 : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Test(org.junit.jupiter.api.Test)

Example 27 with PutIfNotExists

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

the class PutStatementHandlerTest method handle_PutIfNotExistsGiven_ShouldCallUpdateItemWithCondition.

@Test
public void handle_PutIfNotExistsGiven_ShouldCallUpdateItemWithCondition() {
    // Arrange
    when(client.updateItem(any(UpdateItemRequest.class))).thenReturn(updateResponse);
    Put put = preparePut().withCondition(new PutIfNotExists());
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    Map<String, AttributeValue> expectedKeys = dynamoMutation.getKeyMap();
    String updateExpression = dynamoMutation.getUpdateExpressionWithKey();
    String expectedCondition = dynamoMutation.getIfNotExistsCondition();
    Map<String, AttributeValue> expectedBindMap = dynamoMutation.getValueBindMapWithKey();
    // Act Assert
    assertThatCode(() -> handler.handle(put)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<UpdateItemRequest> captor = ArgumentCaptor.forClass(UpdateItemRequest.class);
    verify(client).updateItem(captor.capture());
    UpdateItemRequest actualRequest = captor.getValue();
    assertThat(actualRequest.key()).isEqualTo(expectedKeys);
    assertThat(actualRequest.updateExpression()).isEqualTo(updateExpression);
    assertThat(actualRequest.conditionExpression()).isEqualTo(expectedCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 28 with PutIfNotExists

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithPutIfNotExistsCondition_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingDeleteOperationWithPutIfNotExistsCondition_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    MutationCondition condition = new PutIfNotExists();
    Delete delete = new Delete(partitionKey, clusteringKey).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatThrownBy(() -> operationChecker.check(delete)).isInstanceOf(IllegalArgumentException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) MutationCondition(com.scalar.db.api.MutationCondition) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 29 with PutIfNotExists

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

the class InsertStatementHandlerTest method handle_WTEWithCasThrown_ShouldThrowProperExecutionException.

@Test
public void handle_WTEWithCasThrown_ShouldThrowProperExecutionException() {
    // Arrange
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfNotExists());
    spy = prepareSpiedInsertStatementHandler();
    WriteTimeoutException toThrow = mock(WriteTimeoutException.class);
    when(toThrow.getWriteType()).thenReturn(WriteType.CAS);
    doThrow(toThrow).when(spy).handleInternal(put);
    // Act Assert
    assertThatThrownBy(() -> spy.handle(put)).isInstanceOf(RetriableExecutionException.class).hasCause(toThrow);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) Test(org.junit.jupiter.api.Test)

Example 30 with PutIfNotExists

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

the class InsertStatementHandlerTest method handle_PutWithConditionGivenAndWTEWithSimpleThrown_ShouldThrowProperExecutionException.

@Test
public void handle_PutWithConditionGivenAndWTEWithSimpleThrown_ShouldThrowProperExecutionException() {
    // Arrange
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfNotExists());
    spy = prepareSpiedInsertStatementHandler();
    WriteTimeoutException toThrow = mock(WriteTimeoutException.class);
    when(toThrow.getWriteType()).thenReturn(WriteType.SIMPLE);
    doThrow(toThrow).when(spy).handleInternal(put);
    // Act Assert
    assertThatThrownBy(() -> spy.handle(put)).isInstanceOf(ReadRepairableExecutionException.class).hasCause(toThrow);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

PutIfNotExists (com.scalar.db.api.PutIfNotExists)37 Put (com.scalar.db.api.Put)27 Test (org.junit.jupiter.api.Test)26 Key (com.scalar.db.io.Key)15 Delete (com.scalar.db.api.Delete)8 Test (org.junit.Test)7 DeleteIfExists (com.scalar.db.api.DeleteIfExists)6 MutationCondition (com.scalar.db.api.MutationCondition)6 Result (com.scalar.db.api.Result)6 TextValue (com.scalar.db.io.TextValue)6 PutIf (com.scalar.db.api.PutIf)5 Scan (com.scalar.db.api.Scan)5 IntValue (com.scalar.db.io.IntValue)5 Value (com.scalar.db.io.Value)5 ConditionalExpression (com.scalar.db.api.ConditionalExpression)4 BooleanValue (com.scalar.db.io.BooleanValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 DoubleValue (com.scalar.db.io.DoubleValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3