Skip to content

Commit

Permalink
fix to pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
p1ass committed Mar 30, 2021
1 parent 4c115c4 commit 2266431
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ commands:
steps:
- run:
name: Vendoring
command: go mod download
command: |
go mod download
test:
steps:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/p1ass/feeder
go 1.14

require (
github.com/google/go-cmp v0.5.5
github.com/kr/pretty v0.2.1
github.com/otiai10/opengraph v1.1.3
github.com/p1ass/feeds v1.1.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -29,5 +31,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEha
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
39 changes: 27 additions & 12 deletions rss_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,17 @@ func (crawler *rssCrawler) Crawl() ([]*Item, error) {
}

func convertRssItemToItem(i *feeds.RssItem) (*Item, error) {
layouts := []string{time.RFC1123, time.RFC1123Z}
var t time.Time
var err error
for _, layout := range layouts {
t, err = time.Parse(layout, i.PubDate)
if err == nil {
break
}
}
if err != nil {
return nil, errors.Wrap(err, "Parse Error")
t,err := parseTime(i.PubDate)
if err != nil{
return nil, errors.Wrap(err, "parse pub date")
}

item := &Item{
Title: i.Title,
Link: &Link{Href: i.Link},
Description: i.Description,
ID: i.Guid,
Created: &t,
Created: t,
}

if i.Author != "" {
Expand All @@ -85,3 +77,26 @@ func convertRssItemToItem(i *feeds.RssItem) (*Item, error) {

return item, nil
}

func parseTime(datetime string) (*time.Time, error) {
t, err := time.Parse(time.RFC1123, datetime)
if err == nil {
return &t, nil
}

t, err = time.Parse(time.RFC1123Z, datetime)
if err != nil {
return nil, errors.Wrap(err, "Parse Error")
}

loc, err := time.LoadLocation(t.Location().String())
if err != nil {
return nil, errors.Wrap(err, "Parse Error")
}

if t, err = time.ParseInLocation(time.RFC1123Z, datetime, loc); err == nil {
return &t, nil
}

return nil, errors.Wrap(err, "Parse Error")
}
53 changes: 26 additions & 27 deletions rss_crawler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package feeder_test
import (
"io/ioutil"
"os"
"reflect"
"testing"
"time"

"github.com/kr/pretty"
"github.com/google/go-cmp/cmp"
"github.com/p1ass/feeder"
)

Expand All @@ -28,6 +27,7 @@ func TestRSSFetch(t *testing.T) {

publishedString := "2019-01-01T00:00:00+09:00"
published, _ := time.Parse(time.RFC3339, publishedString)

expected := []*feeder.Item{{
Title: "title RFC1123Z",
Link: &feeder.Link{
Expand All @@ -49,38 +49,37 @@ func TestRSSFetch(t *testing.T) {
},
Content: "",
},
{
Title: "title RFC1123",
Link: &feeder.Link{
Href: "http://example.com",
Rel: "",
},
Source: nil,
Author: &feeder.Author{
Name: "name",
},
Description: "summary_content",
ID: "id",
Updated: nil,
Created: &published,
Enclosure: &feeder.Enclosure{
URL: "http://example.com/image.png",
Type: "image/png",
Length: "0",
},
Content: "",
}}
{
Title: "title RFC1123",
Link: &feeder.Link{
Href: "http://example.com",
Rel: "",
},
Source: nil,
Author: &feeder.Author{
Name: "name",
},
Description: "summary_content",
ID: "id",
Updated: nil,
Created: &published,
Enclosure: &feeder.Enclosure{
URL: "http://example.com/image.png",
Type: "image/png",
Length: "0",
},
Content: "",
}}

crawler := feeder.NewRSSCrawler(server.URL + "/rss")
got, err := crawler.Crawl()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(expected, got) {
diffs := pretty.Diff(expected, got)
t.Log(pretty.Println(diffs))
if !cmp.Equal(expected, got) {
diff := cmp.Diff(expected, got)
t.Log(diff)
t.Error("Failed to convert AtomEntry to Item.")

}
}

0 comments on commit 2266431

Please sign in to comment.