From 6e4c58c3402565bb935445ef5633e6958b6b830e Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Fri, 2 Feb 2024 13:30:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=9F=D0=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- id/id_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++------- id/json_test.go | 4 ++-- id/schema.go | 13 ++++++------- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/id/id_test.go b/id/id_test.go index eb992a23..9fb9fed1 100644 --- a/id/id_test.go +++ b/id/id_test.go @@ -8,9 +8,10 @@ import ( func Test_ParseID(t *testing.T) { tests := []struct { - name string - id string - result *ID + name string + id string + result *ID + wantError bool }{ { name: Service, @@ -75,7 +76,7 @@ func Test_ParseID(t *testing.T) { SpaceID: SpaceID{SpaceID: "<space_id>"}, EnvironmentID: "<env_id>", }, - SchemaID: "<collection_id>", + CollectionID: "<collection_id>", }}, }, { @@ -126,10 +127,45 @@ func Test_ParseID(t *testing.T) { FieldName: "<field_name>", }}, }, + { + name: "With error #1: no backslash in the beginning of id", + id: "spaces/<space_id>", + result: nil, + wantError: true, + }, + { + name: "With error #2: backslash in the end of id", + id: "/spaces/<space_id>/", + result: nil, + wantError: true, + }, + { + name: "With error #3: typo in 'spaces'", + id: "/space/<space_id>", + result: nil, + wantError: true, + }, + { + name: "With error #4: no space_id in id", + id: "/spaces", + result: nil, + wantError: true, + }, + { + name: "With error #5: multiple backslashes in the end of id", + id: "/spaces/<space_id>///", + result: nil, + wantError: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - id, _ := Parse(tt.id) + id, err := Parse(tt.id) + if tt.wantError { + require.Error(t, err) + return + } + require.NoError(t, err) require.Equal(t, tt.result, id) require.Equal(t, tt.id, id.String(), "проверяем корректность работы метода String, полученное ID должно совпадать с исходным") }) @@ -209,13 +245,13 @@ func Test_Map(t *testing.T) { { name: Schema, id: ID{Descriptor: &SchemaID{}}, - from: map[string]any{"space_id": "<space_id>", "env_id": "<env_id>", "schema_id": "<collection_id>"}, + from: map[string]any{"space_id": "<space_id>", "env_id": "<env_id>", "collection_id": "<collection_id>"}, to: ID{Descriptor: &SchemaID{ EnvironmentID: EnvironmentID{ SpaceID: SpaceID{SpaceID: "<space_id>"}, EnvironmentID: "<env_id>", }, - SchemaID: "<collection_id>", + CollectionID: "<collection_id>", }}, }, { diff --git a/id/json_test.go b/id/json_test.go index 501535fa..83a444d1 100644 --- a/id/json_test.go +++ b/id/json_test.go @@ -56,7 +56,7 @@ func TestID_MarshalJSON(t *testing.T) { }, { name: Schema, - ID: ID{Descriptor: &SchemaID{SchemaID: "1", EnvironmentID: EnvironmentID{EnvironmentID: "1", SpaceID: SpaceID{SpaceID: "1"}}}}, + ID: ID{Descriptor: &SchemaID{CollectionID: "1", EnvironmentID: EnvironmentID{EnvironmentID: "1", SpaceID: SpaceID{SpaceID: "1"}}}}, want: `"/spaces/1/envs/1/schema/1"`, }, { @@ -132,7 +132,7 @@ func TestID_UnmarshalJSON(t *testing.T) { }, { id: Schema, - want: ID{Descriptor: &SchemaID{SchemaID: "1", EnvironmentID: EnvironmentID{EnvironmentID: "1", SpaceID: SpaceID{SpaceID: "1"}}}}, + want: ID{Descriptor: &SchemaID{CollectionID: "1", EnvironmentID: EnvironmentID{EnvironmentID: "1", SpaceID: SpaceID{SpaceID: "1"}}}}, b: []byte(`"/spaces/1/envs/1/schema/1"`), }, { diff --git a/id/schema.go b/id/schema.go index 213baa1d..d77577a0 100644 --- a/id/schema.go +++ b/id/schema.go @@ -6,20 +6,19 @@ const ( ) type SchemaID struct { - SpaceID EnvironmentID - SchemaID string `json:"schema_id"` + CollectionID string `json:"collection_id"` } func (t *SchemaID) Type() string { return Schema } func (t *SchemaID) String() string { - return Join(t.EnvironmentID.String(), SchemaPrefix, t.SchemaID) + return Join(t.EnvironmentID.String(), SchemaPrefix, t.CollectionID) } func (t *SchemaID) ToMap() map[string]any { m := t.EnvironmentID.ToMap() - m["schema_id"] = t.SchemaID + m["collection_id"] = t.CollectionID return m } @@ -27,12 +26,12 @@ func (t *SchemaID) FromMap(m map[string]any) error { if err := t.EnvironmentID.FromMap(m); err != nil { return err } - t.SchemaID = m["schema_id"].(string) + t.CollectionID = m["collection_id"].(string) return nil } func (t *SchemaID) Validate() error { - if t.SchemaID == "" { + if t.CollectionID == "" { return ErrInvalidID } @@ -51,6 +50,6 @@ func parseSchemaID(parts []string) (*SchemaID, error) { var id SchemaID id.EnvironmentID = *envID - id.SchemaID = parts[5] + id.CollectionID = parts[5] return &id, nil } -- GitLab