first commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"runtime"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
|
||||
_ "server/routers"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
|
||||
beego.TestBeegoInit(apppath)
|
||||
}
|
||||
|
||||
|
||||
// TestBeego is a sample to run an endpoint test
|
||||
func TestBeego(t *testing.T) {
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
||||
|
||||
logs.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
|
||||
|
||||
Convey("Subject: Test Station Endpoint\n", t, func() {
|
||||
Convey("Status Code Should Be 200", func() {
|
||||
So(w.Code, ShouldEqual, 200)
|
||||
})
|
||||
Convey("The Result Should Not Be Empty", func() {
|
||||
So(w.Body.Len(), ShouldBeGreaterThan, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"server/models"
|
||||
)
|
||||
|
||||
func TestFileModel(t *testing.T) {
|
||||
// 测试创建文件信息
|
||||
file := &models.FileInfo{
|
||||
TenantID: "test-tenant-001",
|
||||
FileName: "test-file.txt",
|
||||
OriginalName: "original-test-file.txt",
|
||||
FilePath: "/uploads/test/test-file.txt",
|
||||
FileURL: "http://localhost:8080/uploads/test/test-file.txt",
|
||||
FileSize: 1024,
|
||||
FileType: "text/plain",
|
||||
FileExt: "txt",
|
||||
Category: "test",
|
||||
SubCategory: "unit-test",
|
||||
Status: 1,
|
||||
IsPublic: 0,
|
||||
UploadBy: "test-user",
|
||||
}
|
||||
|
||||
// 测试添加文件
|
||||
id, err := models.AddFile(file)
|
||||
if err != nil {
|
||||
t.Errorf("添加文件失败: %v", err)
|
||||
}
|
||||
t.Logf("文件添加成功,ID: %d", id)
|
||||
|
||||
// 测试根据ID获取文件
|
||||
retrievedFile, err := models.GetFileById(id)
|
||||
if err != nil {
|
||||
t.Errorf("获取文件失败: %v", err)
|
||||
}
|
||||
if retrievedFile.FileName != file.FileName {
|
||||
t.Errorf("文件名不匹配,期望: %s, 实际: %s", file.FileName, retrievedFile.FileName)
|
||||
}
|
||||
|
||||
// 测试更新文件
|
||||
retrievedFile.FileName = "updated-test-file.txt"
|
||||
err = models.UpdateFile(retrievedFile)
|
||||
if err != nil {
|
||||
t.Errorf("更新文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 测试根据租户获取文件
|
||||
files, err := models.GetFilesByTenant("test-tenant-001")
|
||||
if err != nil {
|
||||
t.Errorf("根据租户获取文件失败: %v", err)
|
||||
}
|
||||
if len(files) == 0 {
|
||||
t.Error("根据租户获取文件为空")
|
||||
}
|
||||
|
||||
// 测试根据分类获取文件
|
||||
filesByCategory, err := models.GetFilesByCategory("test")
|
||||
if err != nil {
|
||||
t.Errorf("根据分类获取文件失败: %v", err)
|
||||
}
|
||||
if len(filesByCategory) == 0 {
|
||||
t.Error("根据分类获取文件为空")
|
||||
}
|
||||
|
||||
// 测试搜索文件
|
||||
searchFiles, err := models.SearchFiles("test", "test-tenant-001")
|
||||
if err != nil {
|
||||
t.Errorf("搜索文件失败: %v", err)
|
||||
}
|
||||
if len(searchFiles) == 0 {
|
||||
t.Error("搜索文件结果为空")
|
||||
}
|
||||
|
||||
// 测试文件统计
|
||||
stats, err := models.GetFileStatistics("test-tenant-001")
|
||||
if err != nil {
|
||||
t.Errorf("获取文件统计失败: %v", err)
|
||||
}
|
||||
t.Logf("文件统计: %+v", stats)
|
||||
|
||||
// 测试软删除文件
|
||||
err = models.DeleteFile(id)
|
||||
if err != nil {
|
||||
t.Errorf("软删除文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证软删除后的状态
|
||||
deletedFile, err := models.GetFileById(id)
|
||||
if err != nil {
|
||||
t.Errorf("获取软删除后的文件失败: %v", err)
|
||||
}
|
||||
if deletedFile.Status != 0 {
|
||||
t.Errorf("软删除后文件状态应为0,实际为: %d", deletedFile.Status)
|
||||
}
|
||||
|
||||
// 测试硬删除文件
|
||||
err = models.HardDeleteFile(id)
|
||||
if err != nil {
|
||||
t.Errorf("硬删除文件失败: %v", err)
|
||||
}
|
||||
|
||||
t.Log("文件模型测试完成")
|
||||
}
|
||||
Reference in New Issue
Block a user