Skip to content

Commit

Permalink
test: add test of json unmarshall for twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 12, 2023
1 parent 3f0d382 commit bcdf45d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/twitter/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import (
"github.com/azimut/cli-view/internal/fetch"
)

func Fetch(url, ua string, timeout time.Duration) (tweet *Embedded, err error) {
url, err = EffectiveUrl(url)
func Fetch(tweetUrl, ua string, timeout time.Duration) (*Embedded, error) {
embedUrl, err := EffectiveUrl(tweetUrl)
if err != nil {
return nil, err
}
res, err := fetch.Fetch(url, ua, timeout)
rawJson, err := fetch.Fetch(embedUrl, ua, timeout)
if err != nil {
return nil, err
}
b := []byte(res)
tweet, err := toEmbedded(rawJson)
if err != nil {
return nil, err
}
return tweet, nil
}

func toEmbedded(rawJson string) (tweet *Embedded, err error) {
b := []byte(rawJson)
if err = json.Unmarshal(b, &tweet); err != nil {
return nil, err
}
Expand Down
25 changes: 25 additions & 0 deletions internal/twitter/fetch_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
package twitter

import (
"io/ioutil"
"testing"
"time"
)

const URL = `https://twitter.com/TwitterDev/status/1443269993676763138`

var TweetFiles = []string{
"tweet-image-AND-quote.json",
"tweet.json",
"tweet-mult-image.json",
"tweet-link.json",
"tweet-quote.json",
"tweet-url-with-image.json",
}

func TestTWFetch(t *testing.T) {
_, err := Fetch(URL, "Twitter_View/0.1", time.Second*10)
if err != nil {
t.Fail()
}
}

func TestUnmarshall(t *testing.T) {
for _, tweetFile := range TweetFiles {
bytes, err := ioutil.ReadFile("../../testdata/" + tweetFile)
if err != nil {
t.Fail()
break
}
_, err = toEmbedded(string(bytes))
if err != nil {
t.Fail()
break
}
}
}

0 comments on commit bcdf45d

Please sign in to comment.