Search in sources :

Example 31 with PutIfNotExists

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

the class OperationCheckerTest method whenCheckingPutOperationWithAllValidArguments_shouldNotThrowAnyException.

@Test
public void whenCheckingPutOperationWithAllValidArguments_shouldNotThrowAnyException() {
    // 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 PutIfNotExists();
    Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatCode(() -> operationChecker.check(put)).doesNotThrowAnyException();
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) 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) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 32 with PutIfNotExists

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

the class StorageIntegrationTestBase method put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly.

@Test
public void put_MultiplePutWithDifferentConditionsGiven_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(COL_NAME2, 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(COL_NAME1, 0)));
    assertThat(results.size()).isEqualTo(2);
    assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME4).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 33 with PutIfNotExists

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

the class StorageIntegrationTestBase method put_MultiplePutWithDifferentPartitionsWithIfNotExistsGiven_ShouldThrowIllegalArgumentException.

@Test
public void put_MultiplePutWithDifferentPartitionsWithIfNotExistsGiven_ShouldThrowIllegalArgumentException() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    puts.get(0).withCondition(new PutIfNotExists());
    puts.get(3).withCondition(new PutIfNotExists());
    puts.get(6).withCondition(new PutIfNotExists());
    // Act
    assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(3), puts.get(6)))).isInstanceOf(IllegalArgumentException.class);
    // Assert
    List<Result> results;
    results = scanAll(new Scan(new Key(COL_NAME1, 0)));
    assertThat(results.size()).isEqualTo(0);
    results = scanAll(new Scan(new Key(COL_NAME1, 3)));
    assertThat(results.size()).isEqualTo(0);
    results = scanAll(new Scan(new Key(COL_NAME1, 6)));
    assertThat(results.size()).isEqualTo(0);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) 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 34 with PutIfNotExists

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

the class StorageIntegrationTestBase method put_MultiplePutWithIfNotExistsGiven_ShouldStoreProperly.

@Test
public void put_MultiplePutWithIfNotExistsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
    // Arrange
    int pKey = 0;
    int cKey = 0;
    List<Put> puts = preparePuts();
    puts.get(0).withCondition(new PutIfNotExists());
    puts.get(1).withCondition(new PutIfNotExists());
    puts.get(2).withCondition(new PutIfNotExists());
    Scan scan = new Scan(new Key(COL_NAME1, pKey));
    // Act
    assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(scan);
    assertThat(results.size()).isEqualTo(3);
    assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey);
    assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 1);
    assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
    assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
    assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 2);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) 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 35 with PutIfNotExists

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

the class StorageIntegrationTestBase method put_SinglePutWithIfNotExistsGiven_ShouldStoreProperly.

@Test
public void put_SinglePutWithIfNotExistsGiven_ShouldStoreProperly() throws ExecutionException {
    // Arrange
    int pKey = 0;
    int cKey = 0;
    List<Put> puts = preparePuts();
    puts.get(0).withCondition(new PutIfNotExists());
    Key partitionKey = new Key(COL_NAME1, pKey);
    Key clusteringKey = new Key(COL_NAME4, cKey);
    Get get = new Get(partitionKey, clusteringKey);
    // Act
    storage.put(puts.get(0));
    puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
    assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
    // Assert
    Optional<Result> actual = storage.get(get);
    assertThat(actual.isPresent()).isTrue();
    assertThat(actual.get().getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
    assertThat(actual.get().getValue(COL_NAME2)).isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
    assertThat(actual.get().getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
    assertThat(actual.get().getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
    assertThat(actual.get().getValue(COL_NAME5)).isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) TextValue(com.scalar.db.io.TextValue) Get(com.scalar.db.api.Get) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) 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