我正在尝试使用 sqlmock 使用 Gorm 为一些代码编写测试。我想为我的插入函数编写测试,但现在我把头发拉出来试图让更新工作。
工作流的第一部分只是从数据库中查询记录。即使日志输出显示它们相同,我也无法让它与我的 SQL 匹配。
这是错误消息:
(/path/to/my/project/database.go:263)
[2020-01-08 10:29:40] Query: could not match actual sql: "SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1" with expected regexp "SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1"
我也尝试使用ExpectQuery 的ExpectExec 插入。
for _, c := range cases {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
DB, err := gorm.Open("sqlite3", db)
if err != nil {
t.Fatal(err)
}
DB.LogMode(true)
mock.ExpectQuery(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1`)
err = UpdateStoragePool(DB, &c.givenPool)
if !reflect.DeepEqual(c.wantedError, err) {
t.Fatalf("expecting errror %q, got %q", c.wantedError, err)
}
// if we didn't have any errors during the tx, check all expectations were met
if c.wantedError == nil {
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf(err.Error())
}
}
}
我也试过:
mock.ExpectQuery(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = '1')) ORDER BY "storage_pools"."id" ASC LIMIT 1`).WithArgs(1)
mock.ExpectExec(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1`)
mock.ExpectExec(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = '1')) ORDER BY "storage_pools"."id" ASC LIMIT 1`).WithArgs(1)
有人知道我在这里做错了什么吗?
* 更新 *
由于某种原因,这不适用于 select 语句:
mock.ExpectExec(`SELECT \* FROM "storage_pools"`).
WithArgs(c.givenPool.PoolId).WillReturnResult(sqlmock.NewResult(1, 1))
[2020-01-13 10:32:21] call to Query 'SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1' with args [{Name: Ordinal:1 Value:1}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:
- matches sql: 'SELECT \* FROM "storage_pools"'
- is with arguments:
0 - 1
- should return Result having:
LastInsertId: 1
RowsAffected: 1
这确实有效,但现在我遇到了一个新问题。对于初学者来说,Gorm 出于某种原因正在执行 2 个选择语句......第一个工作并找到该行,第二个查询没有找到同一行。我在这里不知所措。即将放弃这个图书馆。在我们努力让它工作的时候,我本可以编写自己的。
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Fatal(err)
}
DB, err := gorm.Open("postgres", db)
if err != nil {
t.Fatal(err)
}
DB.LogMode(true)
mockedRow := sqlmock.NewRows([]string{"id", "created_at", "updated_at", "poolid"}).AddRow(1, time.Now(), time.Now(), "1")
// Mock the complete transaction
mock.ExpectQuery(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1`).
WithArgs(c.givenPool.PoolId).
WillReturnRows(mockedRow)
mock.ExpectQuery(`SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND "storage_pools"."id" = ? AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC`).
WithArgs(1, c.givenPool.PoolId).
WillReturnRows(mockedRow)