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);
}
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));
}
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());
}
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());
}
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);
}
Aggregations