summaryrefslogtreecommitdiff
path: root/app/db/publishing_repository_test.go
blob: 4a2c05fb2422b307ab9796c039852c4dd971908d (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
package db

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/event"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
)

func TestPublishingRepository(t *testing.T) {
	aggregator := event.New[*domain.Sparkle]()
	storage := NewRepository[*domain.Sparkle](WithPublishing(aggregator))

	t.Run("Save", func(t *testing.T) {
		t.Run("publishes an event", func(t *testing.T) {
			called := false
			var payload *domain.Sparkle

			aggregator.SubscribeTo("after.create", func(item *domain.Sparkle) {
				called = true
				payload = item
			})

			sparkle := &domain.Sparkle{Sparklee: "@tanuki", Reason: "because"}
			require.NoError(t, storage.Save(t.Context(), sparkle))

			require.True(t, called)
			require.NotNil(t, payload)
			assert.Equal(t, sparkle, payload)
		})
	})
}