All checks were successful
ci/woodpecker/push/govulncheck Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful
ci/woodpecker/tag/deployment Pipeline was successful
ci/woodpecker/cron/govulncheck Pipeline was successful
Reviewed-on: #52
454 lines
14 KiB
Go
454 lines
14 KiB
Go
package adapter_test
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func testServerSnapshotRepository(t *testing.T, newRepos func(t *testing.T) repositories) {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
|
|
t.Run("Create", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const dateFormat = "2006-01-02"
|
|
|
|
repos := newRepos(t)
|
|
|
|
assertCreated := func(t *testing.T, params domain.CreateServerSnapshotParams) {
|
|
t.Helper()
|
|
|
|
require.NotEmpty(t, params)
|
|
|
|
listParams := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, listParams.SetServerKeys([]string{params.ServerKey()}))
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, listParams)
|
|
serverSnapshots := res.ServerSnapshots()
|
|
require.NoError(t, err)
|
|
|
|
date := params.Date().Format(dateFormat)
|
|
|
|
idx := slices.IndexFunc(serverSnapshots, func(ss domain.ServerSnapshot) bool {
|
|
return ss.ServerKey() == params.ServerKey() &&
|
|
ss.Date().Format(dateFormat) == date
|
|
})
|
|
require.GreaterOrEqual(t, idx, 0)
|
|
serverSnapshot := serverSnapshots[idx]
|
|
|
|
assert.Equal(t, params.ServerKey(), serverSnapshot.ServerKey())
|
|
assert.Equal(t, params.NumPlayers(), serverSnapshot.NumPlayers())
|
|
assert.Equal(t, params.NumActivePlayers(), serverSnapshot.NumActivePlayers())
|
|
assert.Equal(t, params.NumInactivePlayers(), serverSnapshot.NumInactivePlayers())
|
|
assert.Equal(t, params.NumTribes(), serverSnapshot.NumTribes())
|
|
assert.Equal(t, params.NumActiveTribes(), serverSnapshot.NumActiveTribes())
|
|
assert.Equal(t, params.NumInactiveTribes(), serverSnapshot.NumInactiveTribes())
|
|
assert.Equal(t, params.NumVillages(), serverSnapshot.NumVillages())
|
|
assert.Equal(t, params.NumPlayerVillages(), serverSnapshot.NumPlayerVillages())
|
|
assert.Equal(t, params.NumBarbarianVillages(), serverSnapshot.NumBarbarianVillages())
|
|
assert.Equal(t, params.NumBonusVillages(), serverSnapshot.NumBonusVillages())
|
|
assert.Equal(t, date, serverSnapshot.Date().Format(dateFormat))
|
|
assert.WithinDuration(t, time.Now(), serverSnapshot.CreatedAt(), time.Minute)
|
|
}
|
|
|
|
assertNoDuplicates := func(t *testing.T, params domain.CreateServerSnapshotParams) {
|
|
t.Helper()
|
|
|
|
listParams := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, listParams.SetServerKeys([]string{params.ServerKey()}))
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, listParams)
|
|
require.NoError(t, err)
|
|
serverSnapshots := res.ServerSnapshots()
|
|
|
|
var indexes []int
|
|
|
|
for i, ss := range serverSnapshots {
|
|
if ss.ServerKey() == params.ServerKey() && ss.Date().Format(dateFormat) == params.Date().Format(dateFormat) {
|
|
indexes = append(indexes, i)
|
|
}
|
|
}
|
|
|
|
assert.Len(t, indexes, 1)
|
|
}
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
listServersParams := domain.NewListServersParams()
|
|
require.NoError(t, listServersParams.SetOpen(domain.NullBool{
|
|
V: true,
|
|
Valid: true,
|
|
}))
|
|
require.NoError(t, listServersParams.SetLimit(1))
|
|
|
|
res, err := repos.server.List(ctx, listServersParams)
|
|
require.NoError(t, err)
|
|
servers := res.Servers()
|
|
require.NotEmpty(t, servers)
|
|
|
|
date := time.Now()
|
|
|
|
createParams, err := domain.NewCreateServerSnapshotParams(servers[0], date)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, repos.serverSnapshot.Create(ctx, createParams))
|
|
assertCreated(t, createParams)
|
|
|
|
require.NoError(t, repos.serverSnapshot.Create(ctx, createParams))
|
|
assertNoDuplicates(t, createParams)
|
|
})
|
|
|
|
t.Run("OK: len(params) == 0", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.NoError(t, repos.serverSnapshot.Create(ctx))
|
|
})
|
|
})
|
|
|
|
t.Run("List & ListWithRelations", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repos := newRepos(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
params func(t *testing.T) domain.ListServerSnapshotsParams
|
|
assertResult func(t *testing.T, params domain.ListServerSnapshotsParams, res domain.ListServerSnapshotsResult)
|
|
assertError func(t *testing.T, err error)
|
|
}{
|
|
{
|
|
name: "OK: default params",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
return domain.NewListServerSnapshotsParams()
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
_ domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Or(
|
|
cmp.Compare(a.ServerKey(), b.ServerKey()),
|
|
a.Date().Compare(b.Date()),
|
|
cmp.Compare(a.ID(), b.ID()),
|
|
)
|
|
}))
|
|
assert.False(t, res.Self().IsZero())
|
|
assert.True(t, res.Next().IsZero())
|
|
},
|
|
},
|
|
{
|
|
name: "OK: sort=[serverKey DESC, date DESC, id DESC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortServerKeyDESC,
|
|
domain.ServerSnapshotSortDateDESC,
|
|
domain.ServerSnapshotSortIDDESC,
|
|
}))
|
|
return params
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
_ domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Or(
|
|
cmp.Compare(a.ServerKey(), b.ServerKey()),
|
|
a.Date().Compare(b.Date()),
|
|
cmp.Compare(a.ID(), b.ID()),
|
|
) * -1
|
|
}))
|
|
},
|
|
},
|
|
{
|
|
name: "OK: sort=[id ASC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortIDASC,
|
|
}))
|
|
return params
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
_ domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Compare(a.ID(), b.ID())
|
|
}))
|
|
},
|
|
},
|
|
{
|
|
name: "OK: sort=[id DESC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortIDDESC,
|
|
}))
|
|
return params
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
_ domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Compare(a.ID(), b.ID()) * -1
|
|
}))
|
|
},
|
|
},
|
|
{
|
|
name: "OK: serverKeys",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
|
|
params := domain.NewListServerSnapshotsParams()
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, res.ServerSnapshots())
|
|
randServerSnapshot := res.ServerSnapshots()[0]
|
|
|
|
require.NoError(t, params.SetServerKeys([]string{randServerSnapshot.ServerKey()}))
|
|
|
|
return params
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
params domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
|
|
serverKeys := params.ServerKeys()
|
|
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotZero(t, serverSnapshots)
|
|
for _, ss := range serverSnapshots {
|
|
assert.True(t, slices.Contains(serverKeys, ss.ServerKey()))
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "OK: cursor serverKeys sort=[id ASC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
|
|
params := domain.NewListServerSnapshotsParams()
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
require.Greater(t, len(res.ServerSnapshots()), 2)
|
|
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{domain.ServerSnapshotSortIDASC}))
|
|
require.NoError(t, params.SetServerKeys([]string{res.ServerSnapshots()[1].ServerKey()}))
|
|
cursor, err := res.ServerSnapshots()[1].ToCursor()
|
|
require.NoError(t, err)
|
|
require.NoError(t, params.SetCursor(cursor))
|
|
|
|
return params
|
|
},
|
|
assertResult: func(t *testing.T, params domain.ListServerSnapshotsParams, res domain.ListServerSnapshotsResult) {
|
|
t.Helper()
|
|
|
|
serverKeys := params.ServerKeys()
|
|
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
for _, ss := range serverSnapshots {
|
|
assert.GreaterOrEqual(t, ss.ID(), params.Cursor().ID())
|
|
assert.True(t, slices.Contains(serverKeys, ss.ServerKey()))
|
|
}
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Compare(a.ID(), b.ID())
|
|
}))
|
|
},
|
|
},
|
|
{
|
|
name: "OK: cursor sort=[serverKey ASC, id ASC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortServerKeyASC,
|
|
domain.ServerSnapshotSortIDASC,
|
|
}))
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
require.Greater(t, len(res.ServerSnapshots()), 2)
|
|
|
|
cursor, err := res.ServerSnapshots()[1].ToCursor()
|
|
require.NoError(t, err)
|
|
require.NoError(t, params.SetCursor(cursor))
|
|
|
|
return params
|
|
},
|
|
assertResult: func(t *testing.T, params domain.ListServerSnapshotsParams, res domain.ListServerSnapshotsResult) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Or(
|
|
cmp.Compare(a.ServerKey(), b.ServerKey()),
|
|
cmp.Compare(a.ID(), b.ID()),
|
|
)
|
|
}))
|
|
assert.GreaterOrEqual(t, serverSnapshots[0].ID(), params.Cursor().ID())
|
|
for _, ss := range serverSnapshots {
|
|
assert.GreaterOrEqual(t, ss.ServerKey(), params.Cursor().ServerKey())
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "OK: cursor sort=[serverKey DESC, id DESC]",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortServerKeyDESC,
|
|
domain.ServerSnapshotSortIDDESC,
|
|
}))
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
require.Greater(t, len(res.ServerSnapshots()), 2)
|
|
|
|
cursor, err := res.ServerSnapshots()[1].ToCursor()
|
|
require.NoError(t, err)
|
|
require.NoError(t, params.SetCursor(cursor))
|
|
|
|
return params
|
|
},
|
|
assertResult: func(t *testing.T, params domain.ListServerSnapshotsParams, res domain.ListServerSnapshotsResult) {
|
|
t.Helper()
|
|
serverSnapshots := res.ServerSnapshots()
|
|
assert.NotEmpty(t, serverSnapshots)
|
|
assert.True(t, slices.IsSortedFunc(serverSnapshots, func(a, b domain.ServerSnapshot) int {
|
|
return cmp.Or(
|
|
cmp.Compare(a.ServerKey(), b.ServerKey()),
|
|
cmp.Compare(a.ID(), b.ID()),
|
|
) * -1
|
|
}))
|
|
assert.LessOrEqual(t, serverSnapshots[0].ID(), params.Cursor().ID())
|
|
for _, ss := range serverSnapshots {
|
|
assert.LessOrEqual(t, ss.ServerKey(), params.Cursor().ServerKey())
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "OK: limit=2",
|
|
params: func(t *testing.T) domain.ListServerSnapshotsParams {
|
|
t.Helper()
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetLimit(2))
|
|
return params
|
|
},
|
|
assertResult: func(
|
|
t *testing.T,
|
|
params domain.ListServerSnapshotsParams,
|
|
res domain.ListServerSnapshotsResult,
|
|
) {
|
|
t.Helper()
|
|
assert.Len(t, res.ServerSnapshots(), params.Limit())
|
|
assert.False(t, res.Self().IsZero())
|
|
assert.False(t, res.Next().IsZero())
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
assertError := tt.assertError
|
|
if assertError == nil {
|
|
assertError = func(t *testing.T, err error) {
|
|
t.Helper()
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
params := tt.params(t)
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
assertError(t, err)
|
|
tt.assertResult(t, params, res)
|
|
|
|
resWithRelations, err := repos.serverSnapshot.ListWithRelations(ctx, params)
|
|
assertError(t, err)
|
|
require.Len(t, resWithRelations.ServerSnapshots(), len(res.ServerSnapshots()))
|
|
for i, ss := range resWithRelations.ServerSnapshots() {
|
|
assert.Equal(t, res.ServerSnapshots()[i], ss.ServerSnapshot())
|
|
assert.Equal(t, ss.ServerSnapshot().ServerKey(), ss.Server().Key())
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repos := newRepos(t)
|
|
|
|
params := domain.NewListServerSnapshotsParams()
|
|
require.NoError(t, params.SetSort([]domain.ServerSnapshotSort{
|
|
domain.ServerSnapshotSortServerKeyASC,
|
|
domain.ServerSnapshotSortDateASC,
|
|
domain.ServerSnapshotSortIDASC,
|
|
}))
|
|
|
|
res, err := repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, res.ServerSnapshots())
|
|
|
|
randSnapshot := res.ServerSnapshots()[0]
|
|
|
|
require.NoError(t, repos.serverSnapshot.Delete(ctx, randSnapshot.ServerKey(), randSnapshot.Date()))
|
|
|
|
require.NoError(t, params.SetServerKeys([]string{randSnapshot.ServerKey()}))
|
|
|
|
res, err = repos.serverSnapshot.List(ctx, params)
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, res.ServerSnapshots())
|
|
for _, ss := range res.ServerSnapshots() {
|
|
assert.True(t, ss.Date().After(randSnapshot.Date()))
|
|
}
|
|
})
|
|
})
|
|
}
|