diff --git a/id/id_test.go b/id/id_test.go index eb992a23f41cd722064b1d81b205a4732a520ce9..9fb9fed14f21e9e65f56657d29d2ac49f4eca8a3 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 501535fac8a0fec970c85ebf50e2d5a3b05107d6..83a444d1ebcbd6f2ed069c931d05a0dbb147e7a1 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 213baa1dffecc8859cf22ca19aadf435ae541a78..d77577a0f018d38b775b3542dc6d311a6c02c93f 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 }