-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
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 |
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 |
@rkalli you could disable the default transaction https://gorm.io/docs/gorm_config.html#SkipDefaultTransaction |
Following the steps here with sql-mock is returning this error Reproducible code:
|
When using MySQL, you need to use |
@jinzhu Thanks for the quick response, it's working now |
gorm2.0 ExpectQuery -> ExpectExec |
Can you give an example of what should run? Maybe even with the original code snippet of this issue? |
please give me example gorm2.0 with sqlmock. i really need this |
try this snippet @DionAngga
|
Thanks @jinzhu .
|
How do I make the code above works with Postgres? Seems like it requires some more tuning( |
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)
}
} |
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:
Copied and pasted to Goland this exact code, and it produces the same error: |
I have the same problem here with the Postgres version. |
@HimangshuKakati did you find any workaround for this issue with Postgres? |
if I use
}` generate a problem although the sql es equal than excpected could not match actual sql: "SELECT * FROM |
Description
Unit testing with sqlmock works great with V1. But after updating to V2 the same test fails with following error:
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.
After updated to V2:
The text was updated successfully, but these errors were encountered: