Search in sources :

Example 6 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.

the class OperationCheckerTest method whenCheckingPutOperationWithInvalidValueType_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingPutOperationWithInvalidValueType_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    List<Value<?>> values = Arrays.asList(new TextValue(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
    assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) DoubleValue(com.scalar.db.io.DoubleValue) MutationCondition(com.scalar.db.api.MutationCondition) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BooleanValue(com.scalar.db.io.BooleanValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 7 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.

the class OperationCheckerTest method whenCheckingPutOperationWithNullValue_shouldNotThrowAnyException.

@Test
public void whenCheckingPutOperationWithNullValue_shouldNotThrowAnyException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    MutationCondition condition = new PutIfNotExists();
    Put put = new Put(partitionKey, clusteringKey).withValue(COL1, 1).withValue(COL2, 0.1D).withBooleanValue(COL3, null).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatCode(() -> operationChecker.check(put)).doesNotThrowAnyException();
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) MutationCondition(com.scalar.db.api.MutationCondition) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 8 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.

the class JdbcDatabaseTest method whenPutOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException.

@Test
public void whenPutOperationWithConditionExecutedAndJdbcServiceReturnsFalse_shouldThrowNoMutationException() throws Exception {
    // Arrange
    when(jdbcService.put(any(), any())).thenReturn(false);
    // Act Assert
    assertThatThrownBy(() -> {
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIfNotExists()).forNamespace(NAMESPACE).forTable(TABLE);
        jdbcDatabase.put(put);
    }).isInstanceOf(NoMutationException.class);
    verify(connection).close();
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 9 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.

the class OperationCheckerTest method whenCheckingPutOperationWithoutAnyClusteringKey_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingPutOperationWithoutAnyClusteringKey_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = null;
    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
    assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) DoubleValue(com.scalar.db.io.DoubleValue) MutationCondition(com.scalar.db.api.MutationCondition) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 10 with PutIfNotExists

use of com.scalar.db.api.PutIfNotExists in project scalardb by scalar-labs.

the class BatchHandlerTest method handle_MultipleMutationsGiven_ShouldCallStoredProcedure.

@Test
public void handle_MultipleMutationsGiven_ShouldCallStoredProcedure() {
    // Arrange
    when(container.getScripts()).thenReturn(cosmosScripts);
    when(cosmosScripts.getStoredProcedure(anyString())).thenReturn(storedProcedure);
    when(storedProcedure.execute(anyList(), any(CosmosStoredProcedureRequestOptions.class))).thenReturn(spResponse);
    Put put1 = preparePut();
    Put put2 = preparePut().withCondition(new PutIfNotExists());
    Delete delete1 = prepareDelete();
    Delete delete2 = prepareDelete().withCondition(new DeleteIfExists());
    CosmosMutation cosmosMutation1 = new CosmosMutation(put1, metadata);
    CosmosMutation cosmosMutation2 = new CosmosMutation(put2, metadata);
    CosmosMutation cosmosMutation3 = new CosmosMutation(delete1, metadata);
    CosmosMutation cosmosMutation4 = new CosmosMutation(delete2, metadata);
    Record record1 = cosmosMutation1.makeRecord();
    Record record2 = cosmosMutation2.makeRecord();
    Record emptyRecord = new Record();
    String query1 = cosmosMutation1.makeConditionalQuery();
    String query2 = cosmosMutation2.makeConditionalQuery();
    String query3 = cosmosMutation3.makeConditionalQuery();
    String query4 = cosmosMutation4.makeConditionalQuery();
    // Act Assert
    assertThatCode(() -> handler.handle(Arrays.asList(put1, put2, delete1, delete2))).doesNotThrowAnyException();
    // Assert
    verify(cosmosScripts).getStoredProcedure("mutate.js");
    verify(storedProcedure).execute(captor.capture(), any(CosmosStoredProcedureRequestOptions.class));
    assertThat(captor.getValue().get(0)).isEqualTo(4);
    assertThat(captor.getValue().get(1)).isEqualTo(CosmosMutation.MutationType.PUT.ordinal());
    assertThat(captor.getValue().get(2)).isEqualTo(CosmosMutation.MutationType.PUT_IF_NOT_EXISTS.ordinal());
    assertThat(captor.getValue().get(3)).isEqualTo(CosmosMutation.MutationType.DELETE_IF.ordinal());
    assertThat(captor.getValue().get(4)).isEqualTo(CosmosMutation.MutationType.DELETE_IF.ordinal());
    assertThat(captor.getValue().get(5)).isEqualTo(record1);
    assertThat(captor.getValue().get(6)).isEqualTo(record2);
    assertThat(captor.getValue().get(7)).isEqualTo(emptyRecord);
    assertThat(captor.getValue().get(8)).isEqualTo(emptyRecord);
    assertThat(captor.getValue().get(9)).isEqualTo(query1);
    assertThat(captor.getValue().get(10)).isEqualTo(query2);
    assertThat(captor.getValue().get(11)).isEqualTo(query3);
    assertThat(captor.getValue().get(12)).isEqualTo(query4);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) Delete(com.scalar.db.api.Delete) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) Test(org.junit.jupiter.api.Test)

Aggregations

PutIfNotExists (com.scalar.db.api.PutIfNotExists)37 Put (com.scalar.db.api.Put)27 Test (org.junit.jupiter.api.Test)26 Key (com.scalar.db.io.Key)15 Delete (com.scalar.db.api.Delete)8 Test (org.junit.Test)7 DeleteIfExists (com.scalar.db.api.DeleteIfExists)6 MutationCondition (com.scalar.db.api.MutationCondition)6 Result (com.scalar.db.api.Result)6 TextValue (com.scalar.db.io.TextValue)6 PutIf (com.scalar.db.api.PutIf)5 Scan (com.scalar.db.api.Scan)5 IntValue (com.scalar.db.io.IntValue)5 Value (com.scalar.db.io.Value)5 ConditionalExpression (com.scalar.db.api.ConditionalExpression)4 BooleanValue (com.scalar.db.io.BooleanValue)4 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)3 DoubleValue (com.scalar.db.io.DoubleValue)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)3