Skip to content

Commit 119cee0

Browse files
committed
Convert unit test to a table
1 parent a807b40 commit 119cee0

File tree

1 file changed

+112
-59
lines changed

1 file changed

+112
-59
lines changed

pkg/epp/datastore/datastore_test.go

Lines changed: 112 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestPool(t *testing.T) {
8989
}
9090
}
9191

92-
func TestModel(t *testing.T) {
92+
func TestModel1(t *testing.T) {
9393
chatModel := "chat"
9494
tsModel := "tweet-summary"
9595
model1ts := testutil.MakeInferenceModel("model1").
@@ -112,68 +112,121 @@ func TestModel(t *testing.T) {
112112
CreationTimestamp(metav1.Unix(1005, 0)).
113113
ModelName(chatModel).ObjRef()
114114

115-
ds := NewDatastore()
116-
dsImpl := ds.(*datastore)
117-
118-
// Step 1: add model1 with tweet-summary as modelName.
119-
ds.ModelSetIfOlder(model1ts)
120-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model1ts}); diff != "" {
121-
t.Errorf("Unexpected models diff: %s", diff)
122-
}
123-
124-
// Step 2: set model1 with the same modelName, but with criticality set and newer creation timestamp, should update.
125-
ds.ModelSetIfOlder(model1tsNewer)
126-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model1tsNewer}); diff != "" {
127-
t.Errorf("Unexpected models diff: %s", diff)
128-
}
129-
130-
// Step 3: set model2 with the same modelName, but newer creation timestamp, should not update.
131-
ds.ModelSetIfOlder(model2tsNewer)
132-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model1tsNewer}); diff != "" {
133-
t.Errorf("Unexpected models diff: %s", diff)
134-
}
135-
136-
// Step 4: set model2 with the same modelName, but older creation timestamp, should update.
137-
ds.ModelSetIfOlder(model2ts)
138-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model2ts}); diff != "" {
139-
t.Errorf("Unexpected models diff: %s", diff)
140-
}
141-
142-
// Step 5: set model2 updated with a new modelName, should update modelName.
143-
ds.ModelSetIfOlder(model2chat)
144-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model2chat}); diff != "" {
145-
t.Errorf("Unexpected models diff: %s", diff)
146-
}
147-
148-
// Step 6: set model1 with the tweet-summary modelName, both models should exist.
149-
ds.ModelSetIfOlder(model1ts)
150-
if diff := diffModelMaps(dsImpl, []*v1alpha2.InferenceModel{model2chat, model1ts}); diff != "" {
151-
t.Errorf("Unexpected models diff: %s", diff)
152-
}
115+
tests := []struct {
116+
name string
117+
existingModels []*v1alpha2.InferenceModel
118+
op func(ds Datastore) bool
119+
wantOpResult bool
120+
wantModels []*v1alpha2.InferenceModel
121+
}{
122+
{
123+
name: "Add model1 with tweet-summary as modelName",
124+
op: func(ds Datastore) bool {
125+
return ds.ModelSetIfOlder(model1ts)
126+
},
127+
wantModels: []*v1alpha2.InferenceModel{model1ts},
128+
wantOpResult: true,
129+
},
130+
{
131+
name: "Set model1 with the same modelName, but with diff criticality and newer creation timestamp, should update.",
132+
existingModels: []*v1alpha2.InferenceModel{model1ts},
133+
op: func(ds Datastore) bool {
134+
return ds.ModelSetIfOlder(model1tsNewer)
135+
},
136+
wantOpResult: true,
137+
wantModels: []*v1alpha2.InferenceModel{model1tsNewer},
138+
},
139+
{
140+
name: "set model2 with the same modelName, but newer creation timestamp, should not update.",
141+
existingModels: []*v1alpha2.InferenceModel{model1tsNewer},
142+
op: func(ds Datastore) bool {
143+
return ds.ModelSetIfOlder(model2tsNewer)
144+
},
145+
wantOpResult: false,
146+
wantModels: []*v1alpha2.InferenceModel{model1tsNewer},
147+
},
148+
{
149+
name: "Set model2 with the same modelName, but older creation timestamp, should update",
150+
existingModels: []*v1alpha2.InferenceModel{model1tsNewer},
151+
op: func(ds Datastore) bool {
152+
return ds.ModelSetIfOlder(model2ts)
153+
},
154+
wantOpResult: true,
155+
wantModels: []*v1alpha2.InferenceModel{model2ts},
156+
},
157+
{
158+
name: "Set model2 updated with a new modelName, should update modelName",
159+
existingModels: []*v1alpha2.InferenceModel{model2ts},
160+
op: func(ds Datastore) bool {
161+
return ds.ModelSetIfOlder(model2chat)
162+
},
163+
wantOpResult: true,
164+
wantModels: []*v1alpha2.InferenceModel{model2chat},
165+
},
166+
{
167+
name: "Set model1 with the tweet-summary modelName, both models should exist",
168+
existingModels: []*v1alpha2.InferenceModel{model2chat},
169+
op: func(ds Datastore) bool {
170+
return ds.ModelSetIfOlder(model1ts)
171+
},
172+
wantOpResult: true,
173+
wantModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
174+
},
175+
{
176+
name: "Set model1 with the tweet-summary modelName, both models should exist",
177+
existingModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
178+
op: func(ds Datastore) bool {
179+
return ds.ModelSetIfOlder(model1ts)
180+
},
181+
wantOpResult: true,
182+
wantModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
183+
},
184+
{
185+
name: "Getting by model name, chat -> model2",
186+
existingModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
187+
op: func(ds Datastore) bool {
188+
gotChat, exists := ds.ModelGetByModelName(chatModel)
189+
return exists && cmp.Diff(model2chat, gotChat) == ""
190+
},
191+
wantOpResult: true,
192+
wantModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
193+
},
194+
{
195+
name: "Getting by obj name, model1 -> tweet-summary",
196+
existingModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
197+
op: func(ds Datastore) bool {
198+
got, exists := ds.ModelGetByObjName(types.NamespacedName{Name: model1ts.Name, Namespace: model1ts.Namespace})
199+
return exists && cmp.Diff(model1ts, got) == ""
200+
},
201+
wantOpResult: true,
202+
wantModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
203+
},
204+
{
205+
name: "Getting by model name, chat -> model2",
206+
existingModels: []*v1alpha2.InferenceModel{model2chat, model1ts},
207+
op: func(ds Datastore) bool {
208+
ds.ModelDelete(types.NamespacedName{Name: model1ts.Name, Namespace: model1ts.Namespace})
209+
_, exists := ds.ModelGetByModelName(tsModel)
210+
return exists
153211

154-
// Step 7: getting the models by model name, chat -> model2; tweet-summary -> model1
155-
gotChat, exists := ds.ModelGetByModelName(chatModel)
156-
if !exists {
157-
t.Error("Chat model should exist!")
158-
}
159-
if diff := cmp.Diff(model2chat, gotChat); diff != "" {
160-
t.Errorf("Unexpected chat model diff: %s", diff)
161-
}
162-
gotSummary, exists := ds.ModelGetByModelName(tsModel)
163-
if !exists {
164-
t.Error("Summary model should exist!")
165-
}
166-
if diff := cmp.Diff(model1ts, gotSummary); diff != "" {
167-
t.Errorf("Unexpected summary model diff: %s", diff)
212+
},
213+
wantOpResult: false,
214+
wantModels: []*v1alpha2.InferenceModel{model2chat},
215+
},
168216
}
217+
for _, test := range tests {
218+
t.Run(test.name, func(t *testing.T) {
219+
ds := NewFakeDatastore(nil, test.existingModels, nil)
220+
gotOpResult := test.op(ds)
221+
if gotOpResult != test.wantOpResult {
222+
t.Errorf("Unexpected operation result, want: %v, got: %v", test.wantOpResult, gotOpResult)
223+
}
224+
if diff := diffModelMaps(ds.(*datastore), test.wantModels); diff != "" {
225+
t.Errorf("Unexpected models diff: %s", diff)
226+
}
169227

170-
// Step 6: delete model1, summary model should not exist.
171-
ds.ModelDelete(types.NamespacedName{Name: model1ts.Name, Namespace: model1ts.Namespace})
172-
_, exists = ds.ModelGetByModelName(tsModel)
173-
if exists {
174-
t.Error("Summary model should not exist!")
228+
})
175229
}
176-
177230
}
178231

179232
func diffModelMaps(ds *datastore, want []*v1alpha2.InferenceModel) string {

0 commit comments

Comments
 (0)