refactor: player snapshot service - simplify tests
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e5ec14c60e
commit
c2a73114cc
|
@ -71,216 +71,188 @@ func TestPlayerSnapshot_Create(t *testing.T) {
|
|||
assert.NoError(t, service.NewPlayerSnapshot(repo, playerSvc).Create(context.Background(), "pl151", date))
|
||||
|
||||
require.Equal(t, 1, repo.CreateCallCount())
|
||||
_, params := repo.CreateArgsForCall(0)
|
||||
assert.Len(t, params, len(players))
|
||||
_, argParams := repo.CreateArgsForCall(0)
|
||||
assert.Len(t, argParams, len(players))
|
||||
for i, p := range players {
|
||||
assert.Equal(t, p.OpponentsDefeated, params[i].OpponentsDefeated)
|
||||
assert.Equal(t, p.ID, params[i].PlayerID)
|
||||
assert.Equal(t, p.NumVillages, params[i].NumVillages)
|
||||
assert.Equal(t, p.Points, params[i].Points)
|
||||
assert.Equal(t, p.Rank, params[i].Rank)
|
||||
assert.Equal(t, p.TribeID, params[i].TribeID)
|
||||
assert.Equal(t, p.ServerKey, params[i].ServerKey)
|
||||
assert.Equal(t, date, params[i].Date)
|
||||
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()
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
limit int32
|
||||
sort []domain.PlayerSnapshotSort
|
||||
}{
|
||||
{
|
||||
name: "default limit, default sort",
|
||||
limit: 0,
|
||||
sort: nil,
|
||||
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: "custom limit",
|
||||
limit: 99,
|
||||
},
|
||||
{
|
||||
name: "OK: custom pagination",
|
||||
params: domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: 99,
|
||||
Offset: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "custom sort",
|
||||
limit: 0,
|
||||
sort: []domain.PlayerSnapshotSort{
|
||||
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
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limit := tt.limit
|
||||
if limit == 0 {
|
||||
limit = 100
|
||||
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)
|
||||
}
|
||||
|
||||
repo := &mock.FakePlayerSnapshotRepository{}
|
||||
repo.ListCountWithRelationsCalls(func(
|
||||
_ context.Context,
|
||||
params domain.ListPlayerSnapshotsParams,
|
||||
) ([]domain.PlayerSnapshotWithRelations, int64, error) {
|
||||
expectedParams := domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: limit,
|
||||
},
|
||||
Sort: func(sort []domain.PlayerSnapshotSort) []domain.PlayerSnapshotSort {
|
||||
if len(sort) == 0 {
|
||||
return []domain.PlayerSnapshotSort{
|
||||
{
|
||||
By: domain.PlayerSnapshotSortByDate,
|
||||
Direction: domain.SortDirectionASC,
|
||||
},
|
||||
{
|
||||
By: domain.PlayerSnapshotSortByID,
|
||||
Direction: domain.SortDirectionASC,
|
||||
},
|
||||
}
|
||||
}
|
||||
return sort
|
||||
}(tt.sort),
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(params, expectedParams); diff != "" {
|
||||
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
||||
}
|
||||
|
||||
return make([]domain.PlayerSnapshotWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
||||
})
|
||||
|
||||
svc := service.NewPlayerSnapshot(repo, &mock.FakePlayerLister{})
|
||||
|
||||
params := domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: tt.limit,
|
||||
},
|
||||
Sort: tt.sort,
|
||||
}
|
||||
|
||||
snapshots, count, err := svc.ListCountWithRelations(context.Background(), params)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, limit, count)
|
||||
assert.Len(t, snapshots, int(limit))
|
||||
return make([]domain.PlayerSnapshotWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ERR: validation failed", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params domain.ListPlayerSnapshotsParams
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "params.Pagination.Limit < 0",
|
||||
params: domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: -1,
|
||||
},
|
||||
},
|
||||
expectedErr: domain.ValidationError{
|
||||
Field: "limit",
|
||||
Err: domain.MinError{
|
||||
Min: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "params.Pagination.Limit > 100",
|
||||
params: domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: 101,
|
||||
},
|
||||
},
|
||||
expectedErr: domain.ValidationError{
|
||||
Field: "limit",
|
||||
Err: domain.MaxError{
|
||||
Max: 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "params.Pagination.Offset < 0",
|
||||
params: domain.ListPlayerSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Offset: -1,
|
||||
},
|
||||
},
|
||||
expectedErr: domain.ValidationError{
|
||||
Field: "offset",
|
||||
Err: domain.MinError{
|
||||
Min: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "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{}
|
||||
|
||||
svc := service.NewPlayerSnapshot(repo, &mock.FakePlayerLister{})
|
||||
|
||||
snapshots, count, err := svc.ListCountWithRelations(context.Background(), tt.params)
|
||||
assert.ErrorIs(t, err, tt.expectedErr)
|
||||
assert.Zero(t, snapshots)
|
||||
assert.Zero(t, count)
|
||||
assert.Equal(t, 0, repo.ListCountWithRelationsCallCount())
|
||||
})
|
||||
}
|
||||
})
|
||||
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"
|
||||
playerSvc := &mock.FakePlayerLister{}
|
||||
repo := &mock.FakePlayerSnapshotRepository{}
|
||||
repo.DeleteReturns(nil)
|
||||
|
||||
svc := service.NewPlayerSnapshot(repo, playerSvc)
|
||||
svc := service.NewPlayerSnapshot(repo, &mock.FakePlayerLister{})
|
||||
|
||||
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
||||
Key: serverKey,
|
||||
|
@ -292,6 +264,7 @@ func TestPlayerSnapshot_CleanUp(t *testing.T) {
|
|||
|
||||
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
||||
Key: serverKey,
|
||||
Special: false,
|
||||
Open: true,
|
||||
PlayerSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
||||
}))
|
||||
|
@ -299,6 +272,7 @@ func TestPlayerSnapshot_CleanUp(t *testing.T) {
|
|||
|
||||
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
||||
Key: serverKey,
|
||||
Special: false,
|
||||
Open: false,
|
||||
PlayerSnapshotsCreatedAt: time.Now(),
|
||||
}))
|
||||
|
@ -306,6 +280,7 @@ func TestPlayerSnapshot_CleanUp(t *testing.T) {
|
|||
|
||||
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
||||
Key: serverKey,
|
||||
Special: false,
|
||||
Open: false,
|
||||
PlayerSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
||||
}))
|
||||
|
|
Reference in New Issue
Block a user