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 ( import (
"context" "context"
"errors"
"fmt" "fmt"
"testing" "testing"
"time" "time"
@ -17,6 +18,8 @@ import (
func TestPlayerSnapshot_Create(t *testing.T) { func TestPlayerSnapshot_Create(t *testing.T) {
t.Parallel() t.Parallel()
serverKey := "pl151"
playerSvc := &mock.FakePlayerLister{} playerSvc := &mock.FakePlayerLister{}
players := []domain.Player{ players := []domain.Player{
{ {
@ -62,17 +65,22 @@ func TestPlayerSnapshot_Create(t *testing.T) {
CreatedAt: time.Now(), 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 := &mock.FakePlayerSnapshotRepository{}
repo.CreateReturns(nil) repo.CreateReturns(nil)
date := time.Now() 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()) require.Equal(t, 1, repo.CreateCallCount())
_, argParams := repo.CreateArgsForCall(0) _, argParams := repo.CreateArgsForCall(0)
assert.Len(t, argParams, len(players)) require.Len(t, argParams, len(players))
for i, p := range players { for i, p := range players {
assert.Equal(t, domain.CreatePlayerSnapshotParams{ assert.Equal(t, domain.CreatePlayerSnapshotParams{
OpponentsDefeated: p.OpponentsDefeated, OpponentsDefeated: p.OpponentsDefeated,

View File

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