I'm trying to do a simple unit test using go-sqlmock to do a select and return a mapped ID. Code snippet below
s.sqlmock.ExpectBegin()
s.sqlmock.
ExpectQuery("select `id` from `project` where `id` = \\? and `archived` = \\1").
WithArgs(2).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(strconv.Itoa(1)))
s.sqlmock.ExpectCommit()
The snippet of the implementation I want to test on is:
...
type Project struct{ Id int64 }
var project Project
tx.Raw("select id from project where id = ? and archived = 1", values["projectId"]).Scan(&project)
...
But the following error occurs:
I've tried some examples but without success. I thank the help of all you
UPDATE
I tried to remove s.sqlmock.ExpectBegin() and s.sqlmock.ExpectCommit()
of code and change a query as below:
s.sqlmock.
ExpectQuery("select id from project where id = ? and archived = 1").
WithArgs(2).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(strconv.Itoa(1)))
But the following error occurs:
Query: could not match actual sql: "select id from project where id = ? and archived = 1" with expected regexp "select id from project where id = ? and archived = 1"