twitch-bot/internal/apimodules/overlays/database_test.go
Knut Ahlers 7189232093
[overlays] Add support for replaying events
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-12-31 14:05:39 +01:00

71 lines
1.9 KiB
Go

package overlays
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Luzifer/twitch-bot/v3/pkg/database"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func TestEventDatabaseRoundtrip(t *testing.T) {
dbc := database.GetTestDatabase(t)
require.NoError(t, dbc.DB().AutoMigrate(&overlaysEvent{}))
var (
channel = "#test"
evtID uint64
tEvent1 = time.Now().UTC()
tEvent2 = tEvent1.Add(time.Second)
)
evts, err := GetChannelEvents(dbc, channel)
assert.NoError(t, err, "getting events on empty db")
assert.Zero(t, evts, "expect no events on empty db")
evtID, err = AddChannelEvent(dbc, channel, SocketMessage{
IsLive: true,
Time: tEvent2,
Type: "event 2",
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
})
assert.Equal(t, uint64(1), evtID)
assert.NoError(t, err, "adding second event")
evtID, err = AddChannelEvent(dbc, channel, SocketMessage{
IsLive: true,
Time: tEvent1,
Type: "event 1",
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
})
assert.Equal(t, uint64(2), evtID)
assert.NoError(t, err, "adding first event")
evtID, err = AddChannelEvent(dbc, "#otherchannel", SocketMessage{
IsLive: true,
Time: tEvent1,
Type: "event",
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
})
assert.Equal(t, uint64(3), evtID)
assert.NoError(t, err, "adding other channel event")
evts, err = GetChannelEvents(dbc, channel)
assert.NoError(t, err, "getting events")
assert.Len(t, evts, 2, "expect 2 events")
assert.Less(t, evts[0].Time, evts[1].Time, "expect sorting")
evt, err := GetEventByID(dbc, 2)
assert.NoError(t, err)
assert.Equal(t, SocketMessage{
EventID: 2,
IsLive: false,
Time: tEvent1,
Type: "event 1",
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
}, evt)
}