func TestMessage(t *testing.T) { mStore := new(mockStore) mTarget := new(mockTarget) mShort := new(mockShort) mockConf.Propagate.QtyPerPage = 1 Propagate(mStore, mTarget, mockPrices, mShort, *mockConf) if lastMsg != "" || lastUrl != "" { t.Error("There should be no messages, but got:", lastMsg, " with this url:", lastUrl) } var result feeds.RssResult result.Item.Title = "boom" result.Link = "http://www.boom.org" messages = append(messages, result) Propagate(mStore, mTarget, mockPrices, mShort, *mockConf) if lastMsg != "boom" || lastUrl != result.Link { t.Error("There should be boom messages, but got:", lastMsg, " with this url:", lastUrl) } var result2 feeds.RssResult result2.Item.Title = "bangbang" result2.Link = "http://www.bangbang.org" messages = append(messages, result2) Propagate(mStore, mTarget, mockPrices, mShort, *mockConf) if lastMsg != "bangbang" || lastUrl != result2.Link { t.Error("There should be bangbang messages, but got:", lastMsg, " with this url:", lastUrl) } }
func TestSend(t *testing.T) { mStore := new(mockStore) mTarget := new(mockTarget) mShort := new(mockShort) mockConf.Propagate.QtyPerPage = 1 var checkLastAreSent = func() { for i := range lastSentItems { if last[i] != lastSentItems[i] { t.Error("The ", i, " item in store was not sent") } } } var result feeds.RssResult result.Item.Title = "boom" result.Link = "http://www.boom.org" last = append(last, result) var result2 feeds.RssResult result2.Item.Title = "boomboom" result2.Link = "http://www.boomboom.org" last = append(last, result2) Propagate(mStore, mTarget, mockPrices, mShort, *mockConf) checkLastAreSent() mockConf.Propagate.QtyPerPage = 1000 Propagate(mStore, mTarget, mockPrices, mShort, *mockConf) checkLastAreSent() }
func (fs FileStore) Save(rssResult feeds.RssResult, link string, filename string) { if len(rssResult.Item.Title) == 0 { log.Printf("Skipping saving as this result has no title") return } if len(GotAlready) == 0 { GotAlready = fs.loadItems(filename) } else { log.Printf("Adding to the %v items done with %v \n", len(GotAlready), link) } GotAlready[link] = rssResult if rssResult.Failed { log.Printf("No message as the rss failed") } else { rssResult.Link = link toMessage = append(toMessage, rssResult) } jsave, _ := json.Marshal(GotAlready) var data []byte = jsave ioutil.WriteFile(filename, data, Perm) }
func TestPutInDateOrder(t *testing.T) { defer tearDown() var store = new(FileStore) store.ClearMessages() var mess = store.GetToMessage() if len(mess) != 0 { fmt.Println(mess) t.Error("Should have no new result to message but has", len(mess)) } var data = new(feeds.RssResult) data.Item.Title = "Golang content two hours ahead" data.Date = time.Now().Add(time.Hour) var link = "http://one" store.Save(*data, link, filename) mess = store.GetToMessage() if len(mess) != 1 { t.Error("Should have one new result to message but has", len(mess)) } var data2 = new(feeds.RssResult) data2.Item.Title = "Golang content one hour ahead" data2.Date = time.Now().Add(time.Hour).Add(time.Hour) var link2 = "http://two" store.Save(*data2, link2, filename) mess = store.GetToMessage() if len(mess) != 2 { t.Error("Should have two new result to message but has", len(mess)) } var gotit = store.AlreadyHave(link2, filename) if !gotit { t.Error(link2, " has been stored but isn't") } var data3 = new(feeds.RssResult) data3.Item.Title = "Golang content now" data3.Date = time.Now() var link3 = "http://three" store.Save(*data3, link3, filename) mess = store.GetToMessage() if len(mess) != 3 { t.Error("Should have three new result to message but has", len(mess)) } var data4 = new(feeds.RssResult) data4.Item.Title = "Golang content one hour behind" data4.Date = time.Now().Add(-time.Hour) data4.Failed = true var link4 = "http://four" store.Save(*data4, link4, filename) mess = store.GetToMessage() if len(mess) != 3 { t.Error("Should still have three new result to message but has", len(mess)) } var data5 = new(feeds.RssResult) data5.Item.Title = "Golang content two hours behind" data5.Date = time.Now().Add(-time.Hour).Add(-time.Hour) data5.Failed = false var link5 = "http://five.rss" store.Save(*data5, link5, filename) mess = store.GetToMessage() if len(mess) != 4 { t.Error("Should have four new result to message but has", len(mess)) } var res = store.GetLast(10, filename) if len(res) != 5 { t.Error("Should have five results with a title") } var check = func(no int, title string) { if res[no].Item.Title != title { fmt.Println(res[no]) t.Error("Expected '" + res[no].Item.Title + "' to equal '" + title + "'") } } check(0, data2.Item.Title) check(1, data.Item.Title) check(2, data3.Item.Title) check(3, data4.Item.Title) check(4, data5.Item.Title) gotit = store.AlreadyHave(link, filename) if !gotit { t.Error(link, " has been stored but isn't") } gotit = store.AlreadyHave(link3, filename) if !gotit { t.Error(link3, " has been stored but isn't") } gotit = store.AlreadyHave(link4, filename) if !gotit { t.Error(link4, " has been stored but isn't") } gotit = store.AlreadyHave(link5, filename) if !gotit { t.Error(link5, " has been stored but isn't") } gotit = store.AlreadyHave("no not yet rss feed link result", filename) if gotit { t.Error("This has not been stored but is") } res = store.GetLast(4, filename) if len(res) != 4 { t.Error("Should be restricted to four results with a title") } }