无法将相等的时间戳值分为时间。
我尝试编写单元测试。一切都很好,直到我开始处理时间包的那一刻。
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
"gorm.io/driver/postgres"
"gorm.io/gorm"
func createDB(t *testing.T) (*DB, sqlmock.Sqlmock) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
require.NotNil(t, db)
require.NotNil(t, mock)
dialector := postgres.New(postgres.Config{
DSN: "sqlmock_db_0",
DriverName: "postgres",
Conn: db,
PreferSimpleProtocol: true,
})
gormDB, err := gorm.Open(dialector, &gorm.Config{})
require.NoError(t, err)
require.NotNil(t, gormDB)
return &DB{
gorm: gormDB,
db: db,
}, mock
}
func (db *DB) CreateUser(ctx context.Context, user model.User) error {
err := db.gorm.Create(&user).Error
return err
}
timeTest, _ := time.Parse("2006-01-02", "2021-01-14")
sourceUser := models.User{
CreatedAt: timeTest,
UpdatedAt: null.NewTime(timeTest, true),
}
db, mock := createDB(t)
mock.MatchExpectationsInOrder(false)
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO "user" ("created_at","updated_at") VALUES ($1,$2) RETURNING "id"`)).
WithArgs(
sourceUser.CreatedAt,
sourceUser.UpdatedAt,
).WillReturnResult(sqlmock.NewResult(sourceUser.ID, 1))
mock.ExpectCommit()
err := db.CreateUser(context.Background(), &sourceUser)
require.NoError(t, err)
错误:收到意外错误: 调用查询'INSERT INTO "user" ("created_at","updated_at") VALUES ($1,$2) RETURNING “id”' with args [{名称:序号:1值:2021-01-14 00:00:00 +0000 UTC}{名称:序号:2值:2021-01-14 00:00:00 +0000 UTC } ] 没有预料到;预计不会调用回滚事务
插入“用户”(“created_at”、“updated_at”)值(“2021-01-14 00:00:00”、“2021-01-14 00:00:00”) 返回“id”
变量 2021-01-14 00:00:00 和 2021-01-14 00:00:00 +0000 UTC 不同。 我尝试以不同的方式解决这个问题,但找不到正确的方法。 如果我像“INSERT”一样编辑我的 QuoteMeta,它也不起作用
I attempt to write unit tests. Everything was fine until the moment when I started dealing with the time package.
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
"gorm.io/driver/postgres"
"gorm.io/gorm"
func createDB(t *testing.T) (*DB, sqlmock.Sqlmock) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
require.NotNil(t, db)
require.NotNil(t, mock)
dialector := postgres.New(postgres.Config{
DSN: "sqlmock_db_0",
DriverName: "postgres",
Conn: db,
PreferSimpleProtocol: true,
})
gormDB, err := gorm.Open(dialector, &gorm.Config{})
require.NoError(t, err)
require.NotNil(t, gormDB)
return &DB{
gorm: gormDB,
db: db,
}, mock
}
func (db *DB) CreateUser(ctx context.Context, user model.User) error {
err := db.gorm.Create(&user).Error
return err
}
timeTest, _ := time.Parse("2006-01-02", "2021-01-14")
sourceUser := models.User{
CreatedAt: timeTest,
UpdatedAt: null.NewTime(timeTest, true),
}
db, mock := createDB(t)
mock.MatchExpectationsInOrder(false)
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO "user" ("created_at","updated_at") VALUES ($1,$2) RETURNING "id"`)).
WithArgs(
sourceUser.CreatedAt,
sourceUser.UpdatedAt,
).WillReturnResult(sqlmock.NewResult(sourceUser.ID, 1))
mock.ExpectCommit()
err := db.CreateUser(context.Background(), &sourceUser)
require.NoError(t, err)
Error: Received unexpected error:
call to Query 'INSERT INTO "user" ("created_at","updated_at") VALUES ($1,$2) RETURNING
"id"' with args [{Name: Ordinal:1 Value:2021-01-14 00:00:00 +0000 UTC}{Name: Ordinal:2 Value:2021-01-14 00:00:00 +0000 UTC}
] was not expected; call to Rollback transaction was not expectedINSERT INTO "user" ("created_at","updated_at") VALUES ('2021-01-14 00:00:00','2021-01-14 00:00:00')
RETURNING "id"
Vars 2021-01-14 00:00:00 and 2021-01-14 00:00:00 +0000 UTC are not the same.
I try to solve this issue in different ways, but I can't find right.
It is also not working if I edit my QuoteMeta like 'INSERT'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论