From 7cefb81451dc1d2d10e62503852cb95761f05741 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Sun, 20 Dec 2020 11:11:05 +0100 Subject: [PATCH] Allow shared vars to be assigned and checked within same result table (#4) --- Database.feature | 8 ++++---- go.mod | 2 +- go.sum | 4 ++-- manager.go | 48 ++++++++++++++++++++++++++++++++++++------------ manager_test.go | 4 ++-- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Database.feature b/Database.feature index 2bc75e7..cb45d7c 100644 --- a/Database.feature +++ b/Database.feature @@ -11,14 +11,14 @@ Feature: Database Query Then only these rows are available in table "my_table" of database "my_db": | id | foo | bar | created_at | deleted_at | - | $id1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL | - | $id2 | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z | + | $id1 | $foo1 | abc | 2021-01-01T00:00:00Z | NULL | + | $id2 | $foo1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z | | $id3 | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z | Then only these rows are available in table "my_table" of database "my_db": | id | foo | bar | created_at | deleted_at | - | $id1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL | - | $id2 | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z | + | $id1 | $foo1 | abc | 2021-01-01T00:00:00Z | NULL | + | $id2 | $foo1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z | | $id3 | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z | And no rows are available in table "my_another_table" of database "my_db" diff --git a/go.mod b/go.mod index e66e79d..8da3d68 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/Masterminds/squirrel v1.5.0 github.com/bool64/dev v0.1.10 - github.com/bool64/shared v0.1.0 + github.com/bool64/shared v0.1.1 github.com/bool64/sqluct v0.1.1 github.com/cucumber/godog v0.10.0 github.com/cucumber/messages-go/v10 v10.0.3 diff --git a/go.sum b/go.sum index 092d0e1..79694e7 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/bool64/ctxd v0.1.3/go.mod h1:rhUkoNE4mKFSJmo9l+78u2j+FVQifRCj0MHRhyZ2 github.com/bool64/dev v0.1.0/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg= github.com/bool64/dev v0.1.10 h1:4L6eLD+qo1QgWDy+Y7OhJxi/gLwOAuV1rd07noMc3dU= github.com/bool64/dev v0.1.10/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg= -github.com/bool64/shared v0.1.0 h1:yE+PJTOjlxOt/wz37HjFiH75Qr3EcpmY1glhUMv3p7k= -github.com/bool64/shared v0.1.0/go.mod h1:khzw051JtnOZH6lX21rQ+sqbeC0FFEcU1wfchRS7EyU= +github.com/bool64/shared v0.1.1 h1:oeJC8LWpaCO9K3dvvVpyoxs9YXkOiGF8onBKUSNdJ2I= +github.com/bool64/shared v0.1.1/go.mod h1:khzw051JtnOZH6lX21rQ+sqbeC0FFEcU1wfchRS7EyU= github.com/bool64/sqluct v0.1.1 h1:u54NdgKNHLzZS8YOGeqZuVZ6BD5XhgVchC1w5KVw/+o= github.com/bool64/sqluct v0.1.1/go.mod h1:Ogo0FvuPkuFMAJcxsxAlOyDTgs5s3SeEH0DOXRP8/Rk= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= diff --git a/manager.go b/manager.go index cb6edfd..a069aa8 100644 --- a/manager.go +++ b/manager.go @@ -475,6 +475,34 @@ func (t *tableQuery) skipDecode(column, value string) bool { return false } +func (t *tableQuery) makeReplaces(onSetErr *error) (map[string]string, error) { + replaces := make(map[string]string) + + if vars := t.vars.GetAll(); len(vars) > 0 { + replaces = make(map[string]string, len(vars)) + + for k, v := range vars { + s, err := t.mapper.Encode(v) + if err != nil { + return nil, err + } + + replaces[k] = s + } + } + + t.vars.OnSet(func(key string, val interface{}) { + s, err := t.mapper.Encode(val) + if err != nil { + *onSetErr = err + } + + replaces[key] = s + }) + + return replaces, nil +} + func (m *Manager) assertRows(tableName, dbName string, data *godog.Table, exhaustiveList bool) (err error) { t, err := m.makeTableQuery(tableName, dbName, data) if err != nil { @@ -499,19 +527,11 @@ func (m *Manager) assertRows(tableName, dbName string, data *godog.Table, exhaus return nil } - var replaces map[string]string - - if vars := t.vars.GetAll(); len(vars) > 0 { - replaces = make(map[string]string, len(vars)) - - for k, v := range vars { - s, err := m.TableMapper.Encode(v) - if err != nil { - return err - } + var onSetErr error - replaces[k] = s - } + replaces, err := t.makeReplaces(&onSetErr) + if err != nil { + return err } // Iterating rows. @@ -523,6 +543,10 @@ func (m *Manager) assertRows(tableName, dbName string, data *godog.Table, exhaus ReceiveRow: t.receiveRow, }) + if err == nil && onSetErr != nil { + err = onSetErr + } + return err } diff --git a/manager_test.go b/manager_test.go index 8d02424..404dedd 100644 --- a/manager_test.go +++ b/manager_test.go @@ -86,9 +86,9 @@ func TestManager_RegisterContext(t *testing.T) { // | 3 | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z | mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(3)) - mock.ExpectQuery(`SELECT .+ FROM my_table WHERE foo = \$1 AND bar = \$2 AND created_at = \$3 AND deleted_at IS NULL`). + mock.ExpectQuery(`SELECT .+ FROM my_table WHERE bar = \$1 AND created_at = \$2 AND deleted_at IS NULL`). WithArgs( - "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), + "abc", mustParseTime("2021-01-01T00:00:00Z"), ). WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}). AddRow(1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), nil))