Search in sources :

Example 36 with PutIfNotExists

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

the class StorageWithReservedKeywordIntegrationTestBase method put_WithReservedKeywordAndMultiplePutWithDifferentConditionsGiven_ShouldStoreProperly.

@Test
public void put_WithReservedKeywordAndMultiplePutWithDifferentConditionsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    storage.put(puts.get(1));
    puts.get(0).withCondition(new PutIfNotExists());
    puts.get(1).withCondition(new PutIf(new ConditionalExpression(columnName2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(columnName1, 0)));
    assertThat(results.size()).isEqualTo(2);
    assertThat(results.get(0).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName4).get().getAsInt()).isEqualTo(1);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 37 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists 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

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