Skip to content

Commit

Permalink
"Refactor: Replace Java assert statements with JUnit5 assertions for …
Browse files Browse the repository at this point in the history
…improved testing consistency and readability."
  • Loading branch information
alyaa999 committed Aug 18, 2024
1 parent c7ed8c2 commit f71f5d0
Show file tree
Hide file tree
Showing 33 changed files with 581 additions and 601 deletions.
70 changes: 33 additions & 37 deletions dbms/src/test/java/org/polypheny/db/cypher/CypherTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.polypheny.db.cypher;

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
Expand Down Expand Up @@ -190,74 +192,68 @@ public static boolean containsRows( GraphResult actual, boolean exclusive, boole
i++;
}

assert !exclusive || actual.getData().length >= rows.length;
// Use assert to validate exclusive condition
assertTrue(!exclusive || actual.getData().length >= rows.length, "Exclusive condition failed: actual data has fewer rows than expected.");


// Use the appropriate matching method based on the 'ordered' flag
return ordered ? matchesExactRows(parsed, rows) : matchesUnorderedRows(parsed, rows);

if ( ordered ) {
return matchesExactRows( parsed, rows );
} else {
return matchesUnorderedRows( parsed, rows );
}
} catch ( Throwable t ) {
fail( "Error while evaluating result: " + t.getMessage() );
throw new RuntimeException();
}
}


private static boolean matchesUnorderedRows( List<List<PolyValue>> parsed, Row[] rows ) {

private static boolean matchesUnorderedRows(List<List<PolyValue>> parsed, Row[] rows) {
List<Integer> used = new ArrayList<>();
for ( Row row : rows ) {

int i = 0;
for (Row row : rows) {
boolean matches = false;
for ( List<PolyValue> objects : parsed ) {

if ( !matches && !used.contains( i ) ) {
if ( row.matches( objects ) ) {
used.add( i );
matches = true;
}
for (int i = 0; i < parsed.size(); i++) {
if (!matches && !used.contains(i) && row.matches(parsed.get(i))) {
used.add(i);
matches = true;
break;
}

i++;
}
if ( !matches ) {
return false;
}

// Use assert to validate that each row finds a match
assertTrue(matches, "Row " + row + " could not be matched in the parsed data.");
}
return true;

}


private static boolean matchesExactRows( List<List<PolyValue>> parsed, Row[] rows ) {
boolean matches = true;
int j = 0;
for ( Row row : rows ) {
matches &= row.matches( parsed.get( j ) );
j++;

private static boolean matchesExactRows(List<List<PolyValue>> parsed, Row[] rows) {
for (int j = 0; j < rows.length; j++) {
// Use assert to ensure each row matches
assertTrue(rows[j].matches(parsed.get(j)), "Row " + j + " does not match the expected value.");
}
return matches;
return true;
}


@SneakyThrows
private <T extends GraphPropertyHolder> boolean contains( String[][] actual, boolean exclusive, int index, Class<T> clazz, TestObject[] expected ) {
private <T extends GraphPropertyHolder> boolean contains(String[][] actual, boolean exclusive, int index, Class<T> clazz, TestObject[] expected) {
List<T> parsed = new ArrayList<>();

for ( String[] entry : actual ) {
parsed.add( PolyValue.JSON_WRAPPER.readValue( entry[index], clazz ) );
for (String[] entry : actual) {
parsed.add(PolyValue.JSON_WRAPPER.readValue(entry[index], clazz));
}

assert !exclusive || parsed.size() == expected.length;
// Assert that if exclusive is true, the number of parsed items equals the number of expected items
assertEquals(exclusive ? expected.length : parsed.size(), parsed.size(), "Exclusive condition failed: parsed size does not match expected length.");

boolean contains = true;
for ( TestObject node : expected ) {
contains &= parsed.stream().anyMatch( n -> node.matches( n, exclusive ) );
for (TestObject node : expected) {
// Assert that each expected node matches at least one parsed element
assertTrue(parsed.stream().anyMatch(n -> node.matches(n, exclusive)), "Expected node does not match any parsed element.");
}

return contains;
return true;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public void singleCountAggregateTest() {

GraphResult res = execute( "MATCH (n:Person) RETURN count(*)" );

assert containsRows( res, true, true,
containsRows( res, true, true,
Row.of( TestLiteral.from( 5 ) ) );

execute( SINGLE_EDGE_2 );
res = execute( "MATCH (n:Person) RETURN count(*)" );

assert containsRows( res, true, true,
containsRows( res, true, true,
Row.of( TestLiteral.from( 7 ) ) );


Expand All @@ -72,7 +72,7 @@ public void countFieldAggregateTest() {

GraphResult res = execute( "MATCH (n) RETURN n.name, count(*)" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( 3 ) ),
Row.of( TestLiteral.from( "Kira" ), TestLiteral.from( 1 ) ),
Row.of( TestLiteral.from( "Hans" ), TestLiteral.from( 2 ) ) );
Expand All @@ -85,7 +85,7 @@ public void countNullAggregateTest() {

GraphResult res = execute( "MATCH (n) RETURN count(*)" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 0 ) ) );
}

Expand All @@ -98,9 +98,9 @@ public void countRenameFieldAggregateTest() {
execute( SINGLE_EDGE_2 );

GraphResult res = execute( "MATCH (n) RETURN n.name, count(*) AS c" );
assert res.getHeader()[1].name.equals( "c" );
res.getHeader()[1].name.equals( "c" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( 3 ) ),
Row.of( TestLiteral.from( "Kira" ), TestLiteral.from( 1 ) ),
Row.of( TestLiteral.from( "Hans" ), TestLiteral.from( 2 ) ) );
Expand All @@ -117,7 +117,7 @@ public void doubleCountRenameAggregateTest() {

GraphResult res = execute( "MATCH (n) RETURN n.name, n.age, count(*) AS c" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( null ), TestLiteral.from( 3 ) ),
Row.of( TestLiteral.from( "Kira" ), TestLiteral.from( 3 ), TestLiteral.from( 1 ) ),
Row.of( TestLiteral.from( "Hans" ), TestLiteral.from( null ), TestLiteral.from( 1 ) ),
Expand All @@ -131,7 +131,7 @@ public void countPropertyAggregateTest() {
execute( SINGLE_NODE_PERSON_1 );
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (n) RETURN count(n.age)" );
assert containsRows( res, true, false, Row.of( TestLiteral.from( 0 ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( 0 ) ) );
}


Expand All @@ -141,7 +141,7 @@ public void countDistinctFunctionTest() {
execute( SINGLE_NODE_PERSON_1 );

GraphResult res = execute( "MATCH (n) RETURN COUNT(DISTINCT n.name)" );
assert containsRows( res, true, false, Row.of( TestLiteral.from( 1 ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( 1 ) ) );


}
Expand All @@ -160,7 +160,7 @@ public void singleAvgAggregateTest() {
// Printing the data using Arrays.deepToString
String[][] data = res.getData();
System.out.println( Arrays.deepToString( data ) );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 26.33333333333333 ) ) );

}
Expand All @@ -172,7 +172,7 @@ public void avgNullAggregateTest() {
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (p:Person) RETURN AVG(p.age)" );

assert containsRows( res, true, false, Row.of( TestLiteral.from( null ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( null ) ) );
}


Expand All @@ -183,7 +183,7 @@ public void avgRenameAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute( "MATCH (p:Person) RETURN AVG(p.age) AS ages" );
assert containsRows( res, true, false, Row.of( TestLiteral.from( 38 ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( 38 ) ) );
}


Expand All @@ -208,7 +208,7 @@ public void singleCollectAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute( "MATCH (p:Person) RETURN COLLECT(p.age) " );
assert containsRows( res, true, false, Row.of( TestLiteral.from( List.of( 45, 31 ) ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( List.of( 45, 31 ) ) ) );

}

Expand All @@ -219,7 +219,7 @@ public void collectRenameAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute( "MATCH (p:Person) RETURN COLLECT(p.age) AS ages" );
assert containsRows( res, true, false, Row.of( TestLiteral.from( List.of( 45, 31 ) ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( List.of( 45, 31 ) ) ) );

}

Expand All @@ -231,7 +231,7 @@ public void collectRenameFieldAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_3 );

GraphResult res = execute( "MATCH (p:Person) RETURN COLLECT(p.age) AS ages , p.depno AS depNumber" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( List.of( 45, 31 ) ), TestLiteral.from( 13 ) ),
Row.of( TestLiteral.from( List.of( 32 ) ), TestLiteral.from( 14 ) ) );
}
Expand All @@ -242,7 +242,7 @@ public void collectNullAggregationTest() {
execute( SINGLE_NODE_PERSON_1 );
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (p:Person) RETURN COLLECT(p.age)" );
assert containsRows( res, true, false, Row.of( TestLiteral.from( List.of( null, null ) ) ) );
containsRows( res, true, false, Row.of( TestLiteral.from( List.of( null, null ) ) ) );
}


Expand All @@ -255,11 +255,11 @@ public void singleMinMaxAggregateTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_1 );

GraphResult res = execute( "MATCH (n) RETURN min(n.age)" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 3 ) ) );

res = execute( "MATCH (n) RETURN max(n.age)" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 45 ) ) );

}
Expand All @@ -271,12 +271,12 @@ void minMaxNullAggregateTest() {
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (n) RETURN min(n.age) as ageMin" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( null ) ) );

res = execute( "MATCH (n) RETURN max(n.age) as ageMax" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( null ) ) );


Expand All @@ -291,12 +291,12 @@ public void minMaxRenameAggregateTest() {

GraphResult res = execute( "MATCH (n) RETURN min(n.age) as ageMin" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 31 ) ) );

res = execute( "MATCH (n) RETURN max(n.age) as ageMax" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 45 ) ) );


Expand All @@ -312,13 +312,13 @@ public void minMaxRenameFieldAggregateTest() {

GraphResult res = execute( "MATCH (n) RETURN n.depno as depNumber , min(n.age) as ageMin" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 13 ), TestLiteral.from( 31 ) ),
Row.of( TestLiteral.from( 14 ), TestLiteral.from( 32 ) ) );

res = execute( "MATCH (n) RETURN n.depno as depNumber , max(n.age) as ageMax" );

assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 13 ), TestLiteral.from( 45 ) ),
Row.of( TestLiteral.from( 14 ), TestLiteral.from( 32 ) ) );

Expand All @@ -333,7 +333,7 @@ public void singleSumAggregateTest() {
execute( SINGLE_EDGE_2 );

GraphResult res = execute( "MATCH (n) RETURN sum(n.age)" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 34 ) ) );
}

Expand All @@ -344,7 +344,7 @@ public void sumNullAggregationTest() {
execute( SINGLE_NODE_PERSON_2 );

GraphResult res = execute( "MATCH (n) RETURN sum(n.age)" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 0 ) ) );

/// Printing the data using Arrays.deepToString
Expand All @@ -361,7 +361,7 @@ public void sumRenameAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute( "MATCH (p:Person) RETURN sum(p.age) As totalAge " );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 76 ) ) );


Expand All @@ -375,7 +375,7 @@ public void sumRenameFieldAggregationTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_3 );

GraphResult res = execute( "MATCH (p:Person) RETURN sum(p.age) AS totalAge, p.depno AS depNumber," );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 13 ), TestLiteral.from( 76 ) ),
Row.of( TestLiteral.from( 14 ), TestLiteral.from( 32 ) ) );

Expand All @@ -388,7 +388,7 @@ public void singleStDevAggregateTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_1 );
execute( SINGLE_NODE_PERSON_COMPLEX_2 );
GraphResult res = execute( "MATCH (p:Person) RETURN stdev(p.age) " );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 9.8994949 ) ) );


Expand All @@ -400,7 +400,7 @@ public void stDevNullAggregateTest() {
execute( SINGLE_NODE_PERSON_1 );
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (p:Person) RETURN stdev(p.age) " );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( null ) ) );
}

Expand All @@ -410,7 +410,7 @@ public void stDevRenameAggregateTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_1 );
execute( SINGLE_NODE_PERSON_COMPLEX_2 );
GraphResult res = execute( "MATCH (p:Person) RETURN stdev(p.age) AS Stdev " );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 9.8994949 ) ) );

}
Expand All @@ -422,7 +422,7 @@ public void stDevRenameFieldAggregateTest() {
execute( SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute( "MATCH (p:Person) RETURN stdev(p.age) AS Stdev , n.depno As department" );
assert containsRows( res, true, false,
containsRows( res, true, false,
Row.of( TestLiteral.from( 9.8994949 ), TestLiteral.from( 13 ) ) );
}

Expand Down
Loading

0 comments on commit f71f5d0

Please sign in to comment.