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
23
votes
3 answers
sqlmock is not matching query, but query is identical and log output shows the same
I'm trying to write tests for some code using Gorm using sqlmock. I figured out writing tests for my insert function but now pulling my hair out trying to get an update working.
First piece of the workflow merely queries the record from the…
user11471017
13
votes
1 answer
How to set mock gin.Context for BindJSON
I'm setting up testing in Go.
I use go-sqlmock to test mysql connection and Go Gin as framework. Now I try to test mysql insert logic.
The problem is I need to set mock gin.Context which is used for BindJSON later.
But I can't set this gin.Context…

jpskgc.v5
- 249
- 1
- 5
- 9
6
votes
1 answer
How to mock gorm insert with go-sql (postgres)
I'm using Gorm with the postgresql driver. I try to mock a database insert with go-sqlmock:
type Test struct {
FirstName string `json:"first_name"`
}
func (db *DB) CreateTest() (*Test, error) {
test := Test{"c"}
if result :=…

user3255061
- 1,757
- 1
- 30
- 50
6
votes
2 answers
How to correctly set Mock Row and Query for go-sqlmock
I'm setting up testing in golang.
I use go-sqlmock to test mysql connection.
But sqlmock.NewRows and mock.ExpectQuery does not work well with error.
I want to know how to resolve this error.
server side: golang
db: mysql
web framework:…

jpskgc.v5
- 249
- 1
- 5
- 9
5
votes
0 answers
Unit testing sqlmock failing with sql update
We've been using sqlmock testing func() that are using select - no problems there.
It's not working with updates.
The func we want to test is:
func PutTag(tag *Tag) error {
...
err = db.DB.Where("id=?", tag.Id).Save(tag).Error
...
}
We've…

Maira Saliyeva
- 61
- 4
4
votes
1 answer
Go unit tests - call to database transaction Begin, was not expected error
I'm trying to write model unit tests in Go using Data Dog's go-sqlmock and testify.
I have the following code for that:
type Suite struct {
suite.Suite
DB *gorm.DB
mock sqlmock.Sqlmock
repository Repository
user…

Gambit2007
- 3,260
- 13
- 46
- 86
3
votes
1 answer
Issue using go-sqlmock and inserting arguments into mock queries
I am trying to mock my query functions using go-sqlmock & replicate the database table similarly. However, I am not getting the results that I expect. The query is not behaving as it should, arguments are not being inserted into the query & the…

Anthony Quinn
- 45
- 5
3
votes
0 answers
Using sqlmock to return nil for query execution in Golang
I am using sqlmock to mock a Snowflake query execution. This may look strange, but for the following query:
rows, err = db.Query(sqlQuery)
I need the mock to return nil for both rows and err. Notice I want nil for rows not an empty set. Here is my…

Goosal Tapal
- 205
- 1
- 2
- 5
3
votes
0 answers
Error "query was not expected" on INSERT while unit testing using sqlmock
I'm trying to run an unit test that executes an INSERT into DB, so I mocked exactly the same query string that the gorm log displays with the same arguments but I'm still having problem while trying to execute the test, (with postgres driver)…

Rafael Reyes
- 2,615
- 8
- 34
- 51
2
votes
1 answer
Unable to scan type of uuid.UUID into UUID in sqlmock Golang
I have the following test function.
func (s *Suite) Test_delete() {
var (
id = uuid.New()
name = "test"
quantity = 2
)
s.mock.ExpectQuery(regexp.QuoteMeta(
`SELECT * FROM "items" WHERE code = $1…

Richard
- 7,037
- 2
- 23
- 76
2
votes
1 answer
Gorm delete with clauses sqlmock test
I have a Gorm delete with the returning result:
expirationDate := time.Now().UTC().Add(-(48 * time.hour))
var deletedUsers Users
res := gormDB.WithContext(ctx).
Table("my_users").
Clauses(clause.Returning{Columns: []clause.Column{{Name:…

freethinker
- 2,257
- 4
- 26
- 49
2
votes
1 answer
testing gorm with go-sqlmock issue comparing queries with mock.ExpectQuery and regexp.QuoteMeta
I am having problems comparing the query expected with the true query of gorm, this is my code:
package repository
import (
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"YOUR_GO_ROOT/pkg/domain"
…

mrpepo877
- 490
- 6
- 23
2
votes
1 answer
Testing with gorm and sqlmock
I am struggling with writing a test for my go lambda function with sqlmock and gorm.
This is the function I want to test:
func DoCleanup(con *gorm.DB) {
sixMonthsAgo := time.Now().AddDate(0, -6, 0).Format("2006-02-01")
con.Where("date_to <=…

Bird87 ZA
- 2,313
- 8
- 36
- 69
2
votes
1 answer
Can I test queries in goroutine with sqlmock?
I have this code in my application, I use goroutine because that queries are very slow. The code works correctly but I've been trying to test that function with sqlmock and that been having a lot of false positives and a lot of dificulties, what can…
2
votes
0 answers
Unit testing GORM associations with sqlmock
I'm working on my first Go + Gin + GORM project. I've two models: List and Todo as follows:
type List struct {
gorm.Model
Name string
Todos []*Todo `gorm:"many2many:list_todos;"`
}
type Todo struct {
gorm.Model
Title …

csmathhc
- 205
- 2
- 12