mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-30 19:22:09 +00:00
- it only is used after calling readCache - add unit test it reduces the number of testcase to be considered in handler
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package artifactcache
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/timshannon/bolthold"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCacheReadWrite(t *testing.T) {
|
|
caches, err := newCaches(t.TempDir(), "secret", logrus.New())
|
|
require.NoError(t, err)
|
|
t.Run("NotFound", func(t *testing.T) {
|
|
found, err := caches.readCache(456, "repo")
|
|
assert.Nil(t, found)
|
|
assert.ErrorIs(t, err, bolthold.ErrNotFound)
|
|
})
|
|
|
|
repo := "repository"
|
|
cache := &Cache{
|
|
Repo: repo,
|
|
Key: "key",
|
|
Version: "version",
|
|
Size: 444,
|
|
}
|
|
now := time.Now().Unix()
|
|
cache.CreatedAt = now
|
|
cache.UsedAt = now
|
|
cache.Repo = repo
|
|
|
|
t.Run("Insert", func(t *testing.T) {
|
|
db, err := caches.openDB()
|
|
require.NoError(t, err)
|
|
defer db.Close()
|
|
assert.NoError(t, insertCache(db, cache))
|
|
})
|
|
|
|
t.Run("Found", func(t *testing.T) {
|
|
found, err := caches.readCache(cache.ID, cache.Repo)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, cache.ID, found.ID)
|
|
})
|
|
|
|
t.Run("InvalidRepo", func(t *testing.T) {
|
|
invalidRepo := "INVALID REPO"
|
|
found, err := caches.readCache(cache.ID, invalidRepo)
|
|
assert.Nil(t, found)
|
|
assert.ErrorContains(t, err, invalidRepo)
|
|
})
|
|
}
|