Search in sources :

Example 41 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyInError.

@Test
public void testCopyInError() throws SQLException {
    setupCopyInformationSchemaResults();
    mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(Status.INVALID_ARGUMENT.asRuntimeException()));
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n")));
        assertTrue(exception.getMessage().contains("io.grpc.StatusRuntimeException: INVALID_ARGUMENT"));
    }
    // The server should receive one commit request, but this commit failed in this case.
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Example 42 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyIn.

@Test
public void testCopyIn() throws SQLException, IOException {
    setupCopyInformationSchemaResults();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n"));
        // Verify that we can use the connection for normal queries.
        try (ResultSet resultSet = connection.createStatement().executeQuery("SELECT 1")) {
            assertTrue(resultSet.next());
            assertEquals(1L, resultSet.getLong(1));
            assertFalse(resultSet.next());
        }
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
    CommitRequest commitRequest = commitRequests.get(0);
    assertEquals(1, commitRequest.getMutationsCount());
    Mutation mutation = commitRequest.getMutations(0);
    assertEquals(OperationCase.INSERT, mutation.getOperationCase());
    assertEquals(3, mutation.getInsert().getValuesCount());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) CopyManager(org.postgresql.copy.CopyManager) Mutation(com.google.spanner.v1.Mutation) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Example 43 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyIn_TableNotFound.

@Test
public void testCopyIn_TableNotFound() throws SQLException {
    setupCopyInformationSchemaResults(false);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n")));
        assertEquals("ERROR: INVALID_ARGUMENT: Table users is not found in information_schema", exception.getMessage());
        // Verify that we can use the connection for normal queries.
        try (ResultSet resultSet = connection.createStatement().executeQuery("SELECT 1")) {
            assertTrue(resultSet.next());
            assertEquals(1L, resultSet.getLong(1));
            assertFalse(resultSet.next());
        }
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertTrue(commitRequests.isEmpty());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) Test(org.junit.Test)

Example 44 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class CopyInMockServerTest method testCopyIn_Cancel.

@Test
public void testCopyIn_Cancel() throws SQLException {
    setupCopyInformationSchemaResults();
    byte[] payload = "5\t5\t5\n".getBytes(StandardCharsets.UTF_8);
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
        CopyIn copyOperation = copyManager.copyIn("COPY users FROM STDIN;");
        copyOperation.writeToCopy(payload, 0, payload.length);
        copyOperation.cancelCopy();
        // Verify that we can use the connection for normal queries.
        try (ResultSet resultSet = connection.createStatement().executeQuery("SELECT 1")) {
            assertTrue(resultSet.next());
            assertEquals(1L, resultSet.getLong(1));
            assertFalse(resultSet.next());
        }
    }
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertTrue(commitRequests.isEmpty());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) Connection(java.sql.Connection) BaseConnection(org.postgresql.core.BaseConnection) ResultSet(java.sql.ResultSet) CopyManager(org.postgresql.copy.CopyManager) BaseConnection(org.postgresql.core.BaseConnection) CopyIn(org.postgresql.copy.CopyIn) Test(org.junit.Test)

Example 45 with CommitRequest

use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.

the class DdlTransactionModeAutocommitImplicitTest method testMixedBatch.

@Test
public void testMixedBatch() throws SQLException {
    String sql = "CREATE TABLE foo (id bigint primary key); INSERT INTO FOO VALUES (1); UPDATE FOO SET BAR=1 WHERE BAZ=2;";
    addDdlResponseToSpannerAdmin();
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (Statement statement = connection.createStatement()) {
            // Statement#execute(String) returns false if the result was either an update count or there
            // was no result. Statement#getUpdateCount() returns 0 if there was no result.
            assertFalse(statement.execute(sql));
            assertEquals(0, statement.getUpdateCount());
            // getMoreResults() returns false as the next result is an update count.
            assertFalse(statement.getMoreResults());
            assertEquals(1, statement.getUpdateCount());
            // getMoreResults() returns false as the next result is an update count.
            assertFalse(statement.getMoreResults());
            assertEquals(2, statement.getUpdateCount());
            // getMoreResults() should now return false. We should also check getUpdateCount() as that
            // method should return -1 to indicate that there is also no update count available.
            assertFalse(statement.getMoreResults());
            assertEquals(-1, statement.getUpdateCount());
        }
    }
    List<UpdateDatabaseDdlRequest> updateDatabaseDdlRequests = mockDatabaseAdmin.getRequests().stream().filter(request -> request instanceof UpdateDatabaseDdlRequest).map(UpdateDatabaseDdlRequest.class::cast).collect(Collectors.toList());
    assertEquals(1, updateDatabaseDdlRequests.size());
    assertEquals(1, updateDatabaseDdlRequests.get(0).getStatementsCount());
    assertEquals("CREATE TABLE foo (id bigint primary key)", updateDatabaseDdlRequests.get(0).getStatements(0));
    // Verify that the DML statements were batched together by PgAdapter.
    List<ExecuteBatchDmlRequest> requests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
    assertEquals(1, requests.size());
    ExecuteBatchDmlRequest request = requests.get(0);
    assertEquals(2, request.getStatementsCount());
    assertEquals(INSERT_STATEMENT.getSql(), request.getStatements(0).getSql());
    assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(1).getSql());
    List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
    assertEquals(1, commitRequests.size());
}
Also used : CommitRequest(com.google.spanner.v1.CommitRequest) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)85 CommitRequest (com.google.spanner.v1.CommitRequest)47 CommitRequest (com.google.firestore.v1.CommitRequest)40 CommitResponse (com.google.firestore.v1.CommitResponse)40 ArrayList (java.util.ArrayList)26 Connection (java.sql.Connection)23 ByteString (com.google.protobuf.ByteString)14 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)12 SQLException (java.sql.SQLException)12 Statement (java.sql.Statement)12 CopyManager (org.postgresql.copy.CopyManager)11 BaseConnection (org.postgresql.core.BaseConnection)11 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)10 ArrayValue (com.google.firestore.v1.ArrayValue)9 MapValue (com.google.firestore.v1.MapValue)9 Value (com.google.firestore.v1.Value)9 Write (com.google.firestore.v1.Write)9 StringReader (java.io.StringReader)9 HashMap (java.util.HashMap)9 AbstractMessage (com.google.protobuf.AbstractMessage)8