Skip to content

Commit 121c6e9

Browse files
Add GDL extension that runs setup/teardown once per class
Co-Authored-By: Sören Reichardt <soren.reichardt@neotechnology.com>
1 parent 13ca96e commit 121c6e9

File tree

10 files changed

+183
-16
lines changed

10 files changed

+183
-16
lines changed

core/src/test/java/org/neo4j/gds/core/loading/GraphStoreCatalogTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import static org.junit.jupiter.api.Assertions.assertNotEquals;
4444
import static org.junit.jupiter.api.Assertions.assertTrue;
4545
import static org.junit.jupiter.api.Assertions.fail;
46-
import static org.neo4j.gds.extension.GdlSupportExtension.DATABASE_ID;
46+
import static org.neo4j.gds.extension.GdlSupportPerMethodExtension.DATABASE_ID;
4747

4848
@GdlExtension
4949
class GraphStoreCatalogTest {

test-utils/src/main/java/org/neo4j/gds/TestSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.neo4j.gds.core.GraphDimensions;
3838
import org.neo4j.gds.core.loading.construction.GraphFactory;
3939
import org.neo4j.gds.core.utils.mem.MemoryEstimation;
40-
import org.neo4j.gds.extension.GdlSupportExtension;
40+
import org.neo4j.gds.extension.GdlSupportPerMethodExtension;
4141
import org.neo4j.gds.extension.IdFunction;
4242
import org.neo4j.gds.extension.TestGraph;
4343
import org.neo4j.gds.gdl.GdlFactory;
@@ -155,7 +155,7 @@ public static TestGraph gdl(
155155
.builder()
156156
.nodeIdFunction(idSupplier.orElse(new OffsetIdSupplier(0L)))
157157
.graphProjectConfig(config)
158-
.namedDatabaseId(GdlSupportExtension.DATABASE_ID)
158+
.namedDatabaseId(GdlSupportPerMethodExtension.DATABASE_ID)
159159
.build();
160160

161161
return new TestGraph(gdlFactory.build().getUnion(), gdlFactory::nodeId, graphName);

test-utils/src/main/java/org/neo4j/gds/extension/GdlSupportExtension.java renamed to test-utils/src/main/java/org/neo4j/gds/extension/BaseGdlSupportExtension.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
package org.neo4j.gds.extension;
2121

2222
import org.immutables.value.Value;
23-
import org.junit.jupiter.api.extension.AfterEachCallback;
24-
import org.junit.jupiter.api.extension.BeforeEachCallback;
2523
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
2624
import org.junit.jupiter.api.extension.ExtensionContext;
2725
import org.neo4j.gds.Orientation;
@@ -52,18 +50,16 @@
5250
import static org.neo4j.gds.extension.ExtensionUtil.getStringValueOfField;
5351
import static org.neo4j.gds.extension.ExtensionUtil.setField;
5452

55-
public class GdlSupportExtension implements BeforeEachCallback, AfterEachCallback {
53+
abstract class BaseGdlSupportExtension {
5654

5755
public static final NamedDatabaseId DATABASE_ID = DatabaseIdFactory.from("GDL", UUID.fromString("42-42-42-42-42"));
5856

59-
@Override
60-
public void beforeEach(ExtensionContext context) {
57+
void beforeAction(ExtensionContext context) {
6158
Class<?> requiredTestClass = context.getRequiredTestClass();
6259
gdlGraphs(requiredTestClass).forEach(setup -> injectGraphStore(setup, context));
6360
}
6461

65-
@Override
66-
public void afterEach(ExtensionContext context) {
62+
void afterAction(ExtensionContext context) {
6763
GraphStoreCatalog.removeAllLoadedGraphs();
6864
}
6965

@@ -73,7 +69,7 @@ private static Collection<GdlGraphSetup> gdlGraphs(Class<?> testClass) {
7369
do {
7470
stream(testClass.getDeclaredFields())
7571
.filter(f -> f.isAnnotationPresent(GdlGraph.class) || f.isAnnotationPresent(GdlGraphs.class))
76-
.flatMap(GdlSupportExtension::gdlGraphsForField)
72+
.flatMap(BaseGdlSupportExtension::gdlGraphsForField)
7773
.peek(setup -> {
7874
if (setups.contains(setup)) {
7975
throw new ExtensionConfigurationException(String.format(
@@ -158,7 +154,13 @@ private static void injectGraphStore(GdlGraphSetup gdlGraphSetup, ExtensionConte
158154
});
159155
}
160156

161-
private static <T> void injectInstance(Object testInstance, String graphNamePrefix, T instance, Class<T> clazz, String suffix) {
157+
private static <T> void injectInstance(
158+
Object testInstance,
159+
String graphNamePrefix,
160+
T instance,
161+
Class<T> clazz,
162+
String suffix
163+
) {
162164
Stream.<Class<?>>iterate(testInstance.getClass(), Objects::nonNull, Class::getSuperclass)
163165
.flatMap(c -> Arrays.stream(c.getDeclaredFields()))
164166
.filter(field -> field.getType() == clazz)

test-utils/src/main/java/org/neo4j/gds/extension/GdlExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
@Inherited
3131
@Target({ElementType.TYPE, ElementType.METHOD})
3232
@Retention(RetentionPolicy.RUNTIME)
33-
@ExtendWith(GdlSupportExtension.class)
33+
@ExtendWith(GdlSupportPerMethodExtension.class)
3434
public @interface GdlExtension {
3535
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.extension;
21+
22+
import org.junit.jupiter.api.TestInstance;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
25+
import java.lang.annotation.ElementType;
26+
import java.lang.annotation.Inherited;
27+
import java.lang.annotation.Retention;
28+
import java.lang.annotation.RetentionPolicy;
29+
import java.lang.annotation.Target;
30+
31+
@Inherited
32+
@Target({ElementType.TYPE, ElementType.METHOD})
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@ExtendWith(GdlSupportPerClassExtension.class)
35+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
36+
public @interface GdlPerClassExtension {
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.extension;
21+
22+
import org.junit.jupiter.api.extension.AfterAllCallback;
23+
import org.junit.jupiter.api.extension.BeforeAllCallback;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
25+
26+
public class GdlSupportPerClassExtension extends BaseGdlSupportExtension implements BeforeAllCallback, AfterAllCallback {
27+
28+
@Override
29+
public void beforeAll(ExtensionContext context) {
30+
beforeAction(context);
31+
}
32+
33+
@Override
34+
public void afterAll(ExtensionContext context) {
35+
afterAction(context);
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.extension;
21+
22+
import org.junit.jupiter.api.extension.AfterEachCallback;
23+
import org.junit.jupiter.api.extension.BeforeEachCallback;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
25+
26+
public class GdlSupportPerMethodExtension extends BaseGdlSupportExtension implements BeforeEachCallback, AfterEachCallback {
27+
28+
@Override
29+
public void beforeEach(ExtensionContext context) {
30+
beforeAction(context);
31+
}
32+
33+
@Override
34+
public void afterEach(ExtensionContext context) {
35+
afterAction(context);
36+
}
37+
}

test-utils/src/main/java/org/neo4j/gds/gdl/GdlFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.neo4j.gds.core.utils.mem.MemoryEstimation;
5555
import org.neo4j.gds.core.utils.mem.MemoryEstimations;
5656
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
57-
import org.neo4j.gds.extension.GdlSupportExtension;
57+
import org.neo4j.gds.extension.GdlSupportPerMethodExtension;
5858
import org.neo4j.kernel.database.NamedDatabaseId;
5959
import org.neo4j.values.storable.NumberType;
6060
import org.neo4j.values.storable.Values;
@@ -106,7 +106,7 @@ static GdlFactory gdlFactory(
106106
.build()
107107
: graphProjectConfig.get();
108108

109-
var databaseId = namedDatabaseId.orElse(GdlSupportExtension.DATABASE_ID);
109+
var databaseId = namedDatabaseId.orElse(GdlSupportPerMethodExtension.DATABASE_ID);
110110

111111
var nextVertexId = nodeIdFunction
112112
.map(supplier -> (Function<Optional<String>, Long>) (ignored) -> supplier.getAsLong())
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.extension;
21+
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
import org.neo4j.gds.api.GraphStore;
25+
import org.neo4j.gds.config.GraphProjectFromStoreConfig;
26+
import org.neo4j.gds.core.loading.GraphStoreCatalog;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@GdlPerClassExtension
31+
class GdlSupportPerClassExtensionTest {
32+
33+
@GdlGraph
34+
public static final String GRAPH = "(a)-[:REL]->(b)";
35+
36+
@Inject
37+
GraphStore graphStore;
38+
39+
@BeforeAll
40+
void setUp() {
41+
var graphProjectConfig = GraphProjectFromStoreConfig.emptyWithName("", "graph");
42+
GraphStoreCatalog.set(graphProjectConfig, graphStore);
43+
}
44+
45+
@Test
46+
void testHasGraph() {
47+
assertThat(GraphStoreCatalog.get("", BaseGdlSupportExtension.DATABASE_ID, "graph")).isNotNull();
48+
}
49+
50+
@Test
51+
void testStillHasGraph() {
52+
assertThat(GraphStoreCatalog.get("", BaseGdlSupportExtension.DATABASE_ID, "graph")).isNotNull();
53+
}
54+
}

test-utils/src/test/java/org/neo4j/gds/extension/GdlSupportExtensionTest.java renamed to test-utils/src/test/java/org/neo4j/gds/extension/GdlSupportPerMethodExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static org.assertj.core.api.Assertions.assertThat;
2525

2626
@GdlExtension
27-
class GdlSupportExtensionTest {
27+
class GdlSupportPerMethodExtensionTest {
2828

2929
@GdlGraph(idOffset = 42, graphNamePrefix = "idOffset")
3030
public static final String ID_OFFSET_GRAPH = "(a)-[:REL]->(b)";

0 commit comments

Comments
 (0)