Skip to content

Commit

Permalink
add unit tests, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed May 13, 2024
1 parent 9858dc7 commit b7d4cf4
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lib/data/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package data_test

import (
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/permafrost-dev/eget/lib/assets"
. "github.com/permafrost-dev/eget/lib/data"
"github.com/permafrost-dev/eget/lib/finders"
)

var _ = Describe("Cache", func() {
var (
cache *Cache
filename string
testEntry *RepositoryCacheEntry
)

BeforeEach(func() {
filename = "test_cache.json"
cache = NewCache(filename)
testEntry = &RepositoryCacheEntry{
Name: "test",
Target: "target",
Filters: []string{"*.zip"},
Assets: []assets.Asset{},
ExpiresAt: time.Now().Add(10 * time.Minute),
}
})

AfterEach(func() {
os.Remove(filename)
})

Describe("NewCache", func() {
It("should create a new cache with specified filename", func() {
Expect(cache.Filename).To(Equal(filename))
})
})

Describe("Set and Get", func() {
It("should add and retrieve an entry from the cache", func() {
cache.Set("test", testEntry)
retrievedEntry, exists := cache.Get("test")
Expect(exists).To(BeTrue())
Expect(retrievedEntry.Name).To(Equal("test"))
})
})

Describe("Has", func() {
It("should correctly report if an entry exists based on name", func() {
cache.Set("test", testEntry)
Expect(cache.Has("test")).To(BeTrue())
Expect(cache.Has("nonexistent")).To(BeFalse())
})
})

Describe("SetRateLimit and SaveToFile", func() {
It("should set the rate limit and save to file", func() {
cache.SetRateLimit(5000, 4999, time.Now().Add(time.Hour))
Expect(cache.Data.RateLimit.Limit).To(Equal(5000))
Expect(cache.Data.RateLimit.Remaining).To(Equal(4999))
_, err := os.Stat(filename)
Expect(err).ToNot(HaveOccurred())
})
})

Describe("AddRepository", func() {
It("should add a repository and save to file", func() {
_, added := cache.AddRepository("owner/testrepo", "target", []string{"zip"}, &finders.FindResult{}, time.Now().Add(10*time.Minute))
Expect(added).To(BeTrue())
Expect(cache.Data.HasRepositoryEntryByKey("owner/testrepo")).To(BeTrue())
})
})

Describe("LoadFromFile", func() {
It("should load cache data from file", func() {
cache.Set("test", testEntry)
cache.SaveToFile()

newCache := NewCache(filename)
err := newCache.LoadFromFile()
Expect(err).ToNot(HaveOccurred())
_, exists := newCache.Get("test")
Expect(exists).To(BeTrue())
})
})

Describe("PurgeExpired", func() {
It("should remove expired entries", func() {
expiredEntry := &RepositoryCacheEntry{
Name: "expired",
ExpiresAt: time.Now().Add(-1 * time.Minute),
}
cache.Set("expired", expiredEntry)
cache.PurgeExpired()
_, exists := cache.Get("expired")
Expect(exists).To(BeFalse())
})
})
})
4 changes: 4 additions & 0 deletions lib/data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ func (rce *RepositoryCacheEntry) UpdateDownloadedAt(tag string) {
func (rce *RepositoryCacheEntry) Exists() bool {
return rce.exists
}

func (rce *RepositoryCacheEntry) GetOwner() *Cache {
return rce.owner
}
94 changes: 94 additions & 0 deletions lib/data/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package data_test

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/permafrost-dev/eget/lib/data"
)

var _ = Describe("ApplicationData", func() {
var (
appData data.ApplicationData
owner *data.Cache // Assuming Cache is a struct in your package with a mutex
)

BeforeEach(func() {
appData = data.ApplicationData{
RateLimit: data.RateLimit{},
Repositories: make(map[string]*data.RepositoryCacheEntry),
}
owner = &data.Cache{} // Setup owner assuming a valid constructor or initial state
})

Describe("HasRepositoryEntryByKey", func() {
It("should return false if the key does not exist", func() {
Expect(appData.HasRepositoryEntryByKey("nonexistent")).To(BeFalse())
})

It("should return true if the key exists", func() {
appData.Repositories["exists"] = &data.RepositoryCacheEntry{}
Expect(appData.HasRepositoryEntryByKey("exists")).To(BeTrue())
})
})

Describe("GetRepositoryEntryByKey", func() {
It("should return a new entry if the key does not exist", func() {
entry := appData.GetRepositoryEntryByKey("nonexistent", owner)
Expect(entry).NotTo(BeNil())
Expect(entry.GetOwner().Filename).To(Equal(owner.Filename))
})

It("should return the existing entry if the key exists", func() {
expectedEntry := &data.RepositoryCacheEntry{}
appData.Repositories["exists"] = expectedEntry
entry := appData.GetRepositoryEntryByKey("exists", owner)
Expect(entry).To(Equal(expectedEntry))
Expect(entry.GetOwner().Filename).To(Equal(owner.Filename))
})
})

Describe("SetRepositoryEntryByKey", func() {
It("should set the repository entry for a given key", func() {
entry := &data.RepositoryCacheEntry{}
appData.SetRepositoryEntryByKey("newEntry", entry, owner)
Expect(appData.Repositories).To(HaveKeyWithValue("newEntry", entry))
Expect(entry.GetOwner().Filename).To(Equal(owner.Filename))
})
})
})

var _ = Describe("RepositoryCacheEntry", func() {
var (
entry *data.RepositoryCacheEntry
)

BeforeEach(func() {
entry = &data.RepositoryCacheEntry{}
})

Describe("UpdateCheckedAt", func() {
It("should update LastCheckAt to the current time", func() {
beforeUpdate := time.Now().Add(-time.Minute)
entry.UpdateCheckedAt()
Expect(entry.LastCheckAt).To(BeTemporally(">", beforeUpdate))
})
})

Describe("UpdateDownloadedAt", func() {
It("should update LastDownloadAt and LastDownloadTag correctly", func() {
tag := "v1.0.0"
beforeUpdate := time.Now().Local().Add(-time.Minute)
entry.UpdateDownloadedAt(tag)
Expect(entry.LastDownloadAt).To(BeTemporally(">", beforeUpdate))
Expect(entry.LastDownloadTag).To(Equal(tag))
})
})

Describe("Exists", func() {
It("should return false by default", func() {
Expect(entry.Exists()).To(BeFalse())
})
})
})

0 comments on commit b7d4cf4

Please sign in to comment.