woodpecker/store/datastore/config_test.go

156 lines
3.3 KiB
Go
Raw Normal View History

2017-05-05 16:59:37 +00:00
package datastore
import (
"testing"
"github.com/drone/drone/model"
)
func TestConfig(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from config")
s.Close()
}()
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
)
2017-05-05 18:05:42 +00:00
if err := s.ConfigCreate(
2017-05-05 16:59:37 +00:00
&model.Config{
2017-05-05 18:05:42 +00:00
RepoID: 2,
Data: data,
Hash: hash,
2017-05-05 16:59:37 +00:00
},
); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
config, err := s.ConfigFind(&model.Repo{ID: 2}, hash)
if err != nil {
t.Error(err)
return
}
if got, want := config.ID, int64(1); got != want {
t.Errorf("Want config id %d, got %d", want, got)
}
if got, want := config.RepoID, int64(2); got != want {
t.Errorf("Want config repo id %d, got %d", want, got)
}
if got, want := config.Data, data; got != want {
t.Errorf("Want config data %s, got %s", want, got)
}
if got, want := config.Hash, hash; got != want {
t.Errorf("Want config hash %s, got %s", want, got)
}
2017-05-05 18:05:42 +00:00
loaded, err := s.ConfigLoad(config.ID)
2017-05-05 16:59:37 +00:00
if err != nil {
2017-05-05 18:05:42 +00:00
t.Errorf("Want config by id, got error %q", err)
2017-05-05 16:59:37 +00:00
return
}
2017-05-05 18:05:42 +00:00
if got, want := loaded.ID, config.ID; got != want {
t.Errorf("Want config by id %d, got %d", want, got)
}
}
2017-05-05 16:59:37 +00:00
2017-05-05 18:05:42 +00:00
func TestConfigApproved(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from config")
s.Exec("delete from builds")
2017-05-23 15:53:40 +00:00
s.Exec("delete from repos")
2017-05-05 18:05:42 +00:00
s.Close()
}()
2017-05-23 15:53:40 +00:00
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/drone",
Owner: "bradrydzewski",
Name: "drone",
}
s.CreateRepo(repo)
2017-05-05 18:05:42 +00:00
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
conf = &model.Config{
2017-05-23 15:53:40 +00:00
RepoID: repo.ID,
2017-05-05 18:05:42 +00:00
Data: data,
Hash: hash,
}
)
if err := s.ConfigCreate(conf); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
2017-05-05 16:59:37 +00:00
return
}
2017-05-05 18:05:42 +00:00
s.CreateBuild(&model.Build{
2017-05-23 15:53:40 +00:00
RepoID: repo.ID,
2017-05-05 18:05:42 +00:00
ConfigID: conf.ID,
Status: model.StatusBlocked,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
})
s.CreateBuild(&model.Build{
2017-05-23 15:53:40 +00:00
RepoID: repo.ID,
2017-05-05 18:05:42 +00:00
ConfigID: conf.ID,
Status: model.StatusPending,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
})
if ok, _ := s.ConfigFindApproved(conf); ok == true {
t.Errorf("Want config not approved, when blocked or pending")
return
}
s.CreateBuild(&model.Build{
2017-05-23 15:53:40 +00:00
RepoID: repo.ID,
2017-05-05 18:05:42 +00:00
ConfigID: conf.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
})
if ok, _ := s.ConfigFindApproved(conf); ok == false {
t.Errorf("Want config approved, when running.")
return
2017-05-05 16:59:37 +00:00
}
}
2017-05-05 18:05:42 +00:00
func TestConfigIndexes(t *testing.T) {
s := newTest()
defer func() {
s.Exec("delete from config")
s.Close()
}()
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
)
if err := s.ConfigCreate(
&model.Config{
RepoID: 2,
Data: data,
Hash: hash,
},
); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
// fail due to duplicate sha
if err := s.ConfigCreate(
&model.Config{
RepoID: 2,
Data: data,
Hash: hash,
},
); err == nil {
t.Errorf("Unexpected error: dupliate sha")
}
}