Skip to content

Commit e130803

Browse files
fixup tests
1 parent 842e28e commit e130803

File tree

1 file changed

+99
-22
lines changed

1 file changed

+99
-22
lines changed

test/manual/search-index-management.prose.test.ts

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { Readable } from 'stream';
44
import { clearTimeout, setTimeout as setTimeoutCb } from 'timers';
55
import { setInterval } from 'timers/promises';
66

7-
import { type Collection, type Document, type MongoClient, ObjectId } from '../mongodb';
7+
import { type Collection, type Document, isSuperset, type MongoClient, ObjectId } from '../mongodb';
88

99
class TimeoutController extends AbortController {
1010
timeoutId: NodeJS.Timeout;
11-
constructor(private timeoutMS: number) {
11+
constructor(timeoutMS: number) {
1212
super();
1313

1414
this.timeoutId = setTimeoutCb(() => {
@@ -39,20 +39,24 @@ describe('Index Management Prose Tests', function () {
3939
let collection: Collection;
4040

4141
/**
42-
* waits until the search indexes for `collection` satisfy `predicate`, optionally pre-filtering
42+
* waits until the search indexes for `collection` satisfy `predicate`.
43+
*
44+
* This optionally pre-filtering
4345
* for indexes with name = `indexName`
4446
*/
4547
function waitForIndexes({
4648
predicate,
47-
indexName
49+
indexNames
4850
}: {
4951
predicate: (arg0: Array<Document>) => boolean;
50-
indexName?: string;
52+
indexNames: string | string[];
5153
}): Promise<Array<Document>> {
54+
const names = new Set([indexNames].flat());
5255
return Readable.from(
5356
setInterval(5000, undefined, { signal: timeoutController.signal, ref: false })
5457
)
55-
.map(() => collection.listSearchIndexes(indexName).toArray())
58+
.map(() => collection.listSearchIndexes().toArray())
59+
.map((indexes: Document[]) => indexes.filter(index => names.has(index.name)))
5660
.find(predicate);
5761
}
5862

@@ -71,6 +75,7 @@ describe('Index Management Prose Tests', function () {
7175
client = this.configuration.newClient();
7276
await client.connect();
7377

78+
// Create a collection with the "create" command using a randomly generated name (referred to as coll0).
7479
collection = await client.db('node-test').createCollection(new ObjectId().toHexString());
7580

7681
timeoutController = new TimeoutController(60 * 1000 * 4);
@@ -86,20 +91,31 @@ describe('Index Management Prose Tests', function () {
8691
'Case 1: Driver can successfully create and list search indexes',
8792
metadata,
8893
async function () {
94+
// Create a new search index on coll0 with the createSearchIndex helper. Use the following definition:
95+
// {
96+
// name: 'test-search-index',
97+
// definition: {
98+
// mappings: { dynamic: false }
99+
// }
100+
// }
89101
const name = await collection.createSearchIndex({
90102
name: 'test-search-index',
91103
definition: {
92104
mappings: { dynamic: false }
93105
}
94106
});
95107

108+
// Assert that the command returns the name of the index: "test-search-index".
96109
expect(name).to.equal('test-search-index');
97110

111+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until the following condition is satisfied and store the value in a variable index:
112+
// 1. An index with the name of test-search-index is present and the index has a field queryable with a value of true.
98113
const [index] = await waitForIndexes({
99114
predicate: indexes => indexes.every(index => index.queryable),
100-
indexName: 'test-search-index'
115+
indexNames: 'test-search-index'
101116
});
102117

118+
// Assert that index has a property latestDefinition whose value is { 'mappings': { 'dynamic': false } }
103119
expect(index).to.exist;
104120
expect(index)
105121
.to.have.property('latestDefinition')
@@ -111,6 +127,19 @@ describe('Index Management Prose Tests', function () {
111127
'Case 2: Driver can successfully create multiple indexes in batch',
112128
metadata,
113129
async function () {
130+
// Create two new search indexes on coll0 with the createSearchIndexes helper. Use the following definitions when creating the indexes. These definitions are referred to as indexDefinitions.
131+
// {
132+
// name: 'test-search-index-1',
133+
// definition: {
134+
// mappings: { dynamic: false }
135+
// }
136+
// }
137+
// {
138+
// name: 'test-search-index-2',
139+
// definition: {
140+
// mappings: { dynamic: false }
141+
// }
142+
// }
114143
const indexDefinitions = [
115144
{
116145
name: 'test-search-index-1',
@@ -127,72 +156,118 @@ describe('Index Management Prose Tests', function () {
127156
];
128157

129158
const names = await collection.createSearchIndexes(indexDefinitions);
159+
160+
// Assert that the command returns an array containing the new indexes' names: ["test-search-index-1", "test-search-index-2"].
130161
expect(names).to.deep.equal(['test-search-index-1', 'test-search-index-2']);
131162

163+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until the following condition is satisfied.
164+
// 1. An index with the name of test-search-index-1 is present and index has a field queryable with the value of true. Store result in index1.
165+
// 2. An index with the name of test-search-index-2 is present and index has a field queryable with the value of true. Store result in index2.
132166
const indexes = await waitForIndexes({
133-
predicate: indexes => indexes.every(index => index.queryable)
167+
predicate: indexes => indexes.every(index => index.queryable),
168+
indexNames: ['test-search-index-1', 'test-search-index-2']
134169
});
135170

136-
for (const indexDescription of indexDefinitions) {
137-
const index = indexes.find(({ name }) => name === indexDescription.name);
138-
expect(index, `expected ${indexDescription.name} to exist`).to.exist;
171+
const index1 = indexes.find(({ name }) => name === 'test-search-index-1');
172+
const index2 = indexes.find(({ name }) => name === 'test-search-index-2');
139173

140-
expect(index)
141-
.to.have.property('latestDefinition')
142-
.to.deep.equal({ mappings: { dynamic: false } });
143-
}
174+
// Assert that index1 and index2 have the property latestDefinition whose value is { "mappings" : { "dynamic" : false } }
175+
expect(index1)
176+
.to.have.property('latestDefinition')
177+
.to.deep.equal({ mappings: { dynamic: false } });
178+
expect(index2)
179+
.to.have.property('latestDefinition')
180+
.to.deep.equal({ mappings: { dynamic: false } });
144181
}
145182
);
146183

147184
it('Case 3: Driver can successfully drop search indexes', metadata, async function () {
185+
// Create a new search index on coll0 with the following definition:
186+
// {
187+
// name: 'test-search-index',
188+
// definition: {
189+
// mappings: { dynamic: false }
190+
// }
191+
// }
148192
const name = await collection.createSearchIndex({
149193
name: 'test-search-index',
150194
definition: {
151195
mappings: { dynamic: false }
152196
}
153197
});
154198

199+
// Assert that the command returns the name of the index: "test-search-index".
155200
expect(name).to.equal('test-search-index');
156201

202+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until the following condition is satisfied:
203+
// 1. An index with the name of test-search-index is present and index has a field queryable with the value of true.
157204
await waitForIndexes({
158205
predicate: indexes => indexes.every(index => index.queryable),
159-
indexName: 'test-search-index'
206+
indexNames: 'test-search-index'
160207
});
161208

209+
// Run a dropSearchIndex on coll0, using test-search-index for the name.
162210
await collection.dropSearchIndex('test-search-index');
163211

212+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until listSearchIndexes returns an empty array.
213+
// This test fails if it times out waiting for the deletion to succeed.
164214
const indexes = await waitForIndexes({
165215
predicate: indexes => indexes.length === 0,
166-
indexName: 'test-search-index'
216+
indexNames: 'test-search-index'
167217
});
168218

169219
expect(indexes).to.deep.equal([]);
170220
});
171221

172222
it('Case 4: Driver can update a search index', metadata, async function () {
223+
// Create a new search index on coll0 with the following definition:
224+
// {
225+
// name: 'test-search-index',
226+
// definition: {
227+
// mappings: { dynamic: false }
228+
// }
229+
// }
173230
const name = await collection.createSearchIndex({
174231
name: 'test-search-index',
175232
definition: {
176233
mappings: { dynamic: false }
177234
}
178235
});
179236

237+
// Assert that the command returns the name of the index: "test-search-index".
180238
expect(name).to.equal('test-search-index');
181239

240+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until the following condition is satisfied:
241+
// 1. An index with the name of test-search-index is present and index has a field queryable with the value of true.
182242
await waitForIndexes({
183243
predicate: indexes => indexes.every(index => index.queryable),
184-
indexName: 'test-search-index'
244+
indexNames: 'test-search-index'
185245
});
186246

247+
// Run a updateSearchIndex on coll0, using the following definition.
248+
// {
249+
// name: 'test-search-index',
250+
// definition: {
251+
// mappings: { dynamic: true }
252+
// }
253+
// }
187254
await collection.updateSearchIndex('test-search-index', { mappings: { dynamic: true } });
188255

189-
const [updatedIndex] = await waitForIndexes({
256+
// Assert that the command does not error and the server responds with a success.
257+
// The above command throws on failure.
258+
259+
// Run coll0.listSearchIndexes() repeatedly every 5 seconds until the following condition is satisfied:
260+
// 1. An index with the name of test-search-index is present. This index is referred to as index.
261+
// 2. The index has a field queryable with a value of true and has a field status with the value of READY.
262+
const [index2] = await waitForIndexes({
190263
predicate: indexes => indexes.every(index => index.queryable && index.status === 'READY'),
191-
indexName: 'test-search-index'
264+
indexNames: 'test-search-index'
192265
});
193266

194-
expect(updatedIndex).to.have.property('name', 'test-search-index');
195-
expect(updatedIndex)
267+
// Assert that an index is present with the name test-search-index and the definition has a
268+
// property latestDefinition whose value is { 'mappings': { 'dynamic': true } }.
269+
expect(index2).to.have.property('name', 'test-search-index');
270+
expect(index2)
196271
.to.have.property('latestDefinition')
197272
.to.deep.equal({ mappings: { dynamic: true } });
198273
});
@@ -201,8 +276,10 @@ describe('Index Management Prose Tests', function () {
201276
'Case 5: `dropSearchIndex` suppresses namespace not found errors',
202277
metadata,
203278
async function () {
279+
// Create a driver-side collection object for a randomly generated collection name. Do not create this collection on the server.
204280
const collection = await client.db('node-test').collection(new ObjectId().toHexString());
205281

282+
// Run a dropSearchIndex command and assert that no error is thrown.
206283
await collection.dropSearchIndex('test-search-index');
207284
}
208285
);

0 commit comments

Comments
 (0)