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

правки по ПР

parent 188bef62
Branches
Tags
No related merge requests found
......@@ -11,6 +11,7 @@ func Test_ParseID(t *testing.T) {
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>",
}},
},
{
......
......@@ -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"`),
},
{
......
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment