Add missing workflow store tests
This commit is contained in:
@@ -20,7 +20,7 @@ func (s Store) convertAutomationSessionFilter(f types.SessionFilter) (query squi
|
||||
}
|
||||
|
||||
if len(f.WorkflowID) > 0 {
|
||||
query = query.Where(squirrel.Eq{"atms.id": f.WorkflowID})
|
||||
query = query.Where(squirrel.Eq{"atms.rel_workflow": f.WorkflowID})
|
||||
}
|
||||
|
||||
if len(f.EventType) > 0 {
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/automation/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza-server/pkg/id"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func testAutomationSessions(t *testing.T, s store.AutomationSessions) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
req = require.New(t)
|
||||
|
||||
makeNew = func(wfID uint64, completed bool) *types.Session {
|
||||
ses := &types.Session{
|
||||
ID: id.Next(),
|
||||
WorkflowID: wfID,
|
||||
CreatedAt: *now(),
|
||||
}
|
||||
if completed {
|
||||
ses.CompletedAt = now()
|
||||
}
|
||||
return ses
|
||||
}
|
||||
|
||||
truncAndCreate = func(t *testing.T) (*require.Assertions, *types.Session) {
|
||||
req := require.New(t)
|
||||
req.NoError(s.TruncateAutomationSessions(ctx))
|
||||
res := makeNew(0, false)
|
||||
req.NoError(s.CreateAutomationSession(ctx, res))
|
||||
return req, res
|
||||
}
|
||||
)
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
req := require.New(t)
|
||||
wf := &types.Session{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
}
|
||||
req.NoError(s.CreateAutomationSession(ctx, wf))
|
||||
})
|
||||
|
||||
t.Run("lookup by ID", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
|
||||
fetched, err := s.LookupAutomationSessionByID(ctx, wf.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(wf.ID, fetched.ID)
|
||||
req.NotNil(fetched.CreatedAt)
|
||||
})
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
wf.Status = types.SessionCompleted
|
||||
req.NoError(s.UpdateAutomationSession(ctx, wf))
|
||||
fetched, err := s.LookupAutomationSessionByID(ctx, wf.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(wf.ID, fetched.ID)
|
||||
req.Equal(types.SessionCompleted, fetched.Status)
|
||||
})
|
||||
|
||||
t.Run("delete", func(t *testing.T) {
|
||||
t.Run("by Session", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationSession(ctx, wf))
|
||||
_, err := s.LookupAutomationSessionByID(ctx, wf.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
|
||||
t.Run("by ID", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationSessionByID(ctx, wf.ID))
|
||||
_, err := s.LookupAutomationSessionByID(ctx, wf.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
prefill := []*types.Session{
|
||||
makeNew(1001, false),
|
||||
makeNew(1001, false),
|
||||
makeNew(1001, false),
|
||||
makeNew(1001, false),
|
||||
makeNew(1001, true),
|
||||
}
|
||||
|
||||
count := len(prefill)
|
||||
|
||||
prefill[4].CompletedAt = &prefill[4].CreatedAt
|
||||
valid := count - 1
|
||||
|
||||
req.NoError(s.TruncateAutomationSessions(ctx))
|
||||
req.NoError(s.CreateAutomationSession(ctx, prefill...))
|
||||
|
||||
// search for all valid
|
||||
set, f, err := s.SearchAutomationSessions(ctx, types.SessionFilter{})
|
||||
req.NoError(err)
|
||||
req.Len(set, valid) // we've deleted one
|
||||
|
||||
// search for ALL
|
||||
set, f, err = s.SearchAutomationSessions(ctx, types.SessionFilter{Completed: filter.StateInclusive})
|
||||
req.NoError(err)
|
||||
req.Len(set, count) // we've deleted one
|
||||
|
||||
// search for deleted only
|
||||
set, f, err = s.SearchAutomationSessions(ctx, types.SessionFilter{Completed: filter.StateExclusive})
|
||||
req.NoError(err)
|
||||
req.Len(set, 1) // we've deleted one
|
||||
|
||||
// find all prefixed
|
||||
set, f, err = s.SearchAutomationSessions(ctx, types.SessionFilter{WorkflowID: []uint64{1001}})
|
||||
req.NoError(err)
|
||||
req.Len(set, 4)
|
||||
|
||||
_ = f // dummy
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/automation/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza-server/pkg/id"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func testAutomationTriggers(t *testing.T, s store.AutomationTriggers) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
req = require.New(t)
|
||||
|
||||
makeNew = func() *types.Trigger {
|
||||
return &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
}
|
||||
}
|
||||
|
||||
truncAndCreate = func(t *testing.T) (*require.Assertions, *types.Trigger) {
|
||||
req := require.New(t)
|
||||
req.NoError(s.TruncateAutomationTriggers(ctx))
|
||||
res := makeNew()
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, res))
|
||||
return req, res
|
||||
}
|
||||
)
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
req := require.New(t)
|
||||
tr := &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
}
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, tr))
|
||||
})
|
||||
|
||||
t.Run("lookup by ID", func(t *testing.T) {
|
||||
req, tr := truncAndCreate(t)
|
||||
|
||||
fetched, err := s.LookupAutomationTriggerByID(ctx, tr.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(tr.ID, fetched.ID)
|
||||
req.NotNil(fetched.CreatedAt)
|
||||
req.Nil(fetched.UpdatedAt)
|
||||
req.Nil(fetched.DeletedAt)
|
||||
})
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
req, tr := truncAndCreate(t)
|
||||
tr.Enabled = true
|
||||
req.NoError(s.UpdateAutomationTrigger(ctx, tr))
|
||||
fetched, err := s.LookupAutomationTriggerByID(ctx, tr.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(tr.ID, fetched.ID)
|
||||
req.True(fetched.Enabled)
|
||||
})
|
||||
|
||||
t.Run("delete", func(t *testing.T) {
|
||||
t.Run("by Trigger", func(t *testing.T) {
|
||||
req, tr := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationTrigger(ctx, tr))
|
||||
_, err := s.LookupAutomationTriggerByID(ctx, tr.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
|
||||
t.Run("by ID", func(t *testing.T) {
|
||||
req, tr := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationTriggerByID(ctx, tr.ID))
|
||||
_, err := s.LookupAutomationTriggerByID(ctx, tr.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
auxWf := id.Next()
|
||||
|
||||
// Create test triggers
|
||||
{
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
Enabled: true,
|
||||
|
||||
WorkflowID: auxWf,
|
||||
}))
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
Enabled: true,
|
||||
|
||||
EventType: "event.test",
|
||||
}))
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
Enabled: true,
|
||||
|
||||
ResourceType: "resource:test",
|
||||
}))
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
Enabled: true,
|
||||
|
||||
DeletedAt: now(),
|
||||
}))
|
||||
req.NoError(s.CreateAutomationTrigger(ctx, &types.Trigger{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
|
||||
Enabled: false,
|
||||
}))
|
||||
}
|
||||
|
||||
tt, _, err := store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{
|
||||
WorkflowID: []uint64{auxWf},
|
||||
})
|
||||
req.NoError(err)
|
||||
req.Len(tt, 1)
|
||||
tt, _, err = store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{
|
||||
EventType: "event.test",
|
||||
})
|
||||
req.NoError(err)
|
||||
req.Len(tt, 1)
|
||||
tt, _, err = store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{
|
||||
ResourceType: "resource:test",
|
||||
})
|
||||
req.NoError(err)
|
||||
req.Len(tt, 1)
|
||||
tt, _, err = store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{
|
||||
Deleted: filter.StateExclusive,
|
||||
})
|
||||
req.NoError(err)
|
||||
req.Len(tt, 1)
|
||||
tt, _, err = store.SearchAutomationTriggers(ctx, s, types.TriggerFilter{
|
||||
Disabled: filter.StateExclusive,
|
||||
})
|
||||
req.NoError(err)
|
||||
req.Len(tt, 1)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/automation/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza-server/pkg/id"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rand"
|
||||
"github.com/cortezaproject/corteza-server/store"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func testAutomationWorkflows(t *testing.T, s store.AutomationWorkflows) {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
req = require.New(t)
|
||||
|
||||
makeNew = func(handle string) *types.Workflow {
|
||||
return &types.Workflow{
|
||||
ID: id.Next(),
|
||||
Handle: handle,
|
||||
CreatedAt: *now(),
|
||||
Enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
truncAndCreate = func(t *testing.T) (*require.Assertions, *types.Workflow) {
|
||||
req := require.New(t)
|
||||
req.NoError(s.TruncateAutomationWorkflows(ctx))
|
||||
res := makeNew(string(rand.Bytes(10)))
|
||||
req.NoError(s.CreateAutomationWorkflow(ctx, res))
|
||||
return req, res
|
||||
}
|
||||
)
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
req := require.New(t)
|
||||
wf := &types.Workflow{
|
||||
ID: id.Next(),
|
||||
CreatedAt: *now(),
|
||||
}
|
||||
req.NoError(s.CreateAutomationWorkflow(ctx, wf))
|
||||
})
|
||||
|
||||
t.Run("lookup by ID", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
|
||||
fetched, err := s.LookupAutomationWorkflowByID(ctx, wf.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(wf.ID, fetched.ID)
|
||||
req.NotNil(fetched.CreatedAt)
|
||||
req.Nil(fetched.UpdatedAt)
|
||||
req.Nil(fetched.DeletedAt)
|
||||
})
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
wf.Enabled = true
|
||||
req.NoError(s.UpdateAutomationWorkflow(ctx, wf))
|
||||
fetched, err := s.LookupAutomationWorkflowByID(ctx, wf.ID)
|
||||
req.NoError(err)
|
||||
req.Equal(wf.ID, fetched.ID)
|
||||
req.True(fetched.Enabled)
|
||||
})
|
||||
|
||||
t.Run("delete", func(t *testing.T) {
|
||||
t.Run("by Workflow", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationWorkflow(ctx, wf))
|
||||
_, err := s.LookupAutomationWorkflowByID(ctx, wf.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
|
||||
t.Run("by ID", func(t *testing.T) {
|
||||
req, wf := truncAndCreate(t)
|
||||
req.NoError(s.DeleteAutomationWorkflowByID(ctx, wf.ID))
|
||||
_, err := s.LookupAutomationWorkflowByID(ctx, wf.ID)
|
||||
req.EqualError(err, store.ErrNotFound.Error())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
prefill := []*types.Workflow{
|
||||
makeNew("one-one"),
|
||||
makeNew("one-two"),
|
||||
makeNew("two-one"),
|
||||
makeNew("two-two"),
|
||||
makeNew("two-deleted"),
|
||||
}
|
||||
|
||||
count := len(prefill)
|
||||
|
||||
prefill[4].DeletedAt = &prefill[4].CreatedAt
|
||||
valid := count - 1
|
||||
|
||||
req.NoError(s.TruncateAutomationWorkflows(ctx))
|
||||
req.NoError(s.CreateAutomationWorkflow(ctx, prefill...))
|
||||
|
||||
// search for all valid
|
||||
set, f, err := s.SearchAutomationWorkflows(ctx, types.WorkflowFilter{})
|
||||
req.NoError(err)
|
||||
req.Len(set, valid) // we've deleted one
|
||||
|
||||
// search for ALL
|
||||
set, f, err = s.SearchAutomationWorkflows(ctx, types.WorkflowFilter{Deleted: filter.StateInclusive})
|
||||
req.NoError(err)
|
||||
req.Len(set, count) // we've deleted one
|
||||
|
||||
// search for deleted only
|
||||
set, f, err = s.SearchAutomationWorkflows(ctx, types.WorkflowFilter{Deleted: filter.StateExclusive})
|
||||
req.NoError(err)
|
||||
req.Len(set, 1) // we've deleted one
|
||||
|
||||
// find all prefixed
|
||||
set, f, err = s.SearchAutomationWorkflows(ctx, types.WorkflowFilter{Query: "two-"})
|
||||
req.NoError(err)
|
||||
req.Len(set, 2)
|
||||
|
||||
_ = f // dummy
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user