improve test

This commit is contained in:
wxiaoguang 2024-11-07 17:42:46 +08:00
parent 5e959b086c
commit 15f73714d4
2 changed files with 20 additions and 21 deletions

View File

@ -285,12 +285,9 @@ func GetPackageDBFile(ctx context.Context, group, arch string, ownerID int64, si
fileName = fmt.Sprintf("%s.db.sig", arch)
}
file, err := packages_model.GetFileForVersionByName(ctx, pv.ID, fileName, group)
// failback to any db
if errors.Is(err, util.ErrNotExist) && arch != "any" {
fileName = "any.db"
if signFile {
fileName = "any.db.sig"
}
// fail back to any db
fileName = util.Iif(signFile, "any.db.sig", "any.db")
file, err = packages_model.GetFileForVersionByName(ctx, pv.ID, fileName, group)
}
if err != nil {

View File

@ -34,14 +34,15 @@ import (
func TestPackageArch(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
unPack := func(s string) []byte {
unpack := func(s string) []byte {
data, _ := base64.StdEncoding.DecodeString(strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(s), "\n", ""), "\r", ""))
return data
}
rootURL := fmt.Sprintf("/api/packages/%s/arch", user.Name)
pkgs := map[string][]byte{
"any": unPack(`
// pkgname = test, arch = any
"any": unpack(`
KLUv/QBYXRMABmOHSbCWag6dY6d8VNtVR3rpBnWdBbkDAxM38Dj3XG3FK01TCKlWtMV9QpskYdsm
e6fh5gWqM8edeurYNESoIUz/RmtyQy68HVrBj1p+AIoAYABFSJh4jcDyWNQgHIKIuNgIll64S4oY
FFIUk6vJQBMIIl2iYtIysqKWVYMCYvXDpAKTMzVGwZTUWhbciFCglIMH1QMbEtjHpohSi8XRYwPr
@ -55,7 +56,7 @@ JtFpalPD30drDWi7Bl8NB3J/P3csijQyldWZ8gy3TNslLsozMw74DhoAXoAfnE8xydUUHPZ3hML4
2zVDGiEXSGYRx4BKQDcDJA5S9Ca25FRgPtSWSowZJpJTYAR9WCPHUDgACm6+hBecGDPNClpwHZ2A
EQ==
`),
"x86_64": unPack(`
"x86_64": unpack(`
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T03RIB8NweEwVx71tDviIFA60O75Rtc5s+9YbxteUHzhUWi
HBkWDcbGcUqCukrLGi4Lv8jIqNsbXhueXW8uzTe79Lr9/TVbnl69c3wR652f21+7rnU5kmjTc/38
@ -336,10 +338,11 @@ HMhNSS1IzUsBcpJAPFAwwUXSM0u4BjoaR8EoGAWjgGQAAILFeyQADAAA
MakeRequest(t, req, http.StatusNoContent)
}
})
t.Run("Package Arch Test", func(t *testing.T) {
t.Run("List Meta.Arch[any]", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgs["any"])).
AddBasicAuth(user.Name)
req := NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgs["any"])).AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusCreated)
req = NewRequest(t, "GET", rootURL+"/x86_64/base.db")
@ -347,18 +350,17 @@ HMhNSS1IzUsBcpJAPFAwwUXSM0u4BjoaR8EoGAWjgGQAAILFeyQADAAA
files, err := listTarGzFiles(respPkg.Body.Bytes())
require.NoError(t, err)
require.Len(t, files, 1)
require.Len(t, files, 1) // only one package: "test"
req = NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgs["otherXZ"])).
AddBasicAuth(user.Name)
req = NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgs["otherXZ"])).AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusCreated)
req = NewRequest(t, "GET", rootURL+"/x86_64/base.db")
respPkg = MakeRequest(t, req, http.StatusOK)
resp := MakeRequest(t, req, http.StatusOK)
files, err = listTarGzFiles(respPkg.Body.Bytes())
files, err = listTarGzFiles(resp.Body.Bytes())
require.NoError(t, err)
require.Len(t, files, 2)
require.Len(t, files, 2) // now there are 2 packages: "test" and "test2"
})
}