我们花了2个令人沮丧的星期,尝试使用sqlmock进行功能和方法的所有可能排列,但无法获得针对INSERT语句的测试。这是非常基本的妈妈和流行SQL。只需使用Gorm进行简单插入即可。
要发布的代码太多,并导致许多错误排列和方法在此处列出,但我将发布最新的代码。
type AnyTime struct{}
func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
func TestInsertStoragePool(t *testing.T) {
cases := []struct {
givenPool StoragePool
wantedError error
}{
{
givenPool: StoragePool{
Name: "POOL1",
PoolId: "1",
Type: "NetworkFilesystem",
Volumes: []Volume{
{
Name: "VOL01",
StorageId: "1",
Type: "ROOT",
VolumeId: "1",
},
},
},
wantedError: nil,
},
}
for _, c := range cases {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
// Every attempt we make we try "Postgres", "mysql" and "sqlite3" here
DB, err := gorm.Open("sqlite3", db)
if err != nil {
t.Fatal(err)
}
DB.LogMode(true)
mock.ExpectBegin()
mock.ExpectExec(`INSERT INTO "storage_pools"`).WithArgs(
AnyTime{},
AnyTime{},
nil,
0,
0,
"",
"",
"",
0,
0,
0,
"",
c.givenPool.Name,
"",
0,
"",
"",
c.givenPool.PoolId,
"",
"",
"",
c.givenPool.Type,
"",
"").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
err = InsertStoragePool(DB, &c.givenPool)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
}
if !reflect.DeepEqual(err, c.wantedError) {
t.Errorf("expecting errror %q, got %q", c.wantedError, err)
}
}
}
此尝试的错误:
$go test ./...
(/database.go:231)
[2019-12-13 09:22:48] [0.11ms] INSERT INTO "storage_pools" ("created_at","updated_at","deleted_at","allocatediops","capacityiops","clusterid","clustername","created","disksizeallocated","disksizetotal","disksizeused","ipaddress","name","path","physicalsize","podid","podname","poolid","provider","scope","state","type","zoneid","zonename") VALUES ('2019-12-13 09:22:48','2019-12-13 09:22:48',NULL,0,0,'','','',0,0,0,'','POOL1','',0,'','','1','','','','NetworkFilesystem','','')
[1 rows affected or returned ]
(/database.go:231)
[2019-12-13 09:22:48] call to ExecQuery 'INSERT INTO "volumes" ("created_at","updated_at","deleted_at","clusterid","clustername","domainid","domain","name","path","physicalsize","podid","podname","size","state","storageid","storage","type","virtualsize","virtualmachineid","vmdisplayname","vmname","vmstate","volumeid","zoneid","zonename") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)' with args [{Name: Ordinal:1 Value:2019-12-13 09:22:48.093749 -0800 PST m=+0.004941653} {Name: Ordinal:2 Value:2019-12-13 09:22:48.093749 -0800 PST m=+0.004941653} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:} {Name: Ordinal:5 Value:} {Name: Ordinal:6 Value:} {Name: Ordinal:7 Value:} {Name: Ordinal:8 Value:VOL01} {Name: Ordinal:9 Value:} {Name: Ordinal:10 Value:0} {Name: Ordinal:11 Value:} {Name: Ordinal:12 Value:} {Name: Ordinal:13 Value:0} {Name: Ordinal:14 Value:} {Name: Ordinal:15 Value:1} {Name: Ordinal:16 Value:} {Name: Ordinal:17 Value:ROOT} {Name: Ordinal:18 Value:0} {Name: Ordinal:19 Value:} {Name: Ordinal:20 Value:} {Name: Ordinal:21 Value:} {Name: Ordinal:22 Value:} {Name: Ordinal:23 Value:1} {Name: Ordinal:24 Value:} {Name: Ordinal:25 Value:}], was not expected, next expectation is: ExpectedCommit => expecting transaction Commit
答案 0 :(得分:3)
错误消息告诉您,在您期望的插入之后正在执行另外的插入:
不期望将INERT插入“卷”(.....),下一个期望是:ExpectedCommit
在您的代码中,我们可以看到您的StoragePool
结构在切片中也有一些Volumes
。
因此,当您调用InsertStoragePool
时,首先会在storage_pools
中插入一行,然后再插入volumes
。
在添加提交期望之前,您应该为该插入添加新期望。