2022-10-22 22:08:02 +00:00
|
|
|
package overlays
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/database"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2022-10-22 22:08:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventDatabaseRoundtrip(t *testing.T) {
|
|
|
|
dbc := database.GetTestDatabase(t)
|
|
|
|
require.NoError(t, dbc.DB().AutoMigrate(&overlaysEvent{}))
|
|
|
|
|
|
|
|
var (
|
|
|
|
channel = "#test"
|
2023-12-25 12:29:16 +00:00
|
|
|
evtID uint64
|
|
|
|
tEvent1 = time.Now().UTC()
|
2022-10-22 22:08:02 +00:00
|
|
|
tEvent2 = tEvent1.Add(time.Second)
|
|
|
|
)
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evts, err := getChannelEvents(dbc, channel)
|
2022-10-22 22:08:02 +00:00
|
|
|
assert.NoError(t, err, "getting events on empty db")
|
|
|
|
assert.Zero(t, evts, "expect no events on empty db")
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evtID, err = addChannelEvent(dbc, channel, socketMessage{
|
2022-10-22 22:08:02 +00:00
|
|
|
IsLive: true,
|
|
|
|
Time: tEvent2,
|
|
|
|
Type: "event 2",
|
|
|
|
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
|
2023-12-25 12:29:16 +00:00
|
|
|
})
|
|
|
|
assert.Equal(t, uint64(1), evtID)
|
|
|
|
assert.NoError(t, err, "adding second event")
|
2022-10-22 22:08:02 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evtID, err = addChannelEvent(dbc, channel, socketMessage{
|
2022-10-22 22:08:02 +00:00
|
|
|
IsLive: true,
|
|
|
|
Time: tEvent1,
|
|
|
|
Type: "event 1",
|
|
|
|
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
|
2023-12-25 12:29:16 +00:00
|
|
|
})
|
|
|
|
assert.Equal(t, uint64(2), evtID)
|
|
|
|
assert.NoError(t, err, "adding first event")
|
2022-10-22 22:08:02 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evtID, err = addChannelEvent(dbc, "#otherchannel", socketMessage{
|
2022-10-22 22:08:02 +00:00
|
|
|
IsLive: true,
|
|
|
|
Time: tEvent1,
|
|
|
|
Type: "event",
|
|
|
|
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
|
2023-12-25 12:29:16 +00:00
|
|
|
})
|
|
|
|
assert.Equal(t, uint64(3), evtID)
|
|
|
|
assert.NoError(t, err, "adding other channel event")
|
2022-10-22 22:08:02 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evts, err = getChannelEvents(dbc, channel)
|
2022-10-22 22:08:02 +00:00
|
|
|
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")
|
2023-12-25 12:29:16 +00:00
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
evt, err := getEventByID(dbc, 2)
|
2023-12-25 12:29:16 +00:00
|
|
|
assert.NoError(t, err)
|
2024-01-01 16:52:18 +00:00
|
|
|
assert.Equal(t, socketMessage{
|
2023-12-25 12:29:16 +00:00
|
|
|
EventID: 2,
|
|
|
|
IsLive: false,
|
|
|
|
Time: tEvent1,
|
|
|
|
Type: "event 1",
|
|
|
|
Fields: plugins.FieldCollectionFromData(map[string]any{"foo": "bar"}),
|
|
|
|
}, evt)
|
2022-10-22 22:08:02 +00:00
|
|
|
}
|