From cdf9d9293d4211154e3a3e5c14f80fcace50586a Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Sat, 17 Nov 2018 08:28:50 +0100 Subject: [PATCH] Add test stubs and helper functions for package api #58 --- internal/api/photo_test.go | 32 ------------------------------- internal/api/photos_test.go | 38 +++++++++++++++++++++++++++++++++++++ internal/api/test.go | 27 ++++++++++++++++++++++++++ internal/test/config.go | 2 +- 4 files changed, 66 insertions(+), 33 deletions(-) delete mode 100644 internal/api/photo_test.go create mode 100644 internal/api/photos_test.go create mode 100644 internal/api/test.go diff --git a/internal/api/photo_test.go b/internal/api/photo_test.go deleted file mode 100644 index 267082ce8..000000000 --- a/internal/api/photo_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package api - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/photoprism/photoprism/internal/test" - "github.com/stretchr/testify/assert" -) - -func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder { - req, _ := http.NewRequest(method, path, nil) - w := httptest.NewRecorder() - r.ServeHTTP(w, req) - return w -} - -func TestGetPhotos(t *testing.T) { - conf := test.NewConfig() - app := gin.Default() - - v1 := app.Group("/api/v1") - { - GetPhotos(v1, conf) - } - - r := performRequest(app, "GET", "/api/v1/photos?count=10") - - assert.Equal(t, http.StatusOK, r.Code) -} diff --git a/internal/api/photos_test.go b/internal/api/photos_test.go new file mode 100644 index 000000000..1fb17dff3 --- /dev/null +++ b/internal/api/photos_test.go @@ -0,0 +1,38 @@ +package api + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetPhotos(t *testing.T) { + app, router, conf := NewTest() + + GetPhotos(router, conf) + + result := TestRequest(app, "GET", "/api/v1/photos?count=10") + + assert.Equal(t, http.StatusOK, result.Code) +} + +func TestLikePhoto(t *testing.T) { + app, router, conf := NewTest() + + LikePhoto(router, conf) + + result := TestRequest(app, "POST", "/api/v1/photos/1/like") + + assert.Equal(t, http.StatusOK, result.Code) +} + +func TestDislikePhoto(t *testing.T) { + app, router, conf := NewTest() + + DislikePhoto(router, conf) + + result := TestRequest(app, "DELETE", "/api/v1/photos/1/like") + + assert.Equal(t, http.StatusOK, result.Code) +} diff --git a/internal/api/test.go b/internal/api/test.go new file mode 100644 index 000000000..221e06043 --- /dev/null +++ b/internal/api/test.go @@ -0,0 +1,27 @@ +package api + +import ( + "net/http" + "net/http/httptest" + + "github.com/gin-gonic/gin" + "github.com/photoprism/photoprism/internal/photoprism" + "github.com/photoprism/photoprism/internal/test" +) + +func NewTest() (app *gin.Engine, router *gin.RouterGroup, conf photoprism.Config) { + conf = test.NewConfig() + gin.SetMode(gin.TestMode) + app = gin.New() + + router = app.Group("/api/v1") + + return app, router, conf +} + +func TestRequest(r http.Handler, method, path string) *httptest.ResponseRecorder { + req, _ := http.NewRequest(method, path, nil) + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w +} \ No newline at end of file diff --git a/internal/test/config.go b/internal/test/config.go index 8453cb97c..23d9458de 100644 --- a/internal/test/config.go +++ b/internal/test/config.go @@ -185,7 +185,7 @@ func (c *Config) GetServerPort() int { // GetServerMode returns the server mode. func (c *Config) GetServerMode() string { - return "release" + return "test" } // GetOriginalsPath returns the originals.