-
Notifications
You must be signed in to change notification settings - Fork 2
/
neo4j_integration_test.go
300 lines (249 loc) · 9.46 KB
/
neo4j_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package neo
import (
"context"
"strconv"
"testing"
"github.com/prahaladd/gograph/core"
itests "github.com/prahaladd/gograph/integrationtests"
"github.com/prahaladd/gograph/neo"
"github.com/prahaladd/gograph/omg"
"github.com/stretchr/testify/suite"
)
const (
defaultProtocol = "neo4j"
defaultHost = "localhost"
defaultRealm = ""
defaultUsername = "neo4j"
defaultPassword = "neo4j"
defaultPort = "7687"
)
type Neo4JIntegrationTestSuite struct {
suite.Suite
connection core.Connection
store omg.Store
}
func (suite *Neo4JIntegrationTestSuite) SetupTest() {
protocol := itests.GetFromEnvWithDefault("NEO4J_PROTOCOL", defaultProtocol)
host := itests.GetFromEnvWithDefault("NEO4J_HOST", defaultHost)
portString := itests.GetFromEnvWithDefault("NEO4J_PORT", "")
var port *int32
if len(portString) > 0 {
parsedPort, err := strconv.ParseInt(portString, 10, 32)
suite.NoError(err)
port = new(int32)
*port = int32(parsedPort)
}
realm := itests.GetFromEnvWithDefault("NEO4J_REALM", defaultRealm)
user := itests.GetFromEnvWithDefault("NEO4J_USER", defaultUsername)
pwd := itests.GetFromEnvWithDefault("NEO4J_PWD", defaultPassword)
neo4jConnectionFactory := core.GetConnectorFactory("neo4j")
connection, err := neo4jConnectionFactory(protocol, host, realm, port, map[string]interface{}{neo.NEO4J_USER_KEY: user, neo.NEO4J_PWD_KEY: pwd}, nil)
suite.connection = connection
suite.NoErrorf(err, "error whe setting up Neo4j Test : %v", err)
suite.cleanupDB()
suite.store = omg.NewGenericStore(connection, omg.NewReflectionMapper())
}
func (suite *Neo4JIntegrationTestSuite) TestWriteAndQuery() {
query := "CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)"
queryResult, err := suite.connection.ExecuteQuery(context.Background(), query, core.Write, map[string]any{"message": "hello, world"})
suite.NoErrorf(err, "error executing result : %v", err)
suite.Equal(1, len(queryResult.Rows))
// attempt to read the node created in the above
query = "MATCH (a:Greeting) return a"
queryResult, err = suite.connection.ExecuteQuery(context.Background(), query, core.Read, nil)
suite.NoErrorf(err, "error executing query : %v", err)
suite.Equal(1, len(queryResult.Rows))
}
func (suite *Neo4JIntegrationTestSuite) TestVertexQuery() {
query := "create (p:Person{name:'Tintin'})-[r:LIVES_IN{since:1929}]->(c:Country{name:'Belgium'}) return p, r, c"
queryResult, err := suite.connection.ExecuteQuery(context.Background(), query, core.Write, map[string]any{"message": "hello, world"})
suite.NoErrorf(err, "error executing result : %v", err)
suite.Equal(1, len(queryResult.Rows))
row := queryResult.Rows[0]
suite.Equal(3, len(row))
vertices, err := suite.connection.QueryVertex(context.Background(), "Person", core.KVMap{"name": "Tintin"}, nil, nil)
suite.NoError(err)
suite.NotNil(vertices)
suite.Equal(1, len(vertices))
vertex := vertices[0]
suite.Equal("Tintin", vertex.Properties["name"])
edge, err := suite.connection.QueryEdge(context.Background(), []string{"Person"}, []string{"Country"}, "LIVES_IN",
core.KVMap{"name": "Tintin"}, core.KVMap{"name": "Belgium"}, nil, nil, nil, nil, nil, core.EdgeWithCompleteVertex)
suite.NoError(err)
suite.Equal(1, len(edge))
suite.Equal("LIVES_IN", edge[0].Type)
}
func (suite *Neo4JIntegrationTestSuite) TestStoreVertex() {
vertex := core.Vertex{
Labels: []string{"OMGStoreVertex"},
Properties: core.KVMap{"Name": "Tom and Jerry", "Genre": "Kids Cartoon series"},
}
err := suite.connection.StoreVertex(context.Background(), &vertex)
suite.NoError(err)
//retrieve the vertex and validate that vertex ids
storedVertex, err := suite.connection.QueryVertex(context.Background(), "OMGStoreVertex", core.KVMap{"Name": "Tom and Jerry"}, nil, nil)
suite.NoError(err)
suite.Equal(1, len(storedVertex))
suite.Equal(storedVertex[0].ID, vertex.ID)
}
func (suite *Neo4JIntegrationTestSuite) TestStoreEdge() {
cv := core.Vertex{
Labels: []string{"Cartoon"},
Properties: core.KVMap{"Name": "Tom and Jerry", "Genre": "Kids Cartoon series"},
}
pv := core.Vertex{
Labels: []string{"Team"},
Properties: core.KVMap{"Name": "William Hanna and Joseph Barbara"},
}
rel := core.Edge{
Type: "CREATED_BY",
Properties: core.KVMap{"Year": 1940},
SourceVertex: &cv,
DestinationVertex: &pv,
}
err := suite.connection.StoreEdge(context.Background(), &rel)
suite.NoError(err)
//retrieve the vertex and validate that vertex ids
storedVertex, err := suite.connection.QueryVertex(context.Background(), "Cartoon", core.KVMap{"Name": "Tom and Jerry"}, nil, nil)
suite.NoError(err)
suite.Equal(1, len(storedVertex))
suite.Equal(storedVertex[0].ID, cv.ID)
storedVertex, err = suite.connection.QueryVertex(context.Background(), "Team", core.KVMap{"Name": "William Hanna and Joseph Barbara"}, nil, nil)
suite.NoError(err)
suite.Equal(1, len(storedVertex))
suite.Equal(storedVertex[0].ID, pv.ID)
storedEdge, err := suite.connection.QueryEdge(context.Background(), []string{"Cartoon"}, []string{"Team"}, "CREATED_BY", nil, nil, core.KVMap{"Year": 1940}, nil, nil, nil, nil, core.EdgeWithVertexIds)
suite.NoError(err)
suite.Equal(1, len(storedEdge))
suite.Equal(storedEdge[0].ID, rel.ID)
suite.Equal(cv.ID, storedEdge[0].SourceVertexID)
suite.Equal(pv.ID, storedEdge[0].DestinationVertexID)
}
func (suite *Neo4JIntegrationTestSuite) TestStoreEdgeWithInvalidData() {
pv := core.Vertex{
Labels: []string{"Team"},
Properties: core.KVMap{"Name": "William Hanna and Joseph Barbara"},
}
{
rel := core.Edge{
Type: "CREATED_BY",
Properties: core.KVMap{"Year": 1940},
DestinationVertex: &pv,
}
err := suite.connection.StoreEdge(context.Background(), &rel)
suite.Error(err)
}
}
func (suite *Neo4JIntegrationTestSuite) TestStoreOmgStructAsVertex() {
p := person{Name: "Tom", Age: 10}
err := suite.store.PersistVertex(context.TODO(), &p)
suite.NoError(err)
p2, err := suite.store.ReadVertex(context.TODO(), &person{Name: "Tom", Age: 10})
suite.NoError(err)
suite.Equal(1, len(p2))
suite.Equal(p, *(p2[0].(*person)))
}
func (suite *Neo4JIntegrationTestSuite) TestStoreOmgStructsAsEdge() {
p := person{Name: "Tom", Age: 10}
c := city{Name: "Mumbai", PinCode: 400001}
r := livesin{Area: "Town Hall", Since: 1982}
vc := omg.VertexRelation{SourceVertex: &p, DestinationVertex: &c, Relationship: &r}
err := suite.store.PersistEdge(context.TODO(), &vc)
suite.NoError(err)
// query the edge back
vrs, err := suite.store.ReadEdge(context.TODO(), &vc)
suite.NoError(err)
suite.Equal(1, len(vrs))
suite.Equal(vc, *vrs[0])
}
func (suite *Neo4JIntegrationTestSuite) TestStoreOmgStructsAsEdgeNoSrcVertex() {
p := person{Name: "Tom", Age: 10}
c := city{Name: "Mumbai", PinCode: 400001}
r := livesin{Area: "Town Hall", Since: 1982}
{
vc := omg.VertexRelation{SourceVertex: &p, DestinationVertex: &c, Relationship: &r}
// query the edge back with no source vertex -- should result in an error
vc.SourceVertex = nil
_, err := suite.store.ReadEdge(context.TODO(), &vc)
suite.Error(err)
}
{
vc := omg.VertexRelation{SourceVertex: &p, DestinationVertex: &c, Relationship: &r}
// query the edge back with no destination vertex -- should result in an error
vc.DestinationVertex = nil
_, err := suite.store.ReadEdge(context.TODO(), &vc)
suite.Error(err)
}
{
vc := omg.VertexRelation{SourceVertex: &p, DestinationVertex: &c, Relationship: &r}
// query the edge back with no relation -- should result in an error
vc.Relationship = nil
_, err := suite.store.ReadEdge(context.TODO(), &vc)
suite.Error(err)
}
{
vc := omg.VertexRelation{}
// query the edge back with empty vertex relation
_, err := suite.store.ReadEdge(context.TODO(), &vc)
suite.Error(err)
}
}
func (suite *Neo4JIntegrationTestSuite) TestCustomDatabase() {
ctx := context.Background()
ctx = context.WithValue(ctx, neo.ContextKeyDbName, "testdb")
res, err := suite.connection.ExecuteQuery(ctx, "match (m)-[r]-(n) return m,r,n", core.Read, nil)
suite.Nil(res)
suite.Error(err)
}
func (suite *Neo4JIntegrationTestSuite) TestCustomDatabaseWithEmptyName() {
ctx := context.Background()
ctx = context.WithValue(ctx, neo.ContextKeyDbName, "")
res, err := suite.connection.ExecuteQuery(ctx, "match (m)-[r]-(n) return m,r,n", core.Read, nil)
suite.NoError(err)
suite.NotNil(res)
}
func (suite *Neo4JIntegrationTestSuite) TearDownTest() {
suite.cleanupDB()
}
func (suite *Neo4JIntegrationTestSuite) cleanupDB() {
query := "MATCH (n) DETACH DELETE(n)"
_, err := suite.connection.ExecuteQuery(context.Background(), query, core.Write, map[string]any{"message": "hello, world"})
suite.NoErrorf(err, "error executing result : %v", err)
}
func TestNeo4JIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(Neo4JIntegrationTestSuite))
}
type person struct {
Name string
Age int64
}
func (p *person) GetLabel() string {
return "person"
}
func (p *person) GetType() omg.GraphObjectType {
return omg.Vertex
}
type city struct {
Name string
PinCode int64
}
// GetLabel returns the label associated with the graph object
func (c *city) GetLabel() string {
return "City"
}
// GraphType() returns the type associated with the Graph object
func (c *city) GetType() omg.GraphObjectType {
return omg.Vertex
}
type livesin struct {
Since int64
Area string
}
// GetLabel returns the label associated with the graph object
func (lv *livesin) GetLabel() string {
return "LIVES_IN"
}
// GraphType() returns the type associated with the Graph object
func (lv *livesin) GetType() omg.GraphObjectType {
return omg.Edge
}