dcbot/internal/service/village_test.go
Dawid Wysokiński cfbaba198a
All checks were successful
ci/woodpecker/push/govulncheck Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
refactor: twhelp - migrate to /v2 API endpoints (#155)
Reviewed-on: #155
2024-05-02 05:57:24 +00:00

177 lines
3.9 KiB
Go

package service_test
import (
"context"
"strconv"
"strings"
"testing"
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"gitea.dwysokinski.me/twhelp/dcbot/internal/service"
"gitea.dwysokinski.me/twhelp/dcbot/internal/service/internal/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVillage_TranslateCoords(t *testing.T) {
t.Parallel()
server := domain.TWServer{
Key: "pl181",
URL: "https://pl181.plemiona.pl",
Open: true,
}
versionCode := server.Key[:2]
villages := []domain.Village{
{
X: 123,
Y: 123,
},
{
X: 897,
Y: 123,
},
{
X: 778,
Y: 689,
},
}
notFound := []string{
"789|123",
"678|123",
}
var builderCoordsStr strings.Builder
for _, v := range villages {
builderCoordsStr.WriteString(v.Coords() + " ")
}
for _, coords := range notFound {
builderCoordsStr.WriteString(coords + " ")
}
params, err := domain.NewTranslateVillageCoordsParams(versionCode, server.Key, builderCoordsStr.String(), 2)
require.NoError(t, err)
tests := []struct {
page int
hasNext bool
hasPrev bool
villages []domain.Village
notFound []string
}{
{
page: 1,
hasNext: true,
hasPrev: false,
villages: []domain.Village{
villages[0],
villages[1],
},
},
{
page: 2,
hasNext: true,
hasPrev: true,
villages: []domain.Village{
villages[2],
},
notFound: []string{
notFound[0],
},
},
{
page: 3,
hasNext: false,
hasPrev: true,
notFound: []string{
notFound[1],
},
},
}
for i := range tests {
tt := tests[i]
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
twhelpSvc := &mock.FakeTWHelpService{}
twhelpSvc.ListVillagesByCoordsCalls(func(
ctx context.Context,
versionCode string,
serverKey string,
coords []string,
) ([]domain.Village, error) {
if versionCode != params.VersionCode() || serverKey != params.ServerKey() {
return nil, nil
}
res := make([]domain.Village, 0, len(coords))
for _, c := range coords {
for _, v := range villages {
if v.Coords() == c {
res = append(res, v)
break
}
}
}
return res, nil
})
twhelpSvc.GetServerCalls(func(ctx context.Context, versionCode string, serverKey string) (domain.TWServer, error) {
if versionCode != params.VersionCode() || serverKey != params.ServerKey() {
return domain.TWServer{}, domain.TWServerNotFoundError{
VersionCode: params.VersionCode(),
Key: params.ServerKey(),
}
}
return server, nil
})
res, err := service.NewVillage(&mock.FakeVillageRepository{}, twhelpSvc).
TranslateCoords(context.Background(), params, tt.page)
assert.NoError(t, err)
assert.Len(t, res.Villages, len(tt.villages))
for _, v := range tt.villages {
assert.Contains(t, res.Villages, v)
}
assert.Len(t, res.NotFound, len(tt.notFound))
for _, coords := range tt.notFound {
assert.Contains(t, res.NotFound, coords)
}
assert.Equal(t, tt.hasPrev, res.HasPrev)
assert.Equal(t, tt.hasNext, res.HasNext)
assert.Equal(t, tt.page, res.Page)
assert.Equal(t, params.MaxPage(), res.MaxPage)
assert.Equal(t, params.SHA256(), res.ParamsSHA256)
})
}
}
func TestVillage_TranslateCoordsFromHash(t *testing.T) {
t.Parallel()
params, err := domain.NewTranslateVillageCoordsParams("pl", "pl181", "123|123 897|123 778|689 789|123 678|123", 2)
require.NoError(t, err)
repo := &mock.FakeVillageRepository{}
repo.GetTranslateCoordsParamsCalls(func(ctx context.Context, hash string) (domain.TranslateVillageCoordsParams, error) {
if hash != params.SHA256() {
return domain.TranslateVillageCoordsParams{}, domain.TranslateVillageCoordsParamsNotFoundError{
SHA256: hash,
}
}
return params, nil
})
res, err := service.NewVillage(repo, &mock.FakeTWHelpService{}).
TranslateCoordsFromHash(context.Background(), params.SHA256(), 1)
assert.NoError(t, err)
assert.NotZero(t, res)
}