Skip to content

Commit

Permalink
Merge pull request #38 from mindstand/fix_#35
Browse files Browse the repository at this point in the history
Fix Issue #35
  • Loading branch information
nikitawootten authored Sep 8, 2020
2 parents 51e8a8d + d63f5dc commit 87580cd
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 26 deletions.
42 changes: 42 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,54 @@
package gogm

import (
uuid2 "github.com/google/uuid"

"testing"
"time"

"github.com/stretchr/testify/require"
)

// This test is to make sure retuning raw results from neo4j actually work. This
// proves that the bug causing empty interfaces to be returned has been fixed.
func TestRawQuery(t *testing.T) {
if testing.Short() {
t.Skip()
return
}

req := require.New(t)

conf := Config{
Username: "neo4j",
Password: "password",
Host: "0.0.0.0",
IsCluster: false,
Port: 7687,
PoolSize: 15,
IndexStrategy: IGNORE_INDEX,
}

req.Nil(Init(&conf, &a{}, &b{}, &c{}))

sess, err := NewSession(false)
req.Nil(err)

uuid := uuid2.New().String()

req.Nil(sess.Save(&a{
BaseNode: BaseNode{
UUID: uuid,
},
}))

raw, err := sess.QueryRaw("match (n) where n.uuid=$uuid return n", map[string]interface{}{
"uuid": uuid,
})
req.Nil(err)
req.NotEmpty(raw)
}

func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip()
Expand Down
30 changes: 7 additions & 23 deletions mocks/ISession.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package gogm

import "github.com/neo4j/neo4j-go-driver/neo4j"

// specifies how edges are loaded
type neoEdgeConfig struct {
Id int64
Expand All @@ -33,3 +35,68 @@ type neoEdgeConfig struct {

Type string
}

// NodeWrap wraps the neo4j node struct because it is private
type NodeWrap struct {
Id int64 `json:"id"`
Labels []string `json:"labels"`
Props map[string]interface{} `json:"props"`
}

func newNodeWrap(node neo4j.Node) *NodeWrap {
return &NodeWrap{
Id: node.Id(),
Labels: node.Labels(),
Props: node.Props(),
}
}

// PathWrap wraps the neo4j path struct because it is private
type PathWrap struct {
Nodes []*NodeWrap `json:"nodes"`
RelNodes []*RelationshipWrap `json:"rel_nodes"`
}

func newPathWrap(path neo4j.Path) *PathWrap {
pw := new(PathWrap)
nodes := path.Nodes()
if nodes != nil && len(nodes) != 0 {
nds := make([]*NodeWrap, len(nodes), cap(nodes))
for i, n := range nodes {
nds[i] = newNodeWrap(n)
}

pw.Nodes = nds
}

rels := path.Relationships()
if rels != nil && len(rels) != 0 {
newRels := make([]*RelationshipWrap, len(rels), cap(rels))
for i, rel := range rels {
newRels[i] = newRelationshipWrap(rel)
}

pw.RelNodes = newRels
}

return pw
}

// RelationshipWrap wraps the neo4j relationship struct because it is private
type RelationshipWrap struct {
Id int64 `json:"id"`
StartId int64 `json:"start_id"`
EndId int64 `json:"end_id"`
Type string `json:"type"`
Props map[string]interface{} `json:"props"`
}

func newRelationshipWrap(rel neo4j.Relationship) *RelationshipWrap {
return &RelationshipWrap{
Id: rel.Id(),
StartId: rel.StartId(),
EndId: rel.EndId(),
Type: rel.Type(),
Props: rel.Props(),
}
}
24 changes: 23 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,30 @@ func (s *Session) QueryRaw(query string, properties map[string]interface{}) ([][

var result [][]interface{}

// we have to wrap everything because the driver only exposes interfaces which are not serializable
for res.Next() {
result = append(result, res.Record().Values())
valLen := len(res.Record().Values())
valCap := cap(res.Record().Values())
if valLen != 0 {
vals := make([]interface{}, valLen, valCap)
for i, val := range res.Record().Values() {
switch val.(type) {
case neo4j.Path:
vals[i] = newPathWrap(val.(neo4j.Path))
break
case neo4j.Relationship:
vals[i] = newRelationshipWrap(val.(neo4j.Relationship))
break
case neo4j.Node:
vals[i] = newNodeWrap(val.(neo4j.Node))
break
default:
vals[i] = val
continue
}
}
result = append(result, vals)
}
}

return result, nil
Expand Down
3 changes: 1 addition & 2 deletions test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (t testPath) Relationships() []neo4j.Relationship {
}

type testRecord struct {

}

func (t *testRecord) Keys() []string {
Expand Down Expand Up @@ -141,7 +140,7 @@ func (t testRecord) GetByIndex(index int) interface{} {

type testResult struct {
empty bool
num int
num int
}

func (t *testResult) Keys() ([]string, error) {
Expand Down

0 comments on commit 87580cd

Please sign in to comment.