Search in sources :

Example 61 with Put

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

the class JdbcTransactionManagerTest method whenCommitFails_shouldThrowCommitExceptionAndRollback.

@Test
public void whenCommitFails_shouldThrowCommitExceptionAndRollback() throws Exception {
    // Arrange
    doThrow(sqlException).when(connection).commit();
    // Act Assert
    assertThatThrownBy(() -> {
        JdbcTransaction transaction = manager.start();
        Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
        transaction.get(get);
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
        transaction.put(put);
        transaction.commit();
    }).isInstanceOf(CommitException.class);
    verify(connection).rollback();
    verify(connection).close();
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 62 with Put

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

the class JdbcTransactionManagerTest method whenPutOperationsExecutedAndJdbcServiceThrowsSQLException_shouldThrowCrudException.

@Test
public void whenPutOperationsExecutedAndJdbcServiceThrowsSQLException_shouldThrowCrudException() throws Exception {
    // Arrange
    when(jdbcService.put(any(), any())).thenThrow(sqlException);
    // Act Assert
    assertThatThrownBy(() -> {
        JdbcTransaction transaction = manager.start();
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
        transaction.put(put);
    }).isInstanceOf(CrudException.class);
}
Also used : Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 63 with Put

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

the class JdbcTransactionManagerTest method whenRollbackFails_shouldThrowAbortException.

@Test
public void whenRollbackFails_shouldThrowAbortException() throws Exception {
    // Arrange
    doThrow(sqlException).when(connection).rollback();
    // Act Assert
    assertThatThrownBy(() -> {
        JdbcTransaction transaction = manager.start();
        Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
        transaction.get(get);
        Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
        transaction.put(put);
        transaction.abort();
    }).isInstanceOf(AbortException.class);
    verify(connection).close();
}
Also used : Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 64 with Put

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

the class JdbcTransactionManagerTest method whenSomeOperationsExecutedAndCommit_shouldCallJdbcService.

@Test
public void whenSomeOperationsExecutedAndCommit_shouldCallJdbcService() throws Exception {
    // Arrange
    when(jdbcService.scan(any(), any())).thenReturn(Collections.emptyList());
    when(jdbcService.put(any(), any())).thenReturn(true);
    when(jdbcService.delete(any(), any())).thenReturn(true);
    // Act
    JdbcTransaction transaction = manager.start();
    Get get = new Get(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
    transaction.get(get);
    Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
    transaction.scan(scan);
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").forNamespace(NAMESPACE).forTable(TABLE);
    transaction.put(put);
    Delete delete = new Delete(new Key("p1", "val1")).forNamespace(NAMESPACE).forTable(TABLE);
    transaction.delete(delete);
    transaction.mutate(Arrays.asList(put, delete));
    transaction.commit();
    // Assert
    verify(jdbcService).get(any(), any());
    verify(jdbcService).scan(any(), any());
    verify(jdbcService, times(2)).put(any(), any());
    verify(jdbcService, times(2)).delete(any(), any());
    verify(connection).commit();
    verify(connection).close();
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 65 with Put

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

the class ScalarDbUtilsTest method copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance.

@Test
public void copyAndSetTargetToIfNot_MutationsGiven_ShouldReturnDifferentInstance() {
    // Arrange
    Put put = new Put(new Key("c1", "v1"));
    Delete delete = new Delete(new Key("c1", "v1"));
    List<Mutation> mutations = Arrays.asList(put, delete);
    // Act
    List<Mutation> actual = ScalarDbUtils.copyAndSetTargetToIfNot(mutations, NAMESPACE, TABLE);
    // Assert
    assertThat(actual == mutations).isFalse();
    assertThat(actual.get(0) == put).isFalse();
    assertThat(actual.get(1) == delete).isFalse();
    assertThat(put.forNamespace()).isNotPresent();
    assertThat(put.forTable()).isNotPresent();
    assertThat(delete.forNamespace()).isNotPresent();
    assertThat(delete.forTable()).isNotPresent();
    assertThat(actual.get(0).forNamespace()).isEqualTo(NAMESPACE);
    assertThat(actual.get(0).forTable()).isEqualTo(TABLE);
    assertThat(actual.get(1).forNamespace()).isEqualTo(NAMESPACE);
    assertThat(actual.get(1).forTable()).isEqualTo(TABLE);
}
Also used : Delete(com.scalar.db.api.Delete) Mutation(com.scalar.db.api.Mutation) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16