mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-23 08:56:29 +00:00
001b5639a6
instead of `if`s
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
// Copyright 2022 Woodpecker Authors
|
|
// Copyright 2019 mhmxs.
|
|
//
|
|
// 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
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// 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 pipeline
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
)
|
|
|
|
type mockUpdatePipelineStore struct{}
|
|
|
|
func (m *mockUpdatePipelineStore) UpdatePipeline(_ *model.Pipeline) error {
|
|
return nil
|
|
}
|
|
|
|
func TestUpdateToStatusRunning(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pipeline, _ := UpdateToStatusRunning(&mockUpdatePipelineStore{}, model.Pipeline{}, int64(1))
|
|
assert.Equal(t, model.StatusRunning, pipeline.Status)
|
|
assert.EqualValues(t, 1, pipeline.Started)
|
|
}
|
|
|
|
func TestUpdateToStatusPending(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().Unix()
|
|
|
|
pipeline, _ := UpdateToStatusPending(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
|
|
|
|
assert.Equal(t, model.StatusPending, pipeline.Status)
|
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
|
assert.LessOrEqual(t, now, pipeline.Reviewed)
|
|
}
|
|
|
|
func TestUpdateToStatusDeclined(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().Unix()
|
|
|
|
pipeline, _ := UpdateToStatusDeclined(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
|
|
|
|
assert.Equal(t, model.StatusDeclined, pipeline.Status)
|
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
|
assert.LessOrEqual(t, now, pipeline.Reviewed)
|
|
}
|
|
|
|
func TestUpdateToStatusToDone(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pipeline, _ := UpdateStatusToDone(&mockUpdatePipelineStore{}, model.Pipeline{}, "status", int64(1))
|
|
|
|
assert.Equal(t, model.StatusValue("status"), pipeline.Status)
|
|
assert.EqualValues(t, 1, pipeline.Finished)
|
|
}
|
|
|
|
func TestUpdateToStatusError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().Unix()
|
|
|
|
pipeline, _ := UpdateToStatusError(&mockUpdatePipelineStore{}, model.Pipeline{}, errors.New("this is an error"))
|
|
|
|
assert.Len(t, pipeline.Errors, 1)
|
|
assert.Equal(t, "[generic] this is an error", pipeline.Errors[0].Error())
|
|
assert.Equal(t, model.StatusError, pipeline.Status)
|
|
assert.Equal(t, pipeline.Started, pipeline.Finished)
|
|
assert.LessOrEqual(t, now, pipeline.Started)
|
|
assert.Equal(t, pipeline.Started, pipeline.Finished)
|
|
}
|
|
|
|
func TestUpdateToStatusKilled(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
now := time.Now().Unix()
|
|
|
|
pipeline, _ := UpdateToStatusKilled(&mockUpdatePipelineStore{}, model.Pipeline{})
|
|
|
|
assert.Equal(t, model.StatusKilled, pipeline.Status)
|
|
assert.LessOrEqual(t, now, pipeline.Finished)
|
|
}
|