2021-12-31 19:52:00 -07:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-12-31 19:52:00 -07:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckUnadoptedRepositories_Add(t *testing.T) {
|
|
|
|
start := 10
|
|
|
|
end := 20
|
2022-10-31 17:16:48 -06:00
|
|
|
unadopted := &unadoptedRepositories{
|
2021-12-31 19:52:00 -07:00
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
total := 30
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
unadopted.add("something")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, total, unadopted.index)
|
|
|
|
assert.Equal(t, end-start, len(unadopted.repositories))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckUnadoptedRepositories(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
//
|
|
|
|
// Non existent user
|
|
|
|
//
|
2022-10-31 17:16:48 -06:00
|
|
|
unadopted := &unadoptedRepositories{start: 0, end: 100}
|
2021-12-31 19:52:00 -07:00
|
|
|
err := checkUnadoptedRepositories("notauser", []string{"repo"}, unadopted)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(unadopted.repositories))
|
|
|
|
//
|
|
|
|
// Unadopted repository is returned
|
|
|
|
// Existing (adopted) repository is not returned
|
|
|
|
//
|
|
|
|
userName := "user2"
|
|
|
|
repoName := "repo2"
|
|
|
|
unadoptedRepoName := "unadopted"
|
2022-10-31 17:16:48 -06:00
|
|
|
unadopted = &unadoptedRepositories{start: 0, end: 100}
|
2021-12-31 19:52:00 -07:00
|
|
|
err = checkUnadoptedRepositories(userName, []string{repoName, unadoptedRepoName}, unadopted)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{path.Join(userName, unadoptedRepoName)}, unadopted.repositories)
|
|
|
|
//
|
|
|
|
// Existing (adopted) repository is not returned
|
|
|
|
//
|
2022-10-31 17:16:48 -06:00
|
|
|
unadopted = &unadoptedRepositories{start: 0, end: 100}
|
2021-12-31 19:52:00 -07:00
|
|
|
err = checkUnadoptedRepositories(userName, []string{repoName}, unadopted)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(unadopted.repositories))
|
|
|
|
assert.Equal(t, 0, unadopted.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListUnadoptedRepositories_ListOptions(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
username := "user2"
|
|
|
|
unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")}
|
|
|
|
for _, unadopted := range unadoptedList {
|
2022-01-20 10:46:10 -07:00
|
|
|
_ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0o755)
|
2021-12-31 19:52:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := db.ListOptions{Page: 1, PageSize: 1}
|
|
|
|
repoNames, count, err := ListUnadoptedRepositories("", &opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, count)
|
|
|
|
assert.Equal(t, unadoptedList[0], repoNames[0])
|
|
|
|
|
|
|
|
opts = db.ListOptions{Page: 2, PageSize: 1}
|
|
|
|
repoNames, count, err = ListUnadoptedRepositories("", &opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 2, count)
|
|
|
|
assert.Equal(t, unadoptedList[1], repoNames[0])
|
|
|
|
}
|