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
2
votes
1 answer
DB mock using sqlmock not working for testing Gin/GORM API?
I've an API written using Gin that uses GORM for ORM. The API works perfectly fine when using a real DB and accessing the API URL from the web browser. But I can't get a mocked unit test to pass:
func TestRespForGetUsersHandlerWithSomeUsers(t…

csmathhc
- 205
- 2
- 12
2
votes
1 answer
`could not match actual sql` error while mocking gorm `updates` using go-sqlmock?
repository.go
func (repo *Repository) Update(info *model.Requests) error{
if info == nil{
return nil
}
columnChanges := map[string]interface{}{
status: “completed",
}
if err :=…

sofs1
- 3,834
- 11
- 51
- 89
1
vote
0 answers
Go - How to mock SQL `SELECT`
I'm practicing testing with Go. I'm building a CRUD operation with Echo and I'm in the state of testing my service.go file. It uses a repository that is a gorm.DB which is connected to my local Postgresql.
Also, previously I tested a Create function…

ChuChuwi
- 719
- 1
- 8
- 13
1
vote
1 answer
Problem writing a unit test using gorm and sql-mock
this is my funckion using package testing, gorm and sql-mock:
func Test_Create(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Errorf("Failed to open mock sql db, got error: %v", err)
}
if db == nil {
…

a_r_e_k97
- 21
- 1
- 3
1
vote
1 answer
Mocking sql.max() in golang using go-sqlmock
I am looking to mock up this query in my code ("select max(a) from public.abc where id = %d")
The way I am mocking this line is
maxOfA := 1
maxOfARows := sqlmock.NewRows([]string{"a"}).AddRow(maxOfA)
suite.mock.ExpectQuery("select max(a) from…

EarthsPiligrim
- 61
- 5
1
vote
0 answers
DATA-DOG/go-sqlmock ExpectExec not working when expecting ID insertion
I am working with DATA-DOG/go-sqlmock to test my repository insert method.
I noticed that everything works just fine when expecting any fields but ID.
Once I want to test my entity insertion with ID i receive call to Query ... was not…

freethinker
- 2,257
- 4
- 26
- 49
1
vote
1 answer
sqlmock database is closed issue
My problem is that the connection to db is closed when I open it twice in the same func.
This is the code example that I need to test:
var openDB := func() *sql.DB {
db, _ := sql.Open()
return db
}
func doSomeQuery(id string) bool {
var…

Dmitry Malys
- 1,293
- 4
- 25
- 46
1
vote
0 answers
Error in multiple insert with sqlmock golang
I am using the sqlmock library to test with mysql's GORM database. However, I have a statement that requires executing an INSERT command multiple times with the same information, so I add my code snippet
file_test.go - Mock database
func…

Carlos Benjumea
- 11
- 1
1
vote
0 answers
How to Unit Test Gorm Golang Preload Query
Recently, I was trying to query data with preload function that gorm provided.
Here's the actual implementation looks like:
func (repo *SectorRepository) GetSectorsByStockCode(stockCode string) *model.Sector {
var foundStocks []*model.Stock
…

otnieldocs
- 325
- 1
- 5
- 20
1
vote
0 answers
Unit Testing sqlboiler with sqlmock failing select
I'm trying to mock a method written with sqlboiler but I'm having massive trouble building the mock-query.
The model I'm trying to mock looks like this:
type Course struct {
ID int, Name string, Description null.String, EnrollKey string, ForumID…

Karparetzu
- 11
- 3
1
vote
1 answer
How do I use sqlmock to mock a table count query (GORM HasTable) in Go?
I'm using sqlmock to unit test a set of Mysql database query handlers written in Go. This works great for standard SELECT / INSERT queries. For our db healthcheck, I'm using GORM's DB.Migrator().HasTable() to make sure a particular table exists in…

Orion
- 11
- 3
1
vote
0 answers
Go GORM Mocking Begin Expected
I am trying to test a function that gets some details then update details in db. I am using gorm for my ORM and mocking db exec with DATA-DOG/sqlmock. But I keep getting expected begin error but not sure where I am messing up. I tried many…

rak1n
- 671
- 1
- 8
- 17
1
vote
1 answer
Go sqlmock test MySQL batch insert
I am using GORM to batch insert multiple rows into a MySQL table and I want to test that the behaviour is the correct one using sqlmock. I haven't found anything online regarding mocking batch inserts with sqlmock.
For inserting a single row, we…

A01XP
- 48
- 6
1
vote
1 answer
Go Unit test - could not match actual sql with expected regexp while mocking gorm using go-sqlmock?
When unit testing I mock gorm using sqlmock. But when running all initial setup are fine but error happen when the actual query and testing query matches. I have given below all codes.
user.go
func (r *users) GetUserByID(userID uint) (*domain.User,…

Md. Abu Farhad
- 373
- 5
- 21
1
vote
1 answer
SQLMock and Gorm: Mocking Postgres Insert
When I try to mock a Postgres insert with SQLMock and Gorm.io I receive an error that the query isn't expected. I tried to use regexp.QuoteMeta() to wrap and escape my string, but it doesn't work. I added and removed args and result, but the error…

AndreaCostanzo1
- 1,799
- 1
- 12
- 30