1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-15 19:15:24 +00:00
amfora/cache/page_test.go

72 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-06-18 20:54:48 +00:00
package cache
import (
"testing"
"github.com/makeworld-the-better-one/amfora/structs"
"github.com/stretchr/testify/assert"
)
var p = structs.Page{URL: "example.com"}
var p2 = structs.Page{URL: "example.org"}
2020-06-18 20:54:48 +00:00
func reset() {
2020-08-05 01:05:12 +00:00
ClearPages()
2020-06-18 20:54:48 +00:00
SetMaxPages(0)
SetMaxSize(0)
}
func TestMaxPages(t *testing.T) {
reset()
SetMaxPages(1)
2020-08-05 01:05:12 +00:00
AddPage(&p)
AddPage(&p2)
2020-06-18 20:54:48 +00:00
assert.Equal(t, 1, NumPages(), "there should only be one page")
}
func TestMaxSize(t *testing.T) {
reset()
assert := assert.New(t)
SetMaxSize(p.Size())
2020-08-05 01:05:12 +00:00
AddPage(&p)
2020-06-18 20:54:48 +00:00
assert.Equal(1, NumPages(), "one page should be added")
2020-08-05 01:05:12 +00:00
AddPage(&p2)
2020-06-18 20:54:48 +00:00
assert.Equal(1, NumPages(), "there should still be just one page due to cache size limits")
assert.Equal(p2.URL, urls[0], "the only page url should be the second page one")
2020-06-18 20:54:48 +00:00
}
func TestRemove(t *testing.T) {
reset()
2020-08-05 01:05:12 +00:00
AddPage(&p)
RemovePage(p.URL)
2020-06-18 20:54:48 +00:00
assert.Equal(t, 0, NumPages(), "there shouldn't be any pages after the removal")
}
func TestClearAndNumPages(t *testing.T) {
reset()
2020-08-05 01:05:12 +00:00
AddPage(&p)
ClearPages()
2020-06-18 20:54:48 +00:00
assert.Equal(t, 0, len(pages), "map should be empty")
assert.Equal(t, 0, len(urls), "urls slice shoulde be empty")
assert.Equal(t, 0, NumPages(), "NumPages should report empty too")
}
func TestSize(t *testing.T) {
reset()
2020-08-05 01:05:12 +00:00
AddPage(&p)
assert.Equal(t, p.Size(), SizePages(), "sizes should match")
2020-06-18 20:54:48 +00:00
}
func TestGet(t *testing.T) {
reset()
2020-08-05 01:05:12 +00:00
AddPage(&p)
AddPage(&p2)
page, ok := GetPage(p.URL)
2020-06-18 20:54:48 +00:00
if !ok {
t.Fatal("Get should say that the page was found")
}
if page.URL != p.URL {
2020-06-18 20:54:48 +00:00
t.Error("page urls don't match")
}
}