use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class UpdateStatementHandlerTest method prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery.
@Test
public void prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery() {
// Arrange
String expected = Joiner.on(" ").skipNulls().join(new String[] { "UPDATE", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "SET", ANY_NAME_3 + "=?", "WHERE", ANY_NAME_1 + "=?", "AND", ANY_NAME_2 + "=?", "IF", ANY_NAME_4 + "=?", "AND", ANY_NAME_4 + "!=?", "AND", ANY_NAME_4 + ">?", "AND", ANY_NAME_4 + ">=?", "AND", ANY_NAME_4 + "<?", "AND", ANY_NAME_4 + "<=?;" });
configureBehavior(expected);
put = preparePutWithClusteringKey();
put.withCondition(new PutIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.NE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GTE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LTE)));
// Act
handler.prepare(put);
// Assert
verify(session).prepare(expected);
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingDeleteOperationWithAllValidArguments_shouldNotThrowAnyException.
@Test
public void whenCheckingDeleteOperationWithAllValidArguments_shouldNotThrowAnyException() {
// Arrange
Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
MutationCondition condition = new DeleteIf(new ConditionalExpression(COL1, new IntValue(1), ConditionalExpression.Operator.EQ));
Delete delete = new Delete(partitionKey, clusteringKey).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatCode(() -> operationChecker.check(delete)).doesNotThrowAnyException();
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException.
@Test
public void whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException() {
// 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 PutIf(new ConditionalExpression(COL1, new TextValue("1"), ConditionalExpression.Operator.EQ));
Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class ConditionalQueryBuilderTest method visit_DeleteIfAcceptCalled_ShouldCallWhere.
@Test
public void visit_DeleteIfAcceptCalled_ShouldCallWhere() {
// Arrange
DeleteIf condition = new DeleteIf(new ConditionalExpression(ANY_NAME_1, ANY_INT_VALUE, Operator.EQ), new ConditionalExpression(ANY_NAME_2, ANY_INT_VALUE, Operator.GT));
ConditionalQueryBuilder builder = new ConditionalQueryBuilder(select);
// Act
condition.accept(builder);
// Assert
verify(select).and(DSL.field("r.values[\"" + ANY_NAME_1 + "\"]").equal(ANY_INT));
verify(select).and(DSL.field("r.values[\"" + ANY_NAME_2 + "\"]").greaterThan(ANY_INT));
}
use of com.scalar.db.api.ConditionalExpression 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());
}
Aggregations