From 9feaaf9ed2a889569cf10f33e5b6e62aee9fde0e Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Sun, 19 Nov 2023 10:17:27 +0100 Subject: [PATCH] Fix GetModelArtifactByParams when no results (#165) --- pkg/core/core.go | 12 ++++++-- pkg/core/core_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/pkg/core/core.go b/pkg/core/core.go index bd4350db..87ee2acc 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -699,9 +699,15 @@ func (serv *modelRegistryService) GetModelArtifactByParams(artifactName *string, if err != nil { return nil, err } + if len(artifactsResponse.Artifacts) > 1 { - return nil, fmt.Errorf("more than an artifact detected matching criteria: %v", artifactsResponse.Artifacts) + return nil, fmt.Errorf("multiple model artifacts found for artifactName=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) + } + + if len(artifactsResponse.Artifacts) == 0 { + return nil, fmt.Errorf("no model artifacts found for artifactName=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(artifactName), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) } + artifact0 = artifactsResponse.Artifacts[0] result, err := serv.mapper.MapToModelArtifact(artifact0) @@ -1072,11 +1078,11 @@ func (serv *modelRegistryService) GetInferenceServiceByParams(name *string, pare } if len(getByParamsResp.Contexts) > 1 { - return nil, fmt.Errorf("multiple inference services found for versionName=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) + return nil, fmt.Errorf("multiple inference services found for name=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) } if len(getByParamsResp.Contexts) == 0 { - return nil, fmt.Errorf("no inference services found for versionName=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) + return nil, fmt.Errorf("no inference services found for name=%v, parentResourceId=%v, externalId=%v", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(parentResourceId), apiutils.ZeroIfNil(externalId)) } toReturn, err := serv.mapper.MapToInferenceService(getByParamsResp.Contexts[0]) diff --git a/pkg/core/core_test.go b/pkg/core/core_test.go index c5b9eecb..aae040e3 100644 --- a/pkg/core/core_test.go +++ b/pkg/core/core_test.go @@ -706,6 +706,18 @@ func TestGetRegisteredModelById(t *testing.T) { assertion.Equal(*registeredModel.CustomProperties, *getModelById.CustomProperties, "saved model custom props should match the original one") } +func TestGetRegisteredModelByParamsWithNoResults(t *testing.T) { + assertion, conn, _, teardown := setup(t) + defer teardown(t) + + // create mode registry service + service := initModelRegistryService(assertion, conn) + + _, err := service.GetRegisteredModelByParams(of("not-present"), nil) + assertion.NotNil(err) + assertion.Equal("no registered models found for name=not-present, externalId=", err.Error()) +} + func TestGetRegisteredModelByParamsName(t *testing.T) { assertion, conn, _, teardown := setup(t) defer teardown(t) @@ -1198,6 +1210,20 @@ func TestGetModelVersionById(t *testing.T) { assertion.Equal(*(*getById.CustomProperties)["author"].MetadataStringValue.StringValue, author, "saved author custom property should match the provided one") } +func TestGetModelVersionByParamsWithNoResults(t *testing.T) { + assertion, conn, _, teardown := setup(t) + defer teardown(t) + + // create mode registry service + service := initModelRegistryService(assertion, conn) + + registeredModelId := registerModel(assertion, service, nil, nil) + + _, err := service.GetModelVersionByParams(of("not-present"), ®isteredModelId, nil) + assertion.NotNil(err) + assertion.Equal("no model versions found for versionName=not-present, parentResourceId=1, externalId=", err.Error()) +} + func TestGetModelVersionByParamsName(t *testing.T) { assertion, conn, client, teardown := setup(t) defer teardown(t) @@ -1726,6 +1752,20 @@ func TestGetModelArtifactByEmptyParams(t *testing.T) { assertion.Equal("invalid parameters call, supply either (artifactName and parentResourceId), or externalId", err.Error()) } +func TestGetModelArtifactByParamsWithNoResults(t *testing.T) { + assertion, conn, _, teardown := setup(t) + defer teardown(t) + + // create mode registry service + service := initModelRegistryService(assertion, conn) + + modelVersionId := registerModelVersion(assertion, service, nil, nil, nil, nil) + + _, err := service.GetModelArtifactByParams(of("not-present"), &modelVersionId, nil) + assertion.NotNil(err) + assertion.Equal("no model artifacts found for artifactName=not-present, parentResourceId=2, externalId=", err.Error()) +} + func TestGetModelArtifacts(t *testing.T) { assertion, conn, _, teardown := setup(t) defer teardown(t) @@ -1989,6 +2029,18 @@ func TestGetServingEnvironmentById(t *testing.T) { assertion.Equal(*eut.CustomProperties, *getEntityById.CustomProperties, "saved custom props should match the original one") } +func TestGetServingEnvironmentByParamsWithNoResults(t *testing.T) { + assertion, conn, _, teardown := setup(t) + defer teardown(t) + + // create mode registry service + service := initModelRegistryService(assertion, conn) + + _, err := service.GetServingEnvironmentByParams(of("not-present"), nil) + assertion.NotNil(err) + assertion.Equal("no serving environments found for name=not-present, externalId=", err.Error()) +} + func TestGetServingEnvironmentByParamsName(t *testing.T) { assertion, conn, _, teardown := setup(t) defer teardown(t) @@ -2595,6 +2647,20 @@ func TestGetModelVersionByInferenceServiceId(t *testing.T) { assertion.Equal(createdVersion1Id, *getVModel.Id, "returned id shall be the specified one") } +func TestGetInferenceServiceByParamsWithNoResults(t *testing.T) { + assertion, conn, _, teardown := setup(t) + defer teardown(t) + + // create mode registry service + service := initModelRegistryService(assertion, conn) + + parentResourceId := registerServingEnvironment(assertion, service, nil, nil) + + _, err := service.GetInferenceServiceByParams(of("not-present"), &parentResourceId, nil) + assertion.NotNil(err) + assertion.Equal("no inference services found for name=not-present, parentResourceId=1, externalId=", err.Error()) +} + func TestGetInferenceServiceByParamsName(t *testing.T) { assertion, conn, client, teardown := setup(t) defer teardown(t)