forked from wgroeneveld/go-jamming
link collecting finishing touches
This commit is contained in:
parent
044483f646
commit
d9ded09383
|
@ -44,7 +44,7 @@ func (snder *Sender) Collect(xml string, since time.Time) ([]RSSItem, error) {
|
||||||
}
|
}
|
||||||
var items []RSSItem
|
var items []RSSItem
|
||||||
for _, rssitem := range feed.ItemList {
|
for _, rssitem := range feed.ItemList {
|
||||||
if since.Before(rssitem.PubDateAsTime()) {
|
if since.IsZero() || since.Before(rssitem.PubDateAsTime()) {
|
||||||
items = append(items, RSSItem{
|
items = append(items, RSSItem{
|
||||||
link: rssitem.Link,
|
link: rssitem.Link,
|
||||||
hrefs: snder.collectUniqueHrefsFromDescription(rssitem.Description),
|
hrefs: snder.collectUniqueHrefsFromDescription(rssitem.Description),
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CollectSuite struct {
|
type CollectSuite struct {
|
||||||
|
@ -43,17 +44,50 @@ func (s *CollectSuite) TestCollectShouldNotContainHrefsFromBlockedDomains() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CollectSuite) TestCollectShouldNotContainHrefsThatPointToImages() {
|
func (s *CollectSuite) TestCollectShouldNotContainHrefsThatPointToImages() {
|
||||||
|
items, err := s.snder.Collect(s.xml, common.IsoToTime("2021-03-14T00:00:00.000Z"))
|
||||||
|
assert.NoError(s.T(), err)
|
||||||
|
last := items[len(items)-1]
|
||||||
|
// test case:
|
||||||
|
// contains e.g. https://chat.brainbaking.com/media/6f8b72ca-9bfb-460b-9609-c4298a8cab2b/EuropeBattle%202021-03-14%2016-20-36-87.jpg
|
||||||
|
assert.ElementsMatch(s.T(), []string{
|
||||||
|
"/about",
|
||||||
|
}, last.hrefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CollectSuite) TestCollectNothingIfDateInFutureAndSinceNothingNewInFeed() {
|
func (s *CollectSuite) TestCollectNothingIfDateInFutureAndSinceNothingNewInFeed() {
|
||||||
|
items, err := s.snder.Collect(s.xml, time.Now().Add(time.Duration(600)*time.Hour))
|
||||||
|
assert.NoError(s.T(), err)
|
||||||
|
assert.Equal(s.T(), 0, len(items))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CollectSuite) TestCollectLatestXLinksWhenASinceParameterIsProvided() {
|
func (s *CollectSuite) TestCollectLatestXLinksWhenASinceParameterIsProvided() {
|
||||||
|
items, err := s.snder.Collect(s.xml, common.IsoToTime("2021-03-15T00:00:00.000Z"))
|
||||||
|
assert.NoError(s.T(), err)
|
||||||
|
assert.Equal(s.T(), 3, len(items))
|
||||||
|
|
||||||
|
last := items[len(items)-1]
|
||||||
|
assert.Equal(s.T(), "https://brainbaking.com/notes/2021/03/15h14m43s49/", last.link)
|
||||||
|
assert.ElementsMatch(s.T(), []string{
|
||||||
|
"http://replit.com",
|
||||||
|
"http://codepen.io",
|
||||||
|
"https://kuleuven-diepenbeek.github.io/osc-course/ch1-c/intro/",
|
||||||
|
"/about",
|
||||||
|
}, last.hrefs)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CollectSuite) TestCollectEveryExternalLinkWithoutAValidSinceDate() {
|
func (s *CollectSuite) TestCollectEveryExternalLinkWithoutAValidSinceDate() {
|
||||||
|
// no valid since date = zero time passed.
|
||||||
|
items, err := s.snder.Collect(s.xml, time.Time{})
|
||||||
|
assert.NoError(s.T(), err)
|
||||||
|
assert.Equal(s.T(), 141, len(items))
|
||||||
|
|
||||||
|
first := items[0]
|
||||||
|
assert.Equal(s.T(), "https://brainbaking.com/notes/2021/03/16h17m07s14/", first.link)
|
||||||
|
assert.ElementsMatch(s.T(), []string{
|
||||||
|
"https://fosstodon.org/@celia",
|
||||||
|
"https://fosstodon.org/@kev",
|
||||||
|
"/about",
|
||||||
|
}, first.hrefs)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// https://labs.yulrizka.com/en/stubbing-time-dot-now-in-golang/
|
// https://labs.yulrizka.com/en/stubbing-time-dot-now-in-golang/
|
||||||
// None of the above are very appealing. For now, just use the lazy way.
|
// None of the above are very appealing. For now, just use the lazy way.
|
||||||
|
@ -8,9 +11,13 @@ var Now = time.Now
|
||||||
|
|
||||||
// since should be in ISO String format, as produced by clients using day.js - e.g. 2021-04-09T15:51:43.732Z
|
// since should be in ISO String format, as produced by clients using day.js - e.g. 2021-04-09T15:51:43.732Z
|
||||||
func IsoToTime(since string) time.Time {
|
func IsoToTime(since string) time.Time {
|
||||||
|
if since == "" {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
layout := "2006-01-02T15:04:05.000Z"
|
layout := "2006-01-02T15:04:05.000Z"
|
||||||
t, err := time.Parse(layout, since)
|
t, err := time.Parse(layout, since)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Warn().Str("time", since).Msg("Invalid ISO date, reverting to now()")
|
||||||
return Now()
|
return Now()
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
|
|
|
@ -34,12 +34,12 @@ func (s *TimeSuite) TestIsoToTimeInISOString() {
|
||||||
assert.Equal(s.T(), expectedtime.Second(), since.Second())
|
assert.Equal(s.T(), expectedtime.Second(), since.Second())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TimeSuite) TestIsoToTimeInvalidStringReturnsNow() {
|
func (s *TimeSuite) TestIsoToTimeInvalidStringReturnsNowTime() {
|
||||||
since := IsoToTime("woef ik ben een hondje")
|
since := IsoToTime("woef ik ben een hondje")
|
||||||
assert.Equal(s.T(), s.nowtime, since)
|
assert.Equal(s.T(), s.nowtime, since)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TimeSuite) TestIsoToTimeEmptyReturnsNow() {
|
func (s *TimeSuite) TestIsoToTimeEmptyReturnsZeroTime() {
|
||||||
since := IsoToTime("")
|
since := IsoToTime("")
|
||||||
assert.Equal(s.T(), s.nowtime, since)
|
assert.True(s.T(), since.IsZero())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue