Dawid Wysokiński
3dfc54e427
All checks were successful
continuous-integration/drone/push Build is passing
278 lines
6.6 KiB
Go
278 lines
6.6 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/tw"
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"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()
|
|
|
|
serverKey, serverURL := "pl151", "https://pl151.plemiona.pl"
|
|
|
|
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.GetVillagesCalls(func(ctx context.Context, baseURL string) ([]tw.Village, error) {
|
|
if baseURL != serverURL {
|
|
return nil, errors.New("incorrect baseURL")
|
|
}
|
|
return villages, nil
|
|
})
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.CreateOrUpdateCalls(func(ctx context.Context, params ...domain.CreateVillageParams) error {
|
|
if len(params) != len(villages) {
|
|
return errors.New("validation failed: len(params) != len(villages)")
|
|
}
|
|
|
|
for i, village := range villages {
|
|
if diff := cmp.Diff(domain.CreateVillageParams{
|
|
ID: village.ID,
|
|
Name: village.Name,
|
|
Points: village.Points,
|
|
X: village.X,
|
|
Y: village.Y,
|
|
Continent: village.Continent,
|
|
Bonus: village.Bonus,
|
|
PlayerID: village.PlayerID,
|
|
ProfileURL: village.ProfileURL,
|
|
ServerKey: serverKey,
|
|
}, params[i]); diff != "" {
|
|
return fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
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)
|
|
}
|
|
|
|
func TestVillage_ListCountWithRelations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var defaultLimit int32 = 500
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListVillagesParams
|
|
expectedParams domain.ListVillagesParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "OK: default limit",
|
|
params: domain.ListVillagesParams{},
|
|
expectedParams: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom pagination",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 499,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expectedParams: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 499,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit < 0",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit > 500",
|
|
params: domain.ListVillagesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 501,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 500,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: 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{}
|
|
repo.ListCountWithRelationsCalls(func(
|
|
_ context.Context,
|
|
params domain.ListVillagesParams,
|
|
) ([]domain.VillageWithRelations, int64, error) {
|
|
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.VillageWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
|
|
players, count, err := service.NewVillage(repo, &mock.FakeVillageGetter{}).
|
|
ListCountWithRelations(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.EqualValues(t, tt.expectedParams.Pagination.Limit, count)
|
|
assert.Len(t, players, int(tt.expectedParams.Pagination.Limit))
|
|
})
|
|
}
|
|
}
|
|
|
|
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.ListCountWithRelationsCalls(func(ctx context.Context, params domain.ListVillagesParams) ([]domain.VillageWithRelations, int64, error) {
|
|
if diff := cmp.Diff(domain.ListVillagesParams{
|
|
IDs: []int64{village.ID},
|
|
ServerKeys: []string{village.ServerKey},
|
|
Pagination: domain.Pagination{
|
|
Limit: 1,
|
|
},
|
|
}, params); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return []domain.VillageWithRelations{village}, 1, nil
|
|
})
|
|
|
|
res, err := service.NewVillage(repo, &mock.FakeVillageGetter{}).
|
|
GetByServerKeyAndIDWithRelations(context.Background(), village.ServerKey, village.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, village, res)
|
|
})
|
|
|
|
t.Run("ERR: village not found", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeVillageRepository{}
|
|
repo.ListCountWithRelationsReturns(nil, 0, nil)
|
|
|
|
var id int64 = 123
|
|
res, err := service.NewVillage(repo, &mock.FakeVillageGetter{}).
|
|
GetByServerKeyAndIDWithRelations(context.Background(), "pl151", id)
|
|
assert.ErrorIs(t, err, domain.VillageNotFoundError{ID: id})
|
|
assert.Zero(t, res)
|
|
})
|
|
}
|