Skip to content

Commit

Permalink
execution: small improvements & more verbose tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgooz committed Nov 1, 2018
1 parent 68487f8 commit 40db360
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 63 deletions.
12 changes: 6 additions & 6 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
type Status int

// Status for an execution
// Created => The execution is created but not yet processed
// Created => The execution is created but not yet processed
// InProgress => The execution is being processed
// Completed => The execution is completed
// Completed => The execution is completed
const (
Created Status = iota
InProgress
Expand All @@ -31,7 +31,7 @@ type Execution struct {
TaskKey string `hash:"taskKey"`
Tags []string `hash:"tags"`
Inputs map[string]interface{} `hash:"inputs"`
Output string `hash:"-"`
OutputKey string `hash:"-"`
OutputData map[string]interface{} `hash:"-"`
CreatedAt time.Time `hash:"-"`
ExecutedAt time.Time `hash:"-"`
Expand Down Expand Up @@ -65,8 +65,8 @@ func New(service *service.Service, eventID string, taskKey string, inputs map[st
func (execution *Execution) Execute() error {
if execution.Status != Created {
return StatusError{
ActualStatus: execution.Status,
ExpectedStatus: Created,
ActualStatus: execution.Status,
}
}
execution.ExecutedAt = time.Now()
Expand All @@ -79,8 +79,8 @@ func (execution *Execution) Execute() error {
func (execution *Execution) Complete(outputKey string, outputData map[string]interface{}) error {
if execution.Status != InProgress {
return StatusError{
ActualStatus: execution.Status,
ExpectedStatus: InProgress,
ActualStatus: execution.Status,
}
}
task, err := execution.Service.GetTask(execution.TaskKey)
Expand All @@ -96,7 +96,7 @@ func (execution *Execution) Complete(outputKey string, outputData map[string]int
}

execution.ExecutionDuration = time.Since(execution.ExecutedAt)
execution.Output = outputKey
execution.OutputKey = outputKey
execution.OutputData = outputData
execution.Status = Completed
return nil
Expand Down
148 changes: 91 additions & 57 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ import (
)

var (
srv = &service.Service{
serviceName = "1"
eventID = "2"
taskKey = "task"
defaultInputs = map[string]interface{}{
"foo": "hello",
"bar": "world",
}
tags = []string{"tag1", "tag2"}
srv, _ = service.FromService(&service.Service{
Name: serviceName,
Tasks: []*service.Task{
&service.Task{
Key: "task",
Key: taskKey,
Inputs: []*service.Parameter{
&service.Parameter{Key: "foo", Type: "String"},
&service.Parameter{Key: "bar", Type: "String"},
Expand All @@ -30,93 +39,118 @@ var (
},
},
},
}
taskKey = "task"
defaultInputs = map[string]interface{}{
"foo": "hello",
"bar": "world",
}
tags = []string{"tag1", "tag2"}
})
)

func TestNewFromService(t *testing.T) {
tests := []struct {
taskKey string
inputs map[string]interface{}
hasError bool
name string
taskKey string
inputs map[string]interface{}
err error
}{
{taskKey: taskKey, inputs: map[string]interface{}{"foo": "hello", "bar": "world"}, hasError: false},
{taskKey: "wrongtask", inputs: map[string]interface{}{}, hasError: true},
{taskKey: taskKey, inputs: map[string]interface{}{"foo": "hello"}, hasError: true},
{name: "1", taskKey: taskKey, inputs: map[string]interface{}{"foo": "hello", "bar": "world"}, err: nil},
{name: "2", taskKey: "wrongtask", inputs: map[string]interface{}{}, err: &service.TaskNotFoundError{
TaskKey: "wrongtask",
ServiceName: serviceName,
}},
{name: "3", taskKey: taskKey, inputs: map[string]interface{}{"foo": "hello"}, err: &service.InvalidTaskInputError{
TaskKey: taskKey,
ServiceName: serviceName,
Warnings: []*service.ParameterWarning{
{
Key: "bar",
Warning: "required",
Parameter: &service.Parameter{Key: "bar", Type: "String"},
},
},
}},
}
for _, test := range tests {
execution, err := New(srv, "xxx", test.taskKey, test.inputs, tags)
if test.hasError {
require.Error(t, err)
execution, err := New(srv, eventID, test.taskKey, test.inputs, tags)
require.Equal(t, test.err, err, test.name)
if test.err != nil {
continue
}
require.NoError(t, err)
require.NotNil(t, execution)
require.Equal(t, srv.ID, execution.Service.ID)
require.Equal(t, taskKey, execution.TaskKey)
require.Equal(t, test.inputs, execution.Inputs)
require.Equal(t, tags, execution.Tags)
require.Equal(t, execution.Status, Created)
require.NotZero(t, execution.CreatedAt)
require.NotNil(t, execution, test.name)
require.Equal(t, srv.ID, execution.Service.ID, test.name)
require.Equal(t, eventID, execution.EventID, test.name)
require.Equal(t, taskKey, execution.TaskKey, test.name)
require.Equal(t, test.inputs, execution.Inputs, test.name)
require.Equal(t, tags, execution.Tags, test.name)
require.Equal(t, execution.Status, Created, test.name)
require.NotZero(t, execution.CreatedAt, test.name)
}
}

func TestExecute(t *testing.T) {
e, _ := New(srv, "xxx", taskKey, map[string]interface{}{"foo": "1", "bar": "2"}, tags)
e, _ := New(srv, eventID, taskKey, map[string]interface{}{"foo": "1", "bar": "2"}, tags)
tests := []struct {
id string
hasError bool
name string
err error
}{
{e.ID, false},
{"doesn't exists", true},
{e.ID, true}, // this one is already executed so it should return an error
{name: "1", err: nil},
{name: "2", err: StatusError{ExpectedStatus: Created, ActualStatus: InProgress}}, // this one is already executed so it should return an error
}
for _, test := range tests {
err := e.Execute()
if test.hasError {
require.Error(t, err)
require.Equal(t, test.err, err, test.name)
if test.err != nil {
continue
}
require.NoError(t, err)
require.NotNil(t, e)
require.NoError(t, err)
require.NotNil(t, e)
require.Equal(t, e.Status, InProgress)
require.NotZero(t, e.ExecutedAt)
require.NotNil(t, e, test.name)
require.Equal(t, e.Status, InProgress, test.name)
require.NotZero(t, e.ExecutedAt, test.name)
}
}

func TestComplete(t *testing.T) {
e, _ := New(srv, "xxx", taskKey, map[string]interface{}{"foo": "1", "bar": "2"}, tags)
e, _ := New(srv, eventID, taskKey, map[string]interface{}{"foo": "1", "bar": "2"}, tags)
e.Execute()
tests := []struct {
id string
key string
data map[string]interface{}
hasError bool
name string
key string
data map[string]interface{}
err error
}{
{id: "doesn't exists", key: "", data: map[string]interface{}{}, hasError: true},
{id: e.ID, key: "output", data: map[string]interface{}{"foo": "bar"}, hasError: true},
{id: e.ID, key: "outputX", data: map[string]interface{}{}, hasError: true},
{id: e.ID, key: "outputX", data: map[string]interface{}{"foo": "bar"}, hasError: false},
{id: e.ID, key: "outputX", data: map[string]interface{}{"foo": "bar"}, hasError: true}, // this one is already proccessed
{name: "1", key: "", data: map[string]interface{}{}, err: &service.TaskOutputNotFoundError{
TaskKey: taskKey,
TaskOutputKey: "",
ServiceName: serviceName},
},
{name: "2", key: "output", data: map[string]interface{}{"foo": "bar"}, err: &service.TaskOutputNotFoundError{
TaskKey: taskKey,
TaskOutputKey: "output",
ServiceName: serviceName,
}},
{name: "3", key: "outputX", data: map[string]interface{}{}, err: &service.InvalidTaskOutputError{
TaskKey: taskKey,
TaskOutputKey: "outputX",
ServiceName: serviceName,
Warnings: []*service.ParameterWarning{
{
Key: "foo",
Warning: "required",
Parameter: &service.Parameter{Key: "foo", Type: "String"},
},
},
}},
{name: "4", key: "outputX", data: map[string]interface{}{"foo": "bar"}, err: nil},
{name: "5", key: "outputX", data: map[string]interface{}{"foo": "bar"}, err: StatusError{
ExpectedStatus: InProgress,
ActualStatus: Completed,
}}, // this one is already proccessed
}
for _, test := range tests {
err := e.Complete(test.key, test.data)
if test.hasError {
require.Error(t, err)
require.Equal(t, test.err, err, test.name)
if test.err != nil {
continue
}
require.NoError(t, err)
require.NotNil(t, e)
require.NoError(t, err)
require.NotNil(t, e)
require.Equal(t, e.Status, Completed)
require.NotZero(t, e.ExecutionDuration)
require.NoError(t, err, test.name)
require.NotNil(t, e, test.name)
require.Equal(t, e.Status, Completed, test.name)
require.NotZero(t, e.ExecutionDuration, test.name)
}
}

0 comments on commit 40db360

Please sign in to comment.