Skip to content

Commit

Permalink
Allow shared vars to be assigned and checked within same result table (
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Dec 20, 2020
1 parent b2e101e commit 7cefb81
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Database.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
48 changes: 36 additions & 12 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 7cefb81

Please sign in to comment.