From e26544ddf550c7fcaf550af88eac5163fa8f2ee2 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 13:19:57 +0100 Subject: [PATCH] Fake pipeline object in tests --- go.mod | 1 + go.sum | 2 ++ main_test.go | 30 ++++++++++-------------------- pipeline.go | 12 ++++++------ 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 3641441..434e79d 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 849fa9a..59db30e 100644 --- a/go.sum +++ b/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= diff --git a/main_test.go b/main_test.go index 3540a04..f887bc5 100644 --- a/main_test.go +++ b/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) diff --git a/pipeline.go b/pipeline.go index c493b23..56ecc4d 100644 --- a/pipeline.go +++ b/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) {