Skip to content

Commit

Permalink
Add unit test for blob streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasHafner committed Aug 6, 2024
1 parent 3bb30f7 commit 84dd3a1
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/test/java/org/polypheny/jdbc/types/StreamingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -40,24 +38,23 @@

public class StreamingTest {

private static final String DROP_IF_STATEMENT = "DROP TABLE IF EXISTS file_table";
private static final String CREATE_STATEMENT = "CREATE TABLE file_table (id INT PRIMARY KEY, data FILE)";
private static final String INSERT_STATEMENT = "INSERT INTO file_table (id, data) VALUES (?, ?)";
private static final String QUERY = "SELECT data FROM file_table WHERE id = 1";

private static final String FILE_DROP_IF_STATEMENT = "DROP TABLE IF EXISTS file_table";
private static final String FILE_CREATE_STATEMENT = "CREATE TABLE file_table (id INT PRIMARY KEY, data FILE)";
private static final String FILE_INSERT_STATEMENT = "INSERT INTO file_table (id, data) VALUES (?, ?)";
private static final String FILE_QUERY = "SELECT data FROM file_table WHERE id = 1";

@Test
public void simpleFileStreamingTest() throws InterruptedException {
public void simpleFileStreamingTest() {
try ( Connection connection = TestHelper.getConnection() ) {
if ( !connection.isWrapperFor( PolyConnection.class ) ) {
fail( "Driver must support unwrapping to PolyphenyConnection" );
}
PolyStatement polyStatement = connection.unwrap( PolyConnection.class ).createPolyStatement();

polyStatement.execute( "public", "sql", DROP_IF_STATEMENT );
polyStatement.execute( "public", "sql", CREATE_STATEMENT );
polyStatement.execute( "public", "sql", FILE_DROP_IF_STATEMENT );
polyStatement.execute( "public", "sql", FILE_CREATE_STATEMENT );

polyStatement.prepare( "public", "sql", INSERT_STATEMENT );
polyStatement.prepare( "public", "sql", FILE_INSERT_STATEMENT );
byte[] expected = createTestData();

List<TypedValue> parameters = new ArrayList<>( 2 );
Expand All @@ -66,7 +63,38 @@ public void simpleFileStreamingTest() throws InterruptedException {
Result result = polyStatement.executePrepared( parameters );
result.unwrap( ScalarResult.class );

RelationalResult result2 = polyStatement.execute( "public", "sql", QUERY ).unwrap( RelationalResult.class );
RelationalResult result2 = polyStatement.execute( "public", "sql", FILE_QUERY ).unwrap( RelationalResult.class );
InputStream bis = result2.iterator().next().get( "data" ).asBlob().getBinaryStream();
byte[] received = collectStream( bis );

Assertions.assertArrayEquals( expected, received );
} catch ( SQLException | IOException e ) {
throw new RuntimeException( e );
}
}


@Test
public void simpleFileStreamingTest2() {
try ( Connection connection = TestHelper.getConnection() ) {
if ( !connection.isWrapperFor( PolyConnection.class ) ) {
fail( "Driver must support unwrapping to PolyphenyConnection" );
}
PolyStatement polyStatement = connection.unwrap( PolyConnection.class ).createPolyStatement();

polyStatement.execute( "public", "sql", FILE_DROP_IF_STATEMENT );
polyStatement.execute( "public", "sql", FILE_CREATE_STATEMENT );

polyStatement.prepare( "public", "sql", FILE_INSERT_STATEMENT );
byte[] expected = createTestData();

List<TypedValue> parameters = new ArrayList<>( 2 );
parameters.add( TypedValue.fromInteger( 1 ) );
parameters.add( TypedValue.fromBlob(new ByteArrayInputStream( expected )));
Result result = polyStatement.executePrepared( parameters );
result.unwrap( ScalarResult.class );

RelationalResult result2 = polyStatement.execute( "public", "sql", FILE_QUERY ).unwrap( RelationalResult.class );
InputStream bis = result2.iterator().next().get( "data" ).asBlob().getBinaryStream();
byte[] received = collectStream( bis );

Expand Down

0 comments on commit 84dd3a1

Please sign in to comment.