Skip to content

Commit

Permalink
Add test coverage for Collect SubQueries
Browse files Browse the repository at this point in the history
  • Loading branch information
alyaa999 committed Jul 29, 2024
1 parent 33ab986 commit 4b3164f
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.polypheny.db.cypher.helper.TestLiteral;
import org.polypheny.db.webui.models.results.GraphResult;
import java.util.Arrays;
import java.util.List;


public class AggregateTest extends CypherTestTemplate {
Expand Down Expand Up @@ -141,7 +142,7 @@ public void countDistinctFunctionTest() {
execute(SINGLE_NODE_PERSON_1);


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


Expand All @@ -159,9 +160,8 @@ public void singleAvgAggregateTest() {
execute( SINGLE_EDGE_2 );
execute( SINGLE_NODE_PERSON_COMPLEX_1 );

GraphResult res = execute( "MATCH (n) RETURN avg(n.age)" );
GraphResult res = execute( "MATCH (n) RETURN AVG(n.age)" );
// Printing the data using Arrays.deepToString
System.out.print( "Alyaa :" );
String[][] data = res.getData();
System.out.println( Arrays.deepToString(data));
assert containsRows( res, true, false,
Expand All @@ -173,7 +173,7 @@ assert containsRows( res, true, false,
public void avgNullAggregateTest() {
execute(SINGLE_NODE_PERSON_1);
execute(SINGLE_NODE_PERSON_2);
GraphResult res = execute("MATCH (p:Person) RETURN avg(p.age)");
GraphResult res = execute("MATCH (p:Person) RETURN AVG(p.age)");

assert containsRows(res, true, false, Row.of(TestLiteral.from(null)));
}
Expand All @@ -184,7 +184,7 @@ public void avgRenameAggregationTest() {
execute(SINGLE_NODE_PERSON_COMPLEX_1);
execute(SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute("MATCH (p:Person) RETURN avg(p.age) AS ages");
GraphResult res = execute("MATCH (p:Person) RETURN AVG(p.age) AS ages");
assert containsRows(res, true, false, Row.of(TestLiteral.from(38)));
}
@Test
Expand All @@ -196,7 +196,7 @@ public void avgRenameFieldAggregationTest() {
execute(SINGLE_NODE_PERSON_COMPLEX_3 );


GraphResult res = execute("MATCH (p:Person) RETURN p.depno As depNumber , avg(p.age) As avgAge");
GraphResult res = execute("MATCH (p:Person) RETURN p.depno As depNumber , AVG(p.age) As avgAge");
containsRows(res, true, false,
Row.of(TestLiteral.from(13) , TestLiteral.from( 38 )),
Row.of( TestLiteral.from( 14 ), TestLiteral.from( 32 ) ));
Expand All @@ -207,14 +207,18 @@ public void singleCollectAggregationTest() {
execute(SINGLE_NODE_PERSON_COMPLEX_1);
execute(SINGLE_NODE_PERSON_COMPLEX_2 );

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

}
@Test
public void collectRenameAggregationTest() {
execute(SINGLE_NODE_PERSON_COMPLEX_1);
execute(SINGLE_NODE_PERSON_COMPLEX_2 );

GraphResult res = execute("MATCH (p:Person) RETURN collect(p.age) AS ages");
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)) ) );

}

@Test
Expand All @@ -223,13 +227,17 @@ public void collectRenameFieldAggregationTest() {
execute(SINGLE_NODE_PERSON_COMPLEX_2 );
execute( SINGLE_NODE_PERSON_COMPLEX_3 );

GraphResult res = execute("MATCH (p:Person) RETURN collect(p.age) AS ages , p.depno AS depNumber");
GraphResult res = execute("MATCH (p:Person) RETURN COLLECT(p.age) AS ages , p.depno AS depNumber");
assert 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 ) ));
}
@Test
public void collectNullAggregationTest() {
execute( SINGLE_NODE_PERSON_1 );
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute("MATCH (p:Person) RETURN collect(p.age)");
GraphResult res = execute("MATCH (p:Person) RETURN COLLECT(p.age)");
assert containsRows( res , true , false , Row.of( TestLiteral.from( List.of(null,null)) ) );
}
@Test
public void singleMinMaxAggregateTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2019-2024 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.cypher.Subqueries;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.polypheny.db.cypher.CypherTestTemplate;
import org.polypheny.db.cypher.helper.TestLiteral;
import org.polypheny.db.util.Pair;
import org.polypheny.db.webui.models.results.GraphResult;
import java.util.List;

public class CollectSubQueriesTest extends CypherTestTemplate {


@BeforeEach
public void reset() {
tearDown();
createGraph();
}


public static final String EDGE_3 = "CREATE (p:Person {name :'Max'}),(p)-[rel:OWNER_OF{ since : 2002}] -> (c:Cat : Animal {name :'Mittens' , age : 3}), (p)-[rel2:OWNER_OF { since : 1999}] -> (d:Dog :Animal { name : 'Andy' , age :10})";


@Test
public void simpleCollectSubQueryTest() {
execute( SINGLE_EDGE_1 );

GraphResult res = execute( "MATCH (person:Person)\n"
+ "WHERE 'Kira' IN COLLECT { MATCH (person)-[:OWNER_OF]->(a:Animal) RETURN a.name }\n"
+ "RETURN person.name AS name" );

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

}


@Test
public void useCollectSubQueryInReturnTest() {
execute( SINGLE_NODE_PERSON_1 );
execute( EDGE_3 );
GraphResult res = execute( "MATCH (person:Person)\n"
+ "RETURN person.name,\n"
+ " COLLECT {\n"
+ " MATCH (person)-[:OWNER_OF]->(d:Dog)\n"
+ " RETURN d.name\n"
+ " } as DogNames" );

assert containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( "Andy" ) ) );

}


@Test
public void whereWithCollectSubQueryTest() {
execute( EDGE_3 );
GraphResult res = execute( "\n"
+ "MATCH (person:Person)\n"
+ "RETURN person.name as name, COLLECT {\n"
+ " MATCH (person)-[r:OWNER_OF]->(a:Dog)\n"
+ " WHERE a.age <= 3\n"
+ " RETURN a.name\n"
+ "} as youngDog \n" );

assert containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( "Andy" ) ) );

}


@Test
public void unionWithCollectSubQueryTest() {
execute( EDGE_3 );
GraphResult res = execute( "MATCH (person:Person)\n"
+ "RETURN\n"
+ " person.name AS name,\n"
+ " COLLECT {\n"
+ " MATCH (person)-[:HAS_DOG]->(dog:Dog)\n"
+ " RETURN dog.name AS petName\n"
+ " UNION\n"
+ " MATCH (person)-[:HAS_CAT]->(cat:Cat)\n"
+ " RETURN cat.name AS petName\n"
+ " } AS petNames" );

assert containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( List.of( "Andy", "Mittens" ) ) ) );


}


@Test
public void withClauseWithCollectSubQueryTest() {
execute( EDGE_3 );
GraphResult res = execute( "MATCH (person:Person)\n"
+ "RETURN person.name AS name, COLLECT {\n"
+ " WITH 1999 AS yearOfTheDog\n"
+ " MATCH (person)-[r:OWNER_OF]->(d:Dog)\n"
+ " WHERE r.since = yearOfTheDog\n"
+ " RETURN d.name\n"
+ "} as dogsOfTheYear" );

assert containsRows( res, true, false,
Row.of( TestLiteral.from( "Max" ), TestLiteral.from( "Andy" ) ) );
}


@Test
public void caseWithCollectSubQueryTest() {
execute( EDGE_3 );
execute( SINGLE_NODE_PERSON_2 );
GraphResult res = execute( "MATCH (person:Person)\n"
+ "RETURN\n"
+ " CASE\n"
+ " WHEN COLLECT { MATCH (person)-[:OWNER_OF]->(d:Dog) RETURN d.name } = [] THEN \" No Dogs \" + person.name\n"
+ " ELSE person.name\n"
+ " END AS result" );

assert containsRows( res, true, false,
Row.of( TestLiteral.from( "No Dogs" ) ),
Row.of( TestLiteral.from( "Max" ) ) );

}


@Test
public void updateWithCollectSubQueryTest() {
execute( SINGLE_NODE_PERSON_2 );
execute( EDGE_3 );
GraphResult res = execute( "MATCH (person:Person)"
+ " WHERE person.name = \"Hans\"\n"
+ "SET person.dogNames = COLLECT { MATCH (person)-[:OWNER_OF]->(d:Dog) RETURN d.name }\n"
+ "RETURN person.dogNames as dogNames" );

assert containsRows( res , true , false ,
Row.of( TestLiteral.from( List.of("Andy") ) ) );
}



}

0 comments on commit 4b3164f

Please sign in to comment.