From 35cc5b220212d5f42ce4caf0450c8d30c0c2cd1b Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Tue, 11 Jul 2023 12:44:14 +0300
Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection_test.go | 39 +++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index 3a033d29..4a1bd6e1 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -23,6 +23,8 @@ func TestSetup_InstallCollections(t *testing.T) {
 		collections     []*collections.Collection
 		collectionsCall func(svc *mockscollections.Collections)
 		envsCall        func(svc *envmocks.Environments)
+		co              CollectionsOption
+		force           bool
 		wantErr         func(t *testing.T, err error)
 	}{
 		{
@@ -106,6 +108,36 @@ func TestSetup_InstallCollections(t *testing.T) {
 			wantErr: func(t *testing.T, err error) {
 				assert.NoError(t, err)
 			},
+			co: UpdateCollectionSchemaMetadata(),
+		},
+		{
+			name:        "Fail to update user collection with same id as in extensions collection",
+			collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata(extension.ExtensionMetadataKey, "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.New("name", field.String())}, nil).Once()
+			},
+			wantErr: func(t *testing.T, err error) {
+				assert.Error(t, err)
+				assert.ErrorIs(t, err, collections.ErrAlreadyExists)
+			},
+			co: UpdateCollectionSchemaMetadata(),
+		},
+		{
+			name:        "Update user collection with same id as in extensions collection with force",
+			collections: []*collections.Collection{{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata(extension.ExtensionMetadataKey, "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.New("name", field.String())}, nil).Once()
+				svc.On("Update", mock.Anything, &collections.Collection{ID: "1", SpaceID: "sp", EnvID: "env", Schema: schema.New("name", field.String()).WithMetadata(extension.ExtensionMetadataKey, "test-extension")}).Return(nil).Once()
+				svc.On("SetSchema", mock.Anything, "sp", "env", "1", schema.New("name", field.String()).WithMetadata(extension.ExtensionMetadataKey, "test-extension")).Return(nil).Once()
+			},
+			envsCall: func(svc *envmocks.Environments) {
+				svc.On("Migrate", mock.Anything, "sp", "env", &environments.MigrateOptions{Wait: true}).Return(nil).Once()
+			},
+			wantErr: func(t *testing.T, err error) {
+				assert.NoError(t, err)
+			},
+			co:    UpdateCollectionSchemaMetadata(),
+			force: true,
 		},
 		{
 			name:        "Update exist view collection with the same id",
@@ -150,7 +182,12 @@ func TestSetup_InstallCollections(t *testing.T) {
 			}
 
 			s := NewSetup(&content.Content{Collections: c, Environments: e}, "sp", "env", nil)
-			s.AddCollections(tt.collections)
+			s.force = tt.force
+			if tt.co != nil {
+				s.AddCollections(tt.collections, tt.co)
+			} else {
+				s.AddCollections(tt.collections)
+			}
 			tt.wantErr(t, s.InstallCollections(context.Background()))
 		})
 	}
-- 
GitLab