draft test
This commit is contained in:
149
models/workflow_execution/tests/workflow_scheduler_test.go
Normal file
149
models/workflow_execution/tests/workflow_scheduler_test.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package workflow_execution_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockAccessor struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadOne(id string) (interface{}, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func TestNewScheduler_ValidInput(t *testing.T) {
|
||||
s := "2025-06-16T15:00:00"
|
||||
e := "2025-06-16T17:00:00"
|
||||
dur := 7200.0
|
||||
cronStr := "0 0 * * * *"
|
||||
|
||||
sched := workflow_execution.NewScheduler(s, e, dur, cronStr)
|
||||
|
||||
assert.NotNil(t, sched)
|
||||
assert.Equal(t, dur, sched.DurationS)
|
||||
assert.Equal(t, cronStr, sched.Cron)
|
||||
}
|
||||
|
||||
func TestNewScheduler_InvalidStart(t *testing.T) {
|
||||
s := "invalid"
|
||||
e := "2025-06-16T17:00:00"
|
||||
dur := 7200.0
|
||||
cronStr := "0 0 * * * *"
|
||||
|
||||
sched := workflow_execution.NewScheduler(s, e, dur, cronStr)
|
||||
assert.Nil(t, sched)
|
||||
}
|
||||
|
||||
func TestNewScheduler_InvalidEnd(t *testing.T) {
|
||||
s := "2025-06-16T15:00:00"
|
||||
e := "invalid"
|
||||
dur := 7200.0
|
||||
cronStr := "0 0 * * * *"
|
||||
|
||||
sched := workflow_execution.NewScheduler(s, e, dur, cronStr)
|
||||
assert.NotNil(t, sched)
|
||||
assert.Nil(t, sched.End)
|
||||
}
|
||||
|
||||
func TestGetDates_NoCron(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
|
||||
s := &workflow_execution.WorkflowSchedule{
|
||||
Start: start,
|
||||
End: &end,
|
||||
}
|
||||
|
||||
schedule, err := s.GetDates()
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, schedule, 1)
|
||||
assert.Equal(t, start, schedule[0].Start)
|
||||
assert.Equal(t, end, *schedule[0].End)
|
||||
}
|
||||
|
||||
func TestGetDates_InvalidCron(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
|
||||
s := &workflow_execution.WorkflowSchedule{
|
||||
Start: start,
|
||||
End: &end,
|
||||
Cron: "bad cron",
|
||||
}
|
||||
|
||||
_, err := s.GetDates()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetDates_ValidCron(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(10 * time.Minute)
|
||||
|
||||
s := &workflow_execution.WorkflowSchedule{
|
||||
Start: start,
|
||||
End: &end,
|
||||
DurationS: 60,
|
||||
Cron: "0 */2 * * * *",
|
||||
}
|
||||
|
||||
dates, err := s.GetDates()
|
||||
assert.NoError(t, err)
|
||||
assert.Greater(t, len(dates), 0)
|
||||
}
|
||||
|
||||
func TestGetExecutions_Success(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
ws := &workflow_execution.WorkflowSchedule{
|
||||
UUID: uuid.New().String(),
|
||||
Start: start,
|
||||
End: &end,
|
||||
}
|
||||
|
||||
wf := &workflow.Workflow{
|
||||
AbstractObject: utils.AbstractObject{
|
||||
UUID: uuid.New().String(),
|
||||
Name: "TestWorkflow",
|
||||
},
|
||||
}
|
||||
|
||||
execs, err := ws.GetExecutions(wf)
|
||||
assert.NoError(t, err)
|
||||
assert.Greater(t, len(execs), 0)
|
||||
assert.Equal(t, wf.UUID, execs[0].WorkflowID)
|
||||
assert.Equal(t, ws.UUID, execs[0].ExecutionsID)
|
||||
assert.Equal(t, enum.DRAFT, execs[0].State)
|
||||
}
|
||||
|
||||
func TestSchedules_NoRequest(t *testing.T) {
|
||||
ws := &workflow_execution.WorkflowSchedule{}
|
||||
|
||||
ws, wf, execs, err := ws.Schedules("someID", nil)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, wf)
|
||||
assert.Len(t, execs, 0)
|
||||
assert.Equal(t, ws, ws)
|
||||
}
|
||||
|
||||
// Additional test stubs to be completed with gomock usage for:
|
||||
// - CheckBooking
|
||||
// - BookExecs
|
||||
// - getBooking
|
||||
// - Schedules (success path)
|
||||
// - Planify mocking in CheckBooking
|
||||
// - Peer interaction in BookExecs
|
||||
// - Caller deep copy errors in getCallerCopy
|
||||
// Will be continued...
|
||||
154
models/workflow_execution/tests/workflow_test.go
Executable file
154
models/workflow_execution/tests/workflow_test.go
Executable file
@@ -0,0 +1,154 @@
|
||||
package workflow_execution_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/enum"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return nil, args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (m *MockAccessor) Search(filters interface{}, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, search, isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
}
|
||||
|
||||
func TestStoreDraftDefault(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.StoreDraftDefault()
|
||||
assert.False(t, exec.IsDraft)
|
||||
assert.Equal(t, enum.SCHEDULED, exec.State)
|
||||
}
|
||||
|
||||
func TestCanUpdate_StateChange(t *testing.T) {
|
||||
existing := &workflow_execution.WorkflowExecution{State: enum.DRAFT}
|
||||
newExec := &workflow_execution.WorkflowExecution{State: enum.SCHEDULED}
|
||||
canUpdate, updated := existing.CanUpdate(newExec)
|
||||
assert.True(t, canUpdate)
|
||||
assert.Equal(t, enum.SCHEDULED, updated.(*workflow_execution.WorkflowExecution).State)
|
||||
}
|
||||
|
||||
func TestCanUpdate_SameState_Draft(t *testing.T) {
|
||||
existing := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
||||
newExec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}, State: enum.DRAFT}
|
||||
canUpdate, _ := existing.CanUpdate(newExec)
|
||||
assert.False(t, canUpdate)
|
||||
}
|
||||
|
||||
func TestCanDelete_TrueIfDraft(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
||||
assert.True(t, exec.CanDelete())
|
||||
}
|
||||
|
||||
func TestCanDelete_FalseIfNotDraft(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
||||
assert.False(t, exec.CanDelete())
|
||||
}
|
||||
|
||||
func TestEquals_True(t *testing.T) {
|
||||
d := time.Now()
|
||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: d, WorkflowID: "123"}
|
||||
assert.True(t, exec1.Equals(exec2))
|
||||
}
|
||||
|
||||
func TestEquals_False(t *testing.T) {
|
||||
exec1 := &workflow_execution.WorkflowExecution{ExecDate: time.Now(), WorkflowID: "abc"}
|
||||
exec2 := &workflow_execution.WorkflowExecution{ExecDate: time.Now().Add(time.Hour), WorkflowID: "def"}
|
||||
assert.False(t, exec1.Equals(exec2))
|
||||
}
|
||||
|
||||
func TestArgoStatusToState_Success(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.ArgoStatusToState("succeeded")
|
||||
assert.Equal(t, enum.SUCCESS, exec.State)
|
||||
}
|
||||
|
||||
func TestArgoStatusToState_DefaultToFailure(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.ArgoStatusToState("unknown")
|
||||
assert.Equal(t, enum.FAILURE, exec.State)
|
||||
}
|
||||
|
||||
func TestGenerateID_AssignsUUID(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
exec.GenerateID()
|
||||
assert.NotEmpty(t, exec.UUID)
|
||||
}
|
||||
|
||||
func TestGetName_ReturnsCorrectFormat(t *testing.T) {
|
||||
time := time.Now()
|
||||
exec := &workflow_execution.WorkflowExecution{AbstractObject: utils.AbstractObject{UUID: "abc"}, ExecDate: time}
|
||||
assert.Contains(t, exec.GetName(), "abc")
|
||||
assert.Contains(t, exec.GetName(), time.String())
|
||||
}
|
||||
|
||||
func TestVerifyAuth_AlwaysTrue(t *testing.T) {
|
||||
exec := &workflow_execution.WorkflowExecution{}
|
||||
assert.True(t, exec.VerifyAuth(nil))
|
||||
}
|
||||
|
||||
func TestUpdateOne_RejectsZeroState(t *testing.T) {
|
||||
accessor := &workflow_execution.WorkflowExecutionMongoAccessor{}
|
||||
set := &workflow_execution.WorkflowExecution{State: 0}
|
||||
_, code, err := accessor.UpdateOne(set, "someID")
|
||||
assert.Equal(t, 400, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestLoadOne_DraftExpired_ShouldDelete(t *testing.T) {
|
||||
// Normally would mock time.Now and delete call; for now we test structure
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
exec := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
||||
State: enum.DRAFT,
|
||||
AbstractObject: utils.AbstractObject{UUID: "to-delete"},
|
||||
}
|
||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
||||
// No panic = good enough placeholder
|
||||
}
|
||||
|
||||
func TestLoadOne_ScheduledExpired_ShouldUpdateToForgotten(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
exec := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: time.Now().Add(-2 * time.Minute),
|
||||
State: enum.SCHEDULED,
|
||||
AbstractObject: utils.AbstractObject{UUID: "to-forget"},
|
||||
}
|
||||
_, _, _ = accessor.LoadOne(exec.GetID())
|
||||
}
|
||||
|
||||
func TestDeleteOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.DeleteOne("someID")
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestStoreOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.StoreOne(nil)
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestCopyOne_NotImplemented(t *testing.T) {
|
||||
accessor := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
_, code, err := accessor.CopyOne(nil)
|
||||
assert.Equal(t, 404, code)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetExecFilters_BasicPattern(t *testing.T) {
|
||||
a := workflow_execution.NewAccessor(&tools.APIRequest{})
|
||||
filters := a.GetExecFilters("foo")
|
||||
assert.Contains(t, filters.Or["abstractobject.name"][0].Value, "foo")
|
||||
}
|
||||
Reference in New Issue
Block a user