This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/msg/publisher_snapshot_test.go
Dawid Wysokiński 8080b2d6fd
All checks were successful
continuous-integration/drone/push Build is passing
refactor: rename fn -> f, loadJSONFile -> readJSONFile
2023-02-24 07:24:16 +01:00

79 lines
1.8 KiB
Go

package msg_test
import (
"context"
"testing"
"time"
"gitea.dwysokinski.me/twhelp/core/internal/domain"
"gitea.dwysokinski.me/twhelp/core/internal/msg"
"gitea.dwysokinski.me/twhelp/core/internal/msg/internal/model"
"github.com/ThreeDotsLabs/watermill/message/subscriber"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSnapshotPublisher_CmdCreate(t *testing.T) {
t.Parallel()
marshaler := msg.GobMarshaler{}
pubSub := newPubSub(t)
publisher := msg.NewSnapshotPublisher(pubSub, marshaler)
payloads := []domain.CreateSnapshotsCmdPayload{
{
Key: "pl151",
VersionCode: "pl",
Date: time.Now(),
},
{
Key: "pl182",
VersionCode: "pl",
Date: time.Now().Add(-25 * time.Hour),
},
}
tests := []struct {
topic string
f func(ctx context.Context, payloads ...domain.CreateSnapshotsCmdPayload) error
}{
{
topic: "players.cmd.create_snapshots",
f: publisher.CmdCreatePlayers,
},
{
topic: "tribes.cmd.create_snapshots",
f: publisher.CmdCreateTribes,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.topic, func(t *testing.T) {
t.Parallel()
msgs, err := pubSub.Subscribe(context.Background(), tt.topic)
require.NoError(t, err)
require.NoError(t, tt.f(context.Background(), payloads...))
receivedMsgs, _ := subscriber.BulkRead(msgs, len(payloads), time.Second)
require.Len(t, receivedMsgs, len(payloads))
for _, m := range receivedMsgs {
var received model.CreateSnapshotsCmdPayload
assert.NoError(t, marshaler.Unmarshal(m, &received))
found := false
for _, payload := range payloads {
if payload.VersionCode == received.VersionCode && payload.Key == received.Key {
found = true
break
}
}
assert.True(t, found)
}
})
}
}