Browse Source

Fake pipeline object in tests

feature/add-pipeline-endpoints
Fabian Vowie 3 years ago
parent
commit
e26544ddf5
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 1
      go.mod
  2. 2
      go.sum
  3. 30
      main_test.go
  4. 12
      pipeline.go

1
go.mod

@ -5,6 +5,7 @@ go 1.17
require github.com/gorilla/mux v1.8.0
require (
github.com/bxcodec/faker/v3 v3.7.0 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/hexops/valast v1.4.1 // indirect

2
go.sum

@ -1,4 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/bxcodec/faker/v3 v3.7.0 h1:qWAFFwcyVS0ukF0UoJju1wBLO0cuPQ7JdVBPggM8kNo=
github.com/bxcodec/faker/v3 v3.7.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=

30
main_test.go

@ -2,10 +2,12 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/bxcodec/faker/v3"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
@ -23,42 +25,30 @@ func TestIndexRoute(t *testing.T) {
}
func TestEndpointRoute(t *testing.T) {
// TODO: Use mock/fake for dummy pipeline data
givenEndpoint := Pipeline{
Name: "example pipeline",
Slug: "example",
Type: 0,
RemoveMetadata: false,
Steps: []Step{
{
Name: "resize image",
Type: 0,
},
{
Name: "compress image",
Type: 1,
},
},
data := Pipeline{}
err := faker.FakeData(&data)
if err != nil {
fmt.Println(err)
}
t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter()
RegisterPipelineRoutes(router, []IPipeline{givenEndpoint})
RegisterPipelineRoutes(router, []IPipeline{data})
request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil)
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, responseRecorder.Code, 200)
body, _ := json.Marshal(givenEndpoint)
body, _ := json.Marshal(data)
assert.JSONEq(t, string(body), responseRecorder.Body.String())
})
t.Run("Unregistered pipelines return 404", func(t *testing.T) {
router := mux.NewRouter()
request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil)
request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)

12
pipeline.go

@ -27,10 +27,10 @@ type IPipeline interface {
}
type Pipeline struct {
Name string `json:"name"`
Slug string `json:"slug"`
Type PipelineType `json:"type"`
RemoveMetadata bool `json:"remove_metadata"`
Name string `json:"name" faker:"name"`
Slug string `json:"slug" faker:"word"`
Type PipelineType `json:"type" faker:"-"`
RemoveMetadata bool `json:"remove_metadata" faker:"-"`
Steps []Step `json:"steps"`
}
@ -68,8 +68,8 @@ const (
)
type Step struct {
Name string
Type StepType
Name string `faker:"name"`
Type StepType `faker:"-"`
}
func (s Step) Translate() (IExecutableStep, error) {

Loading…
Cancel
Save