14f46806cd
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package cache
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type testType struct {
|
|
Prop1 string `json:"prop1"`
|
|
Prop2 string `json:"prop2"`
|
|
}
|
|
|
|
func TestCache(t *testing.T) {
|
|
assert := assert.New(t)
|
|
c := New[testType]("")
|
|
assert.Equal(os.TempDir(), c.Path)
|
|
err := c.Clear()
|
|
assert.NoError(err)
|
|
|
|
err = c.Add("something", &testType{Prop1: "test1", Prop2: "test2"})
|
|
assert.NoError(err)
|
|
|
|
result, err := c.Get("something")
|
|
assert.NoError(err)
|
|
assert.Equal("test1", result.Prop1)
|
|
assert.Equal("test2", result.Prop2)
|
|
|
|
list, err := c.List()
|
|
assert.Equal(1, len(list))
|
|
assert.NoError(err)
|
|
|
|
err = c.Add("something else", &testType{Prop1: "test1", Prop2: "test2"})
|
|
assert.NoError(err)
|
|
|
|
list, err = c.List()
|
|
assert.NoError(err)
|
|
assert.Equal(2, len(list))
|
|
}
|
|
|
|
func TestIsExpired(t *testing.T) {
|
|
layout := "2006-01-02 15:04:05"
|
|
assert := assert.New(t)
|
|
expirationTime, err := time.Parse(layout, "2024-10-04 22:22:00")
|
|
assert.NoError(err)
|
|
now, err := time.Parse(layout, "2024-10-04 22:21:00")
|
|
assert.NoError(err)
|
|
|
|
result := IsExpired(expirationTime, &now)
|
|
assert.False(result)
|
|
|
|
now, err = time.Parse(layout, "2024-10-04 22:22:00")
|
|
assert.NoError(err)
|
|
|
|
result = IsExpired(expirationTime, &now)
|
|
assert.False(result)
|
|
|
|
now, err = time.Parse(layout, "2024-10-04 22:23:00")
|
|
assert.NoError(err)
|
|
|
|
result = IsExpired(expirationTime, &now)
|
|
assert.True(result)
|
|
}
|