From f2af2384ed66b1d7fcdd923ae7f2ae56aee8e217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Fri, 22 Sep 2017 15:02:12 +0200 Subject: [PATCH] iter: ErrNotFound bug fix --- integration_test.go | 20 ++++++++++++++++++++ iterx.go | 16 +++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/integration_test.go b/integration_test.go index 866d726..79a256e 100644 --- a/integration_test.go +++ b/integration_test.go @@ -288,6 +288,16 @@ func TestNotFound(t *testing.T) { Testtext string } + t.Run("get cql error", func(t *testing.T) { + var v NotFoundTable + i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table WRONG`)) + + err := i.Get(&v) + if err == nil || !strings.Contains(err.Error(), "WRONG") { + t.Fatal(err) + } + }) + t.Run("get", func(t *testing.T) { var v NotFoundTable i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table`)) @@ -296,6 +306,16 @@ func TestNotFound(t *testing.T) { } }) + t.Run("select cql error", func(t *testing.T) { + var v []NotFoundTable + i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table WRONG`)) + + err := i.Select(&v) + if err == nil || !strings.Contains(err.Error(), "WRONG") { + t.Fatal(err) + } + }) + t.Run("select", func(t *testing.T) { var v []NotFoundTable i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table`)) diff --git a/iterx.go b/iterx.go index 43eb42b..d19c2f8 100644 --- a/iterx.go +++ b/iterx.go @@ -81,7 +81,7 @@ func (iter *Iterx) scanAny(dest interface{}, structOnly bool) bool { return false } if iter.Iter.NumRows() == 0 { - iter.err = gocql.ErrNotFound + // no results or query error return false } @@ -135,7 +135,7 @@ func (iter *Iterx) scanAll(dest interface{}, structOnly bool) bool { return false } if iter.Iter.NumRows() == 0 { - iter.err = gocql.ErrNotFound + // no results or query error return false } @@ -220,7 +220,7 @@ func (iter *Iterx) StructScan(dest interface{}) bool { } if iter.Iter.NumRows() == 0 { - iter.err = gocql.ErrNotFound + // no results or query error return false } @@ -261,9 +261,15 @@ func columnNames(ci []gocql.ColumnInfo) []string { // the query or the iteration. func (iter *Iterx) Close() error { err := iter.Iter.Close() - if err != nil && iter.err == nil { - iter.err = err + + if iter.err == nil { + if err != nil { + iter.err = err + } else if iter.Iter.NumRows() == 0 { + iter.err = gocql.ErrNotFound + } } + return iter.err }