diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-25 11:08:58 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-25 11:08:58 -0600 |
| commit | 2b1e14690ea6426a67c0faaaddcfb8aa7360dce7 (patch) | |
| tree | 7f764225e3e3a26bbd7532e72ab99a54e465be92 /app/db | |
| parent | 0053db0d265af313dd281db5cf1e73236cde30c6 (diff) | |
refactor: move db and mountable to app
Diffstat (limited to 'app/db')
| -rw-r--r-- | app/db/in_memory_repository.go | 40 | ||||
| -rw-r--r-- | app/db/in_memory_repository_test.go | 50 |
2 files changed, 90 insertions, 0 deletions
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) + }) + }) +} |
