From 51fa2b09d85aefe19d0cc51bc26927faebbc172d Mon Sep 17 00:00:00 2001
From: "a.petraki" <a.petraki@perx.ru>
Date: Fri, 1 Dec 2023 11:53:55 +0300
Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0,=20?=
 =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D0=BE=D0=B9?=
 =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=BD?=
 =?UTF-8?q?=D0=BE=D0=B9=20=D0=B2=20Setup=20=D0=BE=D0=BF=D1=86=D0=B8=D0=B8?=
 =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8?=
 =?UTF-8?q?=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D0=B9=20?=
 =?UTF-8?q?=D0=BE=D0=BD=D0=B8=20=D0=BD=D0=B5=20=D0=BE=D0=B1=D0=BD=D0=BE?=
 =?UTF-8?q?=D0=B2=D0=BB=D1=8F=D0=BB=D0=B8=D1=81=D1=8C=20=D0=BF=D1=80=D0=B8?=
 =?UTF-8?q?=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B8=20?=
 =?UTF-8?q?=D1=81=D1=85=D0=B5=D0=BC=D1=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/setup/collection.go      |  4 +--
 pkg/setup/collection_test.go | 50 ++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go
index 0a7fc65c..9f5f6a46 100644
--- a/pkg/setup/collection.go
+++ b/pkg/setup/collection.go
@@ -61,9 +61,9 @@ func OverwriteCollection() CollectionsOption {
 	return func(c *CollectionConfig) {
 		c.UpdateFn = func(s *Setup, old, new *collections.Collection) (*collections.Collection, bool, bool, error) {
 			update := new.Name != old.Name || new.IsSingle() != old.IsSingle() || new.IsSystem() != old.IsSystem() ||
-				new.IsNoData() != old.IsNoData() || new.Hidden != old.Hidden || new.IsView() != old.IsView() || !data.ElementsMatch(old.Tags, new.Tags)
+				new.IsNoData() != old.IsNoData() || new.Hidden != old.Hidden || !new.View.Equal(old.View) || !data.ElementsMatch(old.Tags, new.Tags)
 
-			return new, update, old.Schema.Equal(new.Schema), nil
+			return new, update, !old.Schema.Equal(new.Schema), nil
 		}
 	}
 }
diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go
index a06c117a..43b45011 100644
--- a/pkg/setup/collection_test.go
+++ b/pkg/setup/collection_test.go
@@ -14,6 +14,7 @@ import (
 	"git.perx.ru/perxis/perxis-go/pkg/schema/field"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/mock"
+	"github.com/stretchr/testify/require"
 )
 
 func TestSetup_InstallCollections(t *testing.T) {
@@ -156,3 +157,52 @@ func TestSetup_InstallCollections(t *testing.T) {
 		})
 	}
 }
+
+func TestOverwriteCollection(t *testing.T) {
+	tests := []struct {
+		name          string
+		old, new      *collections.Collection
+		wantUpdate    bool
+		wantSetSchema bool
+		wantErr       bool
+	}{
+		{
+			name:          "Equal collections should not be updated",
+			old:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Schema: schema.New("a", field.String())},
+			new:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Schema: schema.New("a", field.String())},
+			wantUpdate:    false,
+			wantSetSchema: false,
+		},
+		{
+			name:          "For collections with different schemas and equal other params schemas should be set",
+			old:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll", Schema: schema.New("a", field.String())},
+			new:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll", Schema: schema.New("b", field.String())},
+			wantUpdate:    false,
+			wantSetSchema: true,
+		},
+		{
+			name:          "Collections with different names should be updated",
+			old:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll1", Schema: schema.New("a", field.String())},
+			new:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", Name: "Coll2", Schema: schema.New("a", field.String())},
+			wantUpdate:    true,
+			wantSetSchema: false,
+		},
+		{
+			name:          "Collections with different view params should be updated",
+			old:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", View: &collections.View{SpaceID: "sp1", EnvID: "env1", CollectionID: "coll1"}},
+			new:           &collections.Collection{ID: "coll", SpaceID: "sp", EnvID: "env", View: &collections.View{SpaceID: "sp2", EnvID: "env2", CollectionID: "coll2"}},
+			wantUpdate:    true,
+			wantSetSchema: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			c := new(CollectionConfig)
+			OverwriteCollection()(c)
+			_, update, setSchema, err := c.UpdateFn(new(Setup), tt.old, tt.new)
+			require.NoError(t, err)
+			assert.Equal(t, tt.wantUpdate, update)
+			assert.Equal(t, tt.wantSetSchema, setSchema)
+		})
+	}
+}
-- 
GitLab