ccache/bucket_test.go

65 lines
1.4 KiB
Go
Raw Normal View History

2013-10-19 14:36:33 +02:00
package ccache
import (
"github.com/karlseguin/gspec"
"testing"
"time"
2013-10-19 14:36:33 +02:00
)
func TestGetMissFromBucket(t *testing.T) {
bucket := testBucket()
gspec.New(t).Expect(bucket.get("invalid")).ToBeNil()
2013-10-19 14:36:33 +02:00
}
func TestGetHitFromBucket(t *testing.T) {
bucket := testBucket()
item := bucket.get("power")
assertValue(t, item, "9000")
2013-10-19 14:36:33 +02:00
}
func TestDeleteItemFromBucket(t *testing.T) {
bucket := testBucket()
bucket.delete("power")
gspec.New(t).Expect(bucket.get("power")).ToBeNil()
2013-10-19 14:36:33 +02:00
}
func TestSetsANewBucketItem(t *testing.T) {
spec := gspec.New(t)
bucket := testBucket()
item, new := bucket.set("spice", TestValue("flow"), time.Minute)
assertValue(t, item, "flow")
item = bucket.get("spice")
assertValue(t, item, "flow")
spec.Expect(new).ToEqual(true)
2013-10-19 14:36:33 +02:00
}
func TestSetsAnExistingItem(t *testing.T) {
spec := gspec.New(t)
bucket := testBucket()
item, new := bucket.set("power", TestValue("9002"), time.Minute)
assertValue(t, item, "9002")
item = bucket.get("power")
assertValue(t, item, "9002")
spec.Expect(new).ToEqual(false)
2013-10-19 14:36:33 +02:00
}
func testBucket() *Bucket {
b := &Bucket{lookup: make(map[string]*Item)}
b.lookup["power"] = &Item{
key: "power",
value: TestValue("9000"),
}
return b
2013-10-19 14:36:33 +02:00
}
func assertValue(t *testing.T, item *Item, expected string) {
value := item.value.(TestValue)
gspec.New(t).Expect(value).ToEqual(TestValue(expected))
2013-10-19 14:36:33 +02:00
}
2013-10-19 14:45:30 +02:00
type TestValue string
2013-10-19 14:36:33 +02:00
2013-10-19 14:45:30 +02:00
func (v TestValue) Expires() time.Time {
return time.Now()
2013-10-19 14:36:33 +02:00
}