Search in sources :

Example 1 with PutIfNotExists

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

the class StorageIntegrationTestBase method put_MultiplePutWithIfNotExistsGivenWhenOneExists_ShouldThrowNoMutationException.

@Test
public void put_MultiplePutWithIfNotExistsGivenWhenOneExists_ShouldThrowNoMutationException() throws IOException, ExecutionException {
    // Arrange
    int pKey = 0;
    int cKey = 0;
    List<Put> puts = preparePuts();
    assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
    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
    assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)))).isInstanceOf(NoMutationException.class);
    // Assert
    List<Result> results = scanAll(scan);
    assertThat(results.size()).isEqualTo(1);
    assertThat(results.get(0).getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, pKey + cKey)));
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Scan(com.scalar.db.api.Scan) 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)

Example 2 with PutIfNotExists

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

the class InsertStatementHandlerTest method handle_PutWithoutConditionButNoMutationApplied_ShouldThrowProperExecutionException.

@Test
public void handle_PutWithoutConditionButNoMutationApplied_ShouldThrowProperExecutionException() {
    // Arrange
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfNotExists());
    spy = prepareSpiedInsertStatementHandler();
    ResultSet results = mock(ResultSet.class);
    Row row = mock(Row.class);
    when(results.one()).thenReturn(row);
    when(row.getBool(0)).thenReturn(false);
    doReturn(results).when(spy).execute(bound, put);
    // Act Assert
    assertThatThrownBy(() -> spy.handle(put)).isInstanceOf(NoMutationException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.jupiter.api.Test)

Example 3 with PutIfNotExists

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

the class InsertStatementHandlerTest method handle_PutWithConditionButNoMutationApplied_ShouldThrowProperExecutionException.

@Test
public void handle_PutWithConditionButNoMutationApplied_ShouldThrowProperExecutionException() {
    // Arrange
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfNotExists());
    spy = prepareSpiedInsertStatementHandler();
    ResultSet results = mock(ResultSet.class);
    Row row = mock(Row.class);
    when(results.one()).thenReturn(row);
    when(row.getBool(0)).thenReturn(false);
    doReturn(results).when(spy).execute(bound, put);
    // Act Assert
    assertThatThrownBy(() -> spy.handle(put)).isInstanceOf(NoMutationException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.jupiter.api.Test)

Example 4 with PutIfNotExists

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

the class InsertStatementHandlerTest method prepare_PutOperationWithIfNotExistsGiven_ShouldPrepareProperQuery.

@Test
public void prepare_PutOperationWithIfNotExistsGiven_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "INSERT INTO", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "(" + ANY_NAME_1 + "," + ANY_NAME_2 + "," + ANY_NAME_3 + ")", "VALUES", "(?,?,?)", "IF NOT EXISTS;" });
    configureBehavior(expected);
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIfNotExists());
    // Act
    handler.prepare(put);
    // Assert
    verify(session).prepare(expected);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 5 with PutIfNotExists

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

the class InsertStatementHandlerTest method prepare_PutOperationWithIfNotExistsGiven_ShouldCallAccept.

@Test
public void prepare_PutOperationWithIfNotExistsGiven_ShouldCallAccept() {
    // Arrange
    configureBehavior(null);
    put = preparePutWithClusteringKey();
    PutIfNotExists putIfNotExists = spy(new PutIfNotExists());
    put.withCondition(putIfNotExists);
    // Act
    handler.prepare(put);
    // Assert
    verify(putIfNotExists).accept(any(ConditionSetter.class));
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) 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