Search in sources :

Example 6 with MutationWriter

use of com.google.cloud.spanner.pgadapter.utils.MutationWriter in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testCopyBatchSizeLimit.

@Test
public void testCopyBatchSizeLimit() throws Exception {
    setupQueryInformationSchemaResults();
    byte[] payload = Files.readAllBytes(Paths.get("./src/test/resources/batch-size-test.txt"));
    CopyStatement copyStatement = new CopyStatement(connectionHandler, mock(OptionsMetadata.class), parse("COPY keyvalue FROM STDIN;"));
    assertFalse(copyStatement.isExecuted());
    copyStatement.execute();
    assertTrue(copyStatement.isExecuted());
    MutationWriter mw = copyStatement.getMutationWriter();
    // Inject a mock DatabaseClient for now.
    // TODO: Fix this once we can use the Connection API.
    Field databaseClientField = MutationWriter.class.getDeclaredField("databaseClient");
    databaseClientField.setAccessible(true);
    databaseClientField.set(mw, mock(DatabaseClient.class));
    mw.addCopyData(payload);
    mw.close();
    assertEquals("TEXT", copyStatement.getFormatType());
    assertEquals('\t', copyStatement.getDelimiterChar());
    assertFalse(copyStatement.hasException(0));
    assertEquals(12L, copyStatement.getUpdateCount().longValue());
    assertEquals(12L, mw.getRowCount());
    copyStatement.close();
}
Also used : Field(java.lang.reflect.Field) DatabaseClient(com.google.cloud.spanner.DatabaseClient) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Test(org.junit.Test)

Example 7 with MutationWriter

use of com.google.cloud.spanner.pgadapter.utils.MutationWriter in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testCopyResumeErrorStartOutputFile.

@Test
public void testCopyResumeErrorStartOutputFile() throws Exception {
    setupQueryInformationSchemaResults();
    byte[] payload = Files.readAllBytes(Paths.get("./src/test/resources/test-copy-start-output.txt"));
    // Pre-emptively try to delete the output file if it is lingering from a previous test run.
    // TODO: Make the output file for COPY configurable, so we can use a temp file for this.
    File outputFile = new File("output.txt");
    if (outputFile.exists()) {
        assertTrue(outputFile.delete());
    }
    CopyStatement copyStatement = new CopyStatement(connectionHandler, options, parse("COPY keyvalue FROM STDIN;"));
    assertFalse(copyStatement.isExecuted());
    copyStatement.execute();
    assertTrue(copyStatement.isExecuted());
    MutationWriter mw = copyStatement.getMutationWriter();
    mw.addCopyData(payload);
    SpannerException thrown = assertThrows(SpannerException.class, copyStatement::getUpdateCount);
    assertEquals(ErrorCode.INVALID_ARGUMENT, thrown.getErrorCode());
    assertEquals("INVALID_ARGUMENT: Invalid input syntax for type INT64:\"'1'\"", thrown.getMessage());
    outputFile = new File("output.txt");
    assertTrue(outputFile.exists());
    assertTrue(outputFile.isFile());
    assertTrue(outputFile.delete());
    copyStatement.close();
}
Also used : CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) SpannerException(com.google.cloud.spanner.SpannerException) File(java.io.File) Test(org.junit.Test)

Example 8 with MutationWriter

use of com.google.cloud.spanner.pgadapter.utils.MutationWriter in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testCopyResumeErrorOutputFile.

@Test
public void testCopyResumeErrorOutputFile() throws Exception {
    setupQueryInformationSchemaResults();
    byte[] payload = Files.readAllBytes(Paths.get("./src/test/resources/test-copy-output.txt"));
    CopyStatement copyStatement = new CopyStatement(connectionHandler, mock(OptionsMetadata.class), parse("COPY keyvalue FROM STDIN;"));
    assertFalse(copyStatement.isExecuted());
    copyStatement.execute();
    assertTrue(copyStatement.isExecuted());
    File outputFile = new File("output.txt");
    if (outputFile.exists()) {
        assertTrue(outputFile.delete());
    }
    MutationWriter mw = copyStatement.getMutationWriter();
    mw.addCopyData(payload);
    SpannerException thrown = assertThrows(SpannerException.class, copyStatement::getUpdateCount);
    assertEquals(ErrorCode.INVALID_ARGUMENT, thrown.getErrorCode());
    assertEquals("INVALID_ARGUMENT: Invalid input syntax for type INT64:\"'5'\"", thrown.getMessage());
    outputFile = new File("output.txt");
    assertTrue(outputFile.exists());
    assertTrue(outputFile.isFile());
    assertTrue(outputFile.delete());
    copyStatement.close();
}
Also used : CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) SpannerException(com.google.cloud.spanner.SpannerException) File(java.io.File) Test(org.junit.Test)

Example 9 with MutationWriter

use of com.google.cloud.spanner.pgadapter.utils.MutationWriter in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testCopyFromFilePipe.

@Test
public void testCopyFromFilePipe() throws Exception {
    setupQueryInformationSchemaResults();
    byte[] payload = Files.readAllBytes(Paths.get("./src/test/resources/small-file-test.txt"));
    CopyStatement copyStatement = new CopyStatement(connectionHandler, mock(OptionsMetadata.class), parse("COPY keyvalue FROM STDIN;"));
    copyStatement.execute();
    MutationWriter mw = copyStatement.getMutationWriter();
    mw.addCopyData(payload);
    assertEquals("TEXT", copyStatement.getFormatType());
    assertEquals('\t', copyStatement.getDelimiterChar());
    copyStatement.close();
    verify(resultSet, never()).close();
}
Also used : CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Test(org.junit.Test)

Example 10 with MutationWriter

use of com.google.cloud.spanner.pgadapter.utils.MutationWriter in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testCopyInvalidBuildMutation.

@Test
public void testCopyInvalidBuildMutation() throws Exception {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    setupQueryInformationSchemaResults();
    CopyStatement statement = new CopyStatement(connectionHandler, mock(OptionsMetadata.class), parse("COPY keyvalue FROM STDIN;"));
    statement.execute();
    byte[] payload = "2 3\n".getBytes();
    MutationWriter mutationWriter = statement.getMutationWriter();
    mutationWriter.addCopyData(payload);
    SpannerException thrown = assertThrows(SpannerException.class, statement::getUpdateCount);
    assertEquals(ErrorCode.INVALID_ARGUMENT, thrown.getErrorCode());
    assertEquals("INVALID_ARGUMENT: Invalid COPY data: Row length mismatched. Expected 2 columns, but only found 1", thrown.getMessage());
    statement.close();
    verify(resultSet, never()).close();
}
Also used : CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Aggregations

MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)13 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)10 Test (org.junit.Test)10 SpannerException (com.google.cloud.spanner.SpannerException)5 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)5 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInputStream (java.io.DataInputStream)3 File (java.io.File)3 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)2 ReadyResponse (com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)1 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)1 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)1 ErrorResponse (com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)1