53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package scraper
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// TestParseRankingsFixture parses the real rankings fragment captured live
|
|
// (internal/scraper/testdata/scoreboard_page1.html) and sanity-checks it.
|
|
func TestParseRankingsFixture(t *testing.T) {
|
|
html, err := os.ReadFile("testdata/scoreboard_page1.html")
|
|
if err != nil {
|
|
t.Fatalf("read fixture: %v", err)
|
|
}
|
|
|
|
entries, err := parseRankings(string(html))
|
|
if err != nil {
|
|
t.Fatalf("parseRankings: %v", err)
|
|
}
|
|
|
|
if len(entries) != pageSize {
|
|
t.Errorf("got %d entries, want %d", len(entries), pageSize)
|
|
}
|
|
|
|
first := entries[0]
|
|
if first.Username == "" {
|
|
t.Error("first username is empty")
|
|
}
|
|
if first.Score <= 0 {
|
|
t.Errorf("first score = %d, want > 0", first.Score)
|
|
}
|
|
|
|
// Every entry must have a username and positive score. The fragment is
|
|
// returned in score-descending order (root-me's displayed "# N" rank is
|
|
// noisy around ties, so we assert on score, not rank).
|
|
prev := first.Score
|
|
for i, e := range entries {
|
|
if e.Username == "" {
|
|
t.Errorf("entry %d has empty username", i)
|
|
}
|
|
if e.Score <= 0 {
|
|
t.Errorf("entry %d (%s) has non-positive score %d", i, e.Username, e.Score)
|
|
}
|
|
if e.Score > prev {
|
|
t.Errorf("entry %d score %d > previous %d (not descending)", i, e.Score, prev)
|
|
}
|
|
prev = e.Score
|
|
}
|
|
|
|
t.Logf("parsed %d entries; #1 = %s (%s, grade=%s) score=%d",
|
|
len(entries), first.Username, first.Country, first.Grade, first.Score)
|
|
}
|