Skip to content

Commit

Permalink
Merge pull request #62 from RedisGraph/multi-label
Browse files Browse the repository at this point in the history
multi label api change
  • Loading branch information
swilly22 committed May 30, 2022
2 parents f86cee7 + 69b85d1 commit 3b0ad09
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 70 deletions.
88 changes: 57 additions & 31 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func createGraph() {
graph = GraphNew("social", conn)

// Create 2 nodes connect via a single edge.
japan := NodeNew("Country", "j", nil)
john := NodeNew("Person", "p", nil)
japan := NodeNew([]string{"Country"}, "j", nil)
john := NodeNew([]string{"Person"}, "p", nil)
edge := EdgeNew("Visited", john, japan, nil)

// Set node properties.
Expand Down Expand Up @@ -91,9 +91,9 @@ func checkQueryResults(t *testing.T, res *QueryResult) {
d, ok := r.GetByIndex(2).(*Node)
assert.True(t, ok, "Third column should contain nodes.")

assert.Equal(t, s.Label, "Person", "Node should be of type 'Person'")
assert.Equal(t, s.Labels[0], "Person", "Node should be of type 'Person'")
assert.Equal(t, e.Relation, "Visited", "Edge should be of relation type 'Visited'")
assert.Equal(t, d.Label, "Country", "Node should be of type 'Country'")
assert.Equal(t, d.Labels[0], "Country", "Node should be of type 'Country'")

assert.Equal(t, len(s.Properties), 4, "Person node should have 4 properties")

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestCreateQuery(t *testing.T) {
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*Node)
assert.Equal(t, w.Label, "WorkPlace", "Unexpected node label.")
assert.Equal(t, w.Labels[0], "WorkPlace", "Unexpected node label.")
}

func TestCreateROQueryFailure(t *testing.T) {
Expand Down Expand Up @@ -199,8 +199,8 @@ func TestArray(t *testing.T) {
t.Error(err)
}

a := NodeNew("person", "", nil)
b := NodeNew("person", "", nil)
a := NodeNew([]string{"person"}, "", nil)
b := NodeNew([]string{"person"}, "", nil)

a.SetProperty("name", "a")
a.SetProperty("age", 32)
Expand Down Expand Up @@ -288,9 +288,9 @@ func TestPath(t *testing.T) {
e := p.GetEdge(0)
d := p.LastNode()

assert.Equal(t, s.Label, "Person", "Node should be of type 'Person'")
assert.Equal(t, s.Labels[0], "Person", "Node should be of type 'Person'")
assert.Equal(t, e.Relation, "Visited", "Edge should be of relation type 'Visited'")
assert.Equal(t, d.Label, "Country", "Node should be of type 'Country'")
assert.Equal(t, d.Labels[0], "Country", "Node should be of type 'Country'")

assert.Equal(t, len(s.Properties), 4, "Person node should have 4 properties")

Expand All @@ -308,12 +308,12 @@ func TestPath(t *testing.T) {

func TestParameterizedQuery(t *testing.T) {
createGraph()
params := []interface{}{1, 2.3, "str", true, false, nil, []interface {}{0, 1, 2}, []interface {}{"0", "1", "2"}}
params := []interface{}{1, 2.3, "str", true, false, nil, []interface{}{0, 1, 2}, []interface{}{"0", "1", "2"}}
q := "RETURN $param"
params_map := make(map[string]interface{})
for index, param := range params {
params_map["param"] = param
res, err := graph.ParameterizedQuery(q, params_map);
res, err := graph.ParameterizedQuery(q, params_map)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -348,24 +348,24 @@ func TestCreateIndex(t *testing.T) {
func TestQueryStatistics(t *testing.T) {
graph.Flush()
err := graph.Delete()
assert.Nil(t,err)
assert.Nil(t, err)

q := "CREATE (:Person{name:'a',age:32,array:[0,1,2]})"
res, err := graph.Query(q)
assert.Nil(t,err)
assert.Nil(t, err)

assert.Equal(t, 1, res.NodesCreated(), "Expecting 1 node created")
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
assert.Greater(t, res.InternalExecutionTime(),0.0, "Expecting internal execution time not to be 0.0")
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")

res,err = graph.Query("MATCH (n) DELETE n")
assert.Nil(t,err)
res, err = graph.Query("MATCH (n) DELETE n")
assert.Nil(t, err)
assert.Equal(t, 1, res.NodesDeleted(), "Expecting 1 nodes deleted")

// Create 2 nodes connect via a single edge.
japan := NodeNew("Country", "j", nil)
john := NodeNew("Person", "p", nil)
japan := NodeNew([]string{"Country"}, "j", nil)
john := NodeNew([]string{"Person"}, "p", nil)
edge := EdgeNew("Visited", john, japan, nil)

// Set node properties.
Expand All @@ -386,21 +386,21 @@ func TestQueryStatistics(t *testing.T) {

// Flush graph to DB.
res, err = graph.Commit()
assert.Nil(t,err)
assert.Nil(t, err)
assert.Equal(t, 2, res.NodesCreated(), "Expecting 2 node created")
assert.Equal(t, 0, res.NodesDeleted(), "Expecting 0 nodes deleted")
assert.Equal(t, 7, res.PropertiesSet(), "Expecting 7 properties set")
assert.Equal(t, 1, res.RelationshipsCreated(), "Expecting 1 relationships created")
assert.Equal(t, 0, res.RelationshipsDeleted(), "Expecting 0 relationships deleted")
assert.Greater(t, res.InternalExecutionTime(),0.0, "Expecting internal execution time not to be 0.0")
assert.Greater(t, res.InternalExecutionTime(), 0.0, "Expecting internal execution time not to be 0.0")
assert.Equal(t, true, res.Empty(), "Expecting empty resultset")
q = "MATCH p = (:Person)-[:Visited]->(:Country) RETURN p"
res, err = graph.Query(q)
assert.Nil(t,err)
assert.Nil(t, err)
assert.Equal(t, len(res.results), 1, "expecting 1 result record")
assert.Equal(t, false, res.Empty(), "Expecting resultset to have records")
res,err = graph.Query("MATCH ()-[r]-() DELETE r")
assert.Nil(t,err)
res, err = graph.Query("MATCH ()-[r]-() DELETE r")
assert.Nil(t, err)
assert.Equal(t, 1, res.RelationshipsDeleted(), "Expecting 1 relationships deleted")
}

Expand All @@ -410,39 +410,65 @@ func TestUtils(t *testing.T) {

res = ToString("test_string")
assert.Equal(t, res, "\"test_string\"")

res = ToString(10)
assert.Equal(t, res, "10")
assert.Equal(t, res, "10")

res = ToString(1.2)
assert.Equal(t, res, "1.2")

res = ToString(true)
assert.Equal(t, res, "true")

var arr = []interface{}{1,2,3,"boom"}
var arr = []interface{}{1, 2, 3, "boom"}
res = ToString(arr)
assert.Equal(t, res, "[1,2,3,\"boom\"]")

jsonMap := make(map[string]interface{})
jsonMap["object"] = map[string]interface{} {"foo": 1}
jsonMap["object"] = map[string]interface{}{"foo": 1}
res = ToString(jsonMap)
assert.Equal(t, res, "{object: {foo: 1}}")
}

func TestMultiLabelNode(t *testing.T) {
// clear database
graph.Flush()
err := graph.Delete()
assert.Nil(t, err)

// create a multi label node
multiLabelNode := NodeNew([]string{"A","B"}, "n", nil)
graph.AddNode(multiLabelNode)
_, err = graph.Commit()
assert.Nil(t, err)

// fetch node
res, err := graph.Query("MATCH (n) RETURN n")
assert.Nil(t, err)

res.Next()
r := res.Record()
n := r.GetByIndex(0).(*Node)

// expecting 2 labels
assert.Equal(t, len(n.Labels), 2, "expecting 2 labels")
assert.Equal(t, n.Labels[0], "A")
assert.Equal(t, n.Labels[1], "B")
}

func TestNodeMapDatatype(t *testing.T) {
graph.Flush()
err := graph.Delete()
assert.Nil(t, err)

// Create 2 nodes connect via a single edge.
japan := NodeNew("Country", "j",
japan := NodeNew([]string{"Country"}, "j",
map[string]interface{}{
"name": "Japan",
"population": 126800000,
"states": []string{"Kanto", "Chugoku"},
})
john := NodeNew("Person", "p",
john := NodeNew([]string{"Person"}, "p",
map[string]interface{}{
"name": "John Doe",
"age": 33,
Expand Down Expand Up @@ -488,7 +514,7 @@ func TestTimeout(t *testing.T) {

params := make(map[string]interface{})
params["ub"] = 1000000
res, err = graph.ParameterizedQueryWithOptions("UNWIND range(0, $ub) AS v RETURN v", params, options);
res, err = graph.ParameterizedQueryWithOptions("UNWIND range(0, $ub) AS v RETURN v", params, options)
assert.Nil(t, res)
assert.NotNil(t, err)
}
7 changes: 7 additions & 0 deletions edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Edge struct {
graph *Graph
}

// EdgeNew create a new Edge
func EdgeNew(relation string, srcNode *Node, destNode *Node, properties map[string]interface{}) *Edge {
p := properties
if p == nil {
Expand All @@ -32,15 +33,18 @@ func EdgeNew(relation string, srcNode *Node, destNode *Node, properties map[stri
}
}

// SetProperty assign a new property to edge
func (e *Edge) SetProperty(key string, value interface{}) {
e.Properties[key] = value
}

// GetProperty retrieves property from edge
func (e *Edge) GetProperty(key string) interface{} {
v, _ := e.Properties[key]
return v
}

// SourceNodeID returns edge source node ID
func (e Edge) SourceNodeID() uint64 {
if e.Source != nil {
return e.Source.ID
Expand All @@ -49,6 +53,7 @@ func (e Edge) SourceNodeID() uint64 {
}
}

// DestNodeID returns edge destination node ID
func (e Edge) DestNodeID() uint64 {
if e.Source != nil {
return e.Destination.ID
Expand All @@ -57,6 +62,7 @@ func (e Edge) DestNodeID() uint64 {
}
}

// Returns a string representation of edge
func (e Edge) String() string {
if len(e.Properties) == 0 {
return "{}"
Expand All @@ -71,6 +77,7 @@ func (e Edge) String() string {
return s
}

// Encode makes Edge satisfy the Stringer interface
func (e Edge) Encode() string {
s := []string{"(", e.Source.Alias, ")"}

Expand Down
11 changes: 6 additions & 5 deletions example_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/RedisGraph/redisgraph-go"
"github.com/gomodule/redigo/redis"
"io/ioutil"
"log"
"os"

"github.com/RedisGraph/redisgraph-go"
"github.com/gomodule/redigo/redis"
)

func ExampleGraphNew() {
Expand All @@ -22,7 +23,7 @@ func ExampleGraphNew() {
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Label)
fmt.Println(w.Labels[0])
// Output: WorkPlace
}

Expand All @@ -40,7 +41,7 @@ func ExampleGraphNew_pool() {
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Label)
fmt.Println(w.Labels[0])
// Output: WorkPlace

}
Expand Down Expand Up @@ -103,7 +104,7 @@ func ExampleGraphNew_tls() {
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Label)
fmt.Println(w.Labels[0])
}

func getConnectionDetails() (host string, password string) {
Expand Down
7 changes: 4 additions & 3 deletions examples/redisgraph_tls_client/redisgraph_tls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"crypto/x509"
"flag"
"fmt"
"github.com/RedisGraph/redisgraph-go"
"github.com/gomodule/redigo/redis"
"io/ioutil"
"log"
"os"

"github.com/RedisGraph/redisgraph-go"
"github.com/gomodule/redigo/redis"
)

var (
Expand Down Expand Up @@ -85,6 +86,6 @@ func main() {
res.Next()
r := res.Record()
w := r.GetByIndex(0).(*redisgraph.Node)
fmt.Println(w.Label)
fmt.Println(w.Labels[0])
// Output: WorkPlace
}
8 changes: 7 additions & 1 deletion graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (g *Graph) ExecutionPlan(q string) (string, error) {
// Delete removes the graph.
func (g *Graph) Delete() error {
_, err := g.Conn.Do("GRAPH.DELETE", g.Id)

// clear internal mappings
g.labels = g.labels[:0]
g.properties = g.properties[:0]
g.relationshipTypes = g.relationshipTypes[:0]

return err
}

Expand Down Expand Up @@ -266,7 +272,7 @@ func (g *Graph) CallProcedure(procedure string, yield []string, args ...interfac
}
q += fmt.Sprintf("%s)", strings.Join(tmp, ","))

if yield != nil && len(yield) > 0 {
if len(yield) > 0 {
q += fmt.Sprintf(" YIELD %s", strings.Join(yield, ","))
}

Expand Down

0 comments on commit 3b0ad09

Please sign in to comment.