From 671f07010822010fdabec6c9ab0eade451d429d4 Mon Sep 17 00:00:00 2001 From: Eric Solender Date: Fri, 31 Jul 2020 15:41:35 -0400 Subject: [PATCH] fixed decoder fail on no result --- decoder.go | 5 +++ decoder_test.go | 19 +++++++++- test_util.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) diff --git a/decoder.go b/decoder.go index b350512..47cc51b 100644 --- a/decoder.go +++ b/decoder.go @@ -30,10 +30,15 @@ import ( func decode(result neo4j.Result, respObj interface{}) (err error) { var rows [][]interface{} + numRows := 0 for result.Next() { rows = append(rows, result.Record().Values()) + numRows++ } + if numRows == 0 { + return ErrNotFound + } return innerDecode(rows, respObj) } diff --git a/decoder_test.go b/decoder_test.go index 8b21d92..fb2b200 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -208,7 +208,24 @@ type propsTest struct { Props map[string]interface{} `gogm:"name=props;properties"` } -func TestDecoder(t *testing.T) { +func TestDecode(t *testing.T) { + + req := require.New(t) + req.Nil(setupInit(true, nil, &a{}, &b{}, &c{}, &f{}, &propsTest{})) + + var fNode f + t1 := testResult{ + empty: true, + } + + req.True(errors.Is(decode(&t1, &fNode), ErrNotFound)) + + t1.empty = false + + req.Nil(decode(&t1, &fNode)) +} + +func TestInnerDecode(t *testing.T) { req := require.New(t) req.Nil(setupInit(true, nil, &a{}, &b{}, &c{}, &f{}, &propsTest{})) diff --git a/test_util.go b/test_util.go index b9033b2..e00579a 100644 --- a/test_util.go +++ b/test_util.go @@ -75,3 +75,101 @@ func (t testPath) Relationships() []neo4j.Relationship { } return nodes } + +type testRecord struct { + +} + +func (t *testRecord) Keys() []string { + panic("implement me") +} + +func (t *testRecord) Values() []interface{} { + return []interface{}{ + testPath{ + nodes: []*testNode{ + { + labels: []string{"f"}, + props: map[string]interface{}{ + "uuid": "0", + }, + id: 0, + }, + { + labels: []string{"f"}, + props: map[string]interface{}{ + "uuid": "1", + }, + id: 1, + }, + { + labels: []string{"f"}, + props: map[string]interface{}{ + "uuid": "2", + }, + id: 2, + }, + }, + relNodes: []*testRelationship{ + { + id: 3, + startId: 0, + endId: 1, + _type: "test", + props: nil, + }, + { + id: 4, + startId: 1, + endId: 2, + _type: "test", + props: nil, + }, + }, + }, + } + +} + +func (t testRecord) Get(key string) (interface{}, bool) { + panic("implement me") +} + +func (t testRecord) GetByIndex(index int) interface{} { + panic("implement me") +} + +type testResult struct { + empty bool + num int +} + +func (t *testResult) Keys() ([]string, error) { + panic("implement me") +} + +func (t *testResult) Next() bool { + toRet := !t.empty && t.num == 0 + + if !t.empty { + t.num++ + } + + return toRet +} + +func (t *testResult) Err() error { + panic("implement me") +} + +func (t *testResult) Record() neo4j.Record { + return &testRecord{} +} + +func (t *testResult) Summary() (neo4j.ResultSummary, error) { + panic("implement me") +} + +func (t *testResult) Consume() (neo4j.ResultSummary, error) { + panic("implement me") +}