From fe23867965bd2927277736dd43e6f2e1686bbf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Tue, 16 Mar 2021 17:09:52 +0100 Subject: [PATCH] Add missing workflow store tests --- store/rdbms/automation_sessions.go | 2 +- store/tests/automation_sessions_test.go | 124 +++++++++++++++++++ store/tests/automation_triggers_test.go | 149 +++++++++++++++++++++++ store/tests/automation_workflows_test.go | 124 +++++++++++++++++++ 4 files changed, 398 insertions(+), 1 deletion(-) create mode 100644 store/tests/automation_sessions_test.go create mode 100644 store/tests/automation_triggers_test.go create mode 100644 store/tests/automation_workflows_test.go diff --git a/store/rdbms/automation_sessions.go b/store/rdbms/automation_sessions.go index b1f76d7e6..d76fe0573 100644 --- a/store/rdbms/automation_sessions.go +++ b/store/rdbms/automation_sessions.go @@ -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 { diff --git a/store/tests/automation_sessions_test.go b/store/tests/automation_sessions_test.go new file mode 100644 index 000000000..5023023f4 --- /dev/null +++ b/store/tests/automation_sessions_test.go @@ -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 + }) +} diff --git a/store/tests/automation_triggers_test.go b/store/tests/automation_triggers_test.go new file mode 100644 index 000000000..3ef3b80d8 --- /dev/null +++ b/store/tests/automation_triggers_test.go @@ -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) + }) +} diff --git a/store/tests/automation_workflows_test.go b/store/tests/automation_workflows_test.go new file mode 100644 index 000000000..cb872aa2b --- /dev/null +++ b/store/tests/automation_workflows_test.go @@ -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 + }) +}