67 lines
2.2 KiB
Go
67 lines
2.2 KiB
Go
package ingestionbench
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type fixtureFeed struct{ size int }
|
|
|
|
func (*fixtureFeed) Name() string { return "fixture" }
|
|
func (*fixtureFeed) Scrape(context.Context, []string) ([]scraper.Article, error) {
|
|
return []scraper.Article{{Title: "PRIVATE", URL: "https://private.invalid", Content: "PRIVATE"}, {}, {}, {}}, nil
|
|
}
|
|
func (f *fixtureFeed) Enrich(_ context.Context, a []scraper.Article) ([]scraper.Article, error) {
|
|
f.size = len(a)
|
|
for i := range a {
|
|
a[i].Content = "PRIVATE full body"
|
|
a[i].ContentState = scraper.ContentComplete
|
|
}
|
|
return a, nil
|
|
}
|
|
func TestBenchBoundsAndRedactsAllArticleData(t *testing.T) {
|
|
f := &fixtureFeed{}
|
|
rows, err := Run(context.Background(), []Source{{Key: "yahoo_finance", Feed: f}}, 3)
|
|
if err != nil || f.size != 3 || rows[0].Listed != 3 || rows[0].Content.Complete != 3 || rows[0].Dates.Missing != 3 {
|
|
t.Fatal("bound/date independence failed")
|
|
}
|
|
raw, _ := json.Marshal(rows)
|
|
if strings.Contains(string(raw), "PRIVATE") || strings.Contains(string(raw), "https://") {
|
|
t.Fatal("article data leaked")
|
|
}
|
|
for _, n := range []int{0, 4} {
|
|
if _, err := Run(context.Background(), nil, n); err == nil {
|
|
t.Fatal("limit accepted")
|
|
}
|
|
}
|
|
}
|
|
|
|
type loginFixtureFeed struct {
|
|
fixtureFeed
|
|
login string
|
|
}
|
|
|
|
func (f *loginFixtureFeed) Enrich(ctx context.Context, a []scraper.Article) ([]scraper.Article, error) {
|
|
scraper.MergeFetchMetrics(ctx, scraper.FetchMetrics{SchemaVersion: 1, Supported: true, Login: f.login}, len(a))
|
|
for i := range a {
|
|
a[i].Content = ""
|
|
a[i].ContentState = scraper.ContentUnavailable
|
|
}
|
|
return a, nil
|
|
}
|
|
func TestBenchLoginGateKeepsArticlesNotFetched(t *testing.T) {
|
|
for _, login := range []string{"unconfirmed", "refused"} {
|
|
rows, err := Run(context.Background(), []Source{{Key: "bloomberg", Feed: &loginFixtureFeed{login: login}}}, 3)
|
|
if err != nil || len(rows) != 1 {
|
|
t.Fatal("login fixture failed")
|
|
}
|
|
row := rows[0]
|
|
if row.NotFetched == nil || *row.NotFetched != 3 || row.Fetch.Login != login || row.Fetch.Attempted != 0 || row.Content.Unavailable != 3 {
|
|
t.Fatal("login gate lost not-fetched semantics")
|
|
}
|
|
}
|
|
}
|