From b7d4cf41fd1e49b747532a73f95295625ad5d27c Mon Sep 17 00:00:00 2001 From: Patrick Organ Date: Mon, 13 May 2024 19:17:37 -0400 Subject: [PATCH] add unit tests, wip --- lib/data/cache_test.go | 103 +++++++++++++++++++++++++++++++++++++++++ lib/data/types.go | 4 ++ lib/data/types_test.go | 94 +++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 lib/data/cache_test.go create mode 100644 lib/data/types_test.go diff --git a/lib/data/cache_test.go b/lib/data/cache_test.go new file mode 100644 index 0000000..14c2ae9 --- /dev/null +++ b/lib/data/cache_test.go @@ -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()) + }) + }) +}) diff --git a/lib/data/types.go b/lib/data/types.go index 3f1e76e..e2bccae 100644 --- a/lib/data/types.go +++ b/lib/data/types.go @@ -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 +} diff --git a/lib/data/types_test.go b/lib/data/types_test.go new file mode 100644 index 0000000..98e8a7b --- /dev/null +++ b/lib/data/types_test.go @@ -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()) + }) + }) +})