Dawid Wysokiński
f9556beb2c
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: twhelp/core#143
299 lines
6.7 KiB
Go
299 lines
6.7 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/tw"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/core/internal/service/internal/mock"
|
|
)
|
|
|
|
func TestServer_Refresh(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
host := "plemiona.pl"
|
|
versionCode := "pl"
|
|
|
|
openServers := []tw.Server{
|
|
{
|
|
Key: "pl151",
|
|
URL: "https://pl151.plemiona.pl",
|
|
},
|
|
{
|
|
Key: "pl152",
|
|
URL: "https://pl152.plemiona.pl",
|
|
},
|
|
{
|
|
Key: "pls1",
|
|
URL: "https://pls1.plemiona.pl",
|
|
},
|
|
}
|
|
client := &mock.FakeOpenServerConfigInfoGetter{}
|
|
client.GetOpenServersReturns(openServers, nil)
|
|
|
|
specialServers := []domain.Server{
|
|
{
|
|
Key: "pls1",
|
|
URL: "https://pls1.plemiona.pl",
|
|
Open: false,
|
|
Special: true,
|
|
VersionCode: versionCode,
|
|
},
|
|
}
|
|
currentlyStoredOpenServers := []domain.Server{
|
|
{
|
|
Key: "pl150",
|
|
URL: "https://pl150.plemiona.pl",
|
|
Open: true,
|
|
Special: false,
|
|
VersionCode: versionCode,
|
|
},
|
|
{
|
|
Key: "pl151",
|
|
URL: "https://pl151.plemiona.pl",
|
|
Open: true,
|
|
Special: false,
|
|
VersionCode: versionCode,
|
|
},
|
|
}
|
|
repo := &mock.FakeServerRepository{}
|
|
repo.ListReturnsOnCall(0, specialServers, nil)
|
|
repo.ListReturnsOnCall(1, currentlyStoredOpenServers, nil)
|
|
repo.CreateOrUpdateCalls(func(ctx context.Context, params ...domain.CreateServerParams) ([]domain.Server, error) {
|
|
servers := make([]domain.Server, 0, len(params))
|
|
for _, p := range params {
|
|
servers = append(servers, domain.Server{
|
|
Key: p.Key,
|
|
URL: p.URL,
|
|
Open: p.Open,
|
|
VersionCode: p.VersionCode,
|
|
})
|
|
}
|
|
return servers, nil
|
|
})
|
|
|
|
res, err := service.NewServer(repo, client).Refresh(context.Background(), host, versionCode)
|
|
assert.NoError(t, err)
|
|
expectedServers := []struct {
|
|
key string
|
|
open bool
|
|
}{
|
|
{
|
|
key: "pl150",
|
|
open: false,
|
|
},
|
|
{
|
|
key: "pl151",
|
|
open: true,
|
|
},
|
|
{
|
|
key: "pl152",
|
|
open: true,
|
|
},
|
|
}
|
|
assert.Len(t, res, len(expectedServers))
|
|
for _, srv := range res {
|
|
found := false
|
|
for _, expected := range expectedServers {
|
|
if srv.Key == expected.key && srv.Open == expected.open {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
assert.True(t, found, "server (key=%s) not found", srv.Key)
|
|
}
|
|
}
|
|
|
|
func TestServer_List_ListCount(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
limit int32
|
|
}{
|
|
{
|
|
name: "default limit",
|
|
limit: 0,
|
|
},
|
|
{
|
|
name: "custom limit",
|
|
limit: 499,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := tt.limit
|
|
if limit == 0 {
|
|
limit = 500
|
|
}
|
|
|
|
repo := &mock.FakeServerRepository{}
|
|
repo.ListCountCalls(func(_ context.Context, params domain.ListServersParams) ([]domain.Server, int64, error) {
|
|
return make([]domain.Server, params.Pagination.Limit), int64(params.Pagination.Limit), nil
|
|
})
|
|
repo.ListCalls(func(ctx context.Context, params domain.ListServersParams) ([]domain.Server, error) {
|
|
servers, _, err := repo.ListCount(ctx, params)
|
|
return servers, err
|
|
})
|
|
client := &mock.FakeOpenServerConfigInfoGetter{}
|
|
|
|
svc := service.NewServer(repo, client)
|
|
|
|
params := domain.ListServersParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: tt.limit,
|
|
},
|
|
}
|
|
|
|
serversListCount, count, err := svc.ListCount(context.Background(), params)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, limit, count)
|
|
assert.Len(t, serversListCount, int(limit))
|
|
|
|
serversList, err := svc.List(context.Background(), params)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, serversListCount, serversList)
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("ERR: validation failed", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
params domain.ListServersParams
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "params.Pagination.Limit < 0",
|
|
params: domain.ListServersParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MinError{
|
|
Min: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Limit > 500",
|
|
params: domain.ListServersParams{
|
|
Pagination: domain.Pagination{
|
|
Limit: 501,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "limit",
|
|
Err: domain.MaxError{
|
|
Max: 500,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "params.Pagination.Offset < 0",
|
|
params: domain.ListServersParams{
|
|
Pagination: domain.Pagination{
|
|
Offset: -1,
|
|
},
|
|
},
|
|
expectedErr: domain.ValidationError{
|
|
Field: "offset",
|
|
Err: domain.MinError{
|
|
Min: 0,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeServerRepository{}
|
|
client := &mock.FakeOpenServerConfigInfoGetter{}
|
|
|
|
svc := service.NewServer(repo, client)
|
|
|
|
servers, count, err := svc.ListCount(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Zero(t, servers)
|
|
assert.Zero(t, count)
|
|
assert.Equal(t, 0, repo.ListCountCallCount())
|
|
|
|
servers, err = svc.List(context.Background(), tt.params)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
assert.Zero(t, servers)
|
|
assert.Equal(t, 0, repo.ListCallCount())
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestServer_GetNormalByVersionCodeAndKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
server := domain.Server{
|
|
Key: "pl151",
|
|
VersionCode: "pl",
|
|
}
|
|
repo := &mock.FakeServerRepository{}
|
|
repo.ListReturns([]domain.Server{server}, nil)
|
|
client := &mock.FakeOpenServerConfigInfoGetter{}
|
|
|
|
res, err := service.NewServer(repo, client).
|
|
GetNormalByVersionCodeAndKey(context.Background(), server.VersionCode, server.Key)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, server, res)
|
|
require.Equal(t, 1, repo.ListCallCount())
|
|
_, params := repo.ListArgsForCall(0)
|
|
assert.Equal(t, domain.ListServersParams{
|
|
Special: domain.NullBool{
|
|
Bool: false,
|
|
Valid: true,
|
|
},
|
|
Keys: []string{server.Key},
|
|
VersionCodes: []string{server.VersionCode},
|
|
Pagination: domain.Pagination{
|
|
Limit: 1,
|
|
},
|
|
}, params)
|
|
})
|
|
|
|
t.Run("ERR: server not found", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeServerRepository{}
|
|
client := &mock.FakeOpenServerConfigInfoGetter{}
|
|
|
|
key := "pl151"
|
|
res, err := service.NewServer(repo, client).
|
|
GetNormalByVersionCodeAndKey(context.Background(), "en", key)
|
|
assert.ErrorIs(t, err, domain.ServerNotFoundError{Key: key})
|
|
assert.Zero(t, res)
|
|
})
|
|
}
|