~chrisppy/go-barefeed

ref: 63fc114fd6c8a8779e44c7042a5c1a6f543a76fb go-barefeed/feed_test.go -rw-r--r-- 2.9 KiB
63fc114fChris Palmer updates from schema 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package barefeed

import (
	"sort"
	"testing"
	"time"
)

func testFeeds(t *testing.T, expected, actual map[string]Feed) {
	if len(expected) != len(actual) {
		t.Errorf("Expected %d feeds, actual %d feeds", len(expected), len(actual))
	}

	for k, ev := range expected {
		rv, ok := actual[k]
		if !ok {
			t.Errorf("expected key %s not found", k)
		}

		r := rv.(*FeedV1)
		e := ev.(*FeedV1)
		if e.Path != r.Path {
			t.Errorf("Expected %s, actual %s", e.Path, r.Path)
		}
		if r.ID != e.ID {
			t.Errorf("Expected %s, actual %s", e.ID, r.ID)
		}
		if r.Generator != e.Generator {
			if e.Generator == nil {
				t.Errorf("Expected nil, actual %s", *r.Generator)
			} else if r.Generator == nil {
				t.Errorf("Expected %s, actual nil", *e.Generator)
			} else if *r.Generator != *e.Generator {
				t.Errorf("Expected %s, actual %s", *e.Generator, *r.Generator)
			}
		}
		if r.FeedType != e.FeedType {
			t.Errorf("Expected %d, actual %d", e.FeedType, r.FeedType)
		}
		if r.Updated != e.Updated {
			t.Errorf("Expected %v, actual %v", e.Updated, r.Updated)
		}
		if r.Description != e.Description {
			if e.Description == nil {
				t.Errorf("Expected nil, actual %v", *r.Description)
			} else if r.Description == nil {
				t.Errorf("Expected %v, actual nil", *e.Description)
			} else {
				testText(t, *e.Description, *r.Description)
			}
		}

		testText(t, e.Title, r.Title)
		testPersons(t, e.Authors, r.Authors)
		testLinks(t, e.Links, r.Links)
		testEntryList(t, e.Entries, r.Entries)
	}
}

func TestFeedSort(t *testing.T) {
	a := getFeed("Omega")
	b := getFeed("beta")
	c := getFeed("Alpha")

	f := Feeds{a, c, b}
	sort.Sort(f)

	if f[0].(*FeedV1).Title.Value != c.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", c.(*FeedV1).Title.Value, f[0].(*FeedV1).Title.Value)
	}
	if f[1].(*FeedV1).Title.Value != b.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", b.(*FeedV1).Title.Value, f[1].(*FeedV1).Title.Value)
	}
	if f[2].(*FeedV1).Title.Value != a.(*FeedV1).Title.Value {
		t.Errorf("Expected %v, actual %v", a.(*FeedV1).Title.Value, f[2].(*FeedV1).Title.Value)
	}
}

func getFeeds() map[string]Feed {
	m := make(map[string]Feed)

	fd := getFeed("test title")
	m[fd.(*FeedV1).Path] = fd

	return m
}

func getFeed(title string) Feed {
	gen := "test gen"
	desc := getText()
	f := FeedV1{
		Path:        "https://example.com/feed.xml",
		ID:          "urn:uuid:22fea9cf-095c-4634-95f5-d0b72f99944f",
		Generator:   &gen,
		FeedType:    ATOM,
		Title:       getText(),
		Updated:     ToTimestamp(time.Now()),
		Description: &desc,
		Links:       getLinks(),
		Authors:     getPersons(),
		Entries:     getEntryList(),
	}
	f.Title.Value = title

	var fd Feed = &f
	return fd
}

func TestFeedType(t *testing.T) {
	e := RSS
	a := ToFeedType(e.String())
	if e != a {
		t.Errorf("Expected %d, actual %d", e, a)
	}

	e = ATOM
	a = ToFeedType(e.String())
	if e != a {
		t.Errorf("Expected %d, actual %d", e, a)
	}
}