60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package parser
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/yourname/reddit-scraper/internal/fetcher"
|
|
)
|
|
|
|
func TestParseJSONPost_Valid(t *testing.T) {
|
|
j := fetcher.JSONPost{
|
|
ID: "abc123",
|
|
Subreddit: "golang",
|
|
Title: "An interesting post",
|
|
Author: "alice",
|
|
CreatedUTC: 1620000000,
|
|
Score: 10,
|
|
NumComments: 2,
|
|
Selftext: "Hello world",
|
|
URL: "https://reddit.com/r/golang/comments/abc123",
|
|
Permalink: "/r/golang/comments/abc123",
|
|
}
|
|
p, err := ParseJSONPost(j)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if p.ID != j.ID {
|
|
t.Fatalf("id mismatch: %s != %s", p.ID, j.ID)
|
|
}
|
|
if p.Author != "alice" {
|
|
t.Fatalf("author mismatch: %s", p.Author)
|
|
}
|
|
}
|
|
|
|
func TestParseJSONPost_DeletedAuthor(t *testing.T) {
|
|
j := fetcher.JSONPost{
|
|
ID: "d1",
|
|
Subreddit: "test",
|
|
Title: "post",
|
|
Author: "",
|
|
CreatedUTC: 1600000000,
|
|
}
|
|
p, err := ParseJSONPost(j)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if p.Author != "[deleted]" {
|
|
t.Fatalf("expected [deleted], got %s", p.Author)
|
|
}
|
|
}
|
|
|
|
func TestParseJSONPost_MissingFields(t *testing.T) {
|
|
j := fetcher.JSONPost{
|
|
ID: "",
|
|
}
|
|
_, err := ParseJSONPost(j)
|
|
if err == nil {
|
|
t.Fatalf("expected error for missing id")
|
|
}
|
|
}
|