Skip to content
Snippets Groups Projects
Commit b83b003c authored by ko_oler's avatar ko_oler
Browse files

правки по ПР:

- добавлены функции WithMetaData и SetMetadata
- поправлены тесты
- выделена функция checkSchemaMetadata()
parent 4e33d768
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,21 @@ func (s Schema) WithIncludes(includes ...interface{}) *Schema {
return &s
}
func (s *Schema) WithMetadata(kv ...string) *Schema {
if s.Metadata == nil {
s.Metadata = make(map[string]string, len(s.Metadata))
}
for i := 0; i < len(kv); i += 2 {
s.Metadata[kv[i]] = kv[i+1]
}
return s
}
func (s Schema) SetMetadata(md map[string]string) *Schema {
s.Metadata = md
return &s
}
func (s *Schema) Load(ctx context.Context) error {
if s.Loaded {
return nil
......
......@@ -111,7 +111,7 @@ func TestSchema_JSON(t *testing.T) {
)
sch.Loaded = true
sch.Metadata = map[string]string{
"extension": "perxisweb",
"extension": "test-extension",
}
b, err := json.MarshalIndent(sch, "", " ")
......
......@@ -140,10 +140,9 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
} else {
var upd bool
if _, ok := collection.Schema.Metadata["extension"]; ok {
if _, ok := exist.Schema.Metadata["extension"]; !ok {
return false, errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
}
err = s.checkSchemaMetadata(collection, exist)
if err != nil {
return false, err
}
collection, upd, setSchema = c.UpdateFn(s, exist, c.collection)
......@@ -164,6 +163,15 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS
return setSchema, nil
}
func (s *Setup) checkSchemaMetadata(collection *collections.Collection, exist *collections.Collection) error {
if _, ok := collection.Schema.Metadata["extension"]; ok {
if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() {
return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id")
}
}
return nil
}
func (s *Setup) CheckCollections(ctx context.Context) error {
if len(s.Collections) == 0 {
return nil
......
......@@ -93,10 +93,10 @@ func TestSetup_InstallCollections(t *testing.T) {
},
{
name: "Update extension collection with metadata",
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}},
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}},
collectionsCall: func(svc *mockscollections.Collections) {
svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}, nil).Once()
svc.On("Update", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}).Return(nil).Once()
svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}, nil).Once()
svc.On("Update", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}).Return(nil).Once()
svc.On("SetSchema", mock.Anything, "sp", "env", "1", schema.New("name", field.String())).Return(nil).Once()
},
envsCall: func(svc *envmocks.Environments) {
......@@ -108,9 +108,9 @@ func TestSetup_InstallCollections(t *testing.T) {
},
{
name: "Fail to update collection with the same id",
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("name", field.String()).Field, Metadata: map[string]string{"extension": "tasks"}}}},
collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata("extension", "test-extension")}},
collectionsCall: func(svc *mockscollections.Collections) {
svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: &schema.Schema{Field: schema.New("surname", field.String()).Field}}, nil).Once()
svc.On("Get", mock.Anything, "sp", "env", "1").Return(&collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("surname", field.String())}, nil).Once()
},
wantErr: func(t *testing.T, err error) {
assert.Error(t, err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment