Backend: Add tests to api/labels
This commit is contained in:
parent
73e0d294ff
commit
6e2062ea4b
2 changed files with 80 additions and 33 deletions
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
|
@ -11,45 +12,67 @@ func TestGetLabels(t *testing.T) {
|
|||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetLabels(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels?count=15")
|
||||
t.Log(result.Body)
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/labels?count=15")
|
||||
len := gjson.Get(r.Body.String(), "#")
|
||||
assert.LessOrEqual(t, int64(4), len.Int())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetLabels(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels?xxx=10")
|
||||
t.Log(result.Body)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/labels?xxx=15")
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
t.Log(router)
|
||||
t.Log(ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels?xxx=10")
|
||||
t.Log(result.Body)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
func TestUpdateLabel(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
UpdateLabel(router, conf)
|
||||
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/lt9k3pw1wowuy3c7", `{"LabelName": "Updated01", "LabelPriority": 2}`)
|
||||
val := gjson.Get(r.Body.String(), "LabelName")
|
||||
assert.Equal(t, "Updated01", val.String())
|
||||
val2 := gjson.Get(r.Body.String(), "CustomSlug")
|
||||
assert.Equal(t, "updated01", val2.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
UpdateLabel(router, conf)
|
||||
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/lt9k3pw1wowuy3c7", `{"LabelName": 123, "LabelPriority": 4, "Uncertainty": 80}`)
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
UpdateLabel(router, conf)
|
||||
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/xxx", `{"LabelName": "Updated01", "LabelPriority": 4, "Uncertainty": 80}`)
|
||||
val := gjson.Get(r.Body.String(), "error")
|
||||
assert.Equal(t, "Label not found", val.String())
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLikeLabel(t *testing.T) {
|
||||
t.Run("like not existing label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
|
||||
LikeLabel(router, ctx)
|
||||
|
||||
result := PerformRequest(app, "POST", "/api/v1/labels/8775789/like")
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
r := PerformRequest(app, "POST", "/api/v1/labels/8775789/like")
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
t.Run("like existing label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
|
||||
GetLabels(router, ctx)
|
||||
r2 := PerformRequest(app, "GET", "/api/v1/labels?count=1&q=updatelabel")
|
||||
val := gjson.Get(r2.Body.String(), `#(LabelSlug=="updatelabel").LabelFavorite`)
|
||||
assert.Equal(t, "false", val.String())
|
||||
LikeLabel(router, ctx)
|
||||
|
||||
result := PerformRequest(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c5/like")
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c7/like")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
r3 := PerformRequest(app, "GET", "/api/v1/labels?count=1&q=updatelabel")
|
||||
val2 := gjson.Get(r3.Body.String(), `#(LabelSlug=="updatelabel").LabelFavorite`)
|
||||
assert.Equal(t, "true", val2.String())
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -60,16 +83,24 @@ func TestDislikeLabel(t *testing.T) {
|
|||
|
||||
DislikeLabel(router, ctx)
|
||||
|
||||
result := PerformRequest(app, "DELETE", "/api/v1/labels/5678/like")
|
||||
assert.Equal(t, http.StatusNotFound, result.Code)
|
||||
r := PerformRequest(app, "DELETE", "/api/v1/labels/5678/like")
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
t.Run("dislike existing label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetLabels(router, ctx)
|
||||
r2 := PerformRequest(app, "GET", "/api/v1/labels?count=1&q=landscape")
|
||||
val := gjson.Get(r2.Body.String(), `#(LabelSlug=="landscape").LabelFavorite`)
|
||||
assert.Equal(t, "true", val.String())
|
||||
|
||||
DislikeLabel(router, ctx)
|
||||
|
||||
result := PerformRequest(app, "DELETE", "/api/v1/labels/lt9k3pw1wowuy3c5/like")
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "DELETE", "/api/v1/labels/lt9k3pw1wowuy3c2/like")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
|
||||
r3 := PerformRequest(app, "GET", "/api/v1/labels?count=1&q=landscape")
|
||||
val2 := gjson.Get(r3.Body.String(), `#(LabelSlug=="landscape").LabelFavorite`)
|
||||
assert.Equal(t, "false", val2.String())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -77,21 +108,20 @@ func TestLabelThumbnail(t *testing.T) {
|
|||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/dog/thumbnail/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/labels/lt9k3pw1wowuy3c2/thumbnail/xxx")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("invalid label", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/xxx/thumbnail/tile_500")
|
||||
r := PerformRequest(app, "GET", "/api/v1/labels/xxx/thumbnail/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("could not find original", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
LabelThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/labels/lt9k3pw1wowuy3c3/thumbnail/tile_500")
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/labels/lt9k3pw1wowuy3c3/thumbnail/tile_500")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -90,6 +90,23 @@ var LabelFixtures = map[string]Label{
|
|||
DeletedAt: nil,
|
||||
New: false,
|
||||
},
|
||||
"updateLabel": {
|
||||
ID: 1000005,
|
||||
LabelUUID: "lt9k3pw1wowuy3c7",
|
||||
LabelSlug: "updatelabel",
|
||||
CustomSlug: "updateLabel",
|
||||
LabelName: "updateLabel",
|
||||
LabelPriority: 2,
|
||||
LabelFavorite: false,
|
||||
LabelDescription: "",
|
||||
LabelNotes: "",
|
||||
LabelCategories: []*Label{},
|
||||
Links: []Link{},
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
DeletedAt: nil,
|
||||
New: false,
|
||||
},
|
||||
}
|
||||
|
||||
var LabelFixtureLandscape = LabelFixtures["landscape"]
|
||||
|
|
Loading…
Reference in a new issue