test: fix failed test related to Folder.Create

pull/1257/head
HFO4 3 years ago
parent a523fc4e2c
commit 23642d7597

@ -117,11 +117,9 @@ func TestGenericAfterUpload(t *testing.T) {
WithArgs(1, "我的文件"). WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1 // 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("我的文件", 1, 1). WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found")) mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
mock.ExpectBegin() mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
@ -140,11 +138,9 @@ func TestGenericAfterUpload(t *testing.T) {
WithArgs(1, "我的文件"). WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1 // 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("我的文件", 1, 1). WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows( mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
mock.NewRows([]string{"name"}).AddRow("test.txt"), mock.NewRows([]string{"name"}).AddRow("test.txt"),
) )
@ -160,11 +156,9 @@ func TestGenericAfterUpload(t *testing.T) {
WithArgs(1, "我的文件"). WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1 // 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("我的文件", 1, 1). WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows( mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnRows(
mock.NewRows([]string{"name", "upload_session_id"}).AddRow("test.txt", "1"), mock.NewRows([]string{"name", "upload_session_id"}).AddRow("test.txt", "1"),
) )
@ -180,11 +174,9 @@ func TestGenericAfterUpload(t *testing.T) {
WithArgs(1, "我的文件"). WithArgs(1, "我的文件").
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
// 1 // 1
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("我的文件", 1, 1). WithArgs("我的文件", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found")) mock.ExpectQuery("SELECT(.+)files(.+)").WillReturnError(errors.New("not found"))
mock.ExpectBegin() mock.ExpectBegin()

@ -224,12 +224,10 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
mock.ExpectBegin()
// ab // ab
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1). WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(3, 1))
mock.ExpectCommit()
res, err := fs.CreateDirectory(ctx, "/ad/ab") res, err := fs.CreateDirectory(ctx, "/ad/ab")
asserts.NoError(err) asserts.NoError(err)
asserts.EqualValues(3, res.ID) asserts.EqualValues(3, res.ID)
@ -245,10 +243,10 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(2, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1). WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
_, err = fs.CreateDirectory(ctx, "/ad/ab") _, err = fs.CreateDirectory(ctx, "/ad/ab")
@ -270,18 +268,18 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ad // 创建ad
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("ad", 1, 1). WithArgs("ad", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1)) mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit() mock.ExpectCommit()
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ab // 创建ab
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("ab", 2, 1). WithArgs("ab", 2, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit() mock.ExpectCommit()
_, err = fs.CreateDirectory(ctx, "/ad/ab") _, err = fs.CreateDirectory(ctx, "/ad/ab")
@ -303,12 +301,14 @@ func TestFileSystem_CreateDirectory(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1)) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}).AddRow(1, 1))
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}))
// 创建ad // 创建ad
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)"). mock.ExpectQuery("SELECT(.+)").
WithArgs("ad", 1, 1). WithArgs("ad", 1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"})) WillReturnRows(sqlmock.NewRows([]string{"id", "owner_id"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1)).WillReturnError(errors.New("error")) mock.ExpectExec("INSERT(.+)").WillReturnResult(sqlmock.NewResult(2, 1)).WillReturnError(errors.New("error"))
mock.ExpectRollback() mock.ExpectRollback()
mock.ExpectQuery("SELECT(.+)").
WillReturnError(errors.New("error"))
_, err = fs.CreateDirectory(ctx, "/ad/ab") _, err = fs.CreateDirectory(ctx, "/ad/ab")
asserts.Error(err) asserts.Error(err)
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())

@ -177,8 +177,8 @@ func TestImportTask_Do(t *testing.T) {
// 查找同名文件,不存在 // 查找同名文件,不存在
mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id"})) mock.ExpectQuery("SELECT(.+)files").WillReturnRows(sqlmock.NewRows([]string{"id"}))
// 创建目录 // 创建目录
mock.ExpectBegin()
mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"})) mock.ExpectQuery("SELECT(.+)folders").WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)folders(.+)").WillReturnResult(sqlmock.NewResult(2, 1)) mock.ExpectExec("INSERT(.+)folders(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit() mock.ExpectCommit()
// 插入文件记录 // 插入文件记录

Loading…
Cancel
Save