Skip to content

Commit d6d1ba8

Browse files
authored
Merge pull request #985 from stevebio/task/ensureDoneCalledForTests
MLE-24722 - make sure the done() method called within each test once …
2 parents 818217a + 6fb52f3 commit d6d1ba8

12 files changed

+341
-206
lines changed

test-basic/annTopK.js

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,98 +13,89 @@ let serverConfiguration = {};
1313
const execPlan = pbb.execPlan;
1414

1515
describe('tests for annTopK', function () {
16-
this.timeout(5000)
17-
before(function (done) {
18-
try {
19-
testlib.findServerConfiguration(serverConfiguration);
20-
setTimeout(() => {
21-
if (serverConfiguration.serverVersion < 12) {
22-
this.skip();
23-
}
24-
done();
25-
}, 3000);
26-
} catch (error) {
27-
done(error);
16+
this.timeout(5000);
17+
before(async function () {
18+
await testlib.findServerConfigurationPromise(serverConfiguration);
19+
20+
if (serverConfiguration.serverVersion < 12) {
21+
this.skip();
2822
}
2923
});
3024

31-
it('annTopK without PlanAnnTopKOptions', function (done) {
32-
execPlan(p
25+
it('annTopK without PlanAnnTopKOptions', async function () {
26+
const response = await execPlan(p
3327
.fromView('vectors', 'persons', '')
3428
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'))
3529
.orderBy(p.col('name'))
36-
)
37-
.then(function (response) {
38-
verifyResults(response.rows, done);
39-
})
40-
.catch(error => done(error));
30+
);
31+
verifyResults(response.rows);
4132
});
4233

43-
it('annTopK with PlanAnnTopKOptions as a single string', function (done) {
44-
execPlan(p
34+
it('annTopK with PlanAnnTopKOptions as a single string', async function () {
35+
const response = await execPlan(p
4536
.fromView('vectors', 'persons', '')
4637
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'), 'onlyIndex')
4738
.orderBy(p.col('name'))
48-
)
49-
.then(function (response) {
50-
verifyResults(response.rows, done);
51-
})
52-
.catch(error => done(error));
39+
);
40+
verifyResults(response.rows);
5341
});
5442

55-
it('annTopK with PlanAnnTopKOptions as an array of string', function (done) {
56-
execPlan(p
43+
it('annTopK with PlanAnnTopKOptions as an array of string', async function () {
44+
const response = await execPlan(p
5745
.fromView('vectors', 'persons', '')
5846
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
5947
['onlyIndex', "maxDistance=0.15", "searchFactor=1.0"])
6048
.orderBy(p.col('name'))
61-
).then(function (response) {
62-
verifyResults(response.rows, done);
63-
}).catch(error => done(error));
49+
);
50+
verifyResults(response.rows);
6451
});
6552

66-
it('annTopK with PlanAnnTopKOptions as a map', function (done) {
53+
it('annTopK with PlanAnnTopKOptions as a map', async function () {
6754
const planAnnTopKOptionsMap = new Map();
6855
planAnnTopKOptionsMap.set("maxDistance", 0.158454656600952);
6956
planAnnTopKOptionsMap.set("searchFactor", 10.0);
70-
execPlan(p
57+
const response = await execPlan(p
7158
.fromView('vectors', 'persons', '')
7259
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
7360
planAnnTopKOptionsMap)
7461
.orderBy(p.col('name'))
75-
)
76-
.then(function (response) {
77-
verifyResults(response.rows, done);
78-
})
79-
.catch(error => done(error));
62+
);
63+
verifyResults(response.rows);
8064
});
8165

82-
it('annTopK with invalid PlanAnnTopKOptions', function (done) {
66+
it('annTopK with invalid PlanAnnTopKOptions', async function () {
8367
const planAnnTopKOptionsMap = new Map();
8468
planAnnTopKOptionsMap.set('invalid', 10.0);
85-
try{
86-
execPlan(p
87-
.fromView('vectors', 'persons', '')
88-
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
89-
planAnnTopKOptionsMap)
90-
.orderBy(p.col('name'))
91-
);
92-
} catch(error){
93-
assert(error.message.toString().includes('options argument at 4 of PlanModifyPlan.annTopK() has invalid key- invalid'))
94-
done();
95-
}
69+
70+
await assert.rejects(
71+
async () => {
72+
await execPlan(p
73+
.fromView('vectors', 'persons', '')
74+
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
75+
planAnnTopKOptionsMap)
76+
.orderBy(p.col('name'))
77+
);
78+
},
79+
(error) => {
80+
return error.message.toString().includes('options argument at 4 of PlanModifyPlan.annTopK() has invalid key- invalid');
81+
}
82+
);
9683
});
9784

98-
function verifyResults(rows, done){
99-
try {
100-
assert(rows.length === 2, 'Expecting both rows in the view to be returned.');
101-
assert(rows[0].name.value === 'Alice');
102-
assert(rows[0].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
103-
assert(rows[1].name.value === 'Bob');
104-
assert(rows[1].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
105-
done();
106-
} catch (error){
107-
done(error)
108-
}
85+
function verifyResults(rows) {
86+
87+
assert(Array.isArray(rows), 'Expected rows to be an array');
88+
assert(rows.length === 2, 'Expecting both rows in the view to be returned.');
89+
assert(rows[0].name.value === 'Alice');
90+
assert(rows[0].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
91+
assert(rows[1].name.value === 'Bob');
92+
assert(rows[1].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
93+
94+
// Verify each row has the expected structure
95+
rows.forEach((row, index) => {
96+
assert(row.name && row.name.value, `Row ${index} should have a name with a value`);
97+
assert(row.distance && row.distance.type === 'xs:float',
98+
`Row ${index} should have a distance column of type xs:float`);
99+
});
109100
}
110101
});

test-basic/basePath-test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ describe('basePath tests', function() {
1717
testconfig.restWriterConnectionWithBasePath.basePath = 'invalid';
1818
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithBasePath);
1919
dbWriter.documents.write(writeObject)
20-
.result(function(response){})
20+
.result(function(response){
21+
done(new Error('Expecting an error to be thrown due to invalid basePath'));
22+
})
2123
.catch(err=>
2224
{
2325
assert(err.toString().includes('path: invalid/v1/documents'));
@@ -29,7 +31,9 @@ describe('basePath tests', function() {
2931
testconfig.restWriterConnectionWithBasePath.basePath = '/invalid';
3032
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithBasePath);
3133
dbWriter.documents.write(writeObject)
32-
.result(function(response){})
34+
.result(function(response){
35+
done(new Error('Expecting an error to be thrown due to invalid basePath with a leading slash'));
36+
})
3337
.catch(err=>
3438
{
3539
assert(err.toString().includes('path: /invalid/v1/documents'));
@@ -41,7 +45,9 @@ describe('basePath tests', function() {
4145
testconfig.restWriterConnectionWithBasePath.basePath = 'invalid/';
4246
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithBasePath);
4347
dbWriter.documents.write(writeObject)
44-
.result(function(response){})
48+
.result(function(response){
49+
done(new Error('Expecting an error to be thrown due to invalid basePath with a trailing slash'));
50+
})
4551
.catch(err=>
4652
{
4753
assert(err.toString().includes('path: invalid/v1/documents'));
@@ -53,7 +59,9 @@ describe('basePath tests', function() {
5359
testconfig.restWriterConnectionWithBasePath.basePath = '/invalid/';
5460
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithBasePath);
5561
dbWriter.documents.write(writeObject)
56-
.result(function(response){})
62+
.result(function(response){
63+
done(new Error('Expecting an error to be thrown due to invalid basePath with starting and trailing slashes'));
64+
})
5765
.catch(err=>
5866
{
5967
assert(err.toString().includes('path: /invalid/v1/documents'));
@@ -65,7 +73,9 @@ describe('basePath tests', function() {
6573
testconfig.restWriterConnectionWithBasePath.basePath = '//invalid//';
6674
const dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnectionWithBasePath);
6775
dbWriter.documents.write(writeObject)
68-
.result(function(response){})
76+
.result(function(response){
77+
done(new Error('Expecting an error to be thrown due to invalid basePath with multiple starting and trailing slashes'));
78+
})
6979
.catch(err=>
7080
{
7181
try{

test-basic/bindingFromParam.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ describe('optic-update fromParam tests', function(){
111111
const planBuilderTemplate = op.fromParam('myDocs', 'qualifier', outputCols);
112112
const temp = {myDocs: rows};
113113
db.rows.query(planBuilderTemplate,null, temp);
114+
done(new Error('Expecting an error to be thrown due to invalid row-col-types argument'));
114115
} catch (e) {
115116
e.toString().should.equal('Error: row-col-types argument at 2 of PlanBuilder.fromParam() has invalid argument for PlanRowColTypes value: [object Object]');
116117
done();
@@ -157,6 +158,7 @@ describe('optic-update fromParam tests', function(){
157158
const planBuilderTemplate = op.fromParam('myDocs', 'qualifier', outputCols);
158159
const temp = {myDocs: rows};
159160
db.rows.query(planBuilderTemplate, null, temp);
161+
done(new Error('Expecting an error to be thrown due to invalid row-col-types argument'));
160162
} catch (e) {
161163
e.toString().should.equal('Error: row-col-types argument at 2 of PlanBuilder.fromParam() has another type than string');
162164
done();
@@ -205,7 +207,11 @@ describe('optic-update fromParam tests', function(){
205207
}, {"column": "lastName", "type": "string"}];
206208
const planBuilderTemplate = op.fromParam('myDocs', 'qualifier', outputCols);
207209
const temp = {myDocs: rows};
208-
db.rows.query(planBuilderTemplate, null, temp).catch(e => {
210+
db.rows.query(planBuilderTemplate, null, temp)
211+
.then(function(response){
212+
done(new Error('Expecting an error to be thrown due to null value for non-nullable column'));
213+
})
214+
.catch(e => {
209215
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
210216
done();
211217
});
@@ -221,7 +227,11 @@ describe('optic-update fromParam tests', function(){
221227
}, {"column": "lastName", "type": "string", "nullable": true}];
222228
const planBuilderTemplate = op.fromParam('myDocs', 'qualifier', outputCols);
223229
const temp = {myDocs: rows};
224-
db.rows.query(planBuilderTemplate, null,temp).catch(e => {
230+
db.rows.query(planBuilderTemplate, null,temp)
231+
.then(function(response){
232+
done(new Error('Expecting an error to be thrown due to invalid row-col-types argument'));
233+
})
234+
.catch(e => {
225235
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
226236
done();
227237
});
@@ -237,7 +247,11 @@ describe('optic-update fromParam tests', function(){
237247
}, {"column": "lastName", "type": "string", "nullable": true}];
238248
const planBuilderTemplate = op.fromParam('myDocs', 'qualifier', outputCols);
239249
const temp = {myDocs: rows};
240-
db.rows.query(planBuilderTemplate,null, temp).catch(e => {
250+
db.rows.query(planBuilderTemplate,null, temp)
251+
.then(function(response){
252+
done(new Error('Expecting an error to be thrown due to invalid row-col-types argument'));
253+
})
254+
.catch(e => {
241255
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
242256
done();
243257
});
@@ -255,10 +269,13 @@ describe('optic-update fromParam tests', function(){
255269
const planBuilderTemplate = op.fromParam('myDocs', null, outputCols);
256270
const temp = {myDocs: rows};
257271
db.rows.query(planBuilderTemplate,null, temp)
258-
.catch(e => {
259-
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
260-
done();
261-
});
272+
.then(function(response){
273+
done(new Error('Expecting an error to be thrown due to null value for non-nullable column'));
274+
})
275+
.catch(e => {
276+
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
277+
done();
278+
});
262279
} catch (e) {
263280
done();
264281
}
@@ -274,10 +291,13 @@ describe('optic-update fromParam tests', function(){
274291
const planBuilderTemplate = op.fromParam('myDocs', null, outputCols);
275292
const temp = {myDocs: rows};
276293
db.rows.query(planBuilderTemplate, null,temp)
277-
.catch(e => {
278-
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
279-
done();
280-
});
294+
.then(function(response){
295+
done(new Error('Expecting an error to be thrown due to extra non-defined column types'));
296+
})
297+
.catch(e => {
298+
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
299+
done();
300+
});
281301

282302
});
283303

@@ -290,10 +310,13 @@ describe('optic-update fromParam tests', function(){
290310
const planBuilderTemplate = op.fromParam('myDocs', null, outputCols);
291311
const temp = {bindingParam: rows};
292312
db.rows.query(planBuilderTemplate, null, temp)
293-
.catch(e => {
294-
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
295-
done();
296-
});
313+
.then(function(response){
314+
done(new Error('Expecting an error to be thrown due to non-consistent binding argument name'));
315+
})
316+
.catch(e => {
317+
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
318+
done();
319+
});
297320

298321
});
299322

@@ -311,10 +334,13 @@ describe('optic-update fromParam tests', function(){
311334
const planBuilderTemplate = op.fromParam('myDocs', null, outputCols);
312335
const temp = {myDocs: rows};
313336
db.rows.query(planBuilderTemplate, null, temp)
314-
.catch(e => {
315-
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
316-
done();
317-
});
337+
.then(function(response){
338+
done(new Error('Expecting an error to be thrown due to mismatch type'));
339+
})
340+
.catch(e => {
341+
e.toString().includes('Error: binding arguments /v1/rows: cannot process response with 500 status');
342+
done();
343+
});
318344

319345
});
320346

@@ -333,6 +359,7 @@ describe('optic-update fromParam tests', function(){
333359
const planBuilderTemplate = op.fromParam('myDocs', 1234, outputCols);
334360
const temp = {myDocs: rows};
335361
db.rows.query(planBuilderTemplate, null, temp);
362+
done(new Error('Expecting an error to be thrown due to invalid qualifier argument'));
336363
} catch (e) {
337364
e.toString().includes('Error: qualifier argument at 1 of PlanBuilder.fromParam() must be a XsString value');
338365
done();
@@ -353,11 +380,17 @@ describe('optic-update fromParam tests', function(){
353380
const planBuilderTemplate = op.fromParam('myDocs', null, outputCols);
354381
const temp = {myDocs: rows};
355382
db.rows.query(planBuilderTemplate, null, temp).then(res => {
356-
const rows = res.rows;
357-
rows[0].id.value.should.equal(1);
358-
rows[0].firstName.value.should.equal("firstName_1");
359-
rows[0].lastName.value.should.equal("lastName_1");
360-
done();
383+
try {
384+
const rows = res.rows;
385+
rows[0].id.value.should.equal(1);
386+
rows[0].firstName.value.should.equal("firstName_1");
387+
rows[0].lastName.value.should.equal("lastName_1");
388+
done();
389+
} catch (e) {
390+
done(e);
391+
}
392+
}).catch(e => {
393+
done(e);
361394
});
362395

363396
});

0 commit comments

Comments
 (0)