refactor: tribe 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
89b73d3429
commit
39b7169324
|
@ -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,
|
||||
|
|
|
@ -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,83 +72,43 @@ 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,
|
||||
},
|
||||
{
|
||||
name: "custom limit",
|
||||
limit: 99,
|
||||
},
|
||||
{
|
||||
name: "custom sort",
|
||||
limit: 0,
|
||||
sort: []domain.TribeSnapshotSort{
|
||||
{By: domain.TribeSnapshotSortByDate, Direction: domain.SortDirectionDESC},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
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) {
|
||||
expectedParams := domain.ListTribeSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: limit,
|
||||
},
|
||||
Sort: func(sort []domain.TribeSnapshotSort) []domain.TribeSnapshotSort {
|
||||
if len(sort) == 0 {
|
||||
return []domain.TribeSnapshotSort{
|
||||
var defaultLimit int32 = 100
|
||||
defaultSort := []domain.TribeSnapshotSort{
|
||||
{
|
||||
By: domain.TribeSnapshotSortByDate,
|
||||
Direction: domain.SortDirectionASC,
|
||||
|
@ -155,45 +118,56 @@ func TestTribeSnapshot_ListCount(t *testing.T) {
|
|||
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) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params domain.ListTribeSnapshotsParams
|
||||
expectedParams domain.ListTribeSnapshotsParams
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "params.Pagination.Limit < 0",
|
||||
name: "OK: default limit/default sort",
|
||||
params: domain.ListTribeSnapshotsParams{},
|
||||
expectedParams: domain.ListTribeSnapshotsParams{
|
||||
Sort: defaultSort,
|
||||
Pagination: domain.Pagination{
|
||||
Limit: defaultLimit,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "OK: custom pagination",
|
||||
params: domain.ListTribeSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: 99,
|
||||
Offset: 1,
|
||||
},
|
||||
},
|
||||
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,
|
||||
|
@ -207,7 +181,7 @@ func TestTribeSnapshot_ListCount(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "params.Pagination.Limit > 100",
|
||||
name: "ERR: params.Pagination.Limit > 100",
|
||||
params: domain.ListTribeSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Limit: 101,
|
||||
|
@ -221,7 +195,7 @@ func TestTribeSnapshot_ListCount(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "params.Pagination.Offset < 0",
|
||||
name: "ERR: params.Pagination.Offset < 0",
|
||||
params: domain.ListTribeSnapshotsParams{
|
||||
Pagination: domain.Pagination{
|
||||
Offset: -1,
|
||||
|
@ -235,7 +209,7 @@ func TestTribeSnapshot_ListCount(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "len(params.Sort) > 2",
|
||||
name: "ERR: len(params.Sort) > 2",
|
||||
params: domain.ListTribeSnapshotsParams{
|
||||
Sort: []domain.TribeSnapshotSort{
|
||||
{
|
||||
|
@ -268,17 +242,24 @@ func TestTribeSnapshot_ListCount(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
svc := service.NewTribeSnapshot(repo, &mock.FakeTribeLister{})
|
||||
return make([]domain.TribeSnapshot, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
||||
})
|
||||
|
||||
snapshots, count, err := svc.ListCount(context.Background(), tt.params)
|
||||
snapshots, count, err := service.NewTribeSnapshot(repo, &mock.FakeTribeLister{}).
|
||||
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())
|
||||
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),
|
||||
}))
|
||||
|
|
Reference in New Issue
Block a user