-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection_test.go
More file actions
executable file
·127 lines (106 loc) · 2.79 KB
/
Copy pathconnection_test.go
File metadata and controls
executable file
·127 lines (106 loc) · 2.79 KB
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
package gopherneo
import (
"encoding/json"
"fmt"
"testing"
)
func TestConnect(t *testing.T) {
c, err := NewConnectionWithToken("http://localhost:7474", "cee35b356a500f6bfd640146b4f3a771")
if err != nil {
t.Error(err)
}
errIfBlank(t, "Uri", c.Uri)
errIfBlank(t, "NodeURI", c.NodeURI)
errIfBlank(t, "NodeLabelsURI", c.NodeLabelsURI)
errIfBlank(t, "CypherURI", c.CypherURI)
errIfBlank(t, "TransactionURI", c.TransactionURI)
}
func TestQueryWithProps(t *testing.T) {
db, err := NewConnectionWithToken("http://localhost:7474", "cee35b356a500f6bfd640146b4f3a771")
if err != nil {
t.Error(err)
}
// construct query which creates a thing and returns the node
type Thing struct {
Name string `json:"name"`
Age int `json:"age,int"`
}
// prepare the cypher statement
cypher1 := `
CREATE (t:Thing { myprops })
RETURN t`
// prepare the cypher props for "myprops"
nameVal := "4379473927489327424343"
ageVal := 46
props := &Thing{Name: nameVal, Age: ageVal}
// add my cypher props to a map[string]interface{}
params := &map[string]interface{}{
"myprops": props,
}
cr, err := db.ExecuteCypher(cypher1, params)
if err != nil {
t.Error(err)
}
if len(cr.Rows) != 1 {
t.Errorf("returned rows not 1, query was: %v", cypher1)
}
newThing := &Thing{}
err = json.Unmarshal(*cr.Rows[0][0], &newThing)
if err != nil {
t.Error(err)
}
if newThing.Name != nameVal {
t.Errorf("name incorrect, should be '%v', is '%v'", nameVal, newThing.Name)
}
if newThing.Age != ageVal {
t.Errorf("age incorrect, should be '%v', is '%v'", ageVal, newThing.Age)
}
db.DeleteNodes("Thing", "", "")
}
func TestReturnMultiNodes(t *testing.T) {
db, err := NewConnectionWithToken("http://localhost:7474", "cee35b356a500f6bfd640146b4f3a771")
if err != nil {
t.Error(err)
}
name1 := "name1"
name2 := "name2"
cypher := fmt.Sprintf(`
CREATE (t1:Thing { name: '%v' })
CREATE (t2:Thing { name: '%v' })
RETURN t1, t2`, name1, name2)
params := &map[string]interface{}{}
cr, err := db.ExecuteCypher(cypher, params)
if err != nil {
t.Error(err)
}
if len(cr.Rows) != 1 {
t.Errorf("expected one row returned, query was: %v", cypher)
}
if len(cr.Rows[0]) != 2 {
t.Errorf("expected two nodes returned in first row, query was: %v", cypher)
}
t1 := &Thing{}
err = json.Unmarshal(*cr.Rows[0][0], &t1)
if err != nil {
t.Error(err)
}
t2 := &Thing{}
err = json.Unmarshal(*cr.Rows[0][1], &t2)
if err != nil {
t.Error(err)
}
if t1.Name != name1 {
t.Errorf("name incorrect, should be '%v', is '%v'", name1, t1.Name)
}
if t2.Name != name2 {
t.Errorf("name incorrect, should be '%v', is '%v'", name2, t2.Name)
}
db.DeleteNodes("Thing", "", "")
}
// helpers
func errIfBlank(t *testing.T, key string, val string) {
if len(val) == 0 {
t.Error(fmt.Errorf("'%v' was blank", key))
}
return
}