78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
package hnblogs
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestFetchBlogs(t *testing.T) {
|
|
blogs, err := FetchBlogs()
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if len(blogs) == 0 {
|
|
t.Fatalf("Expected to fetch some blogs, got %d", len(blogs))
|
|
}
|
|
}
|
|
|
|
func TestGetBlogs(t *testing.T) {
|
|
blogs, err := GetBlogs()
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if len(blogs) == 0 {
|
|
t.Fatalf("Expected to fetch some blogs, got %d", len(blogs))
|
|
}
|
|
}
|
|
|
|
func TestRandomBlog(t *testing.T) {
|
|
blog, err := RandomBlog()
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if blog.URL == "" {
|
|
t.Fatalf("Expected a valid blog, got an empty URL")
|
|
}
|
|
}
|
|
|
|
func TestRandomBlogs(t *testing.T) {
|
|
n := 5
|
|
randomBlogs, err := RandomBlogs(n)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if len(randomBlogs) != n {
|
|
t.Fatalf("Expected %d random blogs, got %d", n, len(randomBlogs))
|
|
}
|
|
|
|
for _, blog := range randomBlogs {
|
|
if blog.URL == "" {
|
|
t.Fatalf("Expected a valid blog, got an empty URL")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNthBlog(t *testing.T) {
|
|
blogs, err := GetBlogs()
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
blog, err := NthBlog(5)
|
|
if err != nil {
|
|
t.Fatalf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if blog.URL != blogs[5].URL {
|
|
t.Fatalf("Expected %s, got %s", blogs[5].URL, blog.URL)
|
|
}
|
|
|
|
_, err = NthBlog(len(blogs))
|
|
if err == nil {
|
|
t.Fatalf("Expected error for out-of-range index, got none")
|
|
}
|
|
}
|