Skip to content

Commit

Permalink
Add unit tests for null sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Hafner committed May 6, 2024
1 parent 64fb91d commit e356f41
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 80 additions & 0 deletions dbms/src/test/java/org/polypheny/db/jdbc/JdbcMetaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.polypheny.db.jdbc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.google.common.collect.ImmutableList;
import java.sql.Connection;
Expand All @@ -43,6 +46,14 @@
@Slf4j
public class JdbcMetaTest {

private static final String CREATE_TEST_TABLE = "CREATE TABLE IF NOT EXISTS my_table (id INT PRIMARY KEY, some_value INT)";
private static final String INSERT_TEST_DATA = "INSERT INTO my_table (id, some_value) VALUES " +
"(1, 10), " +
"(2, NULL), " +
"(3, 5), " +
"(4, NULL), " +
"(5, 8)";


private static TestHelper helper;

Expand Down Expand Up @@ -342,6 +353,75 @@ public void testGetTableTypes() throws SQLException {
}


@Test
public void testSortNullsAtEnd() throws SQLException {
try (
JdbcConnection polyphenyDbConnection = new JdbcConnection( false );
Connection connection = polyphenyDbConnection.getConnection();
Statement statement = connection.createStatement()
) {
statement.execute( CREATE_TEST_TABLE );
statement.executeUpdate( INSERT_TEST_DATA );

ResultSet rs = statement.executeQuery( "SELECT * FROM my_table ORDER BY some_value IS NULL, some_value" );

boolean trigger = false;
while ( rs.next() ) {
Integer value = rs.getInt( "some_value" );
if ( value == 0 ) {
trigger = true;
} else if ( trigger && value != null ) {
fail( "Values are not sorted correctly." );
}
}

ResultSet rs2 = statement.executeQuery( "SELECT * FROM my_table ORDER BY some_value IS NULL, some_value" );

trigger = false;
while ( rs2.next() ) {
Integer value = rs2.getInt( "some_value" );
if ( value == 0 ) {
trigger = true;
} else if ( trigger && value != null ) {
fail( "Values are not sorted correctly." );
}
}
}
}

@Test
public void testNullsAreSortedAtEnd() throws SQLException {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false, true );
Connection connection = polyphenyDbConnection.getConnection() ) {
assertTrue(connection.getMetaData().nullsAreSortedAtEnd());
}
}

@Test
public void testNullsAreSortedStart() throws SQLException {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false, true );
Connection connection = polyphenyDbConnection.getConnection() ) {
assertFalse(connection.getMetaData().nullsAreSortedAtStart());
}
}

@Test
public void testNullsAreSortedHigh() throws SQLException {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false, true );
Connection connection = polyphenyDbConnection.getConnection() ) {
assertFalse(connection.getMetaData().nullsAreSortedHigh());
}
}

@Test
public void testNullsAreSortedLow() throws SQLException {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false, true );
Connection connection = polyphenyDbConnection.getConnection() ) {
assertFalse(connection.getMetaData().nullsAreSortedLow());
}
}


@Test
public void testMetaGetColumns() throws SQLException {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void simpleMqlTest() throws SQLException {
if ( !connection.isWrapperFor( PolyConnection.class ) ) {
fail( "Driver must support unwrapping to PolyphenyConnection" );
}
PolyStatement polyStatement = connection.unwrap( PolyConnection.class ).createProtoStatement();
PolyStatement polyStatement = connection.unwrap( PolyConnection.class ).createPolyStatement();
assertThrows( PrismInterfaceServiceException.class, () -> {
// fails due to autogenerated query (would create students collection)
Result result = polyStatement.execute( "mqltest", MQL_LANGUAGE_NAME, TEST_QUERY );
Expand Down

0 comments on commit e356f41

Please sign in to comment.