Dawid Wysokiński
c2a73114cc
All checks were successful
continuous-integration/drone/push Build is passing
292 lines
7.5 KiB
Go
292 lines
7.5 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service/internal/mock"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPlayerSnapshot_Create(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
playerSvc := &mock.FakePlayerLister{}
|
|
players := []domain.Player{
|
|
{
|
|
OpponentsDefeated: domain.OpponentsDefeated{
|
|
RankAtt: 8,
|
|
ScoreAtt: 7,
|
|
RankDef: 6,
|
|
ScoreDef: 5,
|
|
RankSup: 4,
|
|
ScoreSup: 3,
|
|
RankTotal: 2,
|
|
ScoreTotal: 1,
|
|
},
|
|
ID: 997,
|
|
Name: "name 997",
|
|
NumVillages: 5,
|
|
Points: 4,
|
|
Rank: 2,
|
|
TribeID: 1234,
|
|
ProfileURL: "profile-997",
|
|
ServerKey: "pl151",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
{
|
|
OpponentsDefeated: domain.OpponentsDefeated{
|
|
RankAtt: 1,
|
|
ScoreAtt: 2,
|
|
RankDef: 3,
|
|
ScoreDef: 4,
|
|
RankSup: 5,
|
|
ScoreSup: 6,
|
|
RankTotal: 7,
|
|
ScoreTotal: 8,
|
|
},
|
|
ID: 998,
|
|
Name: "name 998",
|
|
NumVillages: 2,
|
|
Points: 3,
|
|
Rank: 4,
|
|
TribeID: 0,
|
|
ProfileURL: "profile-998",
|
|
ServerKey: "pl151",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
}
|
|
playerSvc.ListReturns(players, nil)
|
|
|
|
repo := &mock.FakePlayerSnapshotRepository{}
|
|
repo.CreateReturns(nil)
|
|
|
|
date := time.Now()
|
|
assert.NoError(t, service.NewPlayerSnapshot(repo, playerSvc).Create(context.Background(), "pl151", date))
|
|
|
|
require.Equal(t, 1, repo.CreateCallCount())
|
|
_, argParams := repo.CreateArgsForCall(0)
|
|
assert.Len(t, argParams, len(players))
|
|
for i, p := range players {
|
|
assert.Equal(t, domain.CreatePlayerSnapshotParams{
|
|
OpponentsDefeated: p.OpponentsDefeated,
|
|
PlayerID: p.ID,
|
|
NumVillages: p.NumVillages,
|
|
Points: p.Points,
|
|
Rank: p.Rank,
|
|
TribeID: p.TribeID,
|
|
ServerKey: p.ServerKey,
|
|
Date: date,
|
|
}, argParams[i])
|
|
}
|
|
}
|
|
|
|
func TestPlayerSnapshot_ListCountWithRelations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var defaultLimit int32 = 100
|
|
defaultSort := []domain.PlayerSnapshotSort{
|
|
{
|
|
By: domain.PlayerSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.PlayerSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListPlayerSnapshotsParams
|
|
expectedParams domain.ListPlayerSnapshotsParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "OK: default limit/sort",
|
|
params: domain.ListPlayerSnapshotsParams{},
|
|
expectedParams: domain.ListPlayerSnapshotsParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom pagination",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 99,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expectedParams: domain.ListPlayerSnapshotsParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: 99,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom sort",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Sort: []domain.PlayerSnapshotSort{
|
|
{By: domain.PlayerSnapshotSortByDate, Direction: domain.SortDirectionDESC},
|
|
},
|
|
},
|
|
expectedParams: domain.ListPlayerSnapshotsParams{
|
|
Sort: []domain.PlayerSnapshotSort{
|
|
{By: domain.PlayerSnapshotSortByDate, Direction: domain.SortDirectionDESC},
|
|
},
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit < 0",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit > 100",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 101,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 100,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Offset < 0",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: len(params.Sort) > 2",
|
|
params: domain.ListPlayerSnapshotsParams{
|
|
Sort: []domain.PlayerSnapshotSort{
|
|
{
|
|
By: domain.PlayerSnapshotSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.PlayerSnapshotSortByDate,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.PlayerSnapshotSortByID,
|
|
Direction: domain.SortDirectionDESC,
|
|
},
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "sort",
|
|
Err: domain.MaxLengthError{
|
|
Max: 2,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakePlayerSnapshotRepository{}
|
|
repo.ListCountWithRelationsCalls(func(
|
|
_ context.Context,
|
|
params domain.ListPlayerSnapshotsParams,
|
|
) ([]domain.PlayerSnapshotWithRelations, int64, error) {
|
|
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.PlayerSnapshotWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
|
|
snapshots, count, err := service.NewPlayerSnapshot(repo, &mock.FakePlayerLister{}).
|
|
ListCountWithRelations(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.EqualValues(t, tt.expectedParams.Pagination.Limit, count)
|
|
assert.Len(t, snapshots, int(tt.expectedParams.Pagination.Limit))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlayerSnapshot_CleanUp(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
serverKey := "pl151"
|
|
repo := &mock.FakePlayerSnapshotRepository{}
|
|
repo.DeleteReturns(nil)
|
|
|
|
svc := service.NewPlayerSnapshot(repo, &mock.FakePlayerLister{})
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: true,
|
|
Open: false,
|
|
TribeSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with Special = false
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: true,
|
|
PlayerSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with Open = false
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: false,
|
|
PlayerSnapshotsCreatedAt: time.Now(),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with PlayerSnapshotsCreatedAt < now - 30 days
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: false,
|
|
PlayerSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
require.Equal(t, 1, repo.DeleteCallCount())
|
|
_, argServerKey, argCreatedAtLTE := repo.DeleteArgsForCall(0)
|
|
assert.Equal(t, serverKey, argServerKey)
|
|
assert.WithinDuration(t, time.Now().Add(-180*24*time.Hour), argCreatedAtLTE, time.Second)
|
|
}
|