2
2
3
3
import org .junit .Rule ;
4
4
import org .junit .Test ;
5
- import org .neo4j .graphdb .Direction ;
6
- import org .neo4j .graphdb .Relationship ;
7
- import org .neo4j .graphdb .RelationshipType ;
8
5
import org .neo4j .graphdb .Transaction ;
9
6
import org .neo4j .kernel .internal .GraphDatabaseAPI ;
10
7
import org .neo4j .logging .NullLog ;
20
17
import static org .junit .Assert .assertEquals ;
21
18
import static org .junit .Assert .assertThat ;
22
19
23
- public class SequentialSimilarityExporterTest {
20
+ public class SimilarityExporterTest {
24
21
@ Rule
25
22
public final ImpermanentDatabaseRule DB = new ImpermanentDatabaseRule ();
26
23
27
- public static final String RELATIONSHIP_TYPE = "SIMILAR" ;
28
- public static final String PROPERTY_NAME = "score" ;
24
+ private static final String RELATIONSHIP_TYPE = "SIMILAR" ;
25
+ private static final String PROPERTY_NAME = "score" ;
26
+
27
+ @ Test
28
+ public void createNothing () {
29
+ GraphDatabaseAPI api = DB .getGraphDatabaseAPI ();
30
+ createNodes (api , 2 );
31
+
32
+ SequentialSimilarityExporter exporter = new SequentialSimilarityExporter (api , NullLog .getInstance (), RELATIONSHIP_TYPE , PROPERTY_NAME );
33
+
34
+ Stream <SimilarityResult > similarityPairs = Stream .empty ();
35
+
36
+ int batches = exporter .export (similarityPairs , 1 );
37
+ assertEquals (0 , batches );
38
+
39
+ try (Transaction tx = api .beginTx ()) {
40
+ List <SimilarityRelationship > allRelationships = getSimilarityRelationships (api );
41
+ assertThat (allRelationships , hasSize (0 ));
42
+ }
43
+ }
29
44
30
45
@ Test
31
46
public void createOneRelationship () {
@@ -40,9 +55,9 @@ public void createOneRelationship() {
40
55
assertEquals (1 , batches );
41
56
42
57
try (Transaction tx = api .beginTx ()) {
43
- Relationship relationship = api . getNodeById ( 0 ). getSingleRelationship ( RelationshipType . withName ( RELATIONSHIP_TYPE ), Direction . OUTGOING );
44
- assertEquals ( 1 , relationship . getEndNodeId ( ));
45
- assertEquals ( 0.5 , ( Double ) relationship . getProperty ( PROPERTY_NAME ), 0.01 );
58
+ List < SimilarityRelationship > allRelationships = getSimilarityRelationships ( api );
59
+ assertThat ( allRelationships , hasSize ( 1 ));
60
+ assertThat ( allRelationships , hasItems ( new SimilarityRelationship ( 0 , 1 , 0.5 )) );
46
61
}
47
62
}
48
63
@@ -62,16 +77,46 @@ public void multipleBatches() {
62
77
assertEquals (2 , batches );
63
78
64
79
try (Transaction tx = api .beginTx ()) {
65
- List <SimilarityRelationship > allRelationships = api .getAllRelationships ().stream ()
66
- .map (relationship -> new SimilarityRelationship (relationship .getStartNodeId (), relationship .getEndNodeId (), (double )relationship .getProperty (PROPERTY_NAME )))
67
- .collect (Collectors .toList ());
80
+ List <SimilarityRelationship > allRelationships = getSimilarityRelationships (api );
68
81
69
82
assertThat (allRelationships , hasSize (2 ));
70
83
assertThat (allRelationships , hasItems (new SimilarityRelationship (0 , 1 , 0.5 )));
71
84
assertThat (allRelationships , hasItems (new SimilarityRelationship (2 , 3 , 0.7 )));
72
85
}
73
86
}
74
87
88
+ @ Test
89
+ public void smallerThanBatchSize () {
90
+ GraphDatabaseAPI api = DB .getGraphDatabaseAPI ();
91
+ createNodes (api , 5 );
92
+
93
+ SequentialSimilarityExporter exporter = new SequentialSimilarityExporter (api , NullLog .getInstance (), RELATIONSHIP_TYPE , PROPERTY_NAME );
94
+
95
+ Stream <SimilarityResult > similarityPairs = Stream .of (
96
+ new SimilarityResult (0 , 1 , -1 , -1 , -1 , 0.5 ),
97
+ new SimilarityResult (2 , 3 , -1 , -1 , -1 , 0.7 ),
98
+ new SimilarityResult (3 , 4 , -1 , -1 , -1 , 0.7 )
99
+ );
100
+
101
+ int batches = exporter .export (similarityPairs , 10 );
102
+ assertEquals (1 , batches );
103
+
104
+ try (Transaction tx = api .beginTx ()) {
105
+ List <SimilarityRelationship > allRelationships = getSimilarityRelationships (api );
106
+
107
+ assertThat (allRelationships , hasSize (3 ));
108
+ assertThat (allRelationships , hasItems (new SimilarityRelationship (0 , 1 , 0.5 )));
109
+ assertThat (allRelationships , hasItems (new SimilarityRelationship (2 , 3 , 0.7 )));
110
+ assertThat (allRelationships , hasItems (new SimilarityRelationship (3 , 4 , 0.7 )));
111
+ }
112
+ }
113
+
114
+ private List <SimilarityRelationship > getSimilarityRelationships (GraphDatabaseAPI api ) {
115
+ return api .getAllRelationships ().stream ()
116
+ .map (relationship -> new SimilarityRelationship (relationship .getStartNodeId (), relationship .getEndNodeId (), (double )relationship .getProperty (PROPERTY_NAME )))
117
+ .collect (Collectors .toList ());
118
+ }
119
+
75
120
static class SimilarityRelationship {
76
121
private final long startNodeId ;
77
122
private final long endNodeId ;
0 commit comments