fix: linter
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
Dawid Wysokiński 2023-07-13 06:57:17 +02:00
parent 8eb7032689
commit 92dcdc2145
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
4 changed files with 147 additions and 154 deletions

View File

@ -202,6 +202,18 @@ func (t TranslateVillageCoordsParams) SHA256() string {
return t.sha256
}
func (t TranslateVillageCoordsParams) IsPageInRange(page int32) error {
if page < 1 {
return GreaterEqualThanError{Field: "page", Threshold: 1}
}
if page > t.maxPage {
return LessEqualThanError{Field: "page", Threshold: int(t.maxPage)}
}
return nil
}
type TranslateVillageCoordsResult struct {
Villages []Village
NotFound []string

View File

@ -127,6 +127,19 @@ func TestNewTranslateVillageCoordsParams(t *testing.T) {
}
}
func TestTranslateVillageCoordsParams_IsPageInRange(t *testing.T) {
t.Parallel()
params, err := domain.NewTranslateVillageCoordsParams("pl", "pl181", "123|123 321|321 898|898 613|613 517|517", 2)
require.NoError(t, err)
assert.ErrorIs(t, params.IsPageInRange(0), domain.GreaterEqualThanError{Field: "page", Threshold: 1})
assert.NoError(t, params.IsPageInRange(1))
assert.NoError(t, params.IsPageInRange(2))
assert.NoError(t, params.IsPageInRange(3))
assert.ErrorIs(t, params.IsPageInRange(4), domain.LessEqualThanError{Field: "page", Threshold: 3})
}
func TestUnmarshalTranslateVillageCoordsParamsFromDatabase(t *testing.T) {
t.Parallel()

View File

@ -26,12 +26,8 @@ func (v *Village) TranslateCoords(
params domain.TranslateVillageCoordsParams,
page int32,
) (domain.TranslateVillageCoordsResult, error) {
if page < 1 {
return domain.TranslateVillageCoordsResult{}, domain.GreaterEqualThanError{Field: "page", Threshold: 1}
}
if page > params.MaxPage() {
return domain.TranslateVillageCoordsResult{}, domain.LessEqualThanError{Field: "page", Threshold: int(params.MaxPage())}
if err := params.IsPageInRange(page); err != nil {
return domain.TranslateVillageCoordsResult{}, err
}
if _, err := v.twhelpSvc.GetServer(ctx, params.VersionCode(), params.ServerKey()); err != nil {

View File

@ -16,171 +16,143 @@ import (
func TestVillage_TranslateCoords(t *testing.T) {
t.Parallel()
t.Run("OK", func(t *testing.T) {
t.Parallel()
server := domain.TWServer{
Key: "pl181",
URL: "https://pl181.plemiona.pl",
Open: true,
}
versionCode := server.Key[:2]
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,
},
}
villages := []domain.Village{
{
X: 123,
Y: 123,
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 int32
hasNext bool
hasPrev bool
villages []domain.Village
notFound []string
}{
{
page: 1,
hasNext: true,
hasPrev: false,
villages: []domain.Village{
villages[0],
villages[1],
},
{
X: 897,
Y: 123,
},
{
page: 2,
hasNext: true,
hasPrev: true,
villages: []domain.Village{
villages[2],
},
{
X: 778,
Y: 689,
notFound: []string{
notFound[0],
},
}
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 int32
hasNext bool
hasPrev bool
villages []domain.Village
notFound []string
}{
{
page: 1,
hasNext: true,
hasPrev: false,
villages: []domain.Village{
villages[0],
villages[1],
},
},
{
page: 3,
hasNext: false,
hasPrev: true,
notFound: []string{
notFound[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]
for i := range tests {
tt := tests[i]
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
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,
offset int32,
perPage int32,
) ([]domain.Village, error) {
if versionCode != params.VersionCode() || serverKey != params.ServerKey() {
return nil, nil
}
twhelpSvc := &mock.FakeTWHelpService{}
twhelpSvc.ListVillagesByCoordsCalls(func(
ctx context.Context,
versionCode string,
serverKey string,
coords []string,
offset int32,
perPage int32,
) ([]domain.Village, error) {
if versionCode != params.VersionCode() || serverKey != params.ServerKey() {
return nil, nil
}
res := make([]domain.Village, 0, len(coords))
res := make([]domain.Village, 0, len(coords))
for _, c := range coords {
for _, v := range villages {
if v.Coords() == c {
res = append(res, v)
break
}
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)
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
})
}
})
t.Run("ERR: page < 1", func(t *testing.T) {
t.Parallel()
res, err := service.NewVillage(nil, nil).
TranslateCoords(context.Background(), domain.TranslateVillageCoordsParams{}, 0)
assert.ErrorIs(t, err, domain.GreaterEqualThanError{
Field: "page",
Threshold: 1,
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)
})
assert.Zero(t, res)
})
t.Run("ERR: page > max page", func(t *testing.T) {
t.Parallel()
res, err := service.NewVillage(nil, nil).
TranslateCoords(context.Background(), domain.TranslateVillageCoordsParams{}, 1)
assert.ErrorIs(t, err, domain.LessEqualThanError{
Field: "page",
Threshold: 0,
})
assert.Zero(t, res)
})
}
}
func TestVillage_TranslateCoordsFromHash(t *testing.T) {