|
| 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.procedures.algorithms.pathfinding.mutate; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.neo4j.gds.api.Graph; |
| 24 | +import org.neo4j.gds.api.GraphStore; |
| 25 | +import org.neo4j.gds.applications.algorithms.machinery.MutateRelationshipService; |
| 26 | +import org.neo4j.gds.applications.algorithms.metadata.RelationshipsWritten; |
| 27 | +import org.neo4j.gds.logging.Log; |
| 28 | +import org.neo4j.gds.pathfinding.SpanningTreeMutateStep; |
| 29 | +import org.neo4j.gds.result.TimedAlgorithmResult; |
| 30 | +import org.neo4j.gds.spanningtree.SpanningTree; |
| 31 | + |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.ArgumentMatchers.any; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.times; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
| 42 | +class SpanningTreeMutateResultTransformerTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + void shouldTransformToMutateResult() { |
| 46 | + var config = Map.<String, Object>of("foo", "bar"); |
| 47 | + var graph = mock(Graph.class); |
| 48 | + var graphStore = mock(GraphStore.class); |
| 49 | + var mutateStep = mock(SpanningTreeMutateStep.class); |
| 50 | + |
| 51 | + var algoResult = mock(SpanningTree.class); |
| 52 | + when(algoResult.effectiveNodeCount()).thenReturn(1L); |
| 53 | + when(algoResult.totalWeight()).thenReturn(3d); |
| 54 | + |
| 55 | + |
| 56 | + var relationshipsWritten = new RelationshipsWritten(5L); |
| 57 | + when(mutateStep.execute(any(), any(), any())).thenReturn(relationshipsWritten); |
| 58 | + |
| 59 | + var timedResult = new TimedAlgorithmResult<>(algoResult, 123L); |
| 60 | + |
| 61 | + var transformer = new SpanningTreeMutateResultTransformer(mutateStep, graph, graphStore, config); |
| 62 | + |
| 63 | + var resultStream = transformer.apply(timedResult); |
| 64 | + var result = resultStream.findFirst().orElseThrow(); |
| 65 | + |
| 66 | + assertThat(result.preProcessingMillis()).isZero(); |
| 67 | + assertThat(result.computeMillis()).isEqualTo(123L); |
| 68 | + assertThat(result.mutateMillis()).isNotNegative(); |
| 69 | + assertThat(result.configuration()).isEqualTo(config); |
| 70 | + assertThat(result.effectiveNodeCount()).isEqualTo(1L); |
| 71 | + assertThat(result.totalWeight()).isEqualTo(3d); |
| 72 | + |
| 73 | + assertThat(result.relationshipsWritten()).isEqualTo(5L); |
| 74 | + |
| 75 | + verify(mutateStep, times(1)).execute(graph, graphStore, algoResult); |
| 76 | + verifyNoMoreInteractions(mutateStep); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void shouldTransformEmptyResultToMutateResult() { |
| 81 | + var config = Map.<String, Object>of("boo", "foo"); |
| 82 | + var graph = mock(Graph.class); |
| 83 | + var graphStore = mock(GraphStore.class); |
| 84 | + var mutateRelationshipService = new MutateRelationshipService(Log.noOpLog()); |
| 85 | + var mutateStep = new SpanningTreeMutateStep( |
| 86 | + "r", |
| 87 | + "foo", |
| 88 | + mutateRelationshipService |
| 89 | + ); |
| 90 | + |
| 91 | + var algoResult = SpanningTree.EMPTY; |
| 92 | + |
| 93 | + var timedResult = new TimedAlgorithmResult<>(algoResult, 123L); |
| 94 | + |
| 95 | + var transformer = new SpanningTreeMutateResultTransformer(mutateStep, graph, graphStore, config); |
| 96 | + |
| 97 | + var resultStream = transformer.apply(timedResult); |
| 98 | + var result = resultStream.findFirst().orElseThrow(); |
| 99 | + |
| 100 | + assertThat(result.preProcessingMillis()).isZero(); |
| 101 | + assertThat(result.computeMillis()).isEqualTo(123L); |
| 102 | + assertThat(result.mutateMillis()).isNotNegative(); |
| 103 | + assertThat(result.configuration()).isEqualTo(config); |
| 104 | + assertThat(result.effectiveNodeCount()).isEqualTo(0L); |
| 105 | + assertThat(result.totalWeight()).isEqualTo(0d); |
| 106 | + assertThat(result.relationshipsWritten()).isEqualTo(0); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments