Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Oct 18, 2023
1 parent 0118741 commit 005f853
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ func TestExpectQueryRewriterFail(t *testing.T) {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}

mock.ExpectExec(`INSERT INTO users\(username\) VALUES \(\@user\)`).
mock.ExpectQuery(`INSERT INTO users\(username\) VALUES \(\@user\)`).
WithRewrittenSQL(`INSERT INTO users\(username\) VALUES \(\$1\)`).
WithArgs(failQryRW{})
_, err = mock.Exec(context.Background(), "INSERT INTO users(username) VALUES (@user)", "baz")
_, err = mock.Query(context.Background(), "INSERT INTO users(username) VALUES (@user)", "baz")
assert.Error(t, err)
}

Expand Down
3 changes: 2 additions & 1 deletion expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare {
}

// WillBeClosed is for backward compatibility only and will be removed soon.
// One should use WillBeDeallocated() instead
//
// Deprecated: One should use WillBeDeallocated() instead.
func (e *ExpectedPrepare) WillBeClosed() *ExpectedPrepare {
return e.WillBeDeallocated()
}
Expand Down
19 changes: 19 additions & 0 deletions expectations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,22 @@ func TestMissingWithArgs(t *testing.T) {
t.Error("expectation was not matched error was expected")
}
}

func TestWithRewrittenSQL(t *testing.T) {
t.Parallel()
mock, err := NewConn()
a := assert.New(t)
a.NoError(err)

mock.ExpectQuery(`INSERT INTO users\(username\) VALUES \(\@user\)`).
WithArgs(pgx.NamedArgs{"user": "John"}).
WithRewrittenSQL(`INSERT INTO users\(username\) VALUES \(\$1\)`).
WillReturnRows()

_, err = mock.Query(context.Background(),
"INSERT INTO users(username) VALUES (@user)",
pgx.NamedArgs{"user": "John"},
)
a.NoError(err)
a.NoError(mock.ExpectationsWereMet())
}
2 changes: 1 addition & 1 deletion pgxmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ func TestPreparedStatementCloseExpectation(t *testing.T) {
mock, _ := NewConn()
a := assert.New(t)

ep := mock.ExpectPrepare("foo", "INSERT INTO ORDERS").WillBeDeallocated()
ep := mock.ExpectPrepare("foo", "INSERT INTO ORDERS").WillBeClosed()
ep.ExpectExec().WithArgs(AnyArg(), AnyArg()).WillReturnResult(NewResult("UPDATE", 1))

stmt, err := mock.Prepare(context.Background(), "foo", "INSERT INTO ORDERS(ID, STATUS) VALUES (?, ?)")
Expand Down

0 comments on commit 005f853

Please sign in to comment.