use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class CosmosMutationTest method makeRecord_PutWithNullValueGiven_ShouldReturnWithValues.
@Test
public void makeRecord_PutWithNullValueGiven_ShouldReturnWithValues() {
// Arrange
Put put = preparePut();
put.withIntValue(ANY_NAME_3, null);
CosmosMutation cosmosMutation = new CosmosMutation(put, metadata);
String id = cosmosMutation.getId();
String concatenatedPartitionKey = cosmosMutation.getConcatenatedPartitionKey();
// Act
Record actual = cosmosMutation.makeRecord();
// Assert
assertThat(actual.getId()).isEqualTo(id);
assertThat(actual.getConcatenatedPartitionKey()).isEqualTo(concatenatedPartitionKey);
assertThat(actual.getPartitionKey().get(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
assertThat(actual.getClusteringKey().get(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
assertThat(actual.getValues().containsKey(ANY_NAME_3)).isTrue();
assertThat(actual.getValues().get(ANY_NAME_3)).isNull();
assertThat(actual.getValues().get(ANY_NAME_4)).isEqualTo(ANY_INT_2);
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcServiceTest method whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder.
@Test
public void whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.where(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.build()).thenReturn(updateQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(0);
// Act
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
boolean ret = jdbcService.put(put, connection);
// Assert
assertThat(ret).isFalse();
verify(operationChecker).check(any(Put.class));
verify(queryBuilder).update(any(), any(), any());
}
use of com.scalar.db.api.Put in project scalardb by scalar-labs.
the class JdbcServiceTest method whenPutOperationWithPutIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder.
@Test
public void whenPutOperationWithPutIfExistsConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.where(any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.build()).thenReturn(updateQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(0);
// Act
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfExists()).forNamespace(NAMESPACE).forTable(TABLE);
boolean ret = jdbcService.put(put, connection);
// Assert
assertThat(ret).isFalse();
verify(operationChecker).check(any(Put.class));
verify(queryBuilder).update(any(), any(), any());
}
use of com.scalar.db.api.Put 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.Put in project scalardb by scalar-labs.
the class JdbcServiceTest method whenPutOperationWithPutIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder.
@Test
public void whenPutOperationWithPutIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.where(any(), any(), any())).thenReturn(updateQueryBuilder);
when(updateQueryBuilder.build()).thenReturn(updateQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(1);
// Act
Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
boolean ret = jdbcService.put(put, connection);
// Assert
assertThat(ret).isTrue();
verify(operationChecker).check(any(Put.class));
verify(queryBuilder).update(any(), any(), any());
}
Aggregations