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