Dawid Wysokiński
6cde8d082f
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#68
292 lines
6.4 KiB
Go
292 lines
6.4 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service/internal/mock"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
)
|
|
|
|
func TestVillage_Refresh(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
villages := []domain.BaseVillage{
|
|
{
|
|
ID: 1,
|
|
Name: "111",
|
|
Points: 111,
|
|
X: 111,
|
|
Y: 111,
|
|
Continent: "K11",
|
|
Bonus: 1,
|
|
PlayerID: 123,
|
|
},
|
|
{
|
|
ID: 2,
|
|
Name: "222",
|
|
Points: 222,
|
|
X: 222,
|
|
Y: 222,
|
|
Continent: "K22",
|
|
Bonus: 0,
|
|
PlayerID: 123,
|
|
},
|
|
{
|
|
ID: 3,
|
|
Name: "333",
|
|
Points: 333,
|
|
X: 333,
|
|
Y: 333,
|
|
Continent: "K33",
|
|
Bonus: 3,
|
|
PlayerID: 0,
|
|
},
|
|
{
|
|
ID: 4,
|
|
Name: "444",
|
|
Points: 444,
|
|
X: 444,
|
|
Y: 444,
|
|
Continent: "K44",
|
|
Bonus: 0,
|
|
PlayerID: 0,
|
|
},
|
|
}
|
|
client := &mock.FakeVillagesGetter{}
|
|
client.GetVillagesReturns(villages, nil)
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.CreateOrUpdateReturns(nil)
|
|
|
|
serverKey, serverURL := "pl151", "https://pl151.plemiona.pl"
|
|
|
|
res, err := service.NewVillage(repo, client).Refresh(context.Background(), serverKey, serverURL)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, len(villages), res.NumVillages)
|
|
assert.EqualValues(t, 2, res.NumBarbarianVillages)
|
|
assert.EqualValues(t, 2, res.NumBonusVillages)
|
|
assert.EqualValues(t, 2, res.NumPlayerVillages)
|
|
require.Equal(t, 1, client.GetVillagesCallCount())
|
|
require.Equal(t, 1, repo.CreateOrUpdateCallCount())
|
|
_, params := repo.CreateOrUpdateArgsForCall(0)
|
|
assert.Len(t, params, len(villages))
|
|
for _, village := range villages {
|
|
var villageToCreate domain.CreateVillageParams
|
|
for _, p := range params {
|
|
if p.ID == village.ID {
|
|
villageToCreate = p
|
|
break
|
|
}
|
|
}
|
|
assert.Equal(t, village, villageToCreate.BaseVillage)
|
|
assert.Equal(t, serverKey, villageToCreate.ServerKey)
|
|
}
|
|
}
|
|
|
|
func TestVillage_List(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
limit int32
|
|
}{
|
|
{
|
|
name: "default limit",
|
|
limit: 0,
|
|
},
|
|
{
|
|
name: "custom limit",
|
|
limit: 1999,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := tt.limit
|
|
if limit == 0 {
|
|
limit = 2000
|
|
}
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.ListCalls(func(_ context.Context, params domain.ListVillagesParams) ([]domain.Village, int64, error) {
|
|
expectedParams := domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: limit,
|
|
},
|
|
}
|
|
|
|
if !cmp.Equal(params, expectedParams) {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", cmp.Diff(params, expectedParams))
|
|
}
|
|
|
|
return make([]domain.Village, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
client := &mock.FakeVillagesGetter{}
|
|
|
|
players, count, err := service.NewVillage(repo, client).
|
|
List(context.Background(), domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: tt.limit,
|
|
},
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, limit, count)
|
|
assert.Len(t, players, int(limit))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("ERR: validation failed", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListVillagesParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "params.Pagination.Limit < 0",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Limit > 2000",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 2001,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 2000,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Offset < 0",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
client := &mock.FakeVillagesGetter{}
|
|
|
|
players, count, err := service.NewVillage(repo, client).
|
|
List(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Zero(t, players)
|
|
assert.Zero(t, count)
|
|
assert.Equal(t, 0, repo.ListCallCount())
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestVillage_GetByServerKeyAndID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
player := domain.Village{
|
|
BaseVillage: domain.BaseVillage{
|
|
ID: 123,
|
|
PlayerID: 123,
|
|
},
|
|
Player: domain.NullPlayer{
|
|
Valid: true,
|
|
Player: domain.Player{
|
|
BasePlayer: domain.BasePlayer{
|
|
ID: 123,
|
|
TribeID: 1234,
|
|
},
|
|
Tribe: domain.NullTribe{
|
|
Valid: true,
|
|
Tribe: domain.Tribe{
|
|
BaseTribe: domain.BaseTribe{
|
|
ID: 1234,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
ServerKey: "pl151",
|
|
}
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.ListReturns([]domain.Village{player}, 0, nil)
|
|
client := &mock.FakeVillagesGetter{}
|
|
|
|
res, err := service.NewVillage(repo, client).
|
|
GetByServerKeyAndID(context.Background(), player.ServerKey, player.ID, true, true)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, player, res)
|
|
require.Equal(t, 1, repo.ListCallCount())
|
|
_, params := repo.ListArgsForCall(0)
|
|
assert.Equal(t, domain.ListVillagesParams{
|
|
IDs: []int64{player.ID},
|
|
ServerKeys: []string{player.ServerKey},
|
|
IncludePlayer: true,
|
|
IncludePlayerTribe: true,
|
|
Pagination: domain.Pagination{
|
|
Limit: 1,
|
|
},
|
|
}, params)
|
|
|
|
})
|
|
|
|
t.Run("ERR: player not found", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
client := &mock.FakeVillagesGetter{}
|
|
|
|
var id int64 = 123
|
|
res, err := service.NewVillage(repo, client).
|
|
GetByServerKeyAndID(context.Background(), "pl151", id, false, false)
|
|
assert.ErrorIs(t, err, domain.VillageNotFoundError{ID: id})
|
|
assert.Zero(t, res)
|
|
})
|
|
}
|