forked from jmcvetta/neoism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
173 lines (147 loc) · 4.42 KB
/
database_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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
/*******************************************************************************
The Neo4j Manual section numbers quoted in the test suite refer to the manual
for milestone release 1.8. http://docs.neo4j.org/chunked/1.8/
*******************************************************************************/
/*
To run these tests, a Neo4j 1.8 instance must be running on localhost on the
default port.
If the test suite complete successfully, all new nodes & relationships created
for testing will have been deleted in cleanup. However if the test suite fails
to complete due to a panic, the cleanup code may not get called. Therefore it
is not recommended to run this test suite on a db containing valuable data - run
it on a throwaway testing db instead! It's possible we could reduce this risk by
using defer() for cleanup.
To run these test for Google App Engine, run:
goapp test
*/
package neoism
import (
"log"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jmcvetta/randutil"
)
// neo4jUrl is global in order for TestConnect() to work when NEO4J_URL is set.
var neo4jUrl string
func connectTest(t *testing.T) *Database {
log.SetFlags(log.Ltime | log.Lshortfile)
neo4jUrl = os.Getenv("NEO4J_URL")
if neo4jUrl == "" {
// As of Neo4j v2.2.x, authentication is enabled by default.
neo4jUrl = "http://neo4j:foobar@localhost:7474/db/data/"
}
db, err := Connect(neo4jUrl)
// db.Session.Log = true
if err != nil {
t.Fatal(err)
}
return db
}
func cleanup(t *testing.T, db *Database) {
qs := []*CypherQuery{
&CypherQuery{
Statement: `START r=rel(*) DELETE r`,
},
&CypherQuery{
Statement: `START n=node(*) DELETE n`,
},
}
err := db.CypherBatch(qs)
if err != nil {
t.Fatal(err)
}
}
func rndStr(t *testing.T) string {
// Neo4j doesn't like object names beginning with numerals.
name, err := randutil.String(12, randutil.Alphabet)
if err != nil {
t.Fatal(err)
}
return name
}
func TestConnect(t *testing.T) {
db := connectTest(t)
assert.Equal(t, neo4jUrl, db.Url)
}
func TestConnectInvalidUrl(t *testing.T) {
//
// Missing protocol scheme - url.Parse should fail
//
_, err := dbConnect("://foobar.com")
if err == nil {
t.Fatal("Expected error due to missing protocol scheme")
}
//
// Unsupported protocol scheme - Session.Get should fail
//
_, err = dbConnect("foo://bar.com")
if err == nil {
t.Fatal("Expected error due to unsupported protocol scheme")
}
//
// Not Found
//
_, err = dbConnect(neo4jUrl + "foo/")
assert.Equal(t, InvalidDatabase, err)
}
func TestConnectIncompleteUrl(t *testing.T) {
// url now has the format hostname:port/db/data, delete everything after the port
regex := regexp.MustCompile(`^(https?:\/\/[^:]+:\d+)\/.*$`)
replaced := regex.ReplaceAllString(neo4jUrl, "$1")
//
// 200 Success and HTML returned
//
_, err := dbConnect(replaced)
if err != nil {
t.Fatal("Hardsetting path on incomplete url failed")
}
}
func TestPropertyKeys(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Prepare query for testing data creation
var queryString string
var createdPropertyKeys []string
for i := 0; i < 10; i++ {
propertyKeyNodeA := rndStr(t)
propertyKeyRel := rndStr(t)
propertyKeyNodeB := rndStr(t)
createdPropertyKeys = append(createdPropertyKeys, propertyKeyNodeA)
createdPropertyKeys = append(createdPropertyKeys, propertyKeyRel)
createdPropertyKeys = append(createdPropertyKeys, propertyKeyNodeB)
queryString += `
CREATE ({` + propertyKeyNodeA + `:""})-[:LINK {` + propertyKeyRel + `:""}]->({` + propertyKeyNodeB + `:"Bob"})
`
}
cq := CypherQuery{
Statement: queryString,
}
// Execute the test data creation query
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
// Get all live property keys on nodes and relationships
livePropertyKeys, err := PropertyKeys(db)
if err != nil {
t.Error(err)
}
// Check if the created property keys are among the extracted
for _, createdPropertyKey := range createdPropertyKeys {
keyExists := false
for _, livePropertyKey := range livePropertyKeys {
if createdPropertyKey == livePropertyKey {
keyExists = true
break
}
}
if !keyExists {
t.Error("Could not find the expected property key: " + createdPropertyKey)
}
}
}