This repository has been archived on 2024-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
core-old/internal/service/player_test.go
Dawid Wysokiński 1f88523f5a
All checks were successful
continuous-integration/drone/push Build is passing
refactor: player service - simplify tests (#196)
Reviewed-on: twhelp/core#196
2023-02-28 06:01:53 +00:00

903 lines
24 KiB
Go

package service_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
"gitea.dwysokinski.me/twhelp/core/internal/tw"
"github.com/google/go-cmp/cmp"
"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/stretchr/testify/assert"
)
func TestPlayer_Refresh(t *testing.T) {
t.Parallel()
now := time.Now()
tests := []struct {
name string
serverKey string
serverURL string
clientPlayers []tw.Player
existingPlayers []domain.Player
expectedCreatePlayerParams []domain.CreatePlayerParams
expectedCreateTribeChangeParams []domain.CreateTribeChangeParams
expectedDeletedPlayers []int64
}{
{
name: "OK: create a new player",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 3,
Points: 4,
Rank: 6,
TribeID: 1234,
ProfileURL: "profile-999",
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 3,
Points: 4,
Rank: 6,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 6,
BestRankAt: now,
MostPoints: 4,
MostPointsAt: now,
MostVillages: 3,
MostVillagesAt: now,
LastActivityAt: now,
ServerKey: "pl169",
},
},
expectedCreateTribeChangeParams: []domain.CreateTribeChangeParams{
{
PlayerID: 999,
NewTribeID: 1234,
OldTribeID: 0,
ServerKey: "pl169",
},
},
},
{
name: "OK: Update existing player (without tribe change) + Update last activity at (new.ScoreAtt > old.ScoreAtt)",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 5,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 1,
Points: 2,
Rank: 8,
TribeID: 1234,
ProfileURL: "profile-999",
},
},
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 2,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2,
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 5,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 1,
Points: 2,
Rank: 8,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7, // new.Rank >= old.BestRank, so BestRank remains unchanged
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3, // new.Points <= old.MostPoints, so MostPoints remains unchanged
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2, // new.NumVillages <= old.MostVillages, so MostVillages remains unchanged
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now, // LastActivityAt is updated because of new.ScoreAtt > old.ScoreAtt
ServerKey: "pl169",
},
},
},
{
name: "OK: Update existing player (without tribe change) + Update last activity at (new.NumVillages > old.NumVillages)",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 4,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
},
},
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 3,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 5,
MostVillagesAt: now.Add(-6 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 4,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7, // new.Rank >= old.BestRank, so BestRank remains unchanged
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3, // new.Points <= old.MostPoints, so MostPoints remains unchanged
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 5, // new.NumVillages <= old.MostVillages, so MostVillages remains unchanged
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now, // LastActivityAt is updated because of new.NumVillages > old.NumVillages
ServerKey: "pl169",
},
},
},
{
name: "OK: Update existing player (without tribe change) + Update last activity at (new.Points > old.Points)",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 5,
Points: 33533,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
},
},
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 5,
Points: 33333,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 33933,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 5,
MostVillagesAt: now.Add(-6 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 5,
Points: 33533,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7, // new.Rank >= old.BestRank, so BestRank remains unchanged
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 33933, // new.Points <= old.MostPoints, so MostPoints remains unchanged
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 5, // new.NumVillages <= old.MostVillages, so MostVillages remains unchanged
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now, // LastActivityAt is updated because of new.Points > old.Points
ServerKey: "pl169",
},
},
},
{
name: "OK: Update existing player (with tribe change)",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 1,
Points: 2,
Rank: 8,
TribeID: 1115,
ProfileURL: "profile-999",
},
},
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 2,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2,
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 1,
Points: 2,
Rank: 8,
TribeID: 1115,
ProfileURL: "profile-999",
BestRank: 7, // new.Rank >= old.BestRank, so BestRank remains unchanged
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3, // new.Points <= old.MostPoints, so MostPoints remains unchanged
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2, // new.NumVillages <= old.MostVillages, so MostVillages remains unchanged
MostVillagesAt: now.Add(-2 * time.Hour),
// new.NumVillages <= old.NumVillages, new.Points <= old.Points and new.ScoreAtt <= old.ScoreAtt
// so LastActivityAt remains unchanged
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
},
},
expectedCreateTribeChangeParams: []domain.CreateTribeChangeParams{
{
PlayerID: 999,
NewTribeID: 1115,
OldTribeID: 1234,
ServerKey: "pl169",
},
},
},
{
name: "OK: Update existing player (without tribe change) + update best rank/most points/most villages/last activity at",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: []tw.Player{
{
OpponentsDefeated: tw.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 3,
Points: 4,
Rank: 6,
TribeID: 1234,
ProfileURL: "profile-999",
},
},
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 2,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2,
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
},
expectedCreatePlayerParams: []domain.CreatePlayerParams{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 3,
Points: 4,
Rank: 6,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 6, // BestRank is updated because of new.Rank < old.BestRank
BestRankAt: now,
MostPoints: 4, // MostPoints is updated because of new.Points > old.MostPoints
MostPointsAt: now,
MostVillages: 3, // MostVillages is updated because of new.NumVillages > old.MostVillages
MostVillagesAt: now,
LastActivityAt: now, // LastActivityAt is updated because of new.NumVillages > old.NumVillages and new.Points > old.Points
ServerKey: "pl169",
},
},
},
{
name: "OK: Delete no longer existing players",
serverKey: "pl169",
serverURL: "https://pl169.plemiona.pl",
clientPlayers: nil,
existingPlayers: []domain.Player{
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 999,
Name: "name 999",
NumVillages: 2,
Points: 3,
Rank: 7,
TribeID: 1234,
ProfileURL: "profile-999",
BestRank: 7,
BestRankAt: now.Add(-2 * time.Hour),
MostPoints: 3,
MostPointsAt: now.Add(-2 * time.Hour),
MostVillages: 2,
MostVillagesAt: now.Add(-2 * time.Hour),
LastActivityAt: now.Add(-2 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-24 * time.Hour),
},
{
OpponentsDefeated: domain.OpponentsDefeated{
RankAtt: 1,
ScoreAtt: 2,
RankDef: 3,
ScoreDef: 4,
RankSup: 5,
ScoreSup: 6,
RankTotal: 7,
ScoreTotal: 8,
},
ID: 1000,
Name: "name 1000",
NumVillages: 333,
Points: 333333,
Rank: 3,
TribeID: 0,
ProfileURL: "profile-1000",
BestRank: 1,
BestRankAt: now.Add(-72 * time.Hour),
MostPoints: 4444444,
MostPointsAt: now.Add(-72 * time.Hour),
MostVillages: 444,
MostVillagesAt: now.Add(-72 * time.Hour),
LastActivityAt: now.Add(-72 * time.Hour),
ServerKey: "pl169",
CreatedAt: now.Add(-168 * time.Hour),
},
},
expectedCreatePlayerParams: nil,
expectedDeletedPlayers: []int64{999, 1000},
expectedCreateTribeChangeParams: []domain.CreateTribeChangeParams{
{
PlayerID: 999, // player with id 999 has TribeID > 0, so TribeChange must also be created
NewTribeID: 0,
OldTribeID: 1234,
ServerKey: "pl169",
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
client := &mock.FakePlayerGetter{}
client.GetPlayersCalls(func(ctx context.Context, baseURL string) ([]tw.Player, error) {
if baseURL != tt.serverURL {
return nil, errors.New("incorrect baseURL")
}
return tt.clientPlayers, nil
})
repo := &mock.FakePlayerRepository{}
repo.CreateOrUpdateReturns(nil)
repo.ListReturns(tt.existingPlayers, nil)
repo.DeleteCalls(func(ctx context.Context, key string, _ ...int64) error {
if key != tt.serverKey {
return errors.New("incorrect server key")
}
return nil
})
tribeChangeSvc := &mock.FakeTribeChangeCreator{}
tribeChangeSvc.CreateReturns(nil)
numPlayers, err := service.NewPlayer(repo, tribeChangeSvc, client).Refresh(context.Background(), tt.serverKey, tt.serverURL)
assert.NoError(t, err)
assert.EqualValues(t, len(tt.clientPlayers), numPlayers)
var argCreatePlayerParams []domain.CreatePlayerParams
for i := 0; i < repo.CreateOrUpdateCallCount(); i++ {
_, params := repo.CreateOrUpdateArgsForCall(i)
argCreatePlayerParams = append(argCreatePlayerParams, params...)
}
assert.Empty(t, cmp.Diff(
tt.expectedCreatePlayerParams,
argCreatePlayerParams,
cmp.Comparer(func(x time.Time, y time.Time) bool {
dt := x.Sub(y)
return dt > -time.Second || dt < time.Second
}),
))
var argDeleteIDs []int64
for i := 0; i < repo.DeleteCallCount(); i++ {
_, _, ids := repo.DeleteArgsForCall(i)
argDeleteIDs = append(argDeleteIDs, ids...)
}
assert.Empty(t, cmp.Diff(tt.expectedDeletedPlayers, argDeleteIDs))
var argCreateTribeChangeParams []domain.CreateTribeChangeParams
for i := 0; i < tribeChangeSvc.CreateCallCount(); i++ {
_, params := tribeChangeSvc.CreateArgsForCall(i)
argCreateTribeChangeParams = append(argCreateTribeChangeParams, params...)
}
assert.Empty(t, cmp.Diff(tt.expectedCreateTribeChangeParams, argCreateTribeChangeParams))
})
}
}
func TestPlayer_List_ListCountWithRelations(t *testing.T) {
t.Parallel()
var defaultLimit int32 = 200
defaultSort := []domain.PlayerSort{
{
By: domain.PlayerSortByID,
Direction: domain.SortDirectionASC,
},
}
tests := []struct {
name string
params domain.ListPlayersParams
expectedParams domain.ListPlayersParams
expectedErr error
}{
{
name: "OK: default limit/sort",
params: domain.ListPlayersParams{},
expectedParams: domain.ListPlayersParams{
Sort: defaultSort,
Pagination: domain.Pagination{
Limit: defaultLimit,
},
},
},
{
name: "OK: custom pagination",
params: domain.ListPlayersParams{
Pagination: domain.Pagination{
Offset: 1,
Limit: 199,
},
},
expectedParams: domain.ListPlayersParams{
Sort: defaultSort,
Pagination: domain.Pagination{
Limit: 199,
Offset: 1,
},
},
},
{
name: "OK: custom sort",
params: domain.ListPlayersParams{
Sort: []domain.PlayerSort{
{By: domain.PlayerSortByPoints, Direction: domain.SortDirectionDESC},
},
},
expectedParams: domain.ListPlayersParams{
Sort: []domain.PlayerSort{
{By: domain.PlayerSortByPoints, Direction: domain.SortDirectionDESC},
},
Pagination: domain.Pagination{
Limit: defaultLimit,
},
},
},
{
name: "ERR: params.Pagination.Limit < 0",
params: domain.ListPlayersParams{
Pagination: domain.Pagination{
Limit: -1,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MinError{
Min: 1,
},
},
},
{
name: "ERR: params.Pagination.Limit > 200",
params: domain.ListPlayersParams{
Pagination: domain.Pagination{
Limit: 201,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MaxError{
Max: 200,
},
},
},
{
name: "ERR: params.Pagination.Offset < 0",
params: domain.ListPlayersParams{
Pagination: domain.Pagination{
Offset: -1,
},
},
expectedErr: domain.ValidationError{
Field: "offset",
Err: domain.MinError{
Min: 0,
},
},
},
{
name: "ERR: len(params.Sort) > 3",
params: domain.ListPlayersParams{
Sort: []domain.PlayerSort{
{
By: domain.PlayerSortByID,
Direction: domain.SortDirectionASC,
},
{
By: domain.PlayerSortByScoreDef,
Direction: domain.SortDirectionDESC,
},
{
By: domain.PlayerSortByScoreTotal,
Direction: domain.SortDirectionASC,
},
{
By: domain.PlayerSortByScoreAtt,
Direction: domain.SortDirectionASC,
},
},
},
expectedErr: domain.ValidationError{
Field: "sort",
Err: domain.MaxLengthError{
Max: 3,
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
repo := &mock.FakePlayerRepository{}
repo.ListCountWithRelationsCalls(func(
_ context.Context,
params domain.ListPlayersParams,
) ([]domain.PlayerWithRelations, int64, error) {
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
return nil, 0, fmt.Errorf("validation failed: %s", diff)
}
return make([]domain.PlayerWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
})
repo.ListCalls(func(
ctx context.Context,
params domain.ListPlayersParams,
) ([]domain.Player, error) {
playersWithRelations, _, err := repo.ListCountWithRelations(ctx, params)
if err != nil {
return nil, err
}
players := make([]domain.Player, 0, len(playersWithRelations))
for _, p := range playersWithRelations {
players = append(players, p.Player)
}
return players, nil
})
svc := service.NewPlayer(repo, &mock.FakeTribeChangeCreator{}, &mock.FakePlayerGetter{})
playersListCountWithRelations, count, err := svc.ListCountWithRelations(context.Background(), tt.params)
assert.ErrorIs(t, err, tt.expectedErr)
assert.EqualValues(t, tt.expectedParams.Pagination.Limit, count)
assert.Len(t, playersListCountWithRelations, int(tt.expectedParams.Pagination.Limit))
playersList, err := svc.List(context.Background(), tt.params)
assert.ErrorIs(t, err, tt.expectedErr)
assert.Len(t, playersList, len(playersListCountWithRelations))
})
}
}
func TestPlayer_GetByServerKeyAndIDWithRelations(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
t.Parallel()
p := domain.PlayerWithRelations{
Player: domain.Player{
ID: 123,
TribeID: 123,
ServerKey: "pl151",
},
Tribe: domain.NullTribeMeta{
Valid: true,
Tribe: domain.TribeMeta{
ID: 123,
},
},
}
repo := &mock.FakePlayerRepository{}
repo.ListCountWithRelationsCalls(func(ctx context.Context, params domain.ListPlayersParams) ([]domain.PlayerWithRelations, int64, error) {
if diff := cmp.Diff(domain.ListPlayersParams{
IDs: []int64{p.ID},
ServerKeys: []string{p.ServerKey},
Pagination: domain.Pagination{
Limit: 1,
},
}, params); diff != "" {
return nil, 0, fmt.Errorf("validation failed: %s", diff)
}
return []domain.PlayerWithRelations{p}, 1, nil
})
repo.ListReturns([]domain.Player{p.Player}, nil)
player, err := service.NewPlayer(repo, &mock.FakeTribeChangeCreator{}, &mock.FakePlayerGetter{}).
GetByServerKeyAndIDWithRelations(context.Background(), p.ServerKey, p.ID)
assert.NoError(t, err)
assert.Equal(t, p, player)
})
t.Run("ERR: player not found", func(t *testing.T) {
t.Parallel()
repo := &mock.FakePlayerRepository{}
repo.ListCountWithRelationsReturns(nil, 0, nil)
var id int64 = 123
player, err := service.NewPlayer(repo, &mock.FakeTribeChangeCreator{}, &mock.FakePlayerGetter{}).
GetByServerKeyAndIDWithRelations(context.Background(), "pl151", id)
assert.ErrorIs(t, err, domain.PlayerNotFoundError{ID: id})
assert.Zero(t, player)
})
}