forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_version_test.go
429 lines (335 loc) · 19.1 KB
/
model_version_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package core
import (
"context"
"fmt"
"github.com/kubeflow/model-registry/internal/apiutils"
"github.com/kubeflow/model-registry/internal/converter"
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
"github.com/kubeflow/model-registry/pkg/api"
"github.com/kubeflow/model-registry/pkg/openapi"
)
// MODEL VERSIONS
func (suite *CoreTestSuite) TestCreateModelVersion() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
state := openapi.MODELVERSIONSTATE_LIVE
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Description: &modelVersionDescription,
State: &state,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.Equal((*createdVersion).RegisteredModelId, registeredModelId, "RegisteredModelId should match the actual owner-entity")
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
createdVersionId, _ := converter.StringToInt64(createdVersion.Id)
byId, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*createdVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
suite.Equal(1, len(byId.Contexts), "there should be just one context saved in mlmd")
suite.Equal(*createdVersionId, *byId.Contexts[0].Id, "returned model id should match the mlmd one")
suite.Equal(fmt.Sprintf("%s:%s", registeredModelId, modelVersionName), *byId.Contexts[0].Name, "saved model name should match the provided one")
suite.Equal(versionExternalId, *byId.Contexts[0].ExternalId, "saved external id should match the provided one")
suite.Equal(author, byId.Contexts[0].Properties["author"].GetStringValue(), "saved author property should match the provided one")
suite.Equal(modelVersionDescription, byId.Contexts[0].Properties["description"].GetStringValue(), "saved description should match the provided one")
suite.Equal(string(state), byId.Contexts[0].Properties["state"].GetStringValue(), "saved state should match the provided one")
suite.Equalf(*modelVersionTypeName, *byId.Contexts[0].Type, "saved context should be of type of %s", *modelVersionTypeName)
getAllResp, err := suite.mlmdClient.GetContexts(context.Background(), &proto.GetContextsRequest{})
suite.Nilf(err, "error retrieving all contexts, not related to the test itself: %v", err)
suite.Equal(2, len(getAllResp.Contexts), "there should be two contexts saved in mlmd")
}
func (suite *CoreTestSuite) TestCreateModelVersionFailure() {
// create mode registry service
service := suite.setupModelRegistryService()
_, err := service.UpsertModelVersion(nil, nil)
suite.NotNil(err)
suite.Equal("invalid model version pointer, can't upsert nil: bad request", err.Error())
registeredModelId := "9999"
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
_, err = service.UpsertModelVersion(modelVersion, nil)
suite.NotNil(err)
suite.Equal("missing registered model id, cannot create model version without registered model: bad request", err.Error())
_, err = service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.NotNil(err)
suite.Equal("no registered model found for id 9999: not found", err.Error())
}
func (suite *CoreTestSuite) TestUpdateModelVersion() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
createdVersionId, _ := converter.StringToInt64(createdVersion.Id)
newExternalId := "org.my_awesome_model@v1"
newScore := 0.95
createdVersion.ExternalId = &newExternalId
(*createdVersion.CustomProperties)["score"] = openapi.MetadataValue{
MetadataDoubleValue: converter.NewMetadataDoubleValue(newScore),
}
updatedVersion, err := service.UpsertModelVersion(createdVersion, ®isteredModelId)
suite.Nilf(err, "error updating new model version for %s: %v", registeredModelId, err)
suite.Equal((*updatedVersion).RegisteredModelId, registeredModelId, "RegisteredModelId should match the actual owner-entity")
updateVersionId, _ := converter.StringToInt64(updatedVersion.Id)
suite.Equal(*createdVersionId, *updateVersionId, "created and updated model version should have same id")
byId, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*updateVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
suite.Equal(1, len(byId.Contexts), "there should be just one context saved in mlmd")
suite.Equal(*updateVersionId, *byId.Contexts[0].Id, "returned model id should match the mlmd one")
suite.Equal(fmt.Sprintf("%s:%s", registeredModelId, modelVersionName), *byId.Contexts[0].Name, "saved model name should match the provided one")
suite.Equal(newExternalId, *byId.Contexts[0].ExternalId, "saved external id should match the provided one")
suite.Equal(author, byId.Contexts[0].Properties["author"].GetStringValue(), "saved author property should match the provided one")
suite.Equal(newScore, byId.Contexts[0].CustomProperties["score"].GetDoubleValue(), "saved score custom property should match the provided one")
suite.Equalf(*modelVersionTypeName, *byId.Contexts[0].Type, "saved context should be of type of %s", *modelVersionTypeName)
getAllResp, err := suite.mlmdClient.GetContexts(context.Background(), &proto.GetContextsRequest{})
suite.Nilf(err, "error retrieving all contexts, not related to the test itself: %v", err)
suite.Equal(2, len(getAllResp.Contexts), "there should be two contexts saved in mlmd")
// update with nil name
newExternalId = "org.my_awesome_model_@v1"
updatedVersion.ExternalId = &newExternalId
updatedVersion.Name = ""
updatedVersion, err = service.UpsertModelVersion(updatedVersion, ®isteredModelId)
suite.Nilf(err, "error updating new model version for %s: %v", registeredModelId, err)
updateVersionId, _ = converter.StringToInt64(updatedVersion.Id)
suite.Equal(*createdVersionId, *updateVersionId, "created and updated model version should have same id")
byId, err = suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*updateVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
suite.Equal(1, len(byId.Contexts), "there should be just one context saved in mlmd")
suite.Equal(*updateVersionId, *byId.Contexts[0].Id, "returned model id should match the mlmd one")
suite.Equal(fmt.Sprintf("%s:%s", registeredModelId, modelVersionName), *byId.Contexts[0].Name, "saved model name should match the provided one")
suite.Equal(newExternalId, *byId.Contexts[0].ExternalId, "saved external id should match the provided one")
suite.Equal(author, byId.Contexts[0].Properties["author"].GetStringValue(), "saved author property should match the provided one")
suite.Equal(newScore, byId.Contexts[0].CustomProperties["score"].GetDoubleValue(), "saved score custom property should match the provided one")
suite.Equalf(*modelVersionTypeName, *byId.Contexts[0].Type, "saved context should be of type of %s", *modelVersionTypeName)
}
func (suite *CoreTestSuite) TestUpdateModelVersionFailure() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %s", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
newExternalId := "org.my_awesome_model@v1"
newScore := 0.95
createdVersion.ExternalId = &newExternalId
(*createdVersion.CustomProperties)["score"] = openapi.MetadataValue{
MetadataDoubleValue: converter.NewMetadataDoubleValue(newScore),
}
wrongId := "9999"
createdVersion.Id = &wrongId
_, err = service.UpsertModelVersion(createdVersion, ®isteredModelId)
suite.NotNil(err)
suite.Equal(fmt.Sprintf("no model version found for id %s: not found", wrongId), err.Error())
}
func (suite *CoreTestSuite) TestGetModelVersionById() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
state := openapi.MODELVERSIONSTATE_ARCHIVED
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
State: &state,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
createdVersionId, _ := converter.StringToInt64(createdVersion.Id)
getById, err := service.GetModelVersionById(*createdVersion.Id)
suite.Nilf(err, "error getting model version with id %d", *createdVersionId)
ctxById, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*createdVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx := ctxById.Contexts[0]
suite.Equal(*converter.Int64ToString(ctx.Id), *getById.Id, "returned model version id should match the mlmd context one")
suite.Equal(modelVersion.Name, getById.Name, "saved model name should match the provided one")
suite.Equal(*modelVersion.ExternalId, *getById.ExternalId, "saved external id should match the provided one")
suite.Equal(*modelVersion.State, *getById.State, "saved model state should match the original one")
suite.Equal(*getById.Author, author, "saved author property should match the provided one")
}
func (suite *CoreTestSuite) TestGetModelVersionByParamsWithNoResults() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
_, err := service.GetModelVersionByParams(apiutils.Of("not-present"), ®isteredModelId, nil)
suite.NotNil(err)
suite.Equal("no model versions found for versionName=not-present, registeredModelId=1, externalId=: not found", err.Error())
}
func (suite *CoreTestSuite) TestGetModelVersionByParamsName() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
createdVersionId, _ := converter.StringToInt64(createdVersion.Id)
getByName, err := service.GetModelVersionByParams(&modelVersionName, ®isteredModelId, nil)
suite.Nilf(err, "error getting model version by name %d", *createdVersionId)
ctxById, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*createdVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx := ctxById.Contexts[0]
suite.Equal(*converter.Int64ToString(ctx.Id), *getByName.Id, "returned model version id should match the mlmd context one")
suite.Equal(fmt.Sprintf("%s:%s", registeredModelId, getByName.Name), *ctx.Name, "saved model name should match the provided one")
suite.Equal(*ctx.ExternalId, *getByName.ExternalId, "saved external id should match the provided one")
suite.Equal(ctx.Properties["author"].GetStringValue(), *getByName.Author, "saved author property should match the provided one")
}
func (suite *CoreTestSuite) TestGetModelVersionByParamsExternalId() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
createdVersionId, _ := converter.StringToInt64(createdVersion.Id)
getByExternalId, err := service.GetModelVersionByParams(nil, nil, modelVersion.ExternalId)
suite.Nilf(err, "error getting model version by external id %d", *modelVersion.ExternalId)
ctxById, err := suite.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{
*createdVersionId,
},
})
suite.Nilf(err, "error retrieving context by type and name, not related to the test itself: %v", err)
ctx := ctxById.Contexts[0]
suite.Equal(*converter.Int64ToString(ctx.Id), *getByExternalId.Id, "returned model version id should match the mlmd context one")
suite.Equal(fmt.Sprintf("%s:%s", registeredModelId, getByExternalId.Name), *ctx.Name, "saved model name should match the provided one")
suite.Equal(*ctx.ExternalId, *getByExternalId.ExternalId, "saved external id should match the provided one")
suite.Equal(ctx.Properties["author"].GetStringValue(), *getByExternalId.Author, "saved author property should match the provided one")
}
func (suite *CoreTestSuite) TestGetModelVersionByEmptyParams() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
Author: &author,
}
createdVersion, err := service.UpsertModelVersion(modelVersion, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.NotNilf(createdVersion.Id, "created model version should not have nil Id")
_, err = service.GetModelVersionByParams(nil, nil, nil)
suite.NotNil(err)
suite.Equal("invalid parameters call, supply either (versionName and registeredModelId), or externalId: bad request", err.Error())
}
func (suite *CoreTestSuite) TestGetModelVersions() {
// create mode registry service
service := suite.setupModelRegistryService()
registeredModelId := suite.registerModel(service, nil, nil)
modelVersion1 := &openapi.ModelVersion{
Name: modelVersionName,
ExternalId: &versionExternalId,
}
secondModelVersionName := "v2"
secondModelVersionExtId := "org.myawesomemodel@v2"
modelVersion2 := &openapi.ModelVersion{
Name: secondModelVersionName,
ExternalId: &secondModelVersionExtId,
}
thirdModelVersionName := "v3"
thirdModelVersionExtId := "org.myawesomemodel@v3"
modelVersion3 := &openapi.ModelVersion{
Name: thirdModelVersionName,
ExternalId: &thirdModelVersionExtId,
}
createdVersion1, err := service.UpsertModelVersion(modelVersion1, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
createdVersion2, err := service.UpsertModelVersion(modelVersion2, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
createdVersion3, err := service.UpsertModelVersion(modelVersion3, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
anotherRegModelName := "AnotherModel"
anotherRegModelExtId := "org.another"
anotherRegisteredModelId := suite.registerModel(service, &anotherRegModelName, &anotherRegModelExtId)
anotherModelVersionName := "v1.0"
anotherModelVersionExtId := "[email protected]"
modelVersionAnother := &openapi.ModelVersion{
Name: anotherModelVersionName,
ExternalId: &anotherModelVersionExtId,
}
_, err = service.UpsertModelVersion(modelVersionAnother, &anotherRegisteredModelId)
suite.Nilf(err, "error creating new model version for %d", anotherRegisteredModelId)
createdVersionId1, _ := converter.StringToInt64(createdVersion1.Id)
createdVersionId2, _ := converter.StringToInt64(createdVersion2.Id)
createdVersionId3, _ := converter.StringToInt64(createdVersion3.Id)
getAll, err := service.GetModelVersions(api.ListOptions{}, nil)
suite.Nilf(err, "error getting all model versions")
suite.Equal(int32(4), getAll.Size, "expected four model versions across all registered models")
getAllByRegModel, err := service.GetModelVersions(api.ListOptions{}, ®isteredModelId)
suite.Nilf(err, "error getting all model versions")
suite.Equalf(int32(3), getAllByRegModel.Size, "expected three model versions for registered model %d", registeredModelId)
suite.Equal(*converter.Int64ToString(createdVersionId1), *getAllByRegModel.Items[0].Id)
suite.Equal(*converter.Int64ToString(createdVersionId2), *getAllByRegModel.Items[1].Id)
suite.Equal(*converter.Int64ToString(createdVersionId3), *getAllByRegModel.Items[2].Id)
// order by last update time, expecting last created as first
orderByLastUpdate := "LAST_UPDATE_TIME"
getAllByRegModel, err = service.GetModelVersions(api.ListOptions{
OrderBy: &orderByLastUpdate,
SortOrder: &descOrderDirection,
}, ®isteredModelId)
suite.Nilf(err, "error getting all model versions")
suite.Equalf(int32(3), getAllByRegModel.Size, "expected three model versions for registered model %d", registeredModelId)
suite.Equal(*converter.Int64ToString(createdVersionId1), *getAllByRegModel.Items[2].Id)
suite.Equal(*converter.Int64ToString(createdVersionId2), *getAllByRegModel.Items[1].Id)
suite.Equal(*converter.Int64ToString(createdVersionId3), *getAllByRegModel.Items[0].Id)
// update the second version
newVersionExternalId := "updated.org:v2"
createdVersion2.ExternalId = &newVersionExternalId
createdVersion2, err = service.UpsertModelVersion(createdVersion2, ®isteredModelId)
suite.Nilf(err, "error creating new model version for %d", registeredModelId)
suite.Equal(newVersionExternalId, *createdVersion2.ExternalId)
getAllByRegModel, err = service.GetModelVersions(api.ListOptions{
OrderBy: &orderByLastUpdate,
SortOrder: &descOrderDirection,
}, ®isteredModelId)
suite.Nilf(err, "error getting all model versions")
suite.Equalf(int32(3), getAllByRegModel.Size, "expected three model versions for registered model %d", registeredModelId)
suite.Equal(*converter.Int64ToString(createdVersionId1), *getAllByRegModel.Items[2].Id)
suite.Equal(*converter.Int64ToString(createdVersionId2), *getAllByRegModel.Items[0].Id)
suite.Equal(*converter.Int64ToString(createdVersionId3), *getAllByRegModel.Items[1].Id)
}