Dawid Wysokiński
767eb5d5f2
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#107
298 lines
6.8 KiB
Go
298 lines
6.8 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/tw"
|
|
"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 := []tw.Village{
|
|
{
|
|
ID: 1,
|
|
Name: "111",
|
|
Points: 111,
|
|
X: 111,
|
|
Y: 111,
|
|
Continent: "K11",
|
|
Bonus: 1,
|
|
PlayerID: 123,
|
|
ProfileURL: "profile-1",
|
|
},
|
|
{
|
|
ID: 2,
|
|
Name: "222",
|
|
Points: 222,
|
|
X: 222,
|
|
Y: 222,
|
|
Continent: "K22",
|
|
Bonus: 0,
|
|
PlayerID: 123,
|
|
ProfileURL: "profile-2",
|
|
},
|
|
{
|
|
ID: 3,
|
|
Name: "333",
|
|
Points: 333,
|
|
X: 333,
|
|
Y: 333,
|
|
Continent: "K33",
|
|
Bonus: 3,
|
|
PlayerID: 0,
|
|
ProfileURL: "profile-3",
|
|
},
|
|
{
|
|
ID: 4,
|
|
Name: "444",
|
|
Points: 444,
|
|
X: 444,
|
|
Y: 444,
|
|
Continent: "K44",
|
|
Bonus: 0,
|
|
PlayerID: 0,
|
|
ProfileURL: "profile-4",
|
|
},
|
|
}
|
|
client := &mock.FakeVillageGetter{}
|
|
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 i, village := range villages {
|
|
assert.Equal(t, village.ID, params[i].ID)
|
|
assert.Equal(t, village.Name, params[i].Name)
|
|
assert.Equal(t, village.ProfileURL, params[i].ProfileURL)
|
|
assert.Equal(t, village.X, params[i].X)
|
|
assert.Equal(t, village.Y, params[i].Y)
|
|
assert.Equal(t, village.Points, params[i].Points)
|
|
assert.Equal(t, village.Continent, params[i].Continent)
|
|
assert.Equal(t, village.Bonus, params[i].Bonus)
|
|
assert.Equal(t, village.PlayerID, params[i].PlayerID)
|
|
assert.Equal(t, serverKey, params[i].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: 499,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := tt.limit
|
|
if limit == 0 {
|
|
limit = 500
|
|
}
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.ListCalls(func(
|
|
_ context.Context,
|
|
params domain.ListVillagesParams,
|
|
) ([]domain.VillageWithRelations, int64, error) {
|
|
expectedParams := domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: limit,
|
|
},
|
|
}
|
|
|
|
if diff := cmp.Diff(params, expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.VillageWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
client := &mock.FakeVillageGetter{}
|
|
|
|
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 > 500",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 501,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 500,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
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.FakeVillageGetter{}
|
|
|
|
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()
|
|
|
|
village := domain.VillageWithRelations{
|
|
Village: domain.Village{
|
|
ID: 123,
|
|
PlayerID: 123,
|
|
ServerKey: "pl151",
|
|
},
|
|
Player: domain.NullPlayerMetaWithRelations{
|
|
Valid: true,
|
|
Player: domain.PlayerMetaWithRelations{
|
|
PlayerMeta: domain.PlayerMeta{
|
|
ID: 123,
|
|
},
|
|
Tribe: domain.NullTribeMeta{
|
|
Valid: true,
|
|
Tribe: domain.TribeMeta{
|
|
ID: 1234,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.ListReturns([]domain.VillageWithRelations{village}, 0, nil)
|
|
client := &mock.FakeVillageGetter{}
|
|
|
|
res, err := service.NewVillage(repo, client).
|
|
GetByServerKeyAndID(context.Background(), village.ServerKey, village.ID, true, true)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, village, res)
|
|
require.Equal(t, 1, repo.ListCallCount())
|
|
_, params := repo.ListArgsForCall(0)
|
|
assert.Equal(t, domain.ListVillagesParams{
|
|
IDs: []int64{village.ID},
|
|
ServerKeys: []string{village.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.FakeVillageGetter{}
|
|
|
|
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)
|
|
})
|
|
}
|