From 6e2062ea4b9d5c142d99929426ae38a2ff9f6696 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Mon, 4 May 2020 16:02:49 +0200 Subject: [PATCH] Backend: Add tests to api/labels --- internal/api/label_test.go | 96 ++++++++++++++++++++----------- internal/entity/label_fixtures.go | 17 ++++++ 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/internal/api/label_test.go b/internal/api/label_test.go index f8ae2d0ab..20ba63d57 100644 --- a/internal/api/label_test.go +++ b/internal/api/label_test.go @@ -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) }) } diff --git a/internal/entity/label_fixtures.go b/internal/entity/label_fixtures.go index 9ee4e77aa..22cf47064 100644 --- a/internal/entity/label_fixtures.go +++ b/internal/entity/label_fixtures.go @@ -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"]