yr/pkg/cache/cache_test.go
Tim Hårek Andreassen 437f7f5f9a
feat: Add caching
Implements: https://todo.sr.ht/~timharek/yr/1
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-04 23:20:26 +02:00

52 lines
1.1 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.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)
}
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.True(result)
now, err = time.Parse(layout, "2024-10-04 22:23:00")
assert.NoError(err)
result = IsExpired(expirationTime, &now)
assert.True(result)
}