Skip to content

Commit

Permalink
Improve SevenTV tests (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Mar 26, 2022
1 parent 937c2ec commit 13b901e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Dev: Improve Imgur tests. (#289)
- Dev: Improve migration tests. (#290)
- Dev: Improve Twitter tests. (#293)
- Dev: Improve SevenTV tests. (#294)

## 1.2.3

Expand Down
16 changes: 15 additions & 1 deletion internal/resolvers/seventv/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func init() {
},
},
}

// No emote
emotes["f0f0f0"] = EmoteAPIResponse{
Data: EmoteAPIResponseData{
Emote: nil,
},
}
}

func testServer() *httptest.Server {
Expand All @@ -90,7 +97,14 @@ func testServer() *httptest.Server {
if err != nil {
panic(err)
}
if response, ok := emotes[q.Variables["id"]]; ok {

emoteID := q.Variables["id"]

if emoteID == "bad" {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("xd"))
return
} else if response, ok := emotes[emoteID]; ok {
b, _ := json.Marshal(&response)

w.Header().Set("Content-Type", "application/json")
Expand Down
11 changes: 2 additions & 9 deletions internal/resolvers/seventv/emote_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -60,20 +59,14 @@ query fetchEmote($id: String!) {
}
defer resp.Body.Close()

// Read response into a string
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resolver.Errorf("SevenTV HTTP body read error: %s", err)
}

// Error out if the emote wasn't found or something else went wrong with the request
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusMultipleChoices {
return emoteNotFoundResponse, cache.NoSpecialDur, nil
}

var jsonResponse EmoteAPIResponse
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return resolver.Errorf("SevenTV API response unmarshal error: %s", err)
if err := json.NewDecoder(resp.Body).Decode(&jsonResponse); err != nil {
return resolver.Errorf("SevenTV API response decode error: %s", err)
}

// API returns Data.Emote as null if the emote wasn't found
Expand Down
16 changes: 16 additions & 0 deletions internal/resolvers/seventv/emote_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ func TestEmoteResolver(t *testing.T) {
expectedBytes: []byte(`{"status":404,"message":"No SevenTV emote with this id found"}`),
expectedError: nil,
},
{
label: "Matching link - noemote",
inputURL: utils.MustParseURL("https://7tv.app/emotes/f0f0f0"),
inputEmoteHash: "f0f0f0",
inputReq: nil,
expectedBytes: []byte(`{"status":404,"message":"No SevenTV emote with this id found"}`),
expectedError: nil,
},
{
label: "Matching link - bad response",
inputURL: utils.MustParseURL("https://7tv.app/emotes/bad"),
inputEmoteHash: "bad",
inputReq: nil,
expectedBytes: []byte(`{"status":500,"message":"SevenTV API response decode error: invalid character \u0026#39;x\u0026#39; looking for beginning of value"}`),
expectedError: nil,
},
}

const q = `SELECT value FROM cache WHERE key=$1`
Expand Down

0 comments on commit 13b901e

Please sign in to comment.