From 8f0b6a6a599c34f298def01b6526bde9b8ddfb1a Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 25 Jan 2019 17:10:22 -0600 Subject: [PATCH] add unit test for counters #261 --- backend/app/rest/api/admin.go | 2 +- backend/app/rest/api/admin_test.go | 52 ++++++++++++++++++++- backend/app/rest/api/rest_private_test.go | 33 +++++++++++-- backend/app/store/engine/bolt_admin_test.go | 8 ++++ 4 files changed, 89 insertions(+), 6 deletions(-) diff --git a/backend/app/rest/api/admin.go b/backend/app/rest/api/admin.go index 87667f73..53be9e75 100644 --- a/backend/app/rest/api/admin.go +++ b/backend/app/rest/api/admin.go @@ -58,7 +58,7 @@ func (a *admin) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) { rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't delete comment") return } - a.cache.Flush(cache.Flusher(locator.SiteID).Scopes(locator.URL, lastCommentsScope)) + a.cache.Flush(cache.Flusher(locator.SiteID).Scopes(locator.SiteID, locator.URL, lastCommentsScope)) render.Status(r, http.StatusOK) render.JSON(w, r, R.JSON{"id": id, "locator": locator}) } diff --git a/backend/app/rest/api/admin_test.go b/backend/app/rest/api/admin_test.go index 40062f39..8f79d6e3 100644 --- a/backend/app/rest/api/admin_test.go +++ b/backend/app/rest/api/admin_test.go @@ -36,12 +36,33 @@ func TestAdmin_Delete(t *testing.T) { id1 := addComment(t, c1, ts) addComment(t, c2, ts) + // check last comments + res, code := get(t, ts.URL+"/api/v1/last/2?site=radio-t") + assert.Equal(t, 200, code) + comments := []store.Comment{} + err := json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 2, len(comments), "should have 2 comments") + + // check multi count + resp, err := post(t, ts.URL+"/api/v1/counts?site=radio-t", `["https://radio-t.com/blah","https://radio-t.com/blah2"]`) + assert.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + bb, err := ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + j := []store.PostInfo{} + err = json.Unmarshal(bb, &j) + assert.Nil(t, err) + assert.Equal(t, []store.PostInfo([]store.PostInfo{{URL: "https://radio-t.com/blah", Count: 2}, + {URL: "https://radio-t.com/blah2", Count: 0}}), j) + + // delete a comment client := http.Client{} req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/v1/admin/comment/%s?site=radio-t&url=https://radio-t.com/blah", ts.URL, id1), nil) assert.Nil(t, err) req.SetBasicAuth("admin", "password") - resp, err := client.Do(req) + resp, err = client.Do(req) assert.Nil(t, err) assert.Equal(t, 200, resp.StatusCode) @@ -52,6 +73,35 @@ func TestAdmin_Delete(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "", cr.Text) assert.True(t, cr.Deleted) + + // check last comments updated + res, code = get(t, ts.URL+"/api/v1/last/2?site=radio-t") + assert.Equal(t, 200, code) + comments = []store.Comment{} + err = json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 1, len(comments), "should have 1 comments") + + // check count updated + res, code = get(t, ts.URL+"/api/v1/count?site=radio-t&url=https://radio-t.com/blah") + assert.Equal(t, 200, code) + b := map[string]interface{}{} + err = json.Unmarshal([]byte(res), &b) + assert.Nil(t, err) + t.Logf("%#v", b) + assert.Equal(t, 1.0, b["count"], "should report 1 comments") + + // check multi count updated + resp, err = post(t, ts.URL+"/api/v1/counts?site=radio-t", `["https://radio-t.com/blah","https://radio-t.com/blah2"]`) + assert.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + bb, err = ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + j = []store.PostInfo{} + err = json.Unmarshal(bb, &j) + assert.Nil(t, err) + assert.Equal(t, []store.PostInfo([]store.PostInfo{{URL: "https://radio-t.com/blah", Count: 1}, + {URL: "https://radio-t.com/blah2", Count: 0}}), j) } func TestAdmin_Title(t *testing.T) { diff --git a/backend/app/rest/api/rest_private_test.go b/backend/app/rest/api/rest_private_test.go index 86cf5f6a..5cdf3556 100644 --- a/backend/app/rest/api/rest_private_test.go +++ b/backend/app/rest/api/rest_private_test.go @@ -216,21 +216,34 @@ func TestRest_UpdateDelete(t *testing.T) { Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} id := addComment(t, c1, ts) + // check multi count updated + resp, err := post(t, ts.URL+"/api/v1/counts?site=radio-t", `["https://radio-t.com/blah1","https://radio-t.com/blah2"]`) + require.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + bb, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + j := []store.PostInfo{} + err = json.Unmarshal(bb, &j) + assert.Nil(t, err) + assert.Equal(t, []store.PostInfo([]store.PostInfo{{URL: "https://radio-t.com/blah1", Count: 1}, + {URL: "https://radio-t.com/blah2", Count: 0}}), j) + + // delete a comment client := http.Client{} req, err := http.NewRequest(http.MethodPut, ts.URL+"/api/v1/comment/"+id+"?site=radio-t&url=https://radio-t.com/blah1", strings.NewReader(`{"delete": true, "summary":"removed by user"}`)) - assert.Nil(t, err) + require.NoError(t, err) req.Header.Add("X-JWT", devToken) b, err := client.Do(req) - assert.Nil(t, err) + require.NoError(t, err) body, err := ioutil.ReadAll(b.Body) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, 200, b.StatusCode, string(body)) // comments returned by update c2 := store.Comment{} err = json.Unmarshal(body, &c2) - assert.Nil(t, err) + require.NoError(t, err) assert.Equal(t, id, c2.ID) assert.True(t, c2.Deleted) @@ -243,6 +256,18 @@ func TestRest_UpdateDelete(t *testing.T) { assert.Equal(t, "", c3.Text) assert.Equal(t, "", c3.Orig) assert.True(t, c3.Deleted) + + // check multi count updated + resp, err = post(t, ts.URL+"/api/v1/counts?site=radio-t", `["https://radio-t.com/blah1","https://radio-t.com/blah2"]`) + assert.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + bb, err = ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + j = []store.PostInfo{} + err = json.Unmarshal(bb, &j) + require.NoError(t, err) + assert.Equal(t, []store.PostInfo([]store.PostInfo{{URL: "https://radio-t.com/blah1", Count: 0}, + {URL: "https://radio-t.com/blah2", Count: 0}}), j) } func TestRest_UpdateNotOwner(t *testing.T) { diff --git a/backend/app/store/engine/bolt_admin_test.go b/backend/app/store/engine/bolt_admin_test.go index d9bf0ffe..e1371677 100644 --- a/backend/app/store/engine/bolt_admin_test.go +++ b/backend/app/store/engine/bolt_admin_test.go @@ -20,6 +20,10 @@ func TestBoltAdmin_Delete(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 2, len(res), "initially 2 comments") + count, err := b.Count(loc) + require.NoError(t, err) + assert.Equal(t, 2, count, "count=2 initially") + err = b.Delete(loc, res[0].ID, store.SoftDelete) assert.Nil(t, err) @@ -37,6 +41,10 @@ func TestBoltAdmin_Delete(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 1, len(comments), "1 in last, 1 removed") + count, err = b.Count(loc) + require.NoError(t, err) + assert.Equal(t, 1, count) + err = b.Delete(loc, "123456", store.SoftDelete) assert.NotNil(t, err)