Questions tagged [go-sqlmock]

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.

63 questions
1
vote
1 answer

Why "mock.ExpectQuery" return "is without arguments"

Below is my unit test file: func TestAddLike(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) } defer db.Close() …
yuqcc
  • 76
  • 9
1
vote
1 answer

SQLMock - Mocking out chained DELETE query

I'm currently trying to write a test for a Database query, but stuck on trying to figure out how to mock it out. I have the following struct (for your reference): type User struct { ID uint `gorm:"column:id;primary_key"` CreatedAt…
LDK
  • 11
  • 2
1
vote
1 answer

Can't get a test working for an INSERT statement

We have spent 2 frustrating weeks trying every possible permutation of functions and approaches using sqlmock but can't get a test working for an INSERT statement. This is very basic mom and pop SQL. Just a simple insert using Gorm. Way too much…
user11471017
0
votes
0 answers

gorm different column order and test failure

In my code, I have following model: type ID uint64 type BaseModel struct { ID ID `gorm:"column:id;primaryKey;autoIncrement" json:"id"` UpdateDate time.Time `gorm:"column:update_date;default:CURRENT_TIMESTAMP ON UPDATE…
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
0
votes
0 answers

sqlmock "expecting database transaction Begin"

I am trying to do unit testing with sqlmock. I have the mock.ExpectBegin() and the mock.ExpectCommit(), but I keep getting this error: there is a remaining expectation which was not matched: ExpectedBegin => expecting database transaction Begin I'm…
0
votes
1 answer

How do I pass a protobuf object as a row to sqlmock.AddRow in golang?

I am trying to unit test my go code using sqlmock. Here is the original code that I am trying to test. func enrollCourse(db *gorm.DB, user_id string ,course_id string) error { user := &usermodels.User{} ref := db.First(user, "uuid = ?",…
0
votes
1 answer

How to resolve `invalid packet size, it is shorter than header size` error

I am trying to connect to my db. but getting the following Error: 2022/11/10 13:30:43 invalid packet size, it is shorter than header size My Code: var server = "123.45.67.89" var port = 3030 var user = "myUserId" var password = "MyPassword" var…
Ashutosh Yadav
  • 333
  • 1
  • 12
0
votes
1 answer

Not able to INSERT data at my choice into go-sqlmock command

Trying to execute an api-test, test calls a mysql gorm SELECT command, then subsequently executes an INSERT command, the SELECT command works, but I have not been able to insert data at my choice into the INSERT command, however the INSERT command…
JonB
  • 804
  • 3
  • 12
  • 40
0
votes
1 answer

Sqlmock is not matching the query when replace integer argument with alpha-numeric one

I am trying to test my DB functions using go-sqlmock and I am not able make this one test pass. Here is my model type User struct { ID string `gorm:"primaryKey; size:11"` FirstName string `gorm:"NOT NULL; size:255"` …
0
votes
1 answer

there is a remaining expectation which was not matched: ExpectedQuery => expecting Query, QueryContext or QueryRow which

I have this repository func (r *WorkspaceRepository) Delete(id any) (bool, error) { if err := r.db.Delete(&model.Workspace{}, "id = ?", id).Error; err != nil { return false, err } return true, nil } Which does a Soft-Delete,…
Rodrigo
  • 135
  • 4
  • 45
  • 107
0
votes
1 answer

call to Rollback transaction, was not expected, next expectation is: ExpectedQuery

I am trying to wrote this test bellow, other tests works fine, however I am having problems with the UPDATE query func TestDeleteWorkspace(t *testing.T) { conn, mock, repository, err := setup() defer conn.Close() assert.NoError(t, err) …
Rodrigo
  • 135
  • 4
  • 45
  • 107
0
votes
0 answers

sqlmock does not produce an error on unexpected query

In this test, there is a query that gets executed when this API is called. This query is to select a user. The interesting thing when I comment this query out from the test, which means it is not expected, mock.ExpectationsWereMet it does not give…
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
0
votes
1 answer

Issue with go-sqlmock testing in the expected query part

I am using go-sqlmock for the first time and I am trying to write a test for post operation. I am using gorm and gin. The test is giving me an error where s.mock.ExpectQuery(regexp.QuoteMeta(.... I am not what is the issue here. I have posted both…
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
0
votes
1 answer

Creating a gorm database with go-sqlmock (runtime error)

Summary I'm trying to use go-sqlmock with gorm for testing. I want to write a test for the initial database migration, but I've hit a panic: runtime error: invalid memory address or nil pointer dereference and I've been having trouble figuring out…
LeviJames
  • 188
  • 1
  • 18
0
votes
0 answers

Getting unexpected error while executing queries for unit testing with sqlmock

I have the following function that takes a slice of type SchoolDbEntry and bulk inserts it into a DB. type SchoolRegistry struct { Conn *sqlx.DB } insertStudentEntry = `insert into StudentEntries (registerId, studentId, fee,…