use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException.
@Test
public void put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIf(new ConditionalExpression(COL_NAME3, new IntValue(pKey + cKey + 1), ConditionalExpression.Operator.EQ)));
puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
}
use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class StorageWithReservedKeywordIntegrationTestBase method put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_ShouldUpdateRecord.
@Test
public void put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_ShouldUpdateRecord() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Get get = prepareGet(pKey, cKey);
// Act Assert
storage.put(puts.get(0));
puts.get(0).withCondition(new PutIf(new ConditionalExpression(columnName3, new IntValue(pKey + cKey), ConditionalExpression.Operator.EQ)));
puts.get(0).withValue(columnName3, Integer.MAX_VALUE);
assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
Result result = actual.get();
assertThat(result.getValue(columnName1)).isEqualTo(Optional.of(new IntValue(columnName1, pKey)));
assertThat(result.getValue(columnName4)).isEqualTo(Optional.of(new IntValue(columnName4, cKey)));
assertThat(result.getValue(columnName3)).isEqualTo(Optional.of(new IntValue(columnName3, Integer.MAX_VALUE)));
}
use of com.scalar.db.api.PutIf in project scalardb by scalar-labs.
the class UpdateStatementHandlerTest method setConsistency_PutOperationWithIfGiven_ShouldPrepareWithQuorumAndSerial.
@Test
public void setConsistency_PutOperationWithIfGiven_ShouldPrepareWithQuorumAndSerial() {
// Arrange
configureBehavior(null);
put = preparePutWithClusteringKey();
put.withCondition(new PutIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ))).withConsistency(Consistency.EVENTUAL);
// Act
handler.setConsistency(bound, put);
// Assert
verify(bound).setConsistencyLevel(ConsistencyLevel.QUORUM);
verify(bound).setSerialConsistencyLevel(ConsistencyLevel.SERIAL);
}
use of com.scalar.db.api.PutIf 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.PutIf 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);
}
Aggregations