Dawid Wysokiński
ad9673cef5
All checks were successful
continuous-integration/drone/push Build is passing
334 lines
8.5 KiB
Go
334 lines
8.5 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.Ennoblement
|
|
ennoblementsClient []tw.Ennoblement
|
|
}{
|
|
{
|
|
name: "OK: since=now-20h",
|
|
key: "pl151",
|
|
url: "https://pl151.plemiona.pl",
|
|
ennoblementsRepo: []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, 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.CreateCallCount())
|
|
_, params := repo.CreateArgsForCall(0)
|
|
assert.Len(t, params, len(tt.ennoblementsClient))
|
|
for i, ennoblement := range tt.ennoblementsClient {
|
|
assert.Equal(t, domain.CreateEnnoblementParams{
|
|
VillageID: ennoblement.VillageID,
|
|
NewOwnerID: ennoblement.NewOwnerID,
|
|
NewTribeID: ennoblement.NewTribeID,
|
|
OldOwnerID: ennoblement.OldOwnerID,
|
|
OldTribeID: ennoblement.OldTribeID,
|
|
Points: ennoblement.Points,
|
|
CreatedAt: ennoblement.CreatedAt,
|
|
ServerKey: tt.key,
|
|
}, params[i])
|
|
}
|
|
|
|
require.Equal(t, 1, client.GetEnnoblementsCallCount())
|
|
_, argURL, argSince := client.GetEnnoblementsArgsForCall(0)
|
|
assert.Equal(t, tt.url, argURL)
|
|
if len(tt.ennoblementsRepo) > 0 {
|
|
assert.Equal(t, tt.ennoblementsRepo[0].CreatedAt, argSince)
|
|
} else {
|
|
assert.Zero(t, argSince)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnnoblement_ListCountWithRelations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var defaultLimit int32 = 200
|
|
defaultSort := []domain.EnnoblementSort{
|
|
{
|
|
By: domain.EnnoblementSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListEnnoblementsParams
|
|
expectedParams domain.ListEnnoblementsParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "OK: default limit/sort",
|
|
params: domain.ListEnnoblementsParams{},
|
|
expectedParams: domain.ListEnnoblementsParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom pagination",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 199,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expectedParams: domain.ListEnnoblementsParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: 199,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom sort",
|
|
params: domain.ListEnnoblementsParams{
|
|
Sort: []domain.EnnoblementSort{
|
|
{By: domain.EnnoblementSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
|
},
|
|
},
|
|
expectedParams: domain.ListEnnoblementsParams{
|
|
Sort: []domain.EnnoblementSort{
|
|
{By: domain.EnnoblementSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
|
},
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit < 0",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit > 200",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 201,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 200,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Offset < 0",
|
|
params: domain.ListEnnoblementsParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: len(params.Sort) > 1",
|
|
params: domain.ListEnnoblementsParams{
|
|
Sort: []domain.EnnoblementSort{
|
|
{
|
|
By: domain.EnnoblementSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.EnnoblementSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "sort",
|
|
Err: domain.MaxLengthError{
|
|
Max: 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeEnnoblementRepository{}
|
|
repo.ListCountWithRelationsCalls(func(
|
|
_ context.Context,
|
|
params domain.ListEnnoblementsParams,
|
|
) ([]domain.EnnoblementWithRelations, int64, error) {
|
|
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.EnnoblementWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
|
|
ennoblements, count, err := service.NewEnnoblement(repo, &mock.FakeEnnoblementGetter{}).
|
|
ListCountWithRelations(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.EqualValues(t, tt.expectedParams.Pagination.Limit, count)
|
|
assert.Len(t, ennoblements, int(tt.expectedParams.Pagination.Limit))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnnoblement_CleanUp(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
serverKey := "pl151"
|
|
repo := &mock.FakeEnnoblementRepository{}
|
|
client := &mock.FakeEnnoblementGetter{}
|
|
repo.DeleteReturns(nil)
|
|
|
|
svc := service.NewEnnoblement(repo, client)
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: true,
|
|
Open: false,
|
|
EnnoblementDataUpdatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with Special = false
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: true,
|
|
EnnoblementDataUpdatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with Open = false
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: false,
|
|
EnnoblementDataUpdatedAt: time.Now(),
|
|
}))
|
|
assert.Equal(t, 0, repo.DeleteCallCount()) // only servers with EnnoblementDataUpdatedAt < now - 30 days
|
|
|
|
assert.NoError(t, svc.CleanUp(context.Background(), domain.Server{
|
|
Key: serverKey,
|
|
Special: false,
|
|
Open: false,
|
|
EnnoblementDataUpdatedAt: time.Now().Add(-30 * 24 * time.Hour),
|
|
}))
|
|
require.Equal(t, 1, repo.DeleteCallCount())
|
|
_, argServerKey, argCreatedAtLTE := repo.DeleteArgsForCall(0)
|
|
assert.Equal(t, serverKey, argServerKey)
|
|
assert.WithinDuration(t, time.Now().Add(-365*24*time.Hour), argCreatedAtLTE, time.Second)
|
|
}
|