Roman Zipp
3 years ago
5 changed files with 126 additions and 119 deletions
-
25pipelines/executable_step.go
-
63pipelines/pipeline.go
-
56pipelines/pipeline_test.go
-
39pipelines/step.go
-
62pipelines/step_test.go
@ -0,0 +1,25 @@ |
|||
package pipelines |
|||
|
|||
type IExecutableStep interface { |
|||
Execute() |
|||
} |
|||
|
|||
// Resize image
|
|||
|
|||
type ResizeImageStep struct { |
|||
Step |
|||
} |
|||
|
|||
func (s ResizeImageStep) Execute() { |
|||
// TODO
|
|||
} |
|||
|
|||
// Compress image
|
|||
|
|||
type CompressImageStep struct { |
|||
Step |
|||
} |
|||
|
|||
func (s CompressImageStep) Execute() { |
|||
// TODO
|
|||
} |
@ -0,0 +1,39 @@ |
|||
package pipelines |
|||
|
|||
import "errors" |
|||
|
|||
type StepType int |
|||
|
|||
const ( |
|||
TypeResizeImageStep StepType = iota |
|||
TypeCompressImageStep |
|||
) |
|||
|
|||
type Step struct { |
|||
Name string `faker:"name"` |
|||
Type StepType `faker:"-"` |
|||
} |
|||
|
|||
func (s Step) Translate() (IExecutableStep, error) { |
|||
var step IExecutableStep |
|||
switch s.GetType() { |
|||
case TypeResizeImageStep: |
|||
step = ResizeImageStep{s} |
|||
case TypeCompressImageStep: |
|||
step = CompressImageStep{s} |
|||
} |
|||
|
|||
if step == nil { |
|||
return nil, errors.New("invalid type") |
|||
} |
|||
|
|||
return step, nil |
|||
} |
|||
|
|||
func (s Step) GetType() StepType { |
|||
return s.Type |
|||
} |
|||
|
|||
func (s Step) GetName() string { |
|||
return s.Name |
|||
} |
@ -0,0 +1,62 @@ |
|||
package pipelines |
|||
|
|||
import ( |
|||
"github.com/stretchr/testify/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestTranslateStep(t *testing.T) { |
|||
const Payload string = `{ |
|||
"name": "example pipeline", |
|||
"type": 1, |
|||
"removeMetadata": false, |
|||
"steps": [ |
|||
{ |
|||
"name": "resize image", |
|||
"type": 0 |
|||
} |
|||
] |
|||
}` |
|||
|
|||
t.Run("step type deserialization is successful", func(t *testing.T) { |
|||
values := DeserializePipelines([][]byte{[]byte(Payload)}) |
|||
pipeline := values[0] |
|||
steps := pipeline.GetSteps() |
|||
|
|||
assert.Equal(t, 1, len(steps), "Output steps should contain one element") |
|||
assert.Equal(t, "resize image", steps[0].GetName()) |
|||
assert.Equal(t, TypeResizeImageStep, steps[0].GetType()) |
|||
|
|||
translated, err := steps[0].Translate() |
|||
assert.Equal(t, err, nil) |
|||
assert.IsType(t, ResizeImageStep{}, translated) |
|||
}) |
|||
} |
|||
|
|||
func TestInvalidStepType(t *testing.T) { |
|||
const Payload string = `{ |
|||
"name": "example pipeline", |
|||
"type": 1, |
|||
"removeMetadata": false, |
|||
"steps": [ |
|||
{ |
|||
"name": "resize image", |
|||
"type": 99999 |
|||
} |
|||
] |
|||
}` |
|||
|
|||
t.Run("Video pipelines deserialization is successful", func(t *testing.T) { |
|||
values := DeserializePipelines([][]byte{[]byte(Payload)}) |
|||
pipeline := values[0] |
|||
steps := pipeline.GetSteps() |
|||
|
|||
assert.Equal(t, 1, len(steps), "Output steps should contain one element") |
|||
assert.Equal(t, "resize image", steps[0].GetName()) |
|||
assert.Equal(t, StepType(99999), steps[0].GetType()) |
|||
|
|||
translated, err := steps[0].Translate() |
|||
assert.EqualError(t, err, "invalid type") |
|||
assert.Nil(t, translated) |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue