Skip to content

Commit 6fb52f3

Browse files
committed
More fixes: make sure done() called once and only once for async operations. Change annTopK tests to use await/rejects idiom which is easier to understand. Skip the cloud test that tries to connect to support.beta.marklogic.cloud. That DNS name is not resolving as of today.
1 parent beb7957 commit 6fb52f3

File tree

5 files changed

+164
-126
lines changed

5 files changed

+164
-126
lines changed

test-basic/annTopK.js

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,99 +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-
done(new Error('Expecting an error to be thrown due to invalid key in options argument'));
93-
} catch(error){
94-
assert(error.message.toString().includes('options argument at 4 of PlanModifyPlan.annTopK() has invalid key- invalid'))
95-
done();
96-
}
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+
);
9783
});
9884

99-
function verifyResults(rows, done){
100-
try {
101-
assert(rows.length === 2, 'Expecting both rows in the view to be returned.');
102-
assert(rows[0].name.value === 'Alice');
103-
assert(rows[0].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
104-
assert(rows[1].name.value === 'Bob');
105-
assert(rows[1].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
106-
done();
107-
} catch (error){
108-
done(error)
109-
}
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+
});
110100
}
111101
});

test-basic/cloud_authentication-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ describe('cloud-authentication tests', function() {
3636
}
3737
});
3838

39-
it('should throw error with invalid apiKey.', function (done) {
39+
// skip for now, support.beta.marklogic.cloud is not working for me. Not sure if this should be in test suite anyway.
40+
it.skip('should throw error with invalid apiKey.', function (done) {
41+
this.timeout(10000);
4042
let db = marklogic.createDatabaseClient({
4143
host: 'support.beta.marklogic.cloud',
4244
authType: 'cloud',
@@ -47,7 +49,6 @@ describe('cloud-authentication tests', function() {
4749
try {
4850
// Also verified that it throws 'Error: User's API Key is expired.' when API key has expired a few seconds ago.
4951
expect(()=>db.documents.write(writeObject).throws(Error('API Key is not valid.')));
50-
done();
5152
} catch (error) {
5253
done(error);
5354
}

test-basic/docColTypes-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ describe('optic-update docColTypes tests', function() {
4646
const plan = op.fromParam('bindingParam', null, op.docColTypes(op.col('uri')));
4747
const temp = {bindingParam: rows};
4848
db.rows.query(plan, null, temp);
49+
done(new Error("Expected an error to be thrown due to only 1 argument to fromParam"));
4950
} catch (e) {
5051
e.toString().includes('Error: PlanBuilder.docColTypes takes a maximum of 0 arguments but received: 1');
5152
done();

test-basic/documents-data-movement-queryAll.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ describe('data movement queryAll', function() {
4949
.result(function(response){
5050
done();
5151
})
52-
.catch(err=> done(err))
5352
.catch(done);
5453
}));
5554

5655
it('queryAll should throw error if queryType is not cts', function(done){
5756
try{
5857
const query = q.directory('/test/dataMovement/requests/queryAll/');
5958
dbWriter.documents.queryAll(query);
59+
done(new Error('Expected an error to be thrown because query is not a cts query.'));
6060
} catch(err){
6161
err.toString().should.equal('Error: Query needs to be a cts query.');
6262
done();
@@ -94,12 +94,12 @@ describe('data movement queryAll', function() {
9494
});
9595

9696
it('queryAll should throw error if no query is provided', function (done){
97-
9897
try{
9998
dbWriter.documents.queryAll();
99+
return done(new Error('Expected an error to be thrown because no query was provided'));
100100
} catch(err){
101101
err.toString().should.equal('Error: Query cannot be null or undefined.');
102-
done();
102+
return done();
103103
}
104104
});
105105

@@ -118,14 +118,14 @@ describe('data movement queryAll', function() {
118118
});
119119

120120
it('queryAll should throw error with batchSize=100001', function (done){
121-
122121
try{
123122
dbWriter.documents.queryAll(query, {
124123
batchSize:100001
125124
});
125+
return done(new Error('Expected an error to be thrown because batchSize greater than 100000'));
126126
} catch(err){
127127
err.toString().should.equal('Error: batchSize cannot be greater than 100000');
128-
done();
128+
return done();
129129
}
130130
});
131131

@@ -259,14 +259,14 @@ describe('data movement queryAll', function() {
259259
});
260260

261261
it('queryAll should throw error with consistentSnapshot as Integer', function (done){
262-
263262
try{
264263
dbWriter.documents.queryAll(query, {
265264
consistentSnapshot: 1
266265
});
266+
return done(new Error('Expected an error to be thrown because consistentSnapshot is invalid'));
267267
} catch(err){
268268
err.toString().should.equal('Error: consistentSnapshot needs to be a boolean or DatabaseClient.Timestamp object.');
269-
done();
269+
return done();
270270
}
271271
});
272272
});

0 commit comments

Comments
 (0)