Dawid Wysokiński
d3a6964eb7
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#154
246 lines
5.3 KiB
Go
246 lines
5.3 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()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
limit int32
|
|
sort []domain.TribeChangeSort
|
|
}{
|
|
{
|
|
name: "default limit, default sort",
|
|
limit: 0,
|
|
sort: nil,
|
|
},
|
|
{
|
|
name: "custom limit",
|
|
limit: 99,
|
|
},
|
|
{
|
|
name: "custom sort",
|
|
limit: 0,
|
|
sort: []domain.TribeChangeSort{
|
|
{By: domain.TribeChangeSortByCreatedAt, 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 = 100
|
|
}
|
|
|
|
repo := &mock.FakeTribeChangeRepository{}
|
|
repo.ListCountWithRelationsCalls(func(
|
|
_ context.Context,
|
|
params domain.ListTribeChangesParams,
|
|
) ([]domain.TribeChangeWithRelations, int64, error) {
|
|
expectedParams := domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: limit,
|
|
},
|
|
Sort: func(sort []domain.TribeChangeSort) []domain.TribeChangeSort {
|
|
if len(sort) == 0 {
|
|
return []domain.TribeChangeSort{
|
|
{
|
|
By: domain.TribeChangeSortByCreatedAt,
|
|
Direction: domain.SortDirectionASC,
|
|
},
|
|
{
|
|
By: domain.TribeChangeSortByID,
|
|
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.TribeChangeWithRelations, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
|
|
svc := service.NewTribeChange(repo)
|
|
|
|
params := domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: tt.limit,
|
|
},
|
|
Sort: tt.sort,
|
|
}
|
|
|
|
tcs, count, err := svc.ListCountWithRelations(context.Background(), params)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, limit, count)
|
|
assert.Len(t, tcs, int(limit))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("ERR: validation failed", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListTribeChangesParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "params.Pagination.Limit < 0",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Limit > 100",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 101,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 100,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Offset < 0",
|
|
params: domain.ListTribeChangesParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "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{}
|
|
|
|
svc := service.NewTribeChange(repo)
|
|
|
|
tcs, count, err := svc.ListCountWithRelations(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Zero(t, tcs)
|
|
assert.Zero(t, count)
|
|
assert.Equal(t, 0, repo.ListCountWithRelationsCallCount())
|
|
})
|
|
}
|
|
})
|
|
}
|