Search in sources :

Example 6 with PutIfExists

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithPutIfExistsCondition_shouldThrowIllegalArgumentException.

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

Example 7 with PutIfExists

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

the class ConditionalQueryBuilderTest method visit_PutIfExistsAcceptCalled_ShouldNotCallWhere.

@Test
public void visit_PutIfExistsAcceptCalled_ShouldNotCallWhere() {
    // Arrange
    PutIfExists condition = new PutIfExists();
    ConditionalQueryBuilder builder = new ConditionalQueryBuilder(select);
    // Act
    condition.accept(builder);
    // Assert
    verify(select, never()).and(any(Condition.class));
}
Also used : Condition(org.jooq.Condition) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 8 with PutIfExists

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

the class JdbcServiceTest method whenPutOperationWithPutIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.where(any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.build()).thenReturn(updateQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenReturn(0);
    // Act
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.put(put, connection);
    // Assert
    assertThat(ret).isFalse();
    verify(operationChecker).check(any(Put.class));
    verify(queryBuilder).update(any(), any(), any());
}
Also used : Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 9 with PutIfExists

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

the class PutStatementHandlerTest method handle_PutIfExistsGiven_ShouldCallUpdateItemWithCondition.

@Test
public void handle_PutIfExistsGiven_ShouldCallUpdateItemWithCondition() {
    // Arrange
    when(client.updateItem(any(UpdateItemRequest.class))).thenReturn(updateResponse);
    Put put = preparePut().withCondition(new PutIfExists());
    DynamoMutation dynamoMutation = new DynamoMutation(put, metadata);
    Map<String, AttributeValue> expectedKeys = dynamoMutation.getKeyMap();
    String updateExpression = dynamoMutation.getUpdateExpression();
    String expectedCondition = dynamoMutation.getIfExistsCondition();
    Map<String, AttributeValue> expectedBindMap = dynamoMutation.getValueBindMap();
    // 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 : UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Put(com.scalar.db.api.Put) PutIfExists(com.scalar.db.api.PutIfExists) Test(org.junit.jupiter.api.Test)

Example 10 with PutIfExists

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

the class PutStatementHandlerTest method handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException.

@Test
public void handle_DynamoDbExceptionWithConditionalCheckFailed_ShouldThrowNoMutationException() {
    // Arrange
    ConditionalCheckFailedException toThrow = mock(ConditionalCheckFailedException.class);
    doThrow(toThrow).when(client).updateItem(any(UpdateItemRequest.class));
    Put put = preparePut().withCondition(new PutIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(put)).isInstanceOf(NoMutationException.class);
}
Also used : UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) Put(com.scalar.db.api.Put) 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