Search in sources :

Example 11 with PutIfNotExists

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

the class BatchHandlerTest method handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException.

@Test
public void handle_CosmosExceptionWithPreconditionFailed_ShouldThrowNoMutationException() {
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    CosmosException toThrow = mock(CosmosException.class);
    doThrow(toThrow).when(storedProcedure).execute(anyList(), any(CosmosStoredProcedureRequestOptions.class));
    when(toThrow.getSubStatusCode()).thenReturn(CosmosErrorCode.PRECONDITION_FAILED.get());
    Put put = preparePut().withCondition(new PutIfNotExists());
    Delete delete = prepareDelete().withCondition(new DeleteIfExists());
    // Act Assert
    assertThatThrownBy(() -> handler.handle(Arrays.asList(put, delete))).isInstanceOf(NoMutationException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosException(com.azure.cosmos.CosmosException) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Example 12 with PutIfNotExists

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

the class ConditionalQueryBuilderTest method visit_PutIfNotExistsAcceptCalled_ShouldNotCallWhere.

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

Example 13 with PutIfNotExists

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

the class JdbcServiceTest method whenPutOperationWithPutIfNotExistsConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfNotExistsConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.insertInto(any(), any(), any())).thenReturn(insertQueryBuilder);
    when(insertQueryBuilder.values(any(), any(), any())).thenReturn(insertQueryBuilder);
    when(insertQueryBuilder.build()).thenReturn(insertQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenThrow(sqlException);
    when(sqlException.getSQLState()).thenReturn("23000");
    // Act
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfNotExists()).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.put(put, connection);
    // Assert
    assertThat(ret).isFalse();
    verify(operationChecker).check(any(Put.class));
    verify(queryBuilder).insertInto(any(), any(), any());
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 14 with PutIfNotExists

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

the class JdbcServiceTest method whenPutOperationWithPutIfNotExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfNotExistsConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.insertInto(any(), any(), any())).thenReturn(insertQueryBuilder);
    when(insertQueryBuilder.values(any(), any(), any())).thenReturn(insertQueryBuilder);
    when(insertQueryBuilder.build()).thenReturn(insertQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    // Act
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfNotExists()).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.put(put, connection);
    // Assert
    assertThat(ret).isTrue();
    verify(operationChecker).check(any(Put.class));
    verify(queryBuilder).insertInto(any(), any(), any());
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 15 with PutIfNotExists

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

the class PrepareMutationComposerTest method add_PutAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition.

@Test
public void add_PutAndNullResultGiven_ShouldComposePutWithPutIfNotExistsCondition() {
    // Arrange
    Put put = preparePut();
    // Act
    composer.add(put, null);
    // Assert
    Put actual = (Put) mutations.get(0);
    put.withConsistency(Consistency.LINEARIZABLE);
    put.withCondition(new PutIfNotExists());
    put.withValue(Attribute.toPreparedAtValue(ANY_TIME_5));
    put.withValue(Attribute.toIdValue(ANY_ID_3));
    put.withValue(Attribute.toStateValue(TransactionState.PREPARED));
    put.withValue(Attribute.toVersionValue(1));
    assertThat(actual).isEqualTo(put);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) 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