DATA-DOG/go-sqlmock

call to Rollback transaction, was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow

skhaz opened this issue · 0 comments

skhaz commented

Question

I am trying to test this code

package main

import (
	"database/sql"
	"database/sql/driver"
	"regexp"
	"time"

	"github.com/DATA-DOG/go-sqlmock"
	"github.com/google/uuid"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type Workspace struct {
	ID        uuid.UUID      `gorm:"type:uuid;default:uuid_generate_v4()" json:"id"`
	Name      string         `gorm:"not null,type:text" json:"name"`
	CreatedAt time.Time      `gorm:"autoCreateTime" json:"create_time"`
	UpdatedAt time.Time      `gorm:"autoUpdateTime" json:"update_time"`
	DeletedAt gorm.DeletedAt `gorm:"index,->" json:"-"`
}

type WorkspaceRepository struct {
	db *gorm.DB
}

func (r *WorkspaceRepository) Configure(db *gorm.DB) {
	r.db = db
}

func (r *WorkspaceRepository) Delete(id any) (bool, error) {
	if err := r.db.Delete(&Workspace{}, "id = ?", id).Error; err != nil {
		return false, err
	}

	return true, nil
}

func setup() (conn *sql.DB, mock sqlmock.Sqlmock, repository WorkspaceRepository, err error) {
	conn, mock, err = sqlmock.New()
	if err != nil {
		return
	}

	dialector := postgres.New(postgres.Config{Conn: conn, PreferSimpleProtocol: true})
	db, err := gorm.Open(dialector, &gorm.Config{})
	if err != nil {
		return
	}

	repository = WorkspaceRepository{}

	repository.Configure(db)

	return
}

type AnyTime struct{}

func (a AnyTime) Match(v driver.Value) bool {
	_, ok := v.(time.Time)
	return ok
}

func main() {
	conn, mock, repository, err := setup()
	if err != nil {
		panic(err)
	}

	defer conn.Close()

	id := "31645053-7f8c-4769-ba94-b8948f478540"

	mock.ExpectBegin()
	mock.ExpectQuery(regexp.QuoteMeta(`UPDATE "workspaces" SET`)).WithArgs(AnyTime{}, id)
	mock.ExpectCommit()

	_, err = repository.Delete(id)
	if err != nil {
		panic(err)
	}

	err = mock.ExpectationsWereMet()
	if err != nil {
		panic(err)
	}
}

However I get this error, I do not know what I am doing wrong, my expectations match

2022/07/04 11:22:02 /home/ubuntu/workspace/personal/main.go:32 call to ExecQuery 'UPDATE "workspaces" SET "deleted_at"=$1 WHERE id = $2 AND "workspaces"."deleted_at" IS NULL' with args [{Name: Ordinal:1 Value:2022-07-04 11:22:02.304708 -0300 -03} {Name: Ordinal:2 Value:31645053-7f8c-4769-ba94-b8948f478540}], was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
  - matches sql: 'UPDATE "workspaces" SET'
  - is with arguments:
    0 - {}
    1 - 31645053-7f8c-4769-ba94-b8948f478540; call to Rollback transaction, was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
  - matches sql: 'UPDATE "workspaces" SET'
  - is with arguments:
    0 - {}
    1 - 31645053-7f8c-4769-ba94-b8948f478540
[0.168ms] [rows:0] UPDATE "workspaces" SET "deleted_at"='2022-07-04 11:22:02.304' WHERE id = '31645053-7f8c-4769-ba94-b8948f478540' AND "workspaces"."deleted_at" IS NULL
panic: call to ExecQuery 'UPDATE "workspaces" SET "deleted_at"=$1 WHERE id = $2 AND "workspaces"."deleted_at" IS NULL' with args [{Name: Ordinal:1 Value:2022-07-04 11:22:02.304708 -0300 -03} {Name: Ordinal:2 Value:31645053-7f8c-4769-ba94-b8948f478540}], was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
  - matches sql: 'UPDATE "workspaces" SET'
  - is with arguments:
    0 - {}
    1 - 31645053-7f8c-4769-ba94-b8948f478540; call to Rollback transaction, was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
  - matches sql: 'UPDATE "workspaces" SET'
  - is with arguments:
    0 - {}
    1 - 31645053-7f8c-4769-ba94-b8948f478540

goroutine 1 [running]:
main.main()
        /home/ubuntu/workspace/personal/main.go:81 +0x1da
exit status 2