无法将相等的时间戳值分为时间。

发布于 2025-01-20 14:20:09 字数 2050 浏览 1 评论 0原文

我尝试编写单元测试。一切都很好,直到我开始处理时间包的那一刻。

"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:002021-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 expected

INSERT 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文