Sort feed categories before serialization

A function is added for feeds and its categories normalization.
The test will ensure that the order is right.
This commit is contained in:
Ilya Glotov 2019-07-05 19:05:42 +03:00
parent f783b135c7
commit c840268678
No known key found for this signature in database
GPG Key ID: 5862B629BFB84D34
2 changed files with 54 additions and 17 deletions

View File

@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"encoding/xml"
"sort"
"miniflux.app/logger"
)
@ -18,23 +19,7 @@ func Serialize(subscriptions SubcriptionList) string {
writer := bufio.NewWriter(&b)
writer.WriteString(xml.Header)
feeds := new(opml)
feeds.Version = "2.0"
for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) {
category := outline{Text: categoryName}
for _, subscription := range subs {
category.Outlines = append(category.Outlines, outline{
Title: subscription.Title,
Text: subscription.Title,
FeedURL: subscription.FeedURL,
SiteURL: subscription.SiteURL,
})
}
feeds.Outlines = append(feeds.Outlines, category)
}
feeds := normalizeFeeds(subscriptions)
encoder := xml.NewEncoder(writer)
encoder.Indent(" ", " ")
if err := encoder.Encode(feeds); err != nil {
@ -54,3 +39,31 @@ func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]Subcript
return groups
}
func normalizeFeeds(subscriptions SubcriptionList) *opml {
feeds := new(opml)
feeds.Version = "2.0"
groupedSubs := groupSubscriptionsByFeed(subscriptions)
var categories []string
for k := range groupedSubs {
categories = append(categories, k)
}
sort.Strings(categories)
for _, categoryName := range categories {
category := outline{Text: categoryName}
for _, subscription := range groupedSubs[categoryName] {
category.Outlines = append(category.Outlines, outline{
Title: subscription.Title,
Text: subscription.Title,
FeedURL: subscription.FeedURL,
SiteURL: subscription.SiteURL,
})
}
feeds.Outlines = append(feeds.Outlines, category)
}
return feeds
}

View File

@ -40,3 +40,27 @@ func TestSerialize(t *testing.T) {
t.Error("Serialized feed is incorrect")
}
}
func TestNormalizedCategoriesOrder(t *testing.T) {
var orderTests = []struct {
naturalOrderName string
correctOrderName string
}{
{"Category 2", "Category 1"},
{"Category 3", "Category 2"},
{"Category 1", "Category 3"},
}
var subscriptions SubcriptionList
subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: orderTests[0].naturalOrderName})
subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: orderTests[1].naturalOrderName})
subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: orderTests[2].naturalOrderName})
feeds := normalizeFeeds(subscriptions)
for i, o := range orderTests {
if feeds.Outlines[i].Text != o.correctOrderName {
t.Fatalf("need %v, got %v", o.correctOrderName, feeds.Outlines[i].Text)
}
}
}