newsbot-api/services/twitch_test.go
James Tombleson 333a4f5345
Features/twitch (#7)
* ran go mod tity

* updated formatting and moved to using some common errors

* some common source errors

* bumped go version

* a bulk of the work has been completed to parse twitch videos/users

* Added twitch config values

* added common errors

* moved the scheduler to its own package to get it out of main and also start adding tests

* added a new err for invalid author images

* Updated an error used

* tests updated to avoid name duplication

* new func to replace the cached source recor and updated getcontent to return collected posts

* updated scheduler ref

* moved to services/cron
2022-05-15 21:48:23 -07:00

265 lines
4.9 KiB
Go

package services_test
import (
"log"
"testing"
"github.com/jtom38/newsbot/collector/domain/model"
"github.com/jtom38/newsbot/collector/services"
)
var sourceRecord = model.Sources{
ID: 1,
Name: "nintendo",
Source: "Twitch",
}
var invalidRecord = model.Sources{
ID: 1,
Name: "EvilNintendo",
Source: "Twitch",
}
func TestTwitchLogin(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
}
// reach out and confirms that the API returns posts made by the user.
func TestTwitchReturnsUserPosts(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil {
t.Error(err)
}
if len(posts) == 0 {
t.Error("expected videos but got none")
}
}
func TestTwitchReturnsNothingDueToInvalidUserName(t *testing.T) {
tc, err := services.NewTwitchClient(invalidRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil {
t.Error(err)
}
if len(posts) != 0 {
t.Error("expected videos but got none")
}
}
func TestTwitchReturnsVideoAuthor(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil {
t.Error(err)
}
if posts[0].UserName == "" {
t.Error("uable to parse username")
}
}
func TestTwitchReturnsThumbnail(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {t.Error(err) }
err = tc.Login()
if err != nil { t.Error(err) }
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
value, err := tc.ExtractThumbnail(posts[0])
if err != nil { t.Error(err) }
if value == "" { t.Error("uable to parse username") }
}
func TestTwitchReturnsPubDate(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil { t.Error(err) }
err = tc.Login()
if err != nil { t.Error(err) }
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
date, err := tc.ExtractPubDate(posts[0])
log.Println(date)
if err != nil { t.Error(err) }
}
func TestTwitchReturnsDescription(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil {
t.Error(err)
}
_, err = tc.ExtractDescription(posts[0])
if err != nil {
t.Error(err)
}
}
func TestTwitchReturnsAuthorImage(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {t.Error(err) }
err = tc.Login()
if err != nil { t.Error(err) }
user, err := tc.GetUserDetails()
if err != nil {t.Error(err) }
_, err = tc.ExtractAuthorImage(user)
if err != nil { t.Error(err) }
}
func TestTwitchReturnsTags(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
_, err = tc.ExtractTags(posts[0], user)
if err != nil { t.Error(err) }
}
func TestTwitchReturnsTitle(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil {
t.Error(err)
}
err = tc.Login()
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
res, err := tc.ExtractTitle(posts[0])
if err != nil { t.Error(err) }
if res == "" { t.Error("expected a filled string but got nil")}
}
func TestTwitchReturnsUrl(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil { t.Error(err) }
err = tc.Login()
if err != nil { t.Error(err) }
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
res, err := tc.ExtractUrl(posts[0])
if err != nil { t.Error(err) }
if res == "" { t.Error("expected a filled string but got nil")}
}
func TestTwitchGetContent(t *testing.T) {
tc, err := services.NewTwitchClient(sourceRecord)
if err != nil { t.Error(err) }
err = tc.Login()
if err != nil { t.Error(err) }
posts, err := tc.GetContent()
if err != nil {t.Error(err) }
if len(posts) == 0 { t.Error("posts came back with 0 posts") }
if len(posts) != 20 { t.Error("expected 20 posts") }
}