use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class OperationCheckerTest method whenCheckingPutOperationWithAllValidArguments_shouldNotThrowAnyException.
@Test
public void whenCheckingPutOperationWithAllValidArguments_shouldNotThrowAnyException() {
// 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 PutIfNotExists();
Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
// Act Assert
assertThatCode(() -> operationChecker.check(put)).doesNotThrowAnyException();
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly.
@Test
public void put_MultiplePutWithDifferentConditionsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
storage.put(puts.get(1));
puts.get(0).withCondition(new PutIfNotExists());
puts.get(1).withCondition(new PutIf(new ConditionalExpression(COL_NAME2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
// Act
assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1)))).doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(new Scan(new Key(COL_NAME1, 0)));
assertThat(results.size()).isEqualTo(2);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(1);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_MultiplePutWithDifferentPartitionsWithIfNotExistsGiven_ShouldThrowIllegalArgumentException.
@Test
public void put_MultiplePutWithDifferentPartitionsWithIfNotExistsGiven_ShouldThrowIllegalArgumentException() throws IOException, ExecutionException {
// Arrange
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
puts.get(3).withCondition(new PutIfNotExists());
puts.get(6).withCondition(new PutIfNotExists());
// Act
assertThatThrownBy(() -> storage.put(Arrays.asList(puts.get(0), puts.get(3), puts.get(6)))).isInstanceOf(IllegalArgumentException.class);
// Assert
List<Result> results;
results = scanAll(new Scan(new Key(COL_NAME1, 0)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 3)));
assertThat(results.size()).isEqualTo(0);
results = scanAll(new Scan(new Key(COL_NAME1, 6)));
assertThat(results.size()).isEqualTo(0);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_MultiplePutWithIfNotExistsGiven_ShouldStoreProperly.
@Test
public void put_MultiplePutWithIfNotExistsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
puts.get(1).withCondition(new PutIfNotExists());
puts.get(2).withCondition(new PutIfNotExists());
Scan scan = new Scan(new Key(COL_NAME1, pKey));
// Act
assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)))).doesNotThrowAnyException();
// Assert
List<Result> results = scanAll(scan);
assertThat(results.size()).isEqualTo(3);
assertThat(results.get(0).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(0).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(0).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey);
assertThat(results.get(1).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(1).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(1).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 1);
assertThat(results.get(2).getValue(COL_NAME1).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME1).get().getAsInt()).isEqualTo(0);
assertThat(results.get(2).getValue(COL_NAME4).isPresent()).isTrue();
assertThat(results.get(2).getValue(COL_NAME4).get().getAsInt()).isEqualTo(pKey + cKey + 2);
}
use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.
the class StorageIntegrationTestBase method put_SinglePutWithIfNotExistsGiven_ShouldStoreProperly.
@Test
public void put_SinglePutWithIfNotExistsGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
puts.get(0).withCondition(new PutIfNotExists());
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.put(puts.get(0));
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();
assertThat(actual.get().getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME2)).isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
assertThat(actual.get().getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(actual.get().getValue(COL_NAME5)).isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
Aggregations