Dawid Wysokiński
767eb5d5f2
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#107
317 lines
7.4 KiB
Go
317 lines
7.4 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"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"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service/internal/mock"
|
|
)
|
|
|
|
func TestEnnoblement_Refresh(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
url string
|
|
ennoblementsRepo []domain.EnnoblementWithRelations
|
|
ennoblementsClient []tw.Ennoblement
|
|
}{
|
|
{
|
|
name: "OK: since=now-20h",
|
|
key: "pl151",
|
|
url: "https://pl151.plemiona.pl",
|
|
ennoblementsRepo: []domain.EnnoblementWithRelations{
|
|
{
|
|
Ennoblement: domain.Ennoblement{
|
|
ID: 111,
|
|
VillageID: 112,
|
|
NewOwnerID: 113,
|
|
NewTribeID: 114,
|
|
OldOwnerID: 115,
|
|
OldTribeID: 116,
|
|
Points: 117,
|
|
CreatedAt: time.Now().Add(-20 * time.Hour),
|
|
ServerKey: "pl151",
|
|
},
|
|
},
|
|
},
|
|
ennoblementsClient: []tw.Ennoblement{
|
|
{
|
|
VillageID: 211,
|
|
NewOwnerID: 212,
|
|
NewTribeID: 213,
|
|
OldOwnerID: 214,
|
|
OldTribeID: 215,
|
|
Points: 216,
|
|
CreatedAt: time.Now().Add(-15 * time.Hour),
|
|
},
|
|
{
|
|
VillageID: 221,
|
|
NewOwnerID: 222,
|
|
NewTribeID: 223,
|
|
OldOwnerID: 224,
|
|
OldTribeID: 225,
|
|
Points: 226,
|
|
CreatedAt: time.Now().Add(-5 * time.Hour),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: since=1970",
|
|
key: "pl151",
|
|
url: "https://pl151.plemiona.pl",
|
|
ennoblementsClient: []tw.Ennoblement{
|
|
{
|
|
VillageID: 211,
|
|
NewOwnerID: 212,
|
|
NewTribeID: 213,
|
|
OldOwnerID: 214,
|
|
OldTribeID: 215,
|
|
Points: 216,
|
|
CreatedAt: time.Now().Add(-15 * time.Hour),
|
|
},
|
|
{
|
|
VillageID: 221,
|
|
NewOwnerID: 222,
|
|
NewTribeID: 223,
|
|
OldOwnerID: 224,
|
|
OldTribeID: 225,
|
|
Points: 226,
|
|
CreatedAt: time.Now().Add(-5 * time.Hour),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeEnnoblementRepository{}
|
|
repo.ListReturns(tt.ennoblementsRepo, 0, nil)
|
|
repo.CreateReturns(nil)
|
|
client := &mock.FakeEnnoblementGetter{}
|
|
client.GetEnnoblementsReturns(tt.ennoblementsClient, nil)
|
|
|
|
assert.NoError(t, service.NewEnnoblement(repo, client).Refresh(context.Background(), tt.key, tt.url))
|
|
require.Equal(t, 1, repo.ListCallCount())
|
|
require.Equal(t, 1, repo.CreateCallCount())
|
|
_, params := repo.CreateArgsForCall(0)
|
|
assert.Len(t, params, len(tt.ennoblementsClient))
|
|
for i, ennoblement := range tt.ennoblementsClient {
|
|
assert.Equal(t, ennoblement.Points, params[i].Points)
|
|
assert.Equal(t, ennoblement.VillageID, params[i].VillageID)
|
|
assert.Equal(t, ennoblement.NewOwnerID, params[i].NewOwnerID)
|
|
assert.Equal(t, ennoblement.NewTribeID, params[i].NewTribeID)
|
|
assert.Equal(t, ennoblement.OldOwnerID, params[i].OldOwnerID)
|
|
assert.Equal(t, ennoblement.OldTribeID, params[i].OldTribeID)
|
|
assert.Equal(t, ennoblement.CreatedAt, params[i].CreatedAt)
|
|
assert.Equal(t, tt.key, params[i].ServerKey)
|
|
}
|
|
require.Equal(t, 1, client.GetEnnoblementsCallCount())
|
|
_, url, since := client.GetEnnoblementsArgsForCall(0)
|
|
assert.Equal(t, tt.url, url)
|
|
if len(tt.ennoblementsRepo) > 0 {
|
|
assert.Equal(t, tt.ennoblementsRepo[0].CreatedAt, since)
|
|
} else {
|
|
assert.Zero(t, since)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnnoblement_List(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
limit int32
|
|
sort []domain.EnnoblementSort
|
|
}{
|
|
{
|
|
name: "default limit, default sort",
|
|
limit: 0,
|
|
sort: nil,
|
|
},
|
|
{
|
|
name: "custom limit",
|
|
limit: 199,
|
|
},
|
|
{
|
|
name: "custom sort",
|
|
limit: 0,
|
|
sort: []domain.EnnoblementSort{
|
|
{By: domain.EnnoblementSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := tt.limit
|
|
if limit == 0 {
|
|
limit = 200
|
|
}
|
|
|
|
repo := &mock.FakeEnnoblementRepository{}
|
|
repo.ListCalls(func(
|
|
_ context.Context,
|
|
params domain.ListEnnoblementsParams,
|
|
) ([]domain.EnnoblementWithRelations, int64, error) {
|
|
expectedParams := domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: limit,
|
|
},
|
|
Sort: func(sort []domain.EnnoblementSort) []domain.EnnoblementSort {
|
|
if len(sort) == 0 {
|
|
return []domain.EnnoblementSort{
|
|
{
|
|
By: domain.EnnoblementSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
}
|
|
}
|
|
return sort
|
|
}(tt.sort),
|
|
}
|
|
|
|
if diff := cmp.Diff(params, expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.EnnoblementWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
client := &mock.FakeEnnoblementGetter{}
|
|
|
|
players, count, err := service.NewEnnoblement(repo, client).
|
|
List(context.Background(), domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: tt.limit,
|
|
},
|
|
Sort: tt.sort,
|
|
})
|
|
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.ListEnnoblementsParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "params.Pagination.Limit < 0",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Limit > 200",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 201,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 200,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Offset < 0",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "len(params.Sort) > 2",
|
|
params: domain.ListEnnoblementsParams{
|
|
Sort: []domain.EnnoblementSort{
|
|
{
|
|
By: domain.EnnoblementSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.EnnoblementSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.EnnoblementSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "sort",
|
|
Err: domain.MaxLengthError{
|
|
Max: 2,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeEnnoblementRepository{}
|
|
client := &mock.FakeEnnoblementGetter{}
|
|
|
|
players, count, err := service.NewEnnoblement(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())
|
|
})
|
|
}
|
|
})
|
|
}
|