Add test stubs and helper functions for package api #58

This commit is contained in:
Michael Mayer 2018-11-17 08:28:50 +01:00
parent f7404f838b
commit cdf9d9293d
4 changed files with 66 additions and 33 deletions

View file

@ -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)
}

View file

@ -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)
}

27
internal/api/test.go Normal file
View file

@ -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
}

View file

@ -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.