2024-01-16 06:28:03 +00:00
|
|
|
package domain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain/domaintest"
|
2024-03-16 06:43:46 +00:00
|
|
|
"github.com/brianvoe/gofakeit/v7"
|
2024-01-16 06:28:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewCreatePlayerSnapshotParams(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
players := domain.Players{
|
|
|
|
domaintest.NewPlayer(t),
|
|
|
|
domaintest.NewPlayer(t),
|
|
|
|
domaintest.NewPlayer(t),
|
|
|
|
}
|
|
|
|
date := time.Now()
|
|
|
|
|
|
|
|
res, err := domain.NewCreatePlayerSnapshotParams(players, date)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, res, len(players))
|
|
|
|
for i, p := range players {
|
|
|
|
idx := slices.IndexFunc(res, func(params domain.CreatePlayerSnapshotParams) bool {
|
|
|
|
return params.PlayerID() == p.ID() && params.ServerKey() == p.ServerKey()
|
|
|
|
})
|
|
|
|
require.GreaterOrEqualf(t, idx, 0, "players[%d] not found", i)
|
|
|
|
|
|
|
|
params := res[i]
|
|
|
|
|
|
|
|
assert.Equalf(t, p.ID(), params.PlayerID(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.ServerKey(), params.ServerKey(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.NumVillages(), params.NumVillages(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.Points(), params.Points(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.Rank(), params.Rank(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.TribeID(), params.TribeID(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, p.OD(), params.OD(), "players[%d]", i)
|
|
|
|
assert.Equalf(t, date, params.Date(), "players[%d]", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 08:49:42 +00:00
|
|
|
func TestPlayerSnapshotSort_IsInConflict(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
sorts [2]domain.PlayerSnapshotSort
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedRes bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK: id:ASC serverKey:ASC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortServerKeyASC},
|
|
|
|
},
|
|
|
|
expectedRes: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: id:DESC serverKey:ASC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDDESC, domain.PlayerSnapshotSortServerKeyASC},
|
|
|
|
},
|
|
|
|
expectedRes: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: id:ASC id:ASC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDASC},
|
|
|
|
},
|
|
|
|
expectedRes: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: id:ASC id:DESC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortIDASC, domain.PlayerSnapshotSortIDDESC},
|
|
|
|
},
|
|
|
|
expectedRes: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: date:ASC date:DESC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortDateASC, domain.PlayerSnapshotSortDateDESC},
|
|
|
|
},
|
|
|
|
expectedRes: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: serverKey:DESC serverKey:ASC",
|
|
|
|
args: args{
|
|
|
|
sorts: [2]domain.PlayerSnapshotSort{domain.PlayerSnapshotSortServerKeyDESC, domain.PlayerSnapshotSortServerKeyASC},
|
|
|
|
},
|
|
|
|
expectedRes: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert.Equal(t, tt.expectedRes, tt.args.sorts[0].IsInConflict(tt.args.sorts[1]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
func TestNewPlayerSnapshotCursor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
validPlayerSnapshotCursor := domaintest.NewPlayerSnapshotCursor(t)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
id int
|
|
|
|
serverKey string
|
|
|
|
date time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
|
|
|
id: validPlayerSnapshotCursor.ID(),
|
|
|
|
serverKey: validPlayerSnapshotCursor.ServerKey(),
|
|
|
|
date: validPlayerSnapshotCursor.Date(),
|
|
|
|
},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: id < 1",
|
|
|
|
args: args{
|
|
|
|
id: 0,
|
|
|
|
serverKey: validPlayerSnapshotCursor.ServerKey(),
|
|
|
|
date: validPlayerSnapshotCursor.Date(),
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "PlayerSnapshotCursor",
|
|
|
|
Field: "id",
|
|
|
|
Err: domain.MinGreaterEqualError{
|
|
|
|
Min: 1,
|
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serverKeyTest := range newServerKeyValidationTests() {
|
|
|
|
tests = append(tests, test{
|
|
|
|
name: serverKeyTest.name,
|
|
|
|
args: args{
|
|
|
|
id: validPlayerSnapshotCursor.ID(),
|
|
|
|
serverKey: serverKeyTest.key,
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "PlayerSnapshotCursor",
|
|
|
|
Field: "serverKey",
|
|
|
|
Err: serverKeyTest.expectedErr,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
psc, err := domain.NewPlayerSnapshotCursor(
|
|
|
|
tt.args.id,
|
|
|
|
tt.args.serverKey,
|
|
|
|
tt.args.date,
|
|
|
|
)
|
|
|
|
require.ErrorIs(t, err, tt.expectedErr)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.args.id, psc.ID())
|
|
|
|
assert.Equal(t, tt.args.serverKey, psc.ServerKey())
|
|
|
|
assert.Equal(t, tt.args.date, psc.Date())
|
|
|
|
assert.NotEmpty(t, psc.Encode())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 06:23:46 +00:00
|
|
|
func TestListPlayerSnapshotsParams_SetServerKeys(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
serverKeys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
|
|
|
serverKeys: []string{
|
|
|
|
domaintest.RandServerKey(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serverKeyTest := range newServerKeyValidationTests() {
|
|
|
|
tests = append(tests, test{
|
|
|
|
name: serverKeyTest.name,
|
|
|
|
args: args{
|
|
|
|
serverKeys: []string{serverKeyTest.key},
|
|
|
|
},
|
|
|
|
expectedErr: domain.SliceElementValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "serverKeys",
|
|
|
|
Index: 0,
|
|
|
|
Err: serverKeyTest.expectedErr,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
|
|
|
|
require.ErrorIs(t, params.SetServerKeys(tt.args.serverKeys), tt.expectedErr)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.args.serverKeys, params.ServerKeys())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 07:19:30 +00:00
|
|
|
func TestListPlayerSnapshotsParams_SetPlayerIDs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
playerIDs []int
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
|
|
|
playerIDs: []int{
|
|
|
|
domaintest.RandID(),
|
|
|
|
domaintest.RandID(),
|
|
|
|
domaintest.RandID(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: value < 1",
|
|
|
|
args: args{
|
|
|
|
playerIDs: []int{
|
|
|
|
domaintest.RandID(),
|
|
|
|
domaintest.RandID(),
|
|
|
|
domaintest.RandID(),
|
|
|
|
0,
|
|
|
|
domaintest.RandID(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.SliceElementValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "playerIDs",
|
|
|
|
Index: 3,
|
|
|
|
Err: domain.MinGreaterEqualError{
|
|
|
|
Min: 1,
|
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
|
|
|
|
require.ErrorIs(t, params.SetPlayerIDs(tt.args.playerIDs), tt.expectedErr)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.args.playerIDs, params.PlayerIDs())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:28:03 +00:00
|
|
|
func TestListPlayerSnapshotsParams_SetSort(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
sort []domain.PlayerSnapshotSort
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
|
|
|
sort: []domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortDateASC,
|
|
|
|
domain.PlayerSnapshotSortServerKeyASC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: len(sort) < 1",
|
|
|
|
args: args{
|
|
|
|
sort: nil,
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 3,
|
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: len(sort) > 3",
|
|
|
|
args: args{
|
|
|
|
sort: []domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortDateASC,
|
|
|
|
domain.PlayerSnapshotSortServerKeyASC,
|
|
|
|
domain.PlayerSnapshotSortIDASC,
|
|
|
|
domain.PlayerSnapshotSortIDDESC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 3,
|
|
|
|
Current: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-03-02 08:49:42 +00:00
|
|
|
{
|
|
|
|
name: "ERR: conflict",
|
|
|
|
args: args{
|
|
|
|
sort: []domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortIDASC,
|
|
|
|
domain.PlayerSnapshotSortIDDESC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.SortConflictError{
|
|
|
|
Sort: [2]string{domain.PlayerSnapshotSortIDASC.String(), domain.PlayerSnapshotSortIDDESC.String()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-16 06:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
|
|
|
|
require.ErrorIs(t, params.SetSort(tt.args.sort), tt.expectedErr)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.args.sort, params.Sort())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:10:28 +00:00
|
|
|
func TestListPlayerSnapshotsParams_PrependSortString(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
defaultNewParams := func(t *testing.T) domain.ListPlayerSnapshotsParams {
|
|
|
|
t.Helper()
|
|
|
|
return domain.ListPlayerSnapshotsParams{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
sort []string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
newParams func(t *testing.T) domain.ListPlayerSnapshotsParams
|
|
|
|
args args
|
|
|
|
expectedSort []domain.PlayerSnapshotSort
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK: [date:ASC]",
|
|
|
|
args: args{
|
|
|
|
sort: []string{
|
|
|
|
"date:ASC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSort: []domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortDateASC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OK: [date:DESC]",
|
|
|
|
args: args{
|
|
|
|
sort: []string{
|
|
|
|
"date:DESC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSort: []domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortDateDESC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: len(sort) < 1",
|
|
|
|
args: args{
|
|
|
|
sort: nil,
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 3,
|
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: custom params + len(sort) > 1",
|
|
|
|
newParams: func(t *testing.T) domain.ListPlayerSnapshotsParams {
|
|
|
|
t.Helper()
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
require.NoError(t, params.SetSort([]domain.PlayerSnapshotSort{
|
|
|
|
domain.PlayerSnapshotSortServerKeyASC,
|
|
|
|
domain.PlayerSnapshotSortIDASC,
|
|
|
|
}))
|
|
|
|
return params
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
sort: []string{
|
|
|
|
"date:ASC",
|
|
|
|
"date:DESC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 1,
|
|
|
|
Current: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: unsupported sort string",
|
|
|
|
newParams: defaultNewParams,
|
|
|
|
args: args{
|
|
|
|
sort: []string{
|
|
|
|
"date:",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.SliceElementValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Index: 0,
|
|
|
|
Err: domain.UnsupportedSortStringError{
|
|
|
|
Sort: "date:",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ERR: conflict",
|
|
|
|
args: args{
|
|
|
|
sort: []string{
|
|
|
|
"date:ASC",
|
|
|
|
"date:DESC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "sort",
|
|
|
|
Err: domain.SortConflictError{
|
|
|
|
Sort: [2]string{domain.PlayerSnapshotSortDateASC.String(), domain.PlayerSnapshotSortDateDESC.String()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
newParams := defaultNewParams
|
|
|
|
if tt.newParams != nil {
|
|
|
|
newParams = tt.newParams
|
|
|
|
}
|
|
|
|
params := newParams(t)
|
|
|
|
|
|
|
|
require.ErrorIs(t, params.PrependSortString(tt.args.sort), tt.expectedErr)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, tt.expectedSort, params.Sort())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
func TestListPlayerSnapshotsParams_SetEncodedCursor(t *testing.T) {
|
2024-01-16 06:28:03 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
validCursor := domaintest.NewPlayerSnapshotCursor(t)
|
|
|
|
|
2024-01-16 06:28:03 +00:00
|
|
|
type args struct {
|
2024-03-16 06:43:46 +00:00
|
|
|
cursor string
|
2024-01-16 06:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2024-03-16 06:43:46 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedCursor domain.PlayerSnapshotCursor
|
|
|
|
expectedErr error
|
2024-01-16 06:28:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
2024-03-16 06:43:46 +00:00
|
|
|
cursor: validCursor.Encode(),
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
2024-03-16 06:43:46 +00:00
|
|
|
expectedCursor: validCursor,
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
{
|
2024-03-16 06:43:46 +00:00
|
|
|
name: "ERR: len(cursor) < 1",
|
2024-01-16 06:28:03 +00:00
|
|
|
args: args{
|
2024-03-16 06:43:46 +00:00
|
|
|
cursor: "",
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
2024-03-16 06:43:46 +00:00
|
|
|
Field: "cursor",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
2024-01-16 06:28:03 +00:00
|
|
|
Min: 1,
|
2024-03-16 06:43:46 +00:00
|
|
|
Max: 1000,
|
2024-01-16 06:28:03 +00:00
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-03-16 06:43:46 +00:00
|
|
|
name: "ERR: len(cursor) > 1000",
|
2024-01-16 06:28:03 +00:00
|
|
|
args: args{
|
2024-03-16 06:43:46 +00:00
|
|
|
cursor: gofakeit.LetterN(1001),
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
2024-03-16 06:43:46 +00:00
|
|
|
Field: "cursor",
|
|
|
|
Err: domain.LenOutOfRangeError{
|
|
|
|
Min: 1,
|
|
|
|
Max: 1000,
|
|
|
|
Current: 1001,
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-03-16 06:43:46 +00:00
|
|
|
{
|
|
|
|
name: "ERR: malformed base64",
|
|
|
|
args: args{
|
|
|
|
cursor: "112345",
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "cursor",
|
|
|
|
Err: domain.ErrInvalidCursor,
|
|
|
|
},
|
|
|
|
},
|
2024-01-16 06:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
require.ErrorIs(t, params.SetEncodedCursor(tt.args.cursor), tt.expectedErr)
|
2024-01-16 06:28:03 +00:00
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-03-16 06:43:46 +00:00
|
|
|
assert.Equal(t, tt.args.cursor, params.Cursor().Encode())
|
2024-01-16 06:28:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
func TestListPlayerSnapshotsParams_SetLimit(t *testing.T) {
|
2024-01-16 06:28:03 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type args struct {
|
2024-03-16 06:43:46 +00:00
|
|
|
limit int
|
2024-01-16 06:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
args: args{
|
2024-03-16 06:43:46 +00:00
|
|
|
limit: domain.PlayerSnapshotListMaxLimit,
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-03-16 06:43:46 +00:00
|
|
|
name: "ERR: limit < 1",
|
2024-01-16 06:28:03 +00:00
|
|
|
args: args{
|
2024-03-16 06:43:46 +00:00
|
|
|
limit: 0,
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
2024-03-16 06:43:46 +00:00
|
|
|
Field: "limit",
|
2024-01-16 06:28:03 +00:00
|
|
|
Err: domain.MinGreaterEqualError{
|
2024-03-16 06:43:46 +00:00
|
|
|
Min: 1,
|
|
|
|
Current: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: fmt.Sprintf("ERR: limit > %d", domain.PlayerSnapshotListMaxLimit),
|
|
|
|
args: args{
|
|
|
|
limit: domain.PlayerSnapshotListMaxLimit + 1,
|
|
|
|
},
|
|
|
|
expectedErr: domain.ValidationError{
|
|
|
|
Model: "ListPlayerSnapshotsParams",
|
|
|
|
Field: "limit",
|
|
|
|
Err: domain.MaxLessEqualError{
|
|
|
|
Max: domain.PlayerSnapshotListMaxLimit,
|
|
|
|
Current: domain.PlayerSnapshotListMaxLimit + 1,
|
2024-01-16 06:28:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
params := domain.NewListPlayerSnapshotsParams()
|
|
|
|
|
2024-03-16 06:43:46 +00:00
|
|
|
require.ErrorIs(t, params.SetLimit(tt.args.limit), tt.expectedErr)
|
2024-01-16 06:28:03 +00:00
|
|
|
if tt.expectedErr != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-03-16 06:43:46 +00:00
|
|
|
assert.Equal(t, tt.args.limit, params.Limit())
|
2024-01-16 06:28:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 06:43:46 +00:00
|
|
|
|
|
|
|
func TestNewListPlayerSnapshotsResult(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
snapshots := domain.PlayerSnapshots{
|
|
|
|
domaintest.NewPlayerSnapshot(t),
|
|
|
|
domaintest.NewPlayerSnapshot(t),
|
|
|
|
domaintest.NewPlayerSnapshot(t),
|
|
|
|
}
|
|
|
|
next := domaintest.NewPlayerSnapshot(t)
|
|
|
|
|
|
|
|
t.Run("OK: with next", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsResult(snapshots, next)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, snapshots, res.PlayerSnapshots())
|
|
|
|
assert.Equal(t, snapshots[0].ID(), res.Self().ID())
|
|
|
|
assert.Equal(t, snapshots[0].ServerKey(), res.Self().ServerKey())
|
|
|
|
assert.Equal(t, snapshots[0].Date(), res.Self().Date())
|
|
|
|
assert.Equal(t, next.ID(), res.Next().ID())
|
|
|
|
assert.Equal(t, next.ServerKey(), res.Next().ServerKey())
|
|
|
|
assert.Equal(t, next.Date(), res.Next().Date())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK: without next", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsResult(snapshots, domain.PlayerSnapshot{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, snapshots, res.PlayerSnapshots())
|
|
|
|
assert.Equal(t, snapshots[0].ID(), res.Self().ID())
|
|
|
|
assert.Equal(t, snapshots[0].ServerKey(), res.Self().ServerKey())
|
|
|
|
assert.Equal(t, snapshots[0].Date(), res.Self().Date())
|
|
|
|
assert.True(t, res.Next().IsZero())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK: 0 snapshots", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsResult(nil, domain.PlayerSnapshot{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Zero(t, res.PlayerSnapshots())
|
|
|
|
assert.True(t, res.Self().IsZero())
|
|
|
|
assert.True(t, res.Next().IsZero())
|
|
|
|
})
|
|
|
|
}
|
2024-03-20 05:49:24 +00:00
|
|
|
|
|
|
|
func TestNewListPlayerSnapshotsWithRelationsResult(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
snapshots := domain.PlayerSnapshotsWithRelations{
|
|
|
|
domaintest.NewPlayerSnapshotWithRelations(t),
|
|
|
|
domaintest.NewPlayerSnapshotWithRelations(t),
|
|
|
|
domaintest.NewPlayerSnapshotWithRelations(t),
|
|
|
|
}
|
|
|
|
next := domaintest.NewPlayerSnapshotWithRelations(t)
|
|
|
|
|
|
|
|
t.Run("OK: with next", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsWithRelationsResult(snapshots, next)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, snapshots, res.PlayerSnapshots())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().ID(), res.Self().ID())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().ServerKey(), res.Self().ServerKey())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().Date(), res.Self().Date())
|
|
|
|
assert.Equal(t, next.PlayerSnapshot().ID(), res.Next().ID())
|
|
|
|
assert.Equal(t, next.PlayerSnapshot().ServerKey(), res.Next().ServerKey())
|
|
|
|
assert.Equal(t, next.PlayerSnapshot().Date(), res.Next().Date())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK: without next", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsWithRelationsResult(snapshots, domain.PlayerSnapshotWithRelations{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, snapshots, res.PlayerSnapshots())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().ID(), res.Self().ID())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().ServerKey(), res.Self().ServerKey())
|
|
|
|
assert.Equal(t, snapshots[0].PlayerSnapshot().Date(), res.Self().Date())
|
|
|
|
assert.True(t, res.Next().IsZero())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK: 0 snapshots", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := domain.NewListPlayerSnapshotsWithRelationsResult(nil, domain.PlayerSnapshotWithRelations{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Zero(t, res.PlayerSnapshots())
|
|
|
|
assert.True(t, res.Self().IsZero())
|
|
|
|
assert.True(t, res.Next().IsZero())
|
|
|
|
})
|
|
|
|
}
|