forked from Jeadie/notion-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion_dao_test.go
198 lines (179 loc) · 6.42 KB
/
notion_dao_test.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"github.com/jomei/notionapi"
"net/url"
"os"
"testing"
"time"
)
func runConstructNotionDaoFromEnvWith(rsskey string, content_database_id string, content_feeds_id string) (*NotionDao, error) {
if len(rsskey) > 0 {
os.Setenv("NOTION_RSS_KEY", rsskey)
}
if len(content_database_id) > 0 {
os.Setenv("NOTION_RSS_CONTENT_DATABASE_ID", content_database_id)
}
if len(content_feeds_id) > 0 {
os.Setenv("NOTION_RSS_FEEDS_DATABASE_ID", content_feeds_id)
}
nDao, err := ConstructNotionDaoFromEnv()
if len(rsskey) > 0 {
os.Unsetenv("NOTION_RSS_KEY")
}
if len(content_database_id) > 0 {
os.Unsetenv("NOTION_RSS_CONTENT_DATABASE_ID")
}
if len(content_feeds_id) > 0 {
os.Unsetenv("NOTION_RSS_FEEDS_DATABASE_ID")
}
return nDao, err
}
// getEnvWithDefault returns the environment variable (string), whether it existed (boolean), and
// the value to use (either the defaultValue, or the environment variable).
func getEnvWithDefault(key string, defaultValue string) (string, bool, string) {
value, exists := os.LookupEnv(key)
if exists {
return value, exists, value
} else {
return value, exists, defaultValue
}
}
func TestConstructNotionDaoFromEnv(t *testing.T) {
// Store environment variables, and calculate values (possibly with defaults).
PRIOR_NOTION_RSS_KEY, PNRK_exists, NOTION_RSS_KEY := getEnvWithDefault("NOTION_RSS_KEY", "NOTION_RSS_KEY")
PRIOR_NOTION_RSS_CONTENT_DATABASE_ID, PNRCDI_exists, NOTION_RSS_CONTENT_DATABASE_ID := getEnvWithDefault("NOTION_RSS_CONTENT_DATABASE_ID", "NOTION_RSS_CONTENT_DATABASE_ID")
PRIOR_NOTION_RSS_FEEDS_DATABASE_ID, PNRFDI_exists, NOTION_RSS_FEEDS_DATABASE_ID := getEnvWithDefault("NOTION_RSS_FEEDS_DATABASE_ID", "NOTION_RSS_FEEDS_DATABASE_ID")
os.Unsetenv("NOTION_RSS_KEY")
os.Unsetenv("NOTION_RSS_CONTENT_DATABASE_ID")
os.Unsetenv("NOTION_RSS_FEEDS_DATABASE_ID")
_, err := runConstructNotionDaoFromEnvWith("", NOTION_RSS_CONTENT_DATABASE_ID, NOTION_RSS_FEEDS_DATABASE_ID)
if err == nil {
t.Errorf("ConstructNotionDaoFromEnvWith should return error if `NOTION_RSS_KEY` is not set")
}
_, err = runConstructNotionDaoFromEnvWith(NOTION_RSS_KEY, "", NOTION_RSS_FEEDS_DATABASE_ID)
if err == nil {
t.Errorf("ConstructNotionDaoFromEnvWith should return error if `NOTION_RSS_KEY` is not set")
}
_, err = runConstructNotionDaoFromEnvWith(NOTION_RSS_KEY, NOTION_RSS_CONTENT_DATABASE_ID, "")
if err == nil {
t.Errorf("ConstructNotionDaoFromEnvWith should return error if `NOTION_RSS_KEY` is not set")
}
nDao, err := runConstructNotionDaoFromEnvWith(NOTION_RSS_KEY, NOTION_RSS_CONTENT_DATABASE_ID, NOTION_RSS_FEEDS_DATABASE_ID)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if nDao.client == nil {
t.Errorf("notion client was not constructed")
}
// Reset environment variables
if PNRK_exists {
os.Setenv("NOTION_RSS_KEY", PRIOR_NOTION_RSS_KEY)
}
if PNRCDI_exists {
os.Setenv("NOTION_RSS_CONTENT_DATABASE_ID", PRIOR_NOTION_RSS_CONTENT_DATABASE_ID)
}
if PNRFDI_exists {
os.Setenv("NOTION_RSS_FEEDS_DATABASE_ID", PRIOR_NOTION_RSS_FEEDS_DATABASE_ID)
}
}
// GetRssFeedFromDatabaseObject(p *notionapi.Page) (*FeedDatabaseItem, error) {
// urlProperty := p.Properties["Link"].(*notionapi.URLProperty).URL
// rssUrl, err := url.Parse(urlProperty)
// if err != nil {
// return &FeedDatabaseItem{}, err
// }
//
// nameRichTexts := p.Properties["Title"].(*notionapi.TitleProperty).Title
// if len(nameRichTexts) == 0 {
// return &FeedDatabaseItem{}, fmt.Errorf("RSS Feed database entry does not have any Title in 'Title' field")
// }
//
// return &FeedDatabaseItem{
// FeedLink: rssUrl,
// Created: p.CreatedTime,
// LastModified: p.LastEditedTime,
// Name: nameRichTexts[0].PlainText,
// }, nil
// }
func TestGetRssFeedFromDatabaseObject(t *testing.T) {
type TestCase struct {
page *notionapi.Page
expectedDbItem *FeedDatabaseItem
expectedErr error
subTestName string
}
editedTime := time.Now()
repoUrl, _ := url.Parse("https://github.com/Jeadie/notion-rss")
tests := []TestCase{
{
page: ¬ionapi.Page{
LastEditedTime: editedTime,
Properties: map[string]notionapi.Property{
"Title": ¬ionapi.TitleProperty{Title: []notionapi.RichText{{PlainText: "TestTitle"}}},
"Link": ¬ionapi.URLProperty{URL: repoUrl.String()},
"Enabled": ¬ionapi.CheckboxProperty{Checkbox: true},
},
},
expectedDbItem: &FeedDatabaseItem{
FeedLink: repoUrl,
Name: "TestTitle",
LastModified: editedTime,
},
expectedErr: nil,
subTestName: "valid parsing",
},
{
page: ¬ionapi.Page{
LastEditedTime: editedTime,
Properties: map[string]notionapi.Property{
"Title": ¬ionapi.TitleProperty{},
"Link": ¬ionapi.URLProperty{URL: repoUrl.String()},
"Enabled": ¬ionapi.CheckboxProperty{Checkbox: true},
},
},
expectedDbItem: &FeedDatabaseItem{},
expectedErr: fmt.Errorf("failed"),
subTestName: "no Title element in TitleProperty",
},
{
page: ¬ionapi.Page{
LastEditedTime: editedTime,
Properties: map[string]notionapi.Property{
"Link": ¬ionapi.URLProperty{URL: repoUrl.String()},
"Enabled": ¬ionapi.CheckboxProperty{Checkbox: true},
},
},
expectedDbItem: &FeedDatabaseItem{},
expectedErr: fmt.Errorf("failed"),
subTestName: "Missing TitleProperty",
},
}
for _, test := range tests {
t.Run(test.subTestName, func(t *testing.T) {
item, err := GetRssFeedFromDatabaseObject(test.page)
if (err != nil) != (test.expectedErr != nil) {
if err != nil {
t.Errorf("Unexpected error occurred. Error: %s \n", err.Error())
} else {
t.Errorf("Error was expected, but none returned. Expected error: %s \n", test.expectedErr.Error())
}
}
if test.expectedErr == nil {
expectedItem := test.expectedDbItem
if item.Name != expectedItem.Name {
t.Errorf("Incorrect name of item. Expected %s, returned %s", expectedItem.Name, item.Name)
}
if item.FeedLink.String() != expectedItem.FeedLink.String() {
t.Errorf("Incorrect RSS feed url. Expected %s, returned %s", expectedItem.FeedLink, item.FeedLink)
}
if item.Created != expectedItem.Created {
t.Errorf("Incorrect created timestamp. Expected %s, returned %s", expectedItem.Created, item.Created)
}
if item.LastModified != expectedItem.LastModified {
t.Errorf("Incorrect last modified timestamp. Expected %s, returned %s", expectedItem.LastModified, item.LastModified)
}
}
})
}
}