Files
go-extractor/sites/powerball/extract_test.go
Steve Dudenhoeffer 198906946b
All checks were successful
CI / vet (pull_request) Successful in 1m5s
CI / build (pull_request) Successful in 1m6s
CI / test (pull_request) Successful in 1m6s
test: add mock-based site extractor test infrastructure
Create exported extractortest package with MockBrowser, MockDocument,
and MockNode that support selector-based responses for testing site
extractors without a real browser.

Add extraction tests for DuckDuckGo (result parsing, empty results, no
links, full search flow) and Powerball (drawing parsing, next drawing
parsing with billion/million, error cases, full GetCurrent flow).

Closes #21
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 16:37:58 +00:00

168 lines
4.3 KiB
Go

package powerball
import (
"context"
"testing"
"time"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
"gitea.stevedudenhoeffer.com/steve/go-extractor/extractortest"
)
func makePowerballDoc() *extractortest.MockDocument {
return &extractortest.MockDocument{
URLValue: "https://www.powerball.com/",
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"#numbers .title-date": {
&extractortest.MockNode{TextValue: "Sat, Feb 15, 2026"},
},
"div.game-ball-group div.white-balls": {
&extractortest.MockNode{TextValue: "7"},
&extractortest.MockNode{TextValue: "14"},
&extractortest.MockNode{TextValue: "21"},
&extractortest.MockNode{TextValue: "35"},
&extractortest.MockNode{TextValue: "62"},
},
"div.game-ball-group div.powerball": {
&extractortest.MockNode{TextValue: "10"},
},
"span.power-play span.multiplier": {
&extractortest.MockNode{TextValue: "3X"},
},
"div.next-powerball h5.title-date": {
&extractortest.MockNode{TextValue: "Mon, Feb 17, 2026"},
},
"div.next-powerball div.game-detail-group span.game-jackpot-number": {
&extractortest.MockNode{TextValue: "$1.5 Billion"},
},
},
},
}
}
func TestGetDrawing(t *testing.T) {
doc := makePowerballDoc()
drawing, err := getDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getDrawing() error: %v", err)
}
expectedDate := time.Date(2026, 2, 15, 0, 0, 0, 0, time.UTC)
if !drawing.Date.Equal(expectedDate) {
t.Errorf("Date = %v, want %v", drawing.Date, expectedDate)
}
expectedNums := [5]int{7, 14, 21, 35, 62}
if drawing.Numbers != expectedNums {
t.Errorf("Numbers = %v, want %v", drawing.Numbers, expectedNums)
}
if drawing.PowerBall != 10 {
t.Errorf("PowerBall = %d, want 10", drawing.PowerBall)
}
if drawing.PowerPlay != 3 {
t.Errorf("PowerPlay = %d, want 3", drawing.PowerPlay)
}
}
func TestGetNextDrawing(t *testing.T) {
doc := makePowerballDoc()
nd, err := getNextDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getNextDrawing() error: %v", err)
}
if nd.Date != "Mon, Feb 17, 2026" {
t.Errorf("Date = %q, want %q", nd.Date, "Mon, Feb 17, 2026")
}
if nd.JackpotDollars != 1500000000 {
t.Errorf("JackpotDollars = %d, want 1500000000", nd.JackpotDollars)
}
}
func TestGetNextDrawing_Million(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"div.next-powerball h5.title-date": {
&extractortest.MockNode{TextValue: "Wed, Feb 19, 2026"},
},
"div.next-powerball div.game-detail-group span.game-jackpot-number": {
&extractortest.MockNode{TextValue: "$250 Million"},
},
},
},
}
nd, err := getNextDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getNextDrawing() error: %v", err)
}
if nd.JackpotDollars != 250000000 {
t.Errorf("JackpotDollars = %d, want 250000000", nd.JackpotDollars)
}
}
func TestGetCurrent_Integration(t *testing.T) {
doc := makePowerballDoc()
browser := &extractortest.MockBrowser{
Documents: map[string]*extractortest.MockDocument{
"https://www.powerball.com/": doc,
},
}
drawing, nd, err := GetCurrent(context.Background(), browser)
if err != nil {
t.Fatalf("GetCurrent() error: %v", err)
}
if drawing.PowerBall != 10 {
t.Errorf("PowerBall = %d, want 10", drawing.PowerBall)
}
if nd.JackpotDollars != 1500000000 {
t.Errorf("JackpotDollars = %d, want 1500000000", nd.JackpotDollars)
}
}
func TestGetDrawing_MissingDate(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{},
},
}
_, err := getDrawing(context.Background(), doc)
if err == nil {
t.Error("expected error for missing date element")
}
}
func TestGetDrawing_WrongBallCount(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"#numbers .title-date": {
&extractortest.MockNode{TextValue: "Sat, Feb 15, 2026"},
},
"div.game-ball-group div.white-balls": {
&extractortest.MockNode{TextValue: "7"},
&extractortest.MockNode{TextValue: "14"},
},
},
},
}
_, err := getDrawing(context.Background(), doc)
if err == nil {
t.Error("expected error for wrong number of white balls")
}
}