All checks were successful
continuous-integration/drone/push Build is passing
217 lines
4.9 KiB
Go
217 lines
4.9 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service/internal/mock"
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTribeChange_Create(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
lenParams int
|
|
expectedCallCount int
|
|
}{
|
|
{
|
|
name: "len(params)=499",
|
|
lenParams: 499,
|
|
expectedCallCount: 1,
|
|
},
|
|
{
|
|
name: "len(params)=998",
|
|
lenParams: 998,
|
|
expectedCallCount: 2,
|
|
},
|
|
{
|
|
name: "len(params)=1500",
|
|
lenParams: 1500,
|
|
expectedCallCount: 3,
|
|
},
|
|
{
|
|
name: "len(params)=10000",
|
|
lenParams: 10000,
|
|
expectedCallCount: 20,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeTribeChangeRepository{}
|
|
svc := service.NewTribeChange(repo)
|
|
|
|
assert.NoError(t, svc.Create(context.Background(), make([]domain.CreateTribeChangeParams, tt.lenParams)...))
|
|
assert.Equal(t, tt.expectedCallCount, repo.CreateCallCount())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTribeChange_ListCountWithRelations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var defaultLimit int32 = 100
|
|
defaultSort := []domain.TribeChangeSort{
|
|
{
|
|
By: domain.TribeChangeSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeChangeSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListTribeChangesParams
|
|
expectedParams domain.ListTribeChangesParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "OK: default limit/sort",
|
|
params: domain.ListTribeChangesParams{},
|
|
expectedParams: domain.ListTribeChangesParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom pagination",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 99,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expectedParams: domain.ListTribeChangesParams{
|
|
Sort: defaultSort,
|
|
Pagination: domain.Pagination{
|
|
Limit: 99,
|
|
Offset: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "OK: custom sort",
|
|
params: domain.ListTribeChangesParams{
|
|
Sort: []domain.TribeChangeSort{
|
|
{By: domain.TribeChangeSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
|
},
|
|
},
|
|
expectedParams: domain.ListTribeChangesParams{
|
|
Sort: []domain.TribeChangeSort{
|
|
{By: domain.TribeChangeSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
|
},
|
|
Pagination: domain.Pagination{
|
|
Limit: defaultLimit,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit < 0",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Limit > 100",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 101,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 100,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: params.Pagination.Offset < 0",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "ERR: len(params.Sort) > 2",
|
|
params: domain.ListTribeChangesParams{
|
|
Sort: []domain.TribeChangeSort{
|
|
{
|
|
By: domain.TribeChangeSortByID,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeChangeSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeChangeSortByID,
|
|
Direction: domain.SortDirectionDESC,
|
|
},
|
|
},
|
|
},
|
|
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.FakeTribeChangeRepository{}
|
|
repo.ListCountWithRelationsCalls(func(
|
|
_ context.Context,
|
|
params domain.ListTribeChangesParams,
|
|
) ([]domain.TribeChangeWithRelations, int64, error) {
|
|
if diff := cmp.Diff(params, tt.expectedParams); diff != "" {
|
|
return nil, 0, fmt.Errorf("validation failed: %s", diff)
|
|
}
|
|
|
|
return make([]domain.TribeChangeWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
|
|
tcs, count, err := service.NewTribeChange(repo).
|
|
ListCountWithRelations(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.EqualValues(t, tt.expectedParams.Pagination.Limit, count)
|
|
assert.Len(t, tcs, int(tt.expectedParams.Pagination.Limit))
|
|
})
|
|
}
|
|
}
|