Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gorm v2.0 unit testing with sqlmock #3565

Closed
rkalli opened this issue Oct 2, 2020 · 22 comments
Closed

gorm v2.0 unit testing with sqlmock #3565

rkalli opened this issue Oct 2, 2020 · 22 comments
Assignees
Labels

Comments

@rkalli
Copy link

rkalli commented Oct 2, 2020

Description

Unit testing with sqlmock works great with V1. But after updating to V2 the same test fails with following error:

/workspace/go/src/github.com/go-gorm/playground/gorm-v2-sqlmock_test.go:73 call to ExecQuery 'INSERT INTO "students" ("id","name") VALUES ($1,$2)' with args [{Name: Ordinal:1 Value:123456} {Name: Ordinal:2 Value:Test 1}] was not expected; call to Rollback transaction was not expected

I am unable to push to playground and getting permission denied. But here are the test files and also attaching the repository as a zip file.

package main

import (
	"database/sql"
	"regexp"
	"testing"

	"github.com/jinzhu/gorm"
	"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

type v1Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

func TestGORMV1(t *testing.T) {
	s := &v1Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open("postgres", db)
	if err != nil {
		t.Errorf("Failed to open gorm db, got error: %v", err)
	}

	if s.db == nil {
		t.Error("gorm db is null")
	}

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	s.mock.MatchExpectationsInOrder(false)
	s.mock.ExpectBegin()

	s.mock.ExpectQuery(regexp.QuoteMeta(
		`INSERT INTO "students" ("id","name") 
					VALUES ($1,$2) RETURNING "students"."id"`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnRows(sqlmock.NewRows([]string{"id"}).
			AddRow(s.student.ID))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

After updated to V2:

package main

import (
	"database/sql"
	"regexp"
	"testing"

	"gopkg.in/DATA-DOG/go-sqlmock.v1"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	dialector := postgres.New(postgres.Config{
		DSN:                  "sqlmock_db_0",
		DriverName:           "postgres",
		Conn:                 db,
		PreferSimpleProtocol: true,
	})
	s.db, err = gorm.Open(dialector, &gorm.Config{})
	if err != nil {
		t.Errorf("Failed to open gorm v2 db, got error: %v", err)
	}

	if s.db == nil {
		t.Error("gorm db is null")
	}

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	s.mock.MatchExpectationsInOrder(false)
	s.mock.ExpectBegin()

	s.mock.ExpectQuery(regexp.QuoteMeta(
		`INSERT INTO "students" ("id","name")
					VALUES ($1,$2) RETURNING "students"."id"`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnRows(sqlmock.NewRows([]string{"id"}).
			AddRow(s.student.ID))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}
[playground.zip](https://github.com/go-gorm/gorm/files/5319499/playground.zip)
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Oct 2, 2020
@github-actions
Copy link

github-actions bot commented Oct 2, 2020

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@rkalli
Copy link
Author

rkalli commented Oct 4, 2020 via email

@mohanasundaramn
Copy link

@jinzhu How to mock Gorm v2 with sqlmock. Any example would be greatly helpful

@jinzhu
Copy link
Member

jinzhu commented Oct 12, 2020

Checkout connect with existing database connection, personally I don't use sqlmock for testing, use real databases.

https://gorm.io/docs/connecting_to_the_database.html#Existing-database-connection

@jinzhu
Copy link
Member

jinzhu commented Oct 12, 2020

@rkalli you could disable the default transaction https://gorm.io/docs/gorm_config.html#SkipDefaultTransaction

@hielfx
Copy link

hielfx commented Oct 19, 2020

Checkout connect with existing database connection, personally I don't use sqlmock for testing, use real databases.

https://gorm.io/docs/connecting_to_the_database.html#Existing-database-connection

Following the steps here with sql-mock is returning this error failed to initialize database, got error all expectations were already fulfilled, call to Query 'SELECT VERSION()' with args [] was not expected

Reproducible code:

package main

import (
	"github.com/DATA-DOG/go-sqlmock"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {

	sqlDB, _, err := sqlmock.New()
	if err != nil {
		panic(err)
	}

	gormDB, err := gorm.Open(mysql.New(mysql.Config{
		Conn: sqlDB,
	}), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	_ = gormDB
}

@jinzhu
Copy link
Member

jinzhu commented Oct 19, 2020

When using MySQL, you need to use SkipInitializeWithVersion: true

https://gorm.io/docs/connecting_to_the_database.html#MySQL

@hielfx
Copy link

hielfx commented Oct 19, 2020

@jinzhu Thanks for the quick response, it's working now

@hexuejian
Copy link

gorm2.0 ExpectQuery -> ExpectExec

@sebastianbuechler
Copy link

gorm2.0 ExpectQuery -> ExpectExec

Can you give an example of what should run? Maybe even with the original code snippet of this issue?

@DionAngga
Copy link

please give me example gorm2.0 with sqlmock. i really need this

@addonrizky
Copy link

please give me example gorm2.0 with sqlmock. i really need this

try this snippet @DionAngga

package test

import (
	"database/sql"
	"regexp"
	"strconv"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

type Student struct {
	ID   string
	Name string
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open(mysql.New(mysql.Config{
		Conn:                      db,
		SkipInitializeWithVersion: true,
	}), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	defer db.Close()

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	studentID, _ := strconv.Atoi(s.student.ID)

	s.mock.ExpectBegin()

	s.mock.ExpectExec(
		regexp.QuoteMeta("INSERT INTO `students` (`id`,`name`) VALUES (?,?)")).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnResult(sqlmock.NewResult(int64(studentID), 1))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
		t.FailNow()
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

@josk2
Copy link

josk2 commented Nov 28, 2022

When using MySQL, you need to use SkipInitializeWithVersion: true

https://gorm.io/docs/connecting_to_the_database.html#MySQL

Thanks @jinzhu .
I just add SkipInitializeWithVersion: true and pass

db, err := gorm.Open(mysql.New(mysql.Config{
		Conn:                      sqlDB,
		DriverName:                "mysql",
		SkipInitializeWithVersion: true,
	}),
		&gorm.Config{})

@ityulkanov
Copy link

How do I make the code above works with Postgres? Seems like it requires some more tuning( call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

@sebastianbuechler
Copy link

How do I make the code above works with Postgres? Seems like it requires some more tuning( call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

Here you go:

package test

import (
	"database/sql"
	"regexp"
	"strconv"
	"testing"

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

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

type Student struct {
	ID   string
	Name string
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open(postgres.New(
		postgres.Config{
			Conn:       db,
			DriverName: "postgres",
		},
	), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	defer db.Close()

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	studentID, _ := strconv.Atoi(s.student.ID)

	s.mock.ExpectBegin()

	s.mock.ExpectExec(
		regexp.QuoteMeta(`INSERT INTO "students" ("id","name") VALUES ($1,$2)`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnResult(sqlmock.NewResult(int64(studentID), 1))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
		t.FailNow()
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

@ityulkanov
Copy link

call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

How do I make the code above works with Postgres? Seems like it requires some more tuning( call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

Here you go:

package test

import (
	"database/sql"
	"regexp"
	"strconv"
	"testing"

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

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

type Student struct {
	ID   string
	Name string
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open(postgres.New(
		postgres.Config{
			Conn:       db,
			DriverName: "postgres",
		},
	), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	defer db.Close()

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	studentID, _ := strconv.Atoi(s.student.ID)

	s.mock.ExpectBegin()

	s.mock.ExpectExec(
		regexp.QuoteMeta(`INSERT INTO "students" ("id","name") VALUES ($1,$2)`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnResult(sqlmock.NewResult(int64(studentID), 1))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
		t.FailNow()
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

Copied and pasted to Goland this exact code, and it produces the same error: call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which: I guess there is some curse on my machine(

@sebastianbuechler
Copy link

call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

How do I make the code above works with Postgres? Seems like it requires some more tuning( call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

Here you go:

package test

import (
	"database/sql"
	"regexp"
	"strconv"
	"testing"

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

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

type Student struct {
	ID   string
	Name string
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open(postgres.New(
		postgres.Config{
			Conn:       db,
			DriverName: "postgres",
		},
	), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	defer db.Close()

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	studentID, _ := strconv.Atoi(s.student.ID)

	s.mock.ExpectBegin()

	s.mock.ExpectExec(
		regexp.QuoteMeta(`INSERT INTO "students" ("id","name") VALUES ($1,$2)`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnResult(sqlmock.NewResult(int64(studentID), 1))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
		t.FailNow()
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

Copied and pasted to Goland this exact code, and it produces the same error: call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which: I guess there is some curse on my machine(

Maybe can you try another environment?

I ran it on my machine and it works without an issue:
image
I'm using Windows 10.0.22621, VS Code 1.75.1 and Go 1.18.1
My go.mod file for the test:

module gomocktest

go 1.18

require (
	github.com/DATA-DOG/go-sqlmock v1.5.0
	gorm.io/driver/postgres v1.4.7
	gorm.io/gorm v1.24.5
)

require (
	github.com/jackc/pgpassfile v1.0.0 // indirect
	github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
	github.com/jackc/pgx/v5 v5.2.0 // indirect
	github.com/jinzhu/inflection v1.0.0 // indirect
	github.com/jinzhu/now v1.1.5 // indirect
	golang.org/x/crypto v0.4.0 // indirect
	golang.org/x/text v0.5.0 // indirect
)

@avanshee
Copy link

avanshee commented Feb 18, 2023

call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

How do I make the code above works with Postgres? Seems like it requires some more tuning( call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:

Here you go:

package test

import (
	"database/sql"
	"regexp"
	"strconv"
	"testing"

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

type v2Suite struct {
	db      *gorm.DB
	mock    sqlmock.Sqlmock
	student Student
}

type Student struct {
	ID   string
	Name string
}

func TestGORMV2(t *testing.T) {
	s := &v2Suite{}
	var (
		db  *sql.DB
		err error
	)

	db, s.mock, err = sqlmock.New()
	if err != nil {
		t.Errorf("Failed to open mock sql db, got error: %v", err)
	}

	if db == nil {
		t.Error("mock db is null")
	}

	if s.mock == nil {
		t.Error("sqlmock is null")
	}

	s.db, err = gorm.Open(postgres.New(
		postgres.Config{
			Conn:       db,
			DriverName: "postgres",
		},
	), &gorm.Config{})
	if err != nil {
		panic(err) // Error here
	}

	defer db.Close()

	s.student = Student{
		ID:   "123456",
		Name: "Test 1",
	}

	defer db.Close()

	studentID, _ := strconv.Atoi(s.student.ID)

	s.mock.ExpectBegin()

	s.mock.ExpectExec(
		regexp.QuoteMeta(`INSERT INTO "students" ("id","name") VALUES ($1,$2)`)).
		WithArgs(s.student.ID, s.student.Name).
		WillReturnResult(sqlmock.NewResult(int64(studentID), 1))

	s.mock.ExpectCommit()

	if err = s.db.Create(&s.student).Error; err != nil {
		t.Errorf("Failed to insert to gorm db, got error: %v", err)
		t.FailNow()
	}

	err = s.mock.ExpectationsWereMet()
	if err != nil {
		t.Errorf("Failed to meet expectations, got error: %v", err)
	}
}

Copied and pasted to Goland this exact code, and it produces the same error: call to Query 'INSERT INTO "students" ("name","id") VALUES ($1,$2) RETURNING "id"' with args [{Name: Ordinal:1 Value:Test 1} {Name: Ordinal:2 Value:123456}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which: I guess there is some curse on my machine(

Maybe can you try another environment?

I ran it on my machine and it works without an issue: image I'm using Windows 10.0.22621, VS Code 1.75.1 and Go 1.18.1 My go.mod file for the test:

module gomocktest

go 1.18

require (
	github.com/DATA-DOG/go-sqlmock v1.5.0
	gorm.io/driver/postgres v1.4.7
	gorm.io/gorm v1.24.5
)

require (
	github.com/jackc/pgpassfile v1.0.0 // indirect
	github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
	github.com/jackc/pgx/v5 v5.2.0 // indirect
	github.com/jinzhu/inflection v1.0.0 // indirect
	github.com/jinzhu/now v1.1.5 // indirect
	golang.org/x/crypto v0.4.0 // indirect
	golang.org/x/text v0.5.0 // indirect
)

Hi, I m facing an issue of Error: on line s.db.Create(&s.student).Error.
Error message:
ExecQuery: could not match actual sql: "INSERT INTO mock.mockobj (SUBSCRIPTION_ID,SUBSCRIPTION_PROVIDER,mock_ACCOUNT_ID,PRODUCT_ID,SUBSCRIPTION_SCOPE,HOME_ID,PAY_STATE,VALID,PERIOD_END_DATE) VALUES (?,?,?,?,?,?,?,?,?)" with expected regexp "INSERT INTO 'mock'.'mockobj' ('SUBSCRIPTION_ID', 'SUBSCRIPTION_PROVIDER', 'mock_ACCOUNT_ID', 'PRODUCT_ID', 'SUBSCRIPTION_SCOPE', 'HOME_ID', 'PAY_STATE', 'VALID','PERIOD_END_DATE') VALUES (?, ?, ?, ?, ?, ?,?, ?, ?)"; call to Rollback transaction, was not expected

Not sure why regexp is adding \ or if the \is making any difference?

@HimangshuKakati
Copy link

@rkalli Was this issue fixed after disabling the default transaction. I don't know about mysql but on postgres I'm still facing this issue. @jinzhu

@marcusvnac
Copy link

marcusvnac commented Jun 9, 2023

I have the same problem here with the Postgres version.
Running on Mac OS 13.4, Go 1.20.4, gorm.io/driver/postgres v1.5.2 and gorm.io/gorm v1.25.1

@marcusvnac
Copy link

marcusvnac commented Jun 9, 2023

@rkalli Was this issue fixed after disabling the default transaction. I don't know about mysql but on postgres I'm still facing this issue. @jinzhu

@HimangshuKakati did you find any workaround for this issue with Postgres?

@rcodina-tp
Copy link

if I use
`func TestRepository_FirebaseMessage(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
defer db.Close()

// Expectation for SELECT VERSION()
mock.ExpectQuery("SELECT VERSION()").WillReturnRows(sqlmock.NewRows([]string{"version"}).AddRow("8.0.23"))

gormDB, err := gorm.Open(mysql.New(mysql.Config{
	Conn: db,
}), &gorm.Config{})
assert.NoError(t, err)

type args struct {
	userID int
}
tests := []struct {
	name      string
	args      args
	mockSetup func()
	token     string
	Err       error
	wantErr   bool
}{
	{
		name: "fcm_token retriven sucessfully",
		args: args{
			userID: 1,
		},
		mockSetup: func() {
			mock.ExpectQuery("SELECT * FROM `fcm_tokens` WHERE user_id = ? `fcm_tokens`.`id` LIMIT ?").
				WithArgs(1).
				WillReturnRows(sqlmock.NewRows([]string{"token"}).AddRow("valid_token"))
		},
		token: "valid_token",
	},
}
for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		tt.mockSetup()

		r := &Repository{
			Db: gormDB,
		}

		got, err := r.FirebaseMessage(tt.args.userID)

		if tt.wantErr {
			assert.Error(t, err)
			assert.Equal(t, tt.Err.Error(), err.Error())
		} else {
			assert.NoError(t, err)
			assert.Equal(t, tt.token, got)
		}

		err = mock.ExpectationsWereMet()
		assert.NoError(t, err)

	})
}

}`

generate a problem although the sql es equal than excpected

could not match actual sql: "SELECT * FROM fcm_tokens WHERE user_id = ? ORDER BY fcm_tokens.id LIMIT ?" with expected regexp "SELECT * FROM fcm_tokens WHERE user_id = ? fcm_tokens.id LIMIT ?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests