2024-02-29 05:41:36 +00:00
|
|
|
package feed_test
|
2024-01-19 03:33:37 +00:00
|
|
|
|
2024-03-03 21:10:34 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2024-03-06 20:27:37 +00:00
|
|
|
"git.32bit.cafe/yequari/webweav.ing/feed"
|
2024-03-03 21:10:34 +00:00
|
|
|
)
|
2024-01-19 03:33:37 +00:00
|
|
|
|
2024-03-06 20:27:37 +00:00
|
|
|
func TestTimeParsing(t *testing.T) {
|
|
|
|
testDate, err := time.Parse("2006-Jan-02 15:04:05 -7", "2004-May-14 07:30:55 -7")
|
2024-01-19 03:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("creating test date failed: %s", err)
|
|
|
|
}
|
2024-03-06 20:27:37 +00:00
|
|
|
|
2024-01-19 03:33:37 +00:00
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
input string
|
2024-03-06 20:27:37 +00:00
|
|
|
format string
|
2024-01-19 03:33:37 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-03-06 20:27:37 +00:00
|
|
|
"YYYY-MM-DD",
|
|
|
|
`<html><head></head><body><article><time datetime="2004-05-14">May 14 2004</time>hello world</article></body></html>`,
|
|
|
|
time.DateOnly,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"YYYY-MM-DD HH:MM",
|
|
|
|
`<html><head></head><body><article><time datetime="2004-05-14 07:30">May 14 2004</time>hello world</article></body></html>`,
|
|
|
|
"2006-01-02 15:04",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"YYYY-MM-DD HH:MM:SS",
|
|
|
|
`<html><head></head><body><article><time datetime="2004-05-14 07:30:55">May 14 2004</time>hello world</article></body></html>`,
|
|
|
|
"2006-01-02 15:04:05",
|
2024-01-19 03:33:37 +00:00
|
|
|
},
|
|
|
|
{
|
2024-03-06 20:27:37 +00:00
|
|
|
"YYYY-MM-DDTHH:MM:SS",
|
|
|
|
`<html><head></head><body><article><time datetime="2004-05-14T07:30:55">May 14 2004</time>hello world</article></body></html>`,
|
|
|
|
"2006-01-02T15:04:05",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"YYYY-MM-DDTHH:MM",
|
|
|
|
`<html><head></head><body><article><time datetime="2004-05-14T07:30">May 14 2004</time>hello world</article></body></html>`,
|
|
|
|
"2006-01-02T15:04",
|
2024-01-19 03:33:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func (t *testing.T) {
|
2024-03-06 20:27:37 +00:00
|
|
|
item := feed.FeedItem{}
|
|
|
|
err := item.ParseContent(tt.input)
|
2024-01-19 03:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error: %s", err)
|
|
|
|
}
|
2024-03-06 20:27:37 +00:00
|
|
|
|
|
|
|
actualTime := item.PubTime.Format(tt.format)
|
|
|
|
expectedTime := testDate.Format(tt.format)
|
|
|
|
if actualTime != expectedTime {
|
|
|
|
t.Errorf("got %s, want %s", actualTime, expectedTime)
|
2024-01-19 03:33:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 05:41:36 +00:00
|
|
|
|
2024-03-06 20:27:37 +00:00
|
|
|
|
2024-02-29 05:41:36 +00:00
|
|
|
func TestParseFeedItem(t *testing.T) {
|
|
|
|
testDate, err := time.Parse("2006-Jan-02", "2004-May-14")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("creating test date failed: %s", err)
|
|
|
|
}
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
want_time *time.Time
|
|
|
|
want_article string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"article and time stripped out of basic HTML",
|
|
|
|
"<html><head></head><body><article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article></body></html>",
|
|
|
|
&testDate,
|
|
|
|
"<article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article>",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func (t *testing.T) {
|
|
|
|
item := feed.FeedItem{}
|
|
|
|
err := item.ParseContent(tt.input)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error: %s", err)
|
|
|
|
}
|
|
|
|
if item.RawText != tt.want_article {
|
|
|
|
t.Errorf("got %s, want %s", item.RawText, tt.want_article)
|
|
|
|
}
|
|
|
|
if tt.want_time != nil && !item.PubTime.Equal(*tt.want_time) {
|
|
|
|
t.Errorf("got %s, want %s", item.PubTime, *tt.want_time)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|