From a70c387da857d4a849c41c66b9061ee755195d4d Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 13 Mar 2022 15:11:20 +0100 Subject: [PATCH] Improve BetterTTV emote tests (#282) --- CHANGELOG.md | 4 +- internal/resolvers/betterttv/data_test.go | 2 +- .../resolvers/betterttv/emote_loader_test.go | 54 ------------------- .../resolvers/betterttv/emote_resolver.go | 2 +- .../betterttv/emote_resolver_test.go | 44 +++++---------- 5 files changed, 18 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81a2ae0a..f90f57d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ ## Unreleased - Breaking: Resolver caches are now stored in PostgreSQL. See [docs/build.md](./docs/build.md) for prerequisite instructions. (#271) -- Fix: SevenTV emotes now resolve correctly. +- Fix: SevenTV emotes now resolve correctly. (#281) +- Dev: Improve BetterTTV emote tests. (#282) +- Minor: BetterTTV cache key changed from plural to singular form. (#282) ## 1.2.3 diff --git a/internal/resolvers/betterttv/data_test.go b/internal/resolvers/betterttv/data_test.go index 508f4274..81e50b5a 100644 --- a/internal/resolvers/betterttv/data_test.go +++ b/internal/resolvers/betterttv/data_test.go @@ -62,7 +62,7 @@ func testServer() *httptest.Server { w.Header().Set("Content-Type", "application/json") - if emote == "bad_json" { + if emote == "bad" { w.Write([]byte("xD")) } else if response, ok = data[emote]; !ok { http.Error(w, http.StatusText(404), 404) diff --git a/internal/resolvers/betterttv/emote_loader_test.go b/internal/resolvers/betterttv/emote_loader_test.go index 80dde62d..a3069ceb 100644 --- a/internal/resolvers/betterttv/emote_loader_test.go +++ b/internal/resolvers/betterttv/emote_loader_test.go @@ -1,11 +1,9 @@ package betterttv import ( - "context" "net/url" "testing" - "github.com/Chatterino/api/internal/logger" "github.com/Chatterino/api/pkg/utils" qt "github.com/frankban/quicktest" ) @@ -47,55 +45,3 @@ func TestBuildURL(t *testing.T) { }) } } - -func TestLoad(t *testing.T) { - ctx := logger.OnContext(context.Background(), logger.NewTest()) - c := qt.New(t) - ts := testServer() - defer ts.Close() - loader := NewEmoteLoader(utils.MustParseURL(ts.URL + "/3/emotes/")) - - type tc struct { - emoteHash string - expectedTooltip string - expectedMessage string - } - - tests := []tc{ - { - emoteHash: "kkona", - expectedTooltip: `
KKona
Global BetterTTV Emote
By: zneix
`, - }, - { - emoteHash: "kkona_html", - expectedTooltip: `
<b>KKona</b>
Global BetterTTV Emote
By: <b>zneix</b>
`, - }, - { - emoteHash: "forsenga", - expectedTooltip: `
forsenGa
Shared BetterTTV Emote
By: pajlada
`, - }, - { - emoteHash: "forsenga_html", - expectedTooltip: `
<b>forsenGa</b>
Shared BetterTTV Emote
By: <b>pajlada</b>
`, - }, - { - emoteHash: "bad_json", - expectedMessage: `betterttv api unmarshal error: invalid character 'x' looking for beginning of value`, - }, - } - - for _, test := range tests { - c.Run(test.emoteHash, func(c *qt.C) { - response, _, err := loader.Load(ctx, test.emoteHash, nil) - - c.Assert(err, qt.IsNil) - c.Assert(response, qt.Not(qt.IsNil)) - - cleanTooltip, unescapeErr := url.PathUnescape(response.Tooltip) - c.Assert(unescapeErr, qt.IsNil) - - c.Assert(cleanTooltip, qt.Equals, test.expectedTooltip) - c.Assert(response.Message, qt.Equals, test.expectedMessage) - }) - } -} diff --git a/internal/resolvers/betterttv/emote_resolver.go b/internal/resolvers/betterttv/emote_resolver.go index 22db829a..54d97f00 100644 --- a/internal/resolvers/betterttv/emote_resolver.go +++ b/internal/resolvers/betterttv/emote_resolver.go @@ -49,7 +49,7 @@ func NewEmoteResolver(ctx context.Context, cfg config.APIConfig, pool db.Pool, e emoteLoader := NewEmoteLoader(emoteAPIURL) r := &EmoteResolver{ - emoteCache: cache.NewPostgreSQLCache(ctx, cfg, pool, "betterttv:emotes", resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour), + emoteCache: cache.NewPostgreSQLCache(ctx, cfg, pool, "betterttv:emote", resolver.NewResponseMarshaller(emoteLoader), 1*time.Hour), } return r diff --git a/internal/resolvers/betterttv/emote_resolver_test.go b/internal/resolvers/betterttv/emote_resolver_test.go index 89d24a0c..be2386d3 100644 --- a/internal/resolvers/betterttv/emote_resolver_test.go +++ b/internal/resolvers/betterttv/emote_resolver_test.go @@ -2,9 +2,7 @@ package betterttv import ( "context" - "encoding/json" "net/http" - "net/http/httptest" "net/url" "testing" @@ -12,7 +10,6 @@ import ( "github.com/Chatterino/api/pkg/config" "github.com/Chatterino/api/pkg/utils" qt "github.com/frankban/quicktest" - "github.com/go-chi/chi/v5" "github.com/golang/mock/gomock" "github.com/jackc/pgx/v4" "github.com/pashagolub/pgxmock" @@ -28,30 +25,7 @@ func TestEmoteResolver(t *testing.T) { pool, _ := pgxmock.NewPool() cfg := config.APIConfig{} - r := chi.NewRouter() - r.Get("/3/emotes/{emote}", func(w http.ResponseWriter, r *http.Request) { - emote := chi.URLParam(r, "emote") - - var response *EmoteAPIResponse - var ok bool - - w.Header().Set("Content-Type", "application/json") - - if emote == "500" { - // response = &resolver.Response{ - // Status: http.StatusInternalServerError, - // Message: "betterttv http request error " - // } - } else if response, ok = data[emote]; !ok { - http.Error(w, http.StatusText(404), 404) - return - } - - b, _ := json.Marshal(&response) - - w.Write(b) - }) - ts := httptest.NewServer(r) + ts := testServer() defer ts.Close() emoteAPIURL := utils.MustParseURL(ts.URL + "/3/emotes/") @@ -173,7 +147,7 @@ func TestEmoteResolver(t *testing.T) { c.Run(test.label, func(c *qt.C) { rows := pgxmock.NewRows([]string{"value"}).AddRow(test.expectedBytes) pool.ExpectQuery("SELECT"). - WithArgs("betterttv:emotes:" + test.inputEmoteHash). + WithArgs("betterttv:emote:" + test.inputEmoteHash). WillReturnRows(rows) outputBytes, outputError := resolver.Run(ctx, test.inputURL, test.inputReq) c.Assert(outputError, qt.Equals, test.expectedError) @@ -195,7 +169,7 @@ func TestEmoteResolver(t *testing.T) { tests := []runTest{ { - label: "Matching link - not cached", + label: "Emote", inputURL: utils.MustParseURL("https://betterttv.com/emotes/566ca04265dbbdab32ec054b"), inputEmoteHash: "566ca04265dbbdab32ec054b", inputReq: nil, @@ -203,13 +177,21 @@ func TestEmoteResolver(t *testing.T) { expectedError: nil, }, { - label: "Matching link - 404", + label: "404", inputURL: utils.MustParseURL("https://betterttv.com/emotes/404"), inputEmoteHash: "404", inputReq: nil, expectedBytes: []byte(`{"status":404,"message":"No BetterTTV emote with this hash found"}`), expectedError: nil, }, + { + label: "Bad JSON", + inputURL: utils.MustParseURL("https://betterttv.com/emotes/bad"), + inputEmoteHash: "bad", + inputReq: nil, + expectedBytes: []byte(`{"status":500,"message":"betterttv api unmarshal error: invalid character \u0026#39;x\u0026#39; looking for beginning of value"}`), + expectedError: nil, + }, } const q = `SELECT value FROM cache WHERE key=$1` @@ -218,7 +200,7 @@ func TestEmoteResolver(t *testing.T) { c.Run(test.label, func(c *qt.C) { pool.ExpectQuery("SELECT").WillReturnError(pgx.ErrNoRows) pool.ExpectExec("INSERT INTO cache"). - WithArgs("betterttv:emotes:"+test.inputEmoteHash, test.expectedBytes, pgxmock.AnyArg()). + WithArgs("betterttv:emote:"+test.inputEmoteHash, test.expectedBytes, pgxmock.AnyArg()). WillReturnResult(pgxmock.NewResult("INSERT", 1)) outputBytes, outputError := resolver.Run(ctx, test.inputURL, test.inputReq) c.Assert(outputError, qt.Equals, test.expectedError)