summaryrefslogtreecommitdiff
path: root/app/db/in_memory_repository_test.go
blob: bd9d12f456bc6f9daeb27ec84a8f871e03647efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
		})
	})
}