1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/server/store/datastore/repo_test.go

339 lines
8.2 KiB
Go
Raw Normal View History

2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package datastore
2015-09-30 03:21:17 +02:00
import (
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
2015-09-30 03:21:17 +02:00
)
func TestRepos(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Build))
defer closer()
2015-09-30 03:21:17 +02:00
g := goblin.Goblin(t)
g.Describe("Repo", func() {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
_, err := store.engine.Exec("DELETE FROM builds")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM users")
g.Assert(err).IsNil()
2015-09-30 03:21:17 +02:00
})
g.It("Should Set a Repo", func() {
repo := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
err1 := store.CreateRepo(&repo)
err2 := store.UpdateRepo(&repo)
getRepo, err3 := store.GetRepo(repo.ID)
2016-04-23 13:27:28 +02:00
g.Assert(err1).IsNil()
g.Assert(err2).IsNil()
g.Assert(err3).IsNil()
g.Assert(repo.ID).Equal(getRepo.ID)
2015-09-30 03:21:17 +02:00
})
g.It("Should Add a Repo", func() {
repo := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
err := store.CreateRepo(&repo)
g.Assert(err).IsNil()
2015-09-30 03:21:17 +02:00
g.Assert(repo.ID != 0).IsTrue()
})
g.It("Should Get a Repo by ID", func() {
repo := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
g.Assert(store.CreateRepo(&repo)).IsNil()
getrepo, err := store.GetRepo(repo.ID)
g.Assert(err).IsNil()
2015-09-30 03:21:17 +02:00
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
g.Assert(repo.Owner).Equal(getrepo.Owner)
g.Assert(repo.Name).Equal(getrepo.Name)
})
g.It("Should Get a Repo by Name", func() {
repo := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
g.Assert(store.CreateRepo(&repo)).IsNil()
getrepo, err := store.GetRepoName(repo.FullName)
g.Assert(err).IsNil()
2015-09-30 03:21:17 +02:00
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
g.Assert(repo.Owner).Equal(getrepo.Owner)
g.Assert(repo.Name).Equal(getrepo.Name)
})
g.It("Should Enforce Unique Repo Name", func() {
repo1 := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
repo2 := model.Repo{
2015-09-30 03:21:17 +02:00
UserID: 2,
FullName: "bradrydzewski/test",
2015-09-30 03:21:17 +02:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 03:21:17 +02:00
}
err1 := store.CreateRepo(&repo1)
err2 := store.CreateRepo(&repo2)
g.Assert(err1).IsNil()
g.Assert(err2 == nil).IsFalse()
2015-09-30 03:21:17 +02:00
})
})
}
2017-07-14 21:58:38 +02:00
2017-07-15 18:51:02 +02:00
func TestRepoList(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm))
defer closer()
2017-07-15 18:51:02 +02:00
user := &model.User{
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
assert.NoError(t, store.CreateUser(user))
2017-07-15 18:51:02 +02:00
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
2017-07-15 18:51:02 +02:00
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
2017-07-15 18:51:02 +02:00
}
repo3 := &model.Repo{
Owner: "octocat",
Name: "hello-world",
FullName: "octocat/hello-world",
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
2017-07-15 18:51:02 +02:00
assert.NoError(t, store.PermBatch([]*model.Perm{
2017-07-15 18:51:02 +02:00
{UserID: user.ID, Repo: repo1.FullName},
{UserID: user.ID, Repo: repo2.FullName},
}))
2017-07-15 18:51:02 +02:00
repos, err := store.RepoList(user, false)
if err != nil {
t.Error(err)
return
}
if got, want := len(repos), 2; got != want {
t.Errorf("Want %d repositories, got %d", want, got)
}
if got, want := repos[0].ID, repo1.ID; got != want {
t.Errorf("Want repository id %d, got %d", want, got)
}
if got, want := repos[1].ID, repo2.ID; got != want {
t.Errorf("Want repository id %d, got %d", want, got)
}
}
func TestOwnedRepoList(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm))
defer closer()
user := &model.User{
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
assert.NoError(t, store.CreateUser(user))
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
}
repo3 := &model.Repo{
Owner: "octocat",
Name: "hello-world",
FullName: "octocat/hello-world",
}
repo4 := &model.Repo{
Owner: "demo",
Name: "demo",
FullName: "demo/demo",
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
assert.NoError(t, store.CreateRepo(repo4))
assert.NoError(t, store.PermBatch([]*model.Perm{
{UserID: user.ID, Repo: repo1.FullName, Push: true, Admin: false},
{UserID: user.ID, Repo: repo2.FullName, Push: false, Admin: true},
{UserID: user.ID, Repo: repo3.FullName},
{UserID: user.ID, Repo: repo4.FullName},
}))
repos, err := store.RepoList(user, true)
2017-07-15 18:51:02 +02:00
if err != nil {
t.Error(err)
return
}
if got, want := len(repos), 2; got != want {
t.Errorf("Want %d repositories, got %d", want, got)
}
if got, want := repos[0].ID, repo1.ID; got != want {
t.Errorf("Want repository id %d, got %d", want, got)
}
if got, want := repos[1].ID, repo2.ID; got != want {
t.Errorf("Want repository id %d, got %d", want, got)
}
}
func TestRepoCount(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo))
defer closer()
2017-07-15 18:51:02 +02:00
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
2017-07-25 21:32:22 +02:00
IsActive: true,
2017-07-15 18:51:02 +02:00
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
2017-07-25 21:32:22 +02:00
IsActive: true,
}
repo3 := &model.Repo{
Owner: "test",
Name: "test-ui",
FullName: "test/test-ui",
2017-07-25 21:32:22 +02:00
IsActive: false,
2017-07-15 18:51:02 +02:00
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
2017-07-15 18:51:02 +02:00
count, _ := store.GetRepoCount()
if got, want := count, int64(2); got != want {
2017-07-15 18:51:02 +02:00
t.Errorf("Want %d repositories, got %d", want, got)
}
}
2017-07-14 21:58:38 +02:00
func TestRepoBatch(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm))
defer closer()
if !assert.NoError(t, store.CreateRepo(&model.Repo{
2017-07-14 21:58:38 +02:00
UserID: 1,
FullName: "foo/bar",
Owner: "foo",
Name: "bar",
2017-07-25 21:32:22 +02:00
IsActive: true,
})) {
2017-07-14 21:58:38 +02:00
return
}
if !assert.NoError(t, store.RepoBatch(
2017-07-14 21:58:38 +02:00
[]*model.Repo{
{
UserID: 1,
FullName: "foo/bar",
Owner: "foo",
Name: "bar",
2017-07-25 21:32:22 +02:00
IsActive: true,
2017-07-14 21:58:38 +02:00
},
{
UserID: 1,
FullName: "bar/baz",
Owner: "bar",
Name: "baz",
2017-07-25 21:32:22 +02:00
IsActive: true,
2017-07-14 21:58:38 +02:00
},
{
UserID: 1,
FullName: "baz/qux",
Owner: "baz",
Name: "qux",
2017-07-25 21:32:22 +02:00
IsActive: true,
2017-07-14 21:58:38 +02:00
},
},
)) {
2017-07-14 21:58:38 +02:00
return
}
count, err := store.GetRepoCount()
assert.NoError(t, err)
assert.EqualValues(t, 3, count)
2017-07-14 21:58:38 +02:00
}
2017-07-15 18:51:02 +02:00
func TestRepoCrud(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm))
defer closer()
2017-07-15 18:51:02 +02:00
repo := model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2017-07-15 18:51:02 +02:00
Owner: "bradrydzewski",
Name: "test",
2017-07-15 18:51:02 +02:00
}
assert.NoError(t, store.CreateRepo(&repo))
_, err1 := store.GetRepo(repo.ID)
err2 := store.DeleteRepo(&repo)
_, err3 := store.GetRepo(repo.ID)
2017-07-15 18:51:02 +02:00
if err1 != nil {
t.Errorf("Unexpected error: select repository: %s", err1)
}
if err2 != nil {
t.Errorf("Unexpected error: delete repository: %s", err2)
}
if err3 == nil {
t.Errorf("Expected error: sql.ErrNoRows")
}
}