2024-10-04 19:53:00 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2024-10-04 21:20:26 +00:00
|
|
|
"time"
|
2024-10-04 19:53:00 +00:00
|
|
|
|
|
|
|
"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)
|
2024-10-17 19:16:39 +00:00
|
|
|
err := c.Clear()
|
|
|
|
assert.NoError(err)
|
2024-10-04 19:53:00 +00:00
|
|
|
|
2024-10-17 19:16:39 +00:00
|
|
|
err = c.Add("something", &testType{Prop1: "test1", Prop2: "test2"})
|
2024-10-04 19:53:00 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
result, err := c.Get("something")
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal("test1", result.Prop1)
|
|
|
|
assert.Equal("test2", result.Prop2)
|
2024-10-17 19:16:39 +00:00
|
|
|
|
|
|
|
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))
|
2024-10-04 19:53:00 +00:00
|
|
|
}
|
2024-10-04 21:20:26 +00:00
|
|
|
|
|
|
|
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)
|
2024-11-06 15:01:40 +00:00
|
|
|
assert.False(result)
|
2024-10-04 21:20:26 +00:00
|
|
|
|
|
|
|
now, err = time.Parse(layout, "2024-10-04 22:23:00")
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
result = IsExpired(expirationTime, &now)
|
|
|
|
assert.True(result)
|
|
|
|
}
|