ALR/pkg/repos/find_test.go

148 lines
3.6 KiB
Go
Raw Permalink Normal View History

2025-01-18 16:30:02 +00:00
// This file was originally part of the project "LURE - Linux User REpository", created by Elara Musayelyan.
// It has been modified as part of "ALR - Any Linux Repository" by Евгений Храмов.
//
// ALR - Any Linux Repository
// Copyright (C) 2025 Евгений Храмов
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2024-01-22 10:36:06 +00:00
package repos_test
import (
"reflect"
"strings"
"testing"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/types"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/repos"
2024-01-22 10:36:06 +00:00
)
func TestFindPkgs(t *testing.T) {
2025-01-14 10:18:51 +00:00
e := prepare(t)
defer cleanup(t, e)
2024-01-22 10:36:06 +00:00
2025-01-14 10:18:51 +00:00
rs := repos.New(
e.Cfg,
e.Db,
)
2024-01-22 10:36:06 +00:00
2025-01-14 10:18:51 +00:00
err := rs.Pull(e.Ctx, []types.Repo{
2024-01-22 10:36:06 +00:00
{
Name: "default",
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
2024-01-22 10:36:06 +00:00
},
})
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 10:18:51 +00:00
found, notFound, err := rs.FindPkgs(
e.Ctx,
[]string{"alr", "nonexistentpackage1", "nonexistentpackage2"},
)
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
if !reflect.DeepEqual(notFound, []string{"nonexistentpackage1", "nonexistentpackage2"}) {
t.Errorf("Expected 'nonexistentpackage{1,2} not to be found")
}
if len(found) != 1 {
t.Errorf("Expected 1 package found, got %d", len(found))
}
2025-01-14 10:18:51 +00:00
alrPkgs, ok := found["alr"]
2024-01-22 10:36:06 +00:00
if !ok {
2025-01-14 10:18:51 +00:00
t.Fatalf("Expected 'alr' packages to be found")
2024-01-22 10:36:06 +00:00
}
2025-01-14 10:18:51 +00:00
if len(alrPkgs) < 2 {
t.Errorf("Expected two 'alr' packages to be found")
2024-01-22 10:36:06 +00:00
}
2025-01-14 10:18:51 +00:00
for i, pkg := range alrPkgs {
if !strings.HasPrefix(pkg.Name, "alr") {
t.Errorf("Expected package name of all found packages to start with 'alr', got %s on element %d", pkg.Name, i)
2024-01-22 10:36:06 +00:00
}
}
}
func TestFindPkgsEmpty(t *testing.T) {
2025-01-14 10:18:51 +00:00
e := prepare(t)
defer cleanup(t, e)
2024-01-22 10:36:06 +00:00
2025-01-14 10:18:51 +00:00
rs := repos.New(
e.Cfg,
e.Db,
)
2024-01-22 10:36:06 +00:00
2025-01-14 10:18:51 +00:00
err := e.Db.InsertPackage(e.Ctx, db.Package{
2024-01-22 10:36:06 +00:00
Name: "test1",
Repository: "default",
Version: "0.0.1",
Release: 1,
Description: db.NewJSON(map[string]string{
"en": "Test package 1",
"ru": "Проверочный пакет 1",
}),
Provides: db.NewJSON([]string{""}),
})
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 10:18:51 +00:00
err = e.Db.InsertPackage(e.Ctx, db.Package{
2024-01-22 10:36:06 +00:00
Name: "test2",
Repository: "default",
Version: "0.0.1",
Release: 1,
Description: db.NewJSON(map[string]string{
"en": "Test package 2",
"ru": "Проверочный пакет 2",
}),
Provides: db.NewJSON([]string{"test"}),
})
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2025-01-14 10:18:51 +00:00
found, notFound, err := rs.FindPkgs(e.Ctx, []string{"test", ""})
2024-01-22 10:36:06 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
if len(notFound) != 0 {
t.Errorf("Expected all packages to be found")
}
if len(found) != 1 {
t.Errorf("Expected 1 package found, got %d", len(found))
}
testPkgs, ok := found["test"]
if !ok {
t.Fatalf("Expected 'test' packages to be found")
}
if len(testPkgs) != 1 {
t.Errorf("Expected one 'test' package to be found, got %d", len(testPkgs))
}
if testPkgs[0].Name != "test2" {
t.Errorf("Expected 'test2' package, got '%s'", testPkgs[0].Name)
}
}