From 2b1e14690ea6426a67c0faaaddcfb8aa7360dce7 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 25 Apr 2025 11:08:58 -0600 Subject: refactor: move db and mountable to app --- app/db/in_memory_repository.go | 40 +++++++++++++++++++++++++++++ app/db/in_memory_repository_test.go | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 app/db/in_memory_repository.go create mode 100644 app/db/in_memory_repository_test.go (limited to 'app/db') diff --git a/app/db/in_memory_repository.go b/app/db/in_memory_repository.go new file mode 100644 index 0000000..5b84dbf --- /dev/null +++ b/app/db/in_memory_repository.go @@ -0,0 +1,40 @@ +package db + +import ( + "github.com/xlgmokha/x/pkg/x" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" +) + +type inMemoryRepository[T domain.Entity] struct { + items []T +} + +func NewRepository[T domain.Entity]() domain.Repository[T] { + return &inMemoryRepository[T]{ + items: []T{}, + } +} + +func (r *inMemoryRepository[T]) All() []T { + return r.items +} + +func (r *inMemoryRepository[T]) Find(id domain.ID) T { + return x.Find(r.All(), func(item T) bool { + return item.GetID() == id + }) +} + +func (r *inMemoryRepository[T]) Save(item T) error { + if err := item.Validate(); err != nil { + return err + } + + if item.GetID() == "" { + item.SetID(domain.ID(pls.GenerateULID())) + } + + r.items = append(r.items, item) + return nil +} diff --git a/app/db/in_memory_repository_test.go b/app/db/in_memory_repository_test.go new file mode 100644 index 0000000..bd9d12f --- /dev/null +++ b/app/db/in_memory_repository_test.go @@ -0,0 +1,50 @@ +package db + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" +) + +func TestInMemoryRepository(t *testing.T) { + storage := NewRepository[*domain.Sparkle]() + + t.Run("Save", func(t *testing.T) { + t.Run("an invalid Sparkle", func(t *testing.T) { + err := storage.Save(&domain.Sparkle{Reason: "because"}) + + assert.Error(t, err) + assert.Equal(t, 0, len(storage.All())) + }) + + t.Run("a valid Sparkle", func(t *testing.T) { + sparkle := &domain.Sparkle{Sparklee: "@tanuki", Reason: "because"} + require.NoError(t, storage.Save(sparkle)) + + sparkles := storage.All() + assert.Equal(t, 1, len(sparkles)) + assert.NotEmpty(t, sparkles[0].ID) + assert.Equal(t, "@tanuki", sparkles[0].Sparklee) + assert.Equal(t, "because", sparkles[0].Reason) + }) + }) + + t.Run("Find", func(t *testing.T) { + t.Run("when the entity exists", func(t *testing.T) { + sparkle, err := domain.NewSparkle("@tanuki for testing this func") + require.NoError(t, err) + require.NoError(t, storage.Save(sparkle)) + + result := storage.Find(sparkle.ID) + require.NotNil(t, result) + require.Equal(t, sparkle, result) + }) + + t.Run("when the entity does not exist", func(t *testing.T) { + result := storage.Find("unknown") + require.Nil(t, result) + }) + }) +} -- cgit v1.2.3