-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathknn.journey.test.ts
301 lines (268 loc) · 8.58 KB
/
knn.journey.test.ts
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
import weaviate, {IWeaviateClient} from "../index";
import Connection from "../connection";
const targetDessertId = "cd54852a-209d-423b-bf1c-884468215237";
const targetSavoryId = "e5da0127-327e-4184-85b8-7b9d1af4a850";
const unclassifiedOneId = "8bde517e-01a7-47c9-8db6-d09a2e8d3db7";
const unclassifiedTwoId = "39a04208-b6b6-4df4-9aba-caed9e0df2c3";
describe("a classification journey", () => {
describe("knn - manually polling the status", () => {
const client = weaviate.client({
scheme: "http",
host: "localhost:8080",
});
it("setups the schema and data", () => setup(client));
let id: any; // will be assigned by weaviate, see then block in scheduler
it("triggers a classification without waiting", () => {
return client.classifications
.scheduler()
.withType("knn")
.withSettings({k: 3})
.withClassName("ClassificationJourneySource")
.withClassifyProperties(["toTarget"])
.withBasedOnProperties(["description"])
.do()
.then((res: any) => {
expect(res.id).toBeDefined();
id = res.id;
})
.catch((e: any) => {
throw new Error("it should not have errord: " + e)
});
});
it("is now completed", () => {
return client.classifications
.getter()
.withId(id)
.do()
.then((res: any) => {
expect(res.status).toEqual("completed");
});
});
it("eventually turns to completed", () => {
const timeoutMS = 5 * 1000;
return new Promise((resolve, reject) => {
var timeout = setTimeout(reject, timeoutMS);
var backgroundWork = setInterval(() => {
client.classifications
.getter()
.withId(id)
.do()
.then((res: any) => {
clearInterval(backgroundWork);
clearTimeout(timeout);
res.status == "completed" && resolve(undefined);
});
}, 500);
}).catch(() => {
throw new Error("timed out")
});
});
it("waits for es index updates to have refreshed", () => {
return new Promise((resolve) => setTimeout(resolve, 1200));
});
// removed as the classification results - not affecting the client itself-
// seem to be flaky
// it('has correctly classified the items', () => {
// return Promise.all([
// client.data
// .getterById()
// .withId(unclassifiedOneId)
// .do()
// .then((res: any) => {
// expect(res.schema.toTarget[0].beacon).toEqual(
// beaconTo(targetDessertId),
// );
// })
// .catch(e => {throw new Error('it should not have errord: ' + e)),
// client.data
// .getterById()
// .withId(unclassifiedTwoId)
// .do()
// .then((res: any) => {
// expect(res.schema.toTarget[0].beacon).toEqual(
// beaconTo(targetSavoryId),
// );
// })
// .catch(e => {throw new Error('it should not have errord: ' + e)),
// ]);
// });
it("tears down and cleans up", () => cleanup(client));
});
describe("knn - using the client's wait method", () => {
const client = weaviate.client({
scheme: "http",
host: "localhost:8080",
});
it("setups the schema and data", () => setup(client));
let id; // will be assigned by weaviate, see then block in scheduler
it("triggers a classification and waits", async () => {
return client.classifications
.scheduler()
.withType("knn")
.withSettings({k: 3})
.withClassName("ClassificationJourneySource")
.withClassifyProperties(["toTarget"])
.withBasedOnProperties(["description"])
.withWaitForCompletion()
.withWaitTimeout(60 * 1000)
.do()
.then((res: any) => {
expect(res.status).toEqual("completed");
id = res.id;
})
.catch((e: any) => {
throw new Error("it should not have errord: " + e)
});
});
it("waits for es index updates to have refreshed", () => {
return new Promise((resolve) => setTimeout(resolve, 1200));
});
// removed as the classification results - not affecting the client itself-
// seem to be flaky
// it('has correctly classified the items', () => {
// return Promise.all([
// client.data
// .getterById()
// .withId(unclassifiedOneId)
// .do()
// .then((res: any) => {
// expect(res.schema.toTarget[0].beacon).toEqual(
// beaconTo(targetDessertId),
// );
// })
// .catch(e => {throw new Error('it should not have errord: ' + e)),
// client.data
// .getterById()
// .withId(unclassifiedTwoId)
// .do()
// .then((res: any) => {
// expect(res.schema.toTarget[0].beacon).toEqual(
// beaconTo(targetSavoryId),
// );
// })
// .catch(e => {throw new Error('it should not have errord: ' + e)),
// ]);
// });
it("tears down and cleans up", () => cleanup(client));
});
describe("knn - running into a timeout", () => {
const client = weaviate.client({
scheme: "http",
host: "localhost:8080",
});
it("setups the schema and data", () => setup(client));
it("fails a classification with an impossibly small timeout", () => {
return client.classifications
.scheduler()
.withType("knn")
.withSettings({k: 3})
.withClassName("ClassificationJourneySource")
.withClassifyProperties(["toTarget"])
.withBasedOnProperties(["description"])
.withWaitForCompletion()
.withWaitTimeout(1) // that's going to be difficult ;-)
.do()
.then((res: any) => {
fail("it should have errord");
})
.catch((e: any) => {
expect(e).toEqual(
new Error(
"classification didn't finish within configured timeout, " +
"set larger timeout with .withWaitTimeout(timeout)"
)
);
});
});
it("wait with tear down until the classification actually finishes", () => {
return new Promise((resolve) => setTimeout(resolve, 1000));
});
it("tears down and cleans up", () => cleanup(client));
});
});
const setup = async (client: IWeaviateClient) => {
let targetClass = {
class: "ClassificationJourneyTarget",
properties: [
{
name: "name",
dataType: ["string"],
},
],
};
await client.schema.classCreator().withClass(targetClass).do();
targetClass = {
class: "ClassificationJourneySource",
properties: [
{
name: "description",
dataType: ["text"],
},
{
name: "toTarget",
dataType: ["ClassificationJourneyTarget"],
},
],
};
await client.schema.classCreator().withClass(targetClass).do();
// import targets
await client.data
.creator()
.withClassName("ClassificationJourneyTarget")
.withProperties({name: "Dessert"})
.withId(targetDessertId)
.do();
await client.data
.creator()
.withClassName("ClassificationJourneyTarget")
.withProperties({name: "Savory"})
.withId(targetSavoryId)
.do();
// import training data
await client.data
.creator()
.withClassName("ClassificationJourneySource")
.withProperties({
description: "Lots of Sugar, Cream and Flour. Maybe Eggs.",
toTarget: [{beacon: beaconTo(targetDessertId)}],
})
.do();
await client.data
.creator()
.withClassName("ClassificationJourneySource")
.withProperties({
description: "French Fries and Sausage",
toTarget: [{beacon: beaconTo(targetSavoryId)}],
})
.do();
// import to-be-classifieds
await client.data
.creator()
.withId(unclassifiedOneId)
.withClassName("ClassificationJourneySource")
.withProperties({
description: "This sweet cake contains sugar.",
})
.do();
await client.data
.creator()
.withId(unclassifiedTwoId)
.withClassName("ClassificationJourneySource")
.withProperties({
description: "Potatoes and fried fish",
})
.do();
};
const cleanup = (client: IWeaviateClient) => {
return Promise.all([
client.schema
.classDeleter()
.withClassName("ClassificationJourneySource")
.do(),
client.schema
.classDeleter()
.withClassName("ClassificationJourneyTarget")
.do(),
]);
};
const beaconTo = (target: any) => `weaviate://localhost/${target}`;