Search in sources :

Example 21 with DeleteIfExists

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithoutAnyClusteringKey_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingDeleteOperationWithoutAnyClusteringKey_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = null;
    MutationCondition condition = new DeleteIfExists();
    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) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 22 with DeleteIfExists

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

the class OperationCheckerTest method whenCheckingPutOperationWithDeleteIfExistsCondition_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingPutOperationWithDeleteIfExistsCondition_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    List<Value<?>> values = Arrays.asList(new IntValue(COL1, 1), new DoubleValue(COL2, 0.1), new BooleanValue(COL3, true));
    MutationCondition condition = new DeleteIfExists();
    Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Also used : DoubleValue(com.scalar.db.io.DoubleValue) MutationCondition(com.scalar.db.api.MutationCondition) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 23 with DeleteIfExists

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

the class DeleteStatementHandlerTest method prepare_DeleteOperationWithIfExistsGiven_ShouldCallAccept.

@Test
public void prepare_DeleteOperationWithIfExistsGiven_ShouldCallAccept() {
    // Arrange
    configureBehavior(null);
    del = prepareDeleteWithClusteringKey();
    DeleteIfExists deleteIfExists = spy(new DeleteIfExists());
    del.withCondition(deleteIfExists);
    // Act
    handler.prepare(del);
    // Assert
    verify(deleteIfExists).accept(any(ConditionSetter.class));
}
Also used : DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 24 with DeleteIfExists

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

the class DeleteStatementHandlerTest method prepare_DeleteOperationWithIfExistsGiven_ShouldPrepareProperQuery.

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

Example 25 with DeleteIfExists

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

the class BatchHandlerTest method handle_MultipleMutationsGiven_ShouldCallTransactWriteItems.

@Test
public void handle_MultipleMutationsGiven_ShouldCallTransactWriteItems() {
    // Arrange
    when(client.transactWriteItems(any(TransactWriteItemsRequest.class))).thenReturn(transactWriteResponse);
    Put put1 = preparePut();
    Put put2 = preparePut().withCondition(new PutIfNotExists());
    Delete delete1 = prepareDelete();
    Delete delete2 = prepareDelete().withCondition(new DeleteIfExists());
    DynamoMutation dynamoMutation1 = new DynamoMutation(put1, metadata);
    DynamoMutation dynamoMutation2 = new DynamoMutation(put2, metadata);
    DynamoMutation dynamoMutation3 = new DynamoMutation(delete1, metadata);
    DynamoMutation dynamoMutation4 = new DynamoMutation(delete2, metadata);
    // Act Assert
    assertThatCode(() -> handler.handle(Arrays.asList(put1, put2, delete1, delete2))).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<TransactWriteItemsRequest> captor = ArgumentCaptor.forClass(TransactWriteItemsRequest.class);
    verify(client).transactWriteItems(captor.capture());
    List<TransactWriteItem> items = captor.getValue().transactItems();
    assertThat(items.size()).isEqualTo(4);
    assertThat(items.get(0).update().key()).isEqualTo(dynamoMutation1.getKeyMap());
    assertThat(items.get(0).update().expressionAttributeNames()).isEqualTo(dynamoMutation1.getColumnMapWithKey());
    assertThat(items.get(0).update().expressionAttributeValues()).isEqualTo(dynamoMutation1.getValueBindMapWithKey());
    assertThat(items.get(0).update().conditionExpression()).isNull();
    assertThat(items.get(1).update().key()).isEqualTo(dynamoMutation2.getKeyMap());
    assertThat(items.get(1).update().expressionAttributeNames()).isEqualTo(dynamoMutation2.getColumnMapWithKey());
    assertThat(items.get(1).update().expressionAttributeValues()).isEqualTo(dynamoMutation2.getValueBindMapWithKey());
    assertThat(items.get(1).update().conditionExpression()).isEqualTo(dynamoMutation2.getIfNotExistsCondition());
    assertThat(items.get(2).delete().key()).isEqualTo(dynamoMutation3.getKeyMap());
    assertThat(items.get(2).delete().conditionExpression()).isNull();
    assertThat(items.get(3).delete().key()).isEqualTo(dynamoMutation4.getKeyMap());
    assertThat(items.get(3).delete().conditionExpression()).isEqualTo(dynamoMutation4.getIfExistsCondition());
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.Test)

Aggregations

DeleteIfExists (com.scalar.db.api.DeleteIfExists)25 Delete (com.scalar.db.api.Delete)22 Test (org.junit.jupiter.api.Test)20 Key (com.scalar.db.io.Key)11 Put (com.scalar.db.api.Put)9 PutIfNotExists (com.scalar.db.api.PutIfNotExists)6 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)5 Test (org.junit.Test)5 MutationCondition (com.scalar.db.api.MutationCondition)4 CosmosException (com.azure.cosmos.CosmosException)3 Result (com.scalar.db.api.Result)3 TextValue (com.scalar.db.io.TextValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 DeleteItemRequest (software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3 ConditionalExpression (com.scalar.db.api.ConditionalExpression)2 DeleteIf (com.scalar.db.api.DeleteIf)2 Scan (com.scalar.db.api.Scan)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 TransactionCanceledException (software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException)2