/
http_test.go
149 lines (116 loc) · 4.24 KB
/
http_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
package neo4j_test
import (
"testing"
"github.com/jakewins/neo4j"
)
// NOTE: These are integration tests. They will fail unless you have a neo
// instance running at localhost:7474.
func ExampleUsage() {
// You'll generally want a single driver instance per Neo4j database used
// by your application.
drive, _ := neo4j.NewDriver("http://localhost:7474")
// The driver allows you to create database Sessions
// Sessions can, if you want, be pooled and re-used, although we use TCP
// connection pooling by default even if you don't reuse sessions. That
// is not guaranteed in future versions though.
sess,_ := drive.NewSession()
// Sessions allow you to create transactions
tx,_ := sess.NewTransaction()
// Run a Cypher statement like this
res,_ := tx.Execute("CREATE (n) RETURN id(n)")
// Loop through the result like below. Next() forwards the result cursor,
// which starts at -1. It returns true if there was another row in the result.
var id int64
for res.Next() {
// Once we're on a row, we can read the columns in it
id = res.GetInt("id(n)")
}
// And it's considered good form to close the result when you're done.
res.Close()
// However, you will generally not want raw cypher statement, you'll want
// to use parameters. This allows re-use of query plans, and gets rid of
// issues of SQL-injection style vulnerabilities. {id} is the name of a
// parameter in thie example
res,_ = tx.ExecuteWithParams("START n=node({id}) RETURN id(n)", map[string]interface{}{ "id":id })
res.Close()
// Finally, we commit our changes (or tx.Rollback() if we're unhappy with them)
tx.Commit()
}
func TestBegin__Execute__Commit(t *testing.T) {
// Given
drive, _ := neo4j.NewDriver("http://localhost:7474")
sess,_ := drive.NewSession()
tx,_ := sess.NewTransaction()
res,_ := tx.Execute("CREATE (n) RETURN id(n)")
if res.Next() != true {
t.Error("Expected one result back.")
}
id := res.GetInt("id(n)")
if id < 0 {
t.Error("Expected an id >= 0 back from the server.")
}
if res.Next() != false {
t.Error("Expected only one result.")
}
// When
tx.Commit()
// Then
tx,_ = sess.NewTransaction()
res,_ = tx.ExecuteWithParams("START n=node({id}) RETURN id(n)", map[string]interface{}{ "id":id })
res.Next()
idAgain := res.GetInt("id(n)")
if id != idAgain {
t.Error("Expected the transaction to have commmitted.")
}
}
func TestBegin__Execute__Rollback(t *testing.T) {
// Given
drive, _ := neo4j.NewDriver("http://localhost:7474")
sess,_ := drive.NewSession()
tx,_ := sess.NewTransaction()
_, err := tx.Execute("CREATE (n:ShouldNotBeCommitted)")
if err != nil {
t.Error(err)
}
// When
tx.Rollback()
// Then
tx,_ = sess.NewTransaction()
res,_ := tx.Execute("MATCH (n:ShouldNotBeCommitted) RETURN n")
if res.Next() != false {
t.Error("Expected node to not get committed")
}
}
func TestCellTypes(t *testing.T) {
// Given
drive, _ := neo4j.NewDriver("http://localhost:7474")
sess,_ := drive.NewSession()
tx,_ := sess.NewTransaction()
// When
res, _ := tx.Execute("CREATE (n:ShouldNotBeCommitted) RETURN 1.2 as float, 1 as int, false as bool, \"hello\" as string, {k:1} as map, [1,2.2] as array")
res.Next()
// Then array should be correct (TODO: Is there something like hamcrest matchers for go? This array assertion is silly.)
if int(res.GetArray("array")[0].(float64)) != 1 {
t.Error("Expected entry 0 to be 1")
}
if res.GetArray("array")[1].(float64) != 2.2 {
t.Error("Expected entry 1 to be 2.2")
}
// Then map should be correct
if res.GetMap("map")["k"].(float64) != 1.0 {
t.Error("Expected map to be k->1")
}
// Then primitives should be correct
if res.GetFloat("float") != 1.2 {
t.Error("Expected float to be 1.2")
}
if res.GetInt("int") != 1 {
t.Error("Expected int to be 1")
}
if res.GetBool("bool") != false {
t.Error("Expected bool to be false")
}
if res.GetString("string") != "hello" {
t.Error("Expected string to be 'hello'")
}
}