sqlmock is a mock library implementing sql/driver. Which has one and only purpose - to simulate any sql driver behavior in tests, without needing a real database connection. It helps to maintain correct TDD workflow.
Questions tagged [go-sqlmock]
63 questions
0
votes
0 answers
how create sql.mock in golang for insert query
I want to make a unit test 100% coverage but many commands or checks are missed in the unit testing, how do I fix it so that it can be 100% coverage
func (db *Database) SaveBook(id string, url string, username string, email string, password string,…

ryandi pratama purba
- 384
- 1
- 7
0
votes
1 answer
Mocking a select with mapping in Go is returning expected transaction begin error
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").
…

João Manolo
- 235
- 1
- 4
- 12
0
votes
1 answer
DB Mocking in one Go test case is interfering with other test case
I have two Go test cases as shown below that test a gRPC function called MyEndpoint.
MyEndpoint is supposed to succeed when the database row it selects has Field1 == "A" and return an error otherwise.
I'm mocking out the database with the go-sqlmock…

Saqib Ali
- 11,931
- 41
- 133
- 272
0
votes
1 answer
Use the same DB transaction in different DAOs
I have a use case in one of my Golang applications to update multiple tables in an API and in case one of the updates fails, I would want all the previous updates to roll back (Something that @Transactional in Java does).
I tried doing it in the…

I_dont_know
- 341
- 1
- 4
- 17
0
votes
0 answers
Sqlmock + Gorm fails with Error "unsupported data type: &[]"
I try to test gorm things with sqlmock.
My model looks like:
type Worker struct {
Model
NetworkAddress string
Name string
Version string
Available bool
Services []Service…

Saxy
- 1
- 1
0
votes
0 answers
How do I mock with sqlmock error when test row.scan
I'm trying to mock the following function. I'm struggling to get it to test the rows.next() return.
func (u *userRepository) GetList(idClient int) ([]UserPublic, rest_errors.RestErr) {
stmt, err := u.client.Prepare(queryGetUserList)
if…

Antoni
- 75
- 10
0
votes
1 answer
Call to Query [...] was not expected
I want to test a DB query in go. For that I try to use the sqlmock library. Sadly I don't find a solution to this
My test is
t.Run("call database", func(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
…

Barsch
- 87
- 1
- 2
- 9
0
votes
1 answer
Testing SQL table-valued parameters in sqlmock
I have a function that is designed to insert a large number of elements into an MSSQL database using a table-valued parameter and a procedure.
func (requester *Requester) doQuery(ctx context.Context, dtos interface{}) error {
conn, err :=…

Woody1193
- 7,252
- 5
- 40
- 90
0
votes
1 answer
Go sqlmock SELECT missing expected query
Go here. Trying to figure out how to use SQL mock v2.
Here's my interface:
type OrderPersister interface {
FetchOrderById(string) (*Order, error)
}
And my implementation of that interface:
type DbPersister struct {
Config config.DbConfig
…

hotmeatballsoup
- 385
- 6
- 58
- 136
0
votes
1 answer
sqlmock: expectations do not match (being exactly the same queries)
I can't get the expetations to match. I copied and pasted the same query and it doesn't even have any characters that need to be escaped.
I have the following code:
query := "SELECT id, user_id, name, description, created_at, updated_at FROM…
0
votes
0 answers
SQL Next not advancing cursor
I have a function that I used to iterate over a result set from a query:
func readRows(rows *sql.Rows, translator func(*sql.Rows) error) error {
defer rows.Close()
// Iterate over each row in the rows and scan each; if an error occurs then…

Woody1193
- 7,252
- 5
- 40
- 90
0
votes
1 answer
How to mock db using sqlmock, the db connection obtained within the function
func loadDataFromDB() Data{
db, err := sql.Open("mysql","user:password@tcp(127.0.0.1:3306)/hello")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("select id, name from users where id = ?",…

宫恩来
- 1
0
votes
1 answer
Using same expected rows for multiple expected queries returns result just for the first one with sqlmock
I am writing tests with sqlmock in go. I have a list of rows (e.g. myRows) and two different SELECT statements which I want to use myRows as WillReturnRows argument for both of them:
myRows :=…

Zeinab Abbasimazar
- 9,835
- 23
- 82
- 131
0
votes
0 answers
How to mock db.QueryRow using sqlmock
I created mockrows for sql.rows. I referred this document to create mockrows https://github.com/DATA-DOG/go-sqlmock
I need to create mockrow for sql.row (i.e db.QueryRow). I am not able to find any suitable method in sql.mock.
Is there any way to…

Shoba
- 632
- 1
- 5
- 11
0
votes
2 answers
Call to Query with args [], was not expected in go-sqlmock for compound SQL queries
I was able to successfully mock the query to select from one table like so:
sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
WillReturnRows(myResultRows)
But I was not able to mock the following query that checks for…

dirtyqwerty
- 185
- 2
- 16