Select Git revision
collection_test.go 14.22 KiB
package setup
import (
"context"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/collections"
mockscollections "git.perx.ru/perxis/perxis-go/pkg/collections/mocks"
"git.perx.ru/perxis/perxis-go/pkg/content"
"git.perx.ru/perxis/perxis-go/pkg/environments"
envmocks "git.perx.ru/perxis/perxis-go/pkg/environments/mocks"
"git.perx.ru/perxis/perxis-go/pkg/errors"
"git.perx.ru/perxis/perxis-go/pkg/extension"
"git.perx.ru/perxis/perxis-go/pkg/schema"
"git.perx.ru/perxis/perxis-go/pkg/schema/field"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestSetup_InstallCollections(t *testing.T) {
tests := []struct {
name string
collections []*collections.Collection
collectionsCall func(svc *mockscollections.Collections)
envsCall func(svc *envmocks.Environments)
force bool
wantErr func(t *testing.T, err error)
}{
{
name: "Nil collections",
collections: nil,
collectionsCall: func(svc *mockscollections.Collections) {},
wantErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "Install one collection success",
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", Schema: schema.New("name", field.String())}},
collectionsCall: func(svc *mockscollections.Collections) {
svc.On("Get", mock.Anything, "sp", "env", "1").Return(nil, errors.New("not found")).Once()
svc.On("Create", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", Schema: schema.New("name", field.String())}).Return(&collections.Collection{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", Schema: schema.New("name", field.String())}, nil).Once()
svc.On("SetSchema", mock.Anything, "sp", "env", "1", schema.New("name", field.String())).Return(nil).Once()
},
envsCall: func(svc *envmocks.Environments) {
svc.On("Migrate", mock.Anything, "sp", "env").Return(nil).Once()
},
wantErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "Install one view collection success",
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", View: &collections.View{SpaceID: "sp", EnvID: "env", CollectionID: "3"}}},
collectionsCall: func(svc *mockscollections.Collections) {
svc.On("Get", mock.Anything, "sp", "env", "1").Return(nil, errors.New("not found")).Once()
svc.On("Create", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", View: &collections.View{SpaceID: "sp", EnvID: "env", CollectionID: "3"}}).Return(&collections.Collection{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", View: &collections.View{SpaceID: "sp", EnvID: "env", CollectionID: "3"}}, nil).Once()
},
envsCall: func(svc *envmocks.Environments) {
svc.On("Migrate", mock.Anything, "sp", "env").Return(nil).Once()
},
wantErr: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
{
name: "Install one collection fails",
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", Name: "space", EnvID: "env", Schema: schema.New("name", field.String())}},
collectionsCall: func(svc *mockscollections.Collections) {
svc.On("Get", mock.Anything, "sp", "env", "1").Return(nil, errors.New("not found")).Once()