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 does not work with Postgres uint #738

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gorm.io/playground
go 1.20

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
Expand Down Expand Up @@ -31,5 +32,3 @@ require (
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
)

replace gorm.io/gorm => ./gorm
232 changes: 232 additions & 0 deletions mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package main

import (
"database/sql"
"database/sql/driver"
"github.com/DATA-DOG/go-sqlmock"
"os"
"regexp"
"testing"
"time"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

type AnyTime struct{}

// Match satisfies sqlmock.Argument interface
func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
func TestGORMV2_Failing(t *testing.T) {

type Student struct {
gorm.Model
Name string `gorm:"type:varchar(50);not null"`
}
type v2Suite struct {
db *gorm.DB
mock sqlmock.Sqlmock
student Student
}

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{
Name: "Test 1",
}

defer db.Close()

s.mock.ExpectBegin()

s.mock.ExpectExec(
regexp.QuoteMeta(`INSERT INTO "students" ("created_at","updated_at","deleted_at","name") VALUES ($1,$2,$3,$4) RETURNING "id"`)).
WithArgs(AnyTime{}, AnyTime{}, nil, s.student.Name).
WillReturnResult(sqlmock.NewResult(int64(1), 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)
}
}

func TestGORMV2_Working(t *testing.T) {
type Student struct {
ID string `gorm:"primaryKey,autoIncrement"`
Name string `gorm:"type:varchar(50);not null"`
}
type v2Suite struct {
db *gorm.DB
mock sqlmock.Sqlmock
student Student
}

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{
Name: "Test 1",
}

defer db.Close()

s.mock.ExpectBegin()

s.mock.ExpectExec(
regexp.QuoteMeta(`INSERT INTO "students" ("id","name") VALUES ($1,$2)`)).
WithArgs("", s.student.Name).
WillReturnResult(sqlmock.NewResult(int64(1), 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)
}
}

func TestGORMV2_ID_Working(t *testing.T) {

type Student struct {
gorm.Model
Name string `gorm:"type:varchar(50);not null"`
}
type v2Suite struct {
db *gorm.DB
mock sqlmock.Sqlmock
student Student
}

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{
Name: "Test 1",
}

defer db.Close()
addRow := sqlmock.NewRows([]string{"id"}).AddRow("1")

s.mock.ExpectBegin()

s.mock.ExpectQuery(`INSERT INTO "students" (.+) VALUES (.+) RETURNING "id"`).WillReturnRows(addRow)

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)
}
}

func TestMain(m *testing.M) {

code := m.Run()

os.Exit(code)
}
Loading