ALR

Форк
1
/
find_test.go 
148 строк · 3.5 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package repos_test
20

21
import (
22
	"context"
23
	"reflect"
24
	"strings"
25
	"testing"
26

27
	"plemya-x.ru/alr/internal/db"
28
	"plemya-x.ru/alr/internal/types"
29
	"plemya-x.ru/alr/pkg/repos"
30
)
31

32
func TestFindPkgs(t *testing.T) {
33
	_, err := db.Open(":memory:")
34
	if err != nil {
35
		t.Fatalf("Expected no error, got %s", err)
36
	}
37
	defer db.Close()
38

39
	setCfgDirs(t)
40
	defer removeCacheDir(t)
41

42
	ctx := context.Background()
43

44
	err = repos.Pull(ctx, []types.Repo{
45
		{
46
			Name: "default",
47
			URL:  "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
48
		},
49
	})
50
	if err != nil {
51
		t.Fatalf("Expected no error, got %s", err)
52
	}
53

54
	found, notFound, err := repos.FindPkgs([]string{"itd", "nonexistentpackage1", "nonexistentpackage2"})
55
	if err != nil {
56
		t.Fatalf("Expected no error, got %s", err)
57
	}
58

59
	if !reflect.DeepEqual(notFound, []string{"nonexistentpackage1", "nonexistentpackage2"}) {
60
		t.Errorf("Expected 'nonexistentpackage{1,2} not to be found")
61
	}
62

63
	if len(found) != 1 {
64
		t.Errorf("Expected 1 package found, got %d", len(found))
65
	}
66

67
	itdPkgs, ok := found["itd"]
68
	if !ok {
69
		t.Fatalf("Expected 'itd' packages to be found")
70
	}
71

72
	if len(itdPkgs) < 2 {
73
		t.Errorf("Expected two 'itd' packages to be found")
74
	}
75

76
	for i, pkg := range itdPkgs {
77
		if !strings.HasPrefix(pkg.Name, "itd") {
78
			t.Errorf("Expected package name of all found packages to start with 'itd', got %s on element %d", pkg.Name, i)
79
		}
80
	}
81
}
82

83
func TestFindPkgsEmpty(t *testing.T) {
84
	_, err := db.Open(":memory:")
85
	if err != nil {
86
		t.Fatalf("Expected no error, got %s", err)
87
	}
88
	defer db.Close()
89

90
	setCfgDirs(t)
91
	defer removeCacheDir(t)
92

93
	err = db.InsertPackage(db.Package{
94
		Name:       "test1",
95
		Repository: "default",
96
		Version:    "0.0.1",
97
		Release:    1,
98
		Description: db.NewJSON(map[string]string{
99
			"en": "Test package 1",
100
			"ru": "Проверочный пакет 1",
101
		}),
102
		Provides: db.NewJSON([]string{""}),
103
	})
104
	if err != nil {
105
		t.Fatalf("Expected no error, got %s", err)
106
	}
107

108
	err = db.InsertPackage(db.Package{
109
		Name:       "test2",
110
		Repository: "default",
111
		Version:    "0.0.1",
112
		Release:    1,
113
		Description: db.NewJSON(map[string]string{
114
			"en": "Test package 2",
115
			"ru": "Проверочный пакет 2",
116
		}),
117
		Provides: db.NewJSON([]string{"test"}),
118
	})
119
	if err != nil {
120
		t.Fatalf("Expected no error, got %s", err)
121
	}
122

123
	found, notFound, err := repos.FindPkgs([]string{"test", ""})
124
	if err != nil {
125
		t.Fatalf("Expected no error, got %s", err)
126
	}
127

128
	if len(notFound) != 0 {
129
		t.Errorf("Expected all packages to be found")
130
	}
131

132
	if len(found) != 1 {
133
		t.Errorf("Expected 1 package found, got %d", len(found))
134
	}
135

136
	testPkgs, ok := found["test"]
137
	if !ok {
138
		t.Fatalf("Expected 'test' packages to be found")
139
	}
140

141
	if len(testPkgs) != 1 {
142
		t.Errorf("Expected one 'test' package to be found, got %d", len(testPkgs))
143
	}
144

145
	if testPkgs[0].Name != "test2" {
146
		t.Errorf("Expected 'test2' package, got '%s'", testPkgs[0].Name)
147
	}
148
}
149

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.