refactor: tribe snapshot service - simplify tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dawid Wysokiński 2023-03-02 06:51:32 +01:00
parent 89b73d3429
commit 39b7169324
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
2 changed files with 179 additions and 187 deletions

View File

@ -2,6 +2,7 @@ package service_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
@ -17,6 +18,8 @@ import (
func TestPlayerSnapshot_Create(t *testing.T) {
t.Parallel()
serverKey := "pl151"
playerSvc := &mock.FakePlayerLister{}
players := []domain.Player{
{
@ -62,17 +65,22 @@ func TestPlayerSnapshot_Create(t *testing.T) {
CreatedAt: time.Now(),
},
}
playerSvc.ListReturns(players, nil)
playerSvc.ListCalls(func(ctx context.Context, params domain.ListPlayersParams) ([]domain.Player, error) {
if len(params.ServerKeys) == 0 || params.ServerKeys[0] != serverKey {
return nil, errors.New("incorrect server key")
}
return players, nil
})
repo := &mock.FakePlayerSnapshotRepository{}
repo.CreateReturns(nil)
date := time.Now()
assert.NoError(t, service.NewPlayerSnapshot(repo, playerSvc).Create(context.Background(), "pl151", date))
assert.NoError(t, service.NewPlayerSnapshot(repo, playerSvc).Create(context.Background(), serverKey, date))
require.Equal(t, 1, repo.CreateCallCount())
_, argParams := repo.CreateArgsForCall(0)
assert.Len(t, argParams, len(players))
require.Len(t, argParams, len(players))
for i, p := range players {
assert.Equal(t, domain.CreatePlayerSnapshotParams{
OpponentsDefeated: p.OpponentsDefeated,

View File

@ -2,6 +2,7 @@ package service_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
@ -17,6 +18,8 @@ import (
func TestTribeSnapshot_Create(t *testing.T) {
t.Parallel()
serverKey := "pl151"
tribeSvc := &mock.FakeTribeLister{}
tribes := []domain.Tribe{
{
@ -69,216 +72,194 @@ func TestTribeSnapshot_Create(t *testing.T) {
DeletedAt: time.Now(),
},
}
tribeSvc.ListReturns(tribes, nil)
tribeSvc.ListCalls(func(ctx context.Context, params domain.ListTribesParams) ([]domain.Tribe, error) {
if len(params.ServerKeys) == 0 || params.ServerKeys[0] != serverKey {
return nil, errors.New("incorrect server key")
}
return tribes, nil
})
repo := &mock.FakeTribeSnapshotRepository{}
repo.CreateReturns(nil)
date := time.Now()
assert.NoError(t, service.NewTribeSnapshot(repo, tribeSvc).Create(context.Background(), "pl151", date))
assert.NoError(t, service.NewTribeSnapshot(repo, tribeSvc).Create(context.Background(), serverKey, date))
require.Equal(t, 1, repo.CreateCallCount())
_, params := repo.CreateArgsForCall(0)
assert.Len(t, params, len(tribes))
_, argParams := repo.CreateArgsForCall(0)
require.Len(t, argParams, len(tribes))
for i, tribe := range tribes {
assert.Equal(t, tribe.OpponentsDefeated, params[i].OpponentsDefeated)
assert.Equal(t, tribe.ID, params[i].TribeID)
assert.Equal(t, tribe.NumVillages, params[i].NumVillages)
assert.Equal(t, tribe.NumMembers, params[i].NumMembers)
assert.Equal(t, tribe.Points, params[i].Points)
assert.Equal(t, tribe.AllPoints, params[i].AllPoints)
assert.Equal(t, tribe.Rank, params[i].Rank)
assert.Equal(t, tribe.Dominance, params[i].Dominance)
assert.Equal(t, tribe.ServerKey, params[i].ServerKey)
assert.Equal(t, date, params[i].Date)
assert.Equal(t, domain.CreateTribeSnapshotParams{
OpponentsDefeated: tribe.OpponentsDefeated,
TribeID: tribe.ID,
ServerKey: serverKey,
NumMembers: tribe.NumMembers,
NumVillages: tribe.NumVillages,
Points: tribe.Points,
AllPoints: tribe.AllPoints,
Rank: tribe.Rank,
Dominance: tribe.Dominance,
Date: date,
}, argParams[i])
}
}
func TestTribeSnapshot_ListCount(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
limit int32
sort []domain.TribeSnapshotSort
}{
{
name: "default limit, default sort",
limit: 0,
sort: nil,
var defaultLimit int32 = 100
defaultSort := []domain.TribeSnapshotSort{
{
By: domain.TribeSnapshotSortByDate,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByID,
Direction: domain.SortDirectionASC,
},
}
tests := []struct {
name string
params domain.ListTribeSnapshotsParams
expectedParams domain.ListTribeSnapshotsParams
expectedErr error
}{
{
name: "OK: default limit/default sort",
params: domain.ListTribeSnapshotsParams{},
expectedParams: domain.ListTribeSnapshotsParams{
Sort: defaultSort,
Pagination: domain.Pagination{
Limit: defaultLimit,
},
},
{
name: "custom limit",
limit: 99,
},
{
name: "OK: custom pagination",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: 99,
Offset: 1,
},
},
{
name: "custom sort",
limit: 0,
sort: []domain.TribeSnapshotSort{
expectedParams: domain.ListTribeSnapshotsParams{
Sort: defaultSort,
Pagination: domain.Pagination{
Limit: 99,
Offset: 1,
},
},
},
{
name: "OK: custom sort",
params: domain.ListTribeSnapshotsParams{
Sort: []domain.TribeSnapshotSort{
{By: domain.TribeSnapshotSortByDate, Direction: domain.SortDirectionDESC},
},
},
}
expectedParams: domain.ListTribeSnapshotsParams{
Sort: []domain.TribeSnapshotSort{
{By: domain.TribeSnapshotSortByDate, Direction: domain.SortDirectionDESC},
},
Pagination: domain.Pagination{
Limit: defaultLimit,
},
},
},
{
name: "ERR: params.Pagination.Limit < 0",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: -1,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MinError{
Min: 1,
},
},
},
{
name: "ERR: params.Pagination.Limit > 100",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: 101,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MaxError{
Max: 100,
},
},
},
{
name: "ERR: params.Pagination.Offset < 0",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Offset: -1,
},
},
expectedErr: domain.ValidationError{
Field: "offset",
Err: domain.MinError{
Min: 0,
},
},
},
{
name: "ERR: len(params.Sort) > 2",
params: domain.ListTribeSnapshotsParams{
Sort: []domain.TribeSnapshotSort{
{
By: domain.TribeSnapshotSortByID,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByDate,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByID,
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.FakeTribeSnapshotRepository{}
repo.ListCountCalls(func(
_ context.Context,
params domain.ListTribeSnapshotsParams,
) ([]domain.TribeSnapshot, int64, error) {
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
return nil, 0, fmt.Errorf("validation failed: %s", diff)
}
repo := &mock.FakeTribeSnapshotRepository{}
repo.ListCountCalls(func(
_ context.Context,
params domain.ListTribeSnapshotsParams,
) ([]domain.TribeSnapshot, int64, error) {
expectedParams := domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: limit,
},
Sort: func(sort []domain.TribeSnapshotSort) []domain.TribeSnapshotSort {
if len(sort) == 0 {
return []domain.TribeSnapshotSort{
{
By: domain.TribeSnapshotSortByDate,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByID,
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.TribeSnapshot, params.Pagination.Limit), int64(params.Pagination.Limit), nil
})
svc := service.NewTribeSnapshot(repo, &mock.FakeTribeLister{})
params := domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: tt.limit,
},
Sort: tt.sort,
}
snapshots, count, err := svc.ListCount(context.Background(), params)
assert.NoError(t, err)
assert.EqualValues(t, limit, count)
assert.Len(t, snapshots, int(limit))
return make([]domain.TribeSnapshot, 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.ListTribeSnapshotsParams
expectedErr error
}{
{
name: "params.Pagination.Limit < 0",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: -1,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MinError{
Min: 1,
},
},
},
{
name: "params.Pagination.Limit > 100",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Limit: 101,
},
},
expectedErr: domain.ValidationError{
Field: "limit",
Err: domain.MaxError{
Max: 100,
},
},
},
{
name: "params.Pagination.Offset < 0",
params: domain.ListTribeSnapshotsParams{
Pagination: domain.Pagination{
Offset: -1,
},
},
expectedErr: domain.ValidationError{
Field: "offset",
Err: domain.MinError{
Min: 0,
},
},
},
{
name: "len(params.Sort) > 2",
params: domain.ListTribeSnapshotsParams{
Sort: []domain.TribeSnapshotSort{
{
By: domain.TribeSnapshotSortByID,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByDate,
Direction: domain.SortDirectionASC,
},
{
By: domain.TribeSnapshotSortByID,
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.FakeTribeSnapshotRepository{}
svc := service.NewTribeSnapshot(repo, &mock.FakeTribeLister{})
snapshots, count, err := svc.ListCount(context.Background(), tt.params)
assert.ErrorIs(t, err, tt.expectedErr)
assert.Zero(t, snapshots)
assert.Zero(t, count)
assert.Equal(t, 0, repo.ListCountCallCount())
})
}
})
snapshots, count, err := service.NewTribeSnapshot(repo, &mock.FakeTribeLister{}).
ListCount(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 TestTribeSnapshot_CleanUp(t *testing.T) {
@ -301,6 +282,7 @@ func TestTribeSnapshot_CleanUp(t *testing.T) {
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
Key: serverKey,
Special: false,
Open: true,
TribeSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
}))
@ -308,6 +290,7 @@ func TestTribeSnapshot_CleanUp(t *testing.T) {
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
Key: serverKey,
Special: false,
Open: false,
TribeSnapshotsCreatedAt: time.Now(),
}))
@ -315,6 +298,7 @@ func TestTribeSnapshot_CleanUp(t *testing.T) {
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
Key: serverKey,
Special: false,
Open: false,
TribeSnapshotsCreatedAt: time.Now().Add(-30 * 24 * time.Hour),
}))