aliyunpan/internal/syncdrive/sync_db_test.go

286 lines
8.1 KiB
Go
Raw Normal View History

2022-05-07 15:03:23 +08:00
package syncdrive
import (
"fmt"
2022-05-22 16:42:57 +08:00
"github.com/tickstep/aliyunpan/internal/utils"
2022-05-16 14:22:56 +08:00
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
2022-05-07 15:03:23 +08:00
"testing"
)
func TestGet(t *testing.T) {
b := NewPanSyncDb("D:\\smb\\feny\\goprojects\\dev\\pan.db")
b.Open()
defer b.Close()
fmt.Println(b.Get("/Parallels Desktop/v17/部分电脑安装v17可能有问题请退回v16版本.txt"))
}
2022-05-13 15:34:44 +08:00
func TestGetFileList(t *testing.T) {
b := NewPanSyncDb("D:\\smb\\feny\\goprojects\\dev\\pan.db")
b.Open()
defer b.Close()
fmt.Println(b.GetFileList("/Parallels Desktop/v17"))
}
2022-05-16 14:22:56 +08:00
func WalkAllFile(dirPath string, walkFn filepath.WalkFunc) error {
dirPath = strings.ReplaceAll(dirPath, "\\", "/")
info, err := os.Lstat(dirPath)
if err != nil {
err = walkFn(dirPath, nil, err)
} else {
err = walkAllFile(dirPath, info, walkFn)
}
return err
}
func walkAllFile(dirPath string, info os.FileInfo, walkFn filepath.WalkFunc) error {
if !info.IsDir() {
return walkFn(dirPath, info, nil)
}
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return walkFn(dirPath, nil, err)
}
for _, fi := range files {
subFilePath := dirPath + "/" + fi.Name()
err = walkFn(subFilePath, fi, err)
if err != nil {
return err
}
if fi.IsDir() {
err = walkAllFile(subFilePath, fi, walkFn)
if err != nil {
return err
}
}
}
return nil
}
func TestLocalSyncDb(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()
defer b.Close()
var walkFunc filepath.WalkFunc
walkFunc = func(file string, fi os.FileInfo, err error) error {
fmt.Println(file)
fileType := "file"
if fi.IsDir() {
fileType = "folder"
}
item := &LocalFileItem{
FileName: path.Base(file),
FileSize: fi.Size(),
FileType: fileType,
CreatedAt: fi.ModTime().Format("2006-01-02 15:04:05"),
UpdatedAt: fi.ModTime().Format("2006-01-02 15:04:05"),
FileExtension: "",
Sha1Hash: "",
Path: file,
}
if _, e := b.Add(item); e != nil {
fmt.Println(e)
}
return nil
}
WalkAllFile("D:\\smb\\feny\\goprojects\\dl\\a761171495", walkFunc)
}
func TestLocalSyncDbAddFileList(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\tmp.db")
b.Open()
defer b.Close()
fl := LocalFileList{}
fl = append(fl, &LocalFileItem{
FileName: "aliyunpan_command_history.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\aliyunpan_command_history.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f1.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\f1.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f2.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\f2.txt",
})
fl = append(fl, &LocalFileItem{
FileName: "f1.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: "",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\fo\\f1.txt",
})
fmt.Println(b.AddFileList(fl))
}
2022-05-16 14:22:56 +08:00
func TestLocalGet(t *testing.T) {
2022-05-22 16:42:57 +08:00
b := NewLocalSyncDb("/Volumes/Downloads/dev/sync_drive/840f28af799747848c0b3155e0bdfeab/local.bolt")
2022-05-16 14:22:56 +08:00
b.Open()
defer b.Close()
2022-05-22 16:42:57 +08:00
v, _ := b.Get("/Volumes/Downloads/dev/upload/未命名文件夹/[HAIDAN.VIDEO].绣春刀.2014.mp4.torrent")
2022-05-24 19:56:06 +08:00
fmt.Println(utils.ObjectToJsonStr(v, true))
2022-05-16 14:22:56 +08:00
}
func TestLocalGetFileList(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()
defer b.Close()
fmt.Println(b.GetFileList("D:/smb/feny/goprojects/dl/a761171495"))
}
2022-06-01 21:55:03 +08:00
func TestSyncDbAdd(t *testing.T) {
b := NewSyncFileDb("D:\\smb\\feny\\goprojects\\dev\\sync_drive\\sync.db")
b.Open()
defer b.Close()
b.Add(&SyncFileItem{
Action: SyncFileActionDownload,
Status: SyncFileStatusCreate,
LocalFile: nil,
PanFile: &PanFileItem{
Category: "others",
Crc64Hash: "16173291050517323365",
CreatedAt: "2021-07-09 23:17:52",
DomainId: "bj29",
DriveId: "19519221",
FileExtension: "apk",
FileId: "60e868a0a005315fe2b149b4ade47b2df8bdccee",
FileName: "10000996@yunpan-release.apk",
FileSize: 61029089,
FileType: "file",
ParentFileId: "60f29e3ac420ae8c08e645db8b80b881e2a3633c",
Path: "/sync_drive/10000996@yunpan-release.apk",
Sha1Hash: "05A7597967C3C947D84564A7D55309DEA56ED985",
UpdatedAt: "2021-07-21 17:05:49",
UploadId: "rapid-9141d56d-21c3-4fcc-b41d-5df7bf47540f",
},
StatusUpdateTime: "",
})
b.Add(&SyncFileItem{
Action: SyncFileActionDownload,
Status: SyncFileStatusCreate,
LocalFile: nil,
PanFile: &PanFileItem{
Category: "others",
Crc64Hash: "16173291050517323365",
CreatedAt: "2021-07-09 23:17:52",
DomainId: "bj29",
DriveId: "19519221",
FileExtension: "apk",
FileId: "60e868a0a005315fe2b149b4ade47b2df8bdccee",
FileName: "10000996@yunpan-release-1.apk",
FileSize: 61029089,
FileType: "file",
ParentFileId: "60f29e3ac420ae8c08e645db8b80b881e2a3633c",
Path: "/sync_drive/10000996@yunpan-release-1.apk",
Sha1Hash: "05A7597967C3C947D84564A7D55309DEA56ED985",
UpdatedAt: "2021-07-21 17:05:49",
UploadId: "rapid-9141d56d-21c3-4fcc-b41d-5df7bf47540f",
},
StatusUpdateTime: "",
})
b.Add(&SyncFileItem{
Action: SyncFileActionUpload,
Status: SyncFileStatusCreate,
LocalFile: &LocalFileItem{
FileName: "f1.txt",
FileSize: 1542,
FileType: "file",
CreatedAt: "2020-12-12 12:51:12",
UpdatedAt: "2020-12-12 12:51:12",
FileExtension: ".txt",
Sha1Hash: "",
Path: "D:\\smb\\feny\\goprojects\\dev\\fo\\f1.txt",
},
PanFile: nil,
StatusUpdateTime: "",
})
//b.AddUnique(&SyncFileItem{
// Action: SyncFileActionDownload,
// Status: SyncFileStatusCreate,
// LocalFile: nil,
// PanFile: &PanFileItem{
// Category: "others",
// Crc64Hash: "16173291050517323365",
// CreatedAt: "2021-07-09 23:17:52",
// DomainId: "bj29",
// DriveId: "19519221",
// FileExtension: "apk",
// FileId: "60e868a0a005315fe2b149b4ade47b2df8bdccee",
// FileName: "10000996@yunpan-release-1.apk",
// FileSize: 61029089,
// FileType: "file",
// ParentFileId: "60f29e3ac420ae8c08e645db8b80b881e2a3633c",
// Path: "/sync_drive/10000996@yunpan-release-1.apk",
// Sha1Hash: "05A7597967C3C947D84564A7D55309DEA56ED985",
// UpdatedAt: "2021-07-21 17:05:49",
// UploadId: "rapid-9141d56d-21c3-4fcc-b41d-5df7bf47540f",
// },
// StatusUpdateTime: "",
//})
//b.AddUnique(&SyncFileItem{
// Action: SyncFileActionDownload,
// Status: SyncFileStatusCreate,
// LocalFile: nil,
// PanFile: &PanFileItem{
// Category: "others",
// Crc64Hash: "16173291050517323365",
// CreatedAt: "2021-07-09 23:17:52",
// DomainId: "bj29",
// DriveId: "19519221",
// FileExtension: "apk",
// FileId: "60e868a0a005315fe2b149b4ade47b2df8bdccee",
// FileName: "10000996@yunpan-release-2.apk",
// FileSize: 61029089,
// FileType: "file",
// ParentFileId: "60f29e3ac420ae8c08e645db8b80b881e2a3633c",
// Path: "/sync_drive/10000996@yunpan-release-2.apk",
// Sha1Hash: "05A7597967C3C947D84564A7D55309DEA56ED985",
// UpdatedAt: "2021-07-21 17:05:49",
// UploadId: "rapid-9141d56d-21c3-4fcc-b41d-5df7bf47540f",
// },
// StatusUpdateTime: "",
//})
}
2022-06-28 15:10:29 +08:00
func TestSyncDbClear(t *testing.T) {
b := NewSyncFileDb("D:\\smb\\feny\\goprojects\\dev\\sync.bolt")
b.Open()
defer b.Close()
files, _ := b.GetFileList(SyncFileStatusSuccess)
for _, file := range files {
b.Delete(file.Id())
}
}