weiro/models/posts_test.go
Leon Mika e77cac2fd5 Started working on the frontend
- Added the new post frontend
- Hooked up publishing of posts to the site publisher
- Added an site exporter as a publishing target
2026-02-21 10:22:10 +11:00

79 lines
2.1 KiB
Go

package models
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFirstNWords(t *testing.T) {
tests := []struct {
words int
input string
expected string
}{
{words: 3, input: "This is a test string with multiple words", expected: "This is a"},
{words: 5, input: "Short string", expected: "Short string"},
{words: 0, input: "Empty string", expected: ""},
{words: 3, input: " The rain in Spain etc.", expected: "The rain in"},
{words: 3, input: " The? rain! in$ Spain etc.", expected: "The rain in"},
{words: 3, input: " !!! The 23123 rain ++_+_+ in Spain etc.", expected: "The 23123 rain"},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
result := firstNWords(test.input, test.words, wordForSlug)
assert.Equal(t, test.expected, result)
})
}
}
func TestPost_BestSlug(t *testing.T) {
postDate := time.Date(2023, time.January, 1, 15, 12, 11, 0, time.UTC)
tests := []struct {
name string
post Post
expected string
}{
{
name: "returns slug when slug is set",
post: Post{Slug: "my-custom-slug", Title: "My Title", PublishedAt: postDate},
expected: "my-custom-slug",
},
{
name: "use title when slug is empty",
post: Post{Slug: "", Title: "My Title", PublishedAt: postDate},
expected: "/2023/01/01/my-title",
},
{
name: "use body when slug is empty",
post: Post{Slug: "", Body: "My body", PublishedAt: postDate},
expected: "/2023/01/01/my-body",
},
{
name: "use guid when body is empty",
post: Post{GUID: "abc123", PublishedAt: postDate},
expected: "/2023/01/01/abc123",
},
{
name: "use time component when guid is empty",
post: Post{Slug: "", Title: "", PublishedAt: postDate},
expected: "/2023/01/01/151211",
},
{
name: "use created date if publish date is unset",
post: Post{Slug: "", Title: "a title", CreatedAt: postDate},
expected: "/2023/01/01/a-title",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.post.BestSlug()
assert.Equal(t, test.expected, result)
})
}
}