Skip to content

Commit

Permalink
[Go] Make manual test-ready (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano-ottolenghi committed Nov 20, 2023
1 parent 8fd3956 commit 0c6a177
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 85 deletions.
44 changes: 33 additions & 11 deletions go-manual/modules/ROOT/pages/bookmarks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@ In this case, you have the guarantee that subsequent queries can read previous c

[source, go]
----
neo4j.ExecuteQuery("<QUERY 1>")
neo4j.ExecuteQuery(ctx, driver, "<QUERY 1>", nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
// subsequent ExecuteQuery calls will be causally chained
neo4j.ExecuteQuery("<QUERY 2>") // can read result of <QUERY 1>
neo4j.ExecuteQuery("<QUERY 3>") // can read result of <QUERY 2>
neo4j.ExecuteQuery(ctx, driver, "<QUERY 2>", nil, // can read result of <QUERY 1>
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
neo4j.ExecuteQuery(ctx, driver, "<QUERY 3>", nil, // can read result of <QUERY 2>
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
----

To disable bookmark management and causal consistency, use the configuration callback `neo4j.ExecuteQueryWithoutBookmarkManager()` in `ExecuteQuery()` calls.

[source, go]
----
neo4j.ExecuteQuery(
ctx, driver, query, nil, neo4j.EagerResultTransformer,
ctx, driver, "<QUERY>", nil, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"),
neo4j.ExecuteQueryWithoutBookmarkManager())
----
Expand All @@ -43,9 +49,18 @@ Bookmark management happens automatically for queries run within a single sessio
----
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
defer session.Close(ctx)
session.ExecuteWrite(/* <QUERY 1> */)
session.ExecuteWrite(/* <QUERY 2> */) // can read QUERY 1
session.ExecuteWrite(/* <QUERY 3> */) // can read QUERY 1 and 2
session.ExecuteWrite(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "<QUERY 1>", nil)
})
session.ExecuteWrite(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "<QUERY 2>", nil) // can read QUERY 1
})
session.ExecuteWrite(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "<QUERY 3>", nil) // can read QUERY 1 and 2
})
----


Expand All @@ -71,7 +86,7 @@ func main() {
ctx := context.Background()
// Connection to database
dbUri := "<URI to Neo4j database>"
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, err := neo4j.NewDriverWithContext(
Expand Down Expand Up @@ -194,20 +209,27 @@ Since that is the default bookmark manager for `ExecuteQuery()` calls, this will

[source, go]
----
neo4j.ExecuteQuery(/* <QUERY 1> */)
neo4j.ExecuteQuery(ctx, driver, "<QUERY 1>", nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
session := driver.NewSession(ctx, neo4j.SessionConfig{
DatabaseName: "neo4j",
BookmarkManager: driver.ExecuteQueryBookmarkManager(),
})
// every query inside this session will be causally chained
// (i.e., can read what was written by <QUERY 1>)
session.ExecuteWrite(/* <QUERY 2> */)
session.ExecuteWrite(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "<QUERY 2>", nil)
})
session.Close(ctx)
// subsequent ExecuteQuery calls will be causally chained
// (i.e., can read what was written by <QUERY 2>)
neo4j.ExecuteQuery(/* <QUERY 3>*/)
neo4j.ExecuteQuery(ctx, driver, "<QUERY 3>", nil,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
----


Expand Down
17 changes: 6 additions & 11 deletions go-manual/modules/ROOT/pages/connect-advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The basic authentication scheme can also be used to authenticate against an LDAP
The Kerberos authentication scheme requires a base64-encoded ticket.
It can only be used if the server has the link:{neo4j-docs-base-uri}/kerberos-add-on/current/deployment/[Kerberos Add-on installed].

[source, go]
[source, go, test-skip]
----
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.KerberosAuth(ticket))
----
Expand All @@ -83,7 +83,7 @@ driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.KerberosAuth(ticket))

The bearer authentication scheme requires a base64-encoded token provided by an Identity Provider through Neo4j's link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/sso-integration[Single Sign-On feature].

[source, go]
[source, go, test-skip]
----
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.BearerAuth(token))
----
Expand All @@ -100,7 +100,7 @@ Use the function link:https://pkg.go.dev/github.com/neo4j/neo4j-go-driver/v5/neo

Use the function link:https://pkg.go.dev/github.com/neo4j/neo4j-go-driver/v5/neo4j#NoAuth[`NoAuth`] to access a server where authentication is disabled.

[source, go]
[source, go, test-skip]
----
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.NoAuth())
----
Expand All @@ -116,20 +116,15 @@ In the following example, the connection to `example.com` on port `9999` is reso

[source, go]
----
import (
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
)
// import "github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
driver, err := neo4j.NewDriverWithContext(
"neo4j://example.com:9999", neo4j.BasicAuth(dbUser, dbPassword, ""),
func(conf *config.Config) {
conf.AddressResolver = func(address config.ServerAddress) []config.ServerAddress {
return []config.ServerAddress{
neo4j.NewServerAddress("localhost", "7687")
}
return []config.ServerAddress{ neo4j.NewServerAddress("localhost", "7687") }
}
})
})
defer driver.Close(ctx)
----

Expand Down
6 changes: 3 additions & 3 deletions go-manual/modules/ROOT/pages/connect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
func main() {
ctx := context.Background() // <2>
dbUri := "neo4j://localhost"
dbUser := "neo4j"
dbPassword := "verysecret"
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, err := neo4j.NewDriverWithContext( // <1>
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
Expand Down
35 changes: 19 additions & 16 deletions go-manual/modules/ROOT/pages/data-types.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func main() {
ctx := context.Background()
// Connection to database
dbUri := "neo4j://localhost"
dbUser := "neo4j"
dbPassword := "verysecret"
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, _ := neo4j.NewDriverWithContext(
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
Expand All @@ -88,6 +88,7 @@ func main() {
date, _ := result.Records[0].Get("date")
fmt.Println(reflect.TypeOf(date)) // time.Time
fmt.Println(date) // 2006-12-16 13:59:59.999999999 +0200 EET
}
----

.Using driver's temporal types
Expand All @@ -107,9 +108,9 @@ func main() {
ctx := context.Background()
// Connection to database
dbUri := "neo4j://localhost"
dbUser := "neo4j"
dbPassword := "verysecret"
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, _ := neo4j.NewDriverWithContext(
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
Expand All @@ -124,16 +125,16 @@ func main() {
`, map[string]any{
"name": "Alice",
"friend": "Sofia",
"friendsSince": friendsSince,
}, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
if err != nil {
panic(err)
}
time, _ := result.Records[0].Get("time")
fmt.Println(reflect.TypeOf(time)) // time.Time
castDate, _ := date.(neo4j.Time) // cast from `any` to `neo4j.Time`
castDate, _ := time.(neo4j.Time) // cast from `any` to `neo4j.Time`
fmt.Println(castDate.Time()) // -0001-11-30 12:18:08.973 +0000 Offset
}
----


Expand All @@ -149,7 +150,7 @@ duration := neo4j.Duration{
Seconds: 3,
Nanos: 4,
}
fmt.Println(duration) # 'P1Y2DT3.000000004S'
fmt.Println(duration) // 'P1Y2DT3.000000004S'
----

For full documentation, see link:https://pkg.go.dev/github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype#Duration[API documentation -- Duration].
Expand All @@ -165,10 +166,10 @@ You can think of it as a unique identifier for each spatial type.
[options="header"]
|===
| Cypher Type | Go Type | SpatialRefId
| `POINT` (2D Cartesian) | `dbtype.Point2D` | 7203
| `POINT` (2D WGS-84) | `dbtype.Point2D` | 9157
| `POINT` (3D Cartesian) | `dbtype.Point3D` | 4326
| `POINT` (3D WGS-84) | `dbtype.Point2D` | 4979
| `POINT` (2D Cartesian) | `neo4j.Point2D` | 7203
| `POINT` (2D WGS-84) | `neo4j.Point2D` | 9157
| `POINT` (3D Cartesian) | `neo4j.Point3D` | 4326
| `POINT` (3D WGS-84) | `neo4j.Point2D` | 4979
|===

[NOTE]
Expand Down Expand Up @@ -209,7 +210,7 @@ The type `Point3D` can be used to represent either a 3D Cartesian point or a 3D
[source, go]
----
// A 3D Cartesian Point
cartesian3d := dbtype.Point3D{
cartesian3d := neo4j.Point3D{
X: 1.23,
Y: 4.56,
Z: 7.89,
Expand All @@ -219,7 +220,7 @@ fmt.Println(cartesian3d)
// Point{srId=9157, x=1.230000, y=4.560000, z=7.890000}
// A 3D WGS84 Point
wgs843d := dbtype.Point3D{
wgs843d := neo4j.Point3D{
X: 1.23,
Y: 4.56,
Z: 7.89,
Expand Down Expand Up @@ -317,6 +318,8 @@ Represents a path in a graph.
.Example of path creation, retrieval, and processing
[source, go]
----
package main
import (
"fmt"
"context"
Expand All @@ -327,7 +330,7 @@ func main() {
ctx := context.Background()
// Connection to database
dbUri := "<URI to Neo4j database>"
dbUri := "<URI for Neo4j database>"
dbUser := "<Username>"
dbPassword := "<Password>"
driver, _ := neo4j.NewDriverWithContext(
Expand Down
16 changes: 9 additions & 7 deletions go-manual/modules/ROOT/pages/performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver, "<QUERY>", nil,
[source, go]
----
session := driver.NewSession(ctx, neo4j.SessionConfig{
DatabaseName: "neo4j"
DatabaseName: "neo4j",
})
----

Expand All @@ -34,7 +34,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver, "<QUERY>", nil,

[source, go]
----
session := driver.NewSession(ctx)
session := driver.NewSession(ctx, neo4j.SessionConfig{})
----


Expand Down Expand Up @@ -114,7 +114,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver,
[source, go]
----
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
session.Run(ctx,
result, err := session.Run(ctx,
"MATCH (p:Person|Animal {name: $name}) RETURN p",
map[string]any{
"name": "Alice",
Expand All @@ -137,7 +137,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver,
[source, go]
----
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
session.Run(ctx,
result, err := session.Run(ctx,
"MATCH (p {name: $name}) RETURN p",
map[string]any{
"name": "Alice",
Expand All @@ -157,7 +157,7 @@ session.Run(ctx,
----
numbers := make([]int, 10000)
for i := range numbers { numbers[i] = i }
result, err := neo4j.ExecuteQuery(ctx, driver, `
neo4j.ExecuteQuery(ctx, driver, `
WITH $numbers AS batch
UNWIND batch AS value
MERGE (n:Number)
Expand All @@ -174,7 +174,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver, `
[source, go]
----
for i := 0; i < 10000; i++ {
result, err := neo4j.ExecuteQuery(ctx, driver,
neo4j.ExecuteQuery(ctx, driver,
"MERGE (:Number {value: $value})",
map[string]any{
"value": i,
Expand Down Expand Up @@ -209,6 +209,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver,

[source, go]
----
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
result, err := session.ExecuteRead(ctx,
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "MATCH (p:Person) RETURN p", nil)
Expand All @@ -229,6 +230,7 @@ result, err := neo4j.ExecuteQuery(ctx, driver,

[source, go]
----
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
result, err := session.ExecuteWrite(ctx, // don't ask to write on a read-only operation
func(tx neo4j.ManagedTransaction) (any, error) {
return tx.Run(ctx, "MATCH (p:Person) RETURN p", nil)
Expand All @@ -246,7 +248,7 @@ For more information, see link:{neo4j-docs-base-uri}/cypher-manual/current/index
[source, go]
----
// Create an index on Person.name
result, err := neo4j.ExecuteQuery(ctx, driver,
neo4j.ExecuteQuery(ctx, driver,
"CREATE INDEX personName FOR (n:Person) ON (n.name)",
nil, neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase("neo4j"))
Expand Down
13 changes: 5 additions & 8 deletions go-manual/modules/ROOT/pages/query-advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
defer session.Close(ctx)
result, err := session.Run(
ctx,
"CREATE (:Person {name: $name})",
"CREATE (p:Person {name: $name}) RETURN p",
map[string]any{
"name": name
"name": "Lucia",
})
----

Expand Down Expand Up @@ -51,7 +51,7 @@ result, err := session.Run(
`, nil)
summary, _ := result.Consume(ctx)
fmt.Println("Query updated the database?",
result.Summary.Counters().ContainsUpdates())
summary.Counters().ContainsUpdates())
----

[NOTE]
Expand Down Expand Up @@ -97,7 +97,7 @@ To link:https://neo4j.com/developer/kb/protecting-against-cypher-injection/[prot
----
dangerousLabel := "Person\\u0060n"
// convert \u0060 to literal backtick and then escape backticks
//remember to import `strings`
// remember to import `strings`
escapedLabel := strings.ReplaceAll(dangerousLabel, "\\u0060", "`")
escapedLabel = strings.ReplaceAll(escapedLabel, "`", "``")
Expand Down Expand Up @@ -141,10 +141,7 @@ To enable driver logging, use the `Config.Log` option when instantiating the dri

[source, go]
----
import (
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
)
// import "github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
driver, err := neo4j.NewDriverWithContext(
dbUri,
Expand Down
Loading

0 comments on commit 0c6a177

Please sign in to comment.