Skip to content

Commit 51e1678

Browse files
add tests that enforce default behavior of index listing functions
1 parent 37aa7d1 commit 51e1678

File tree

1 file changed

+120
-2
lines changed

1 file changed

+120
-2
lines changed

test/integration/index_management.test.ts

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Indexes', function () {
7070
});
7171

7272
it('shouldCorrectlyHandleMultipleColumnIndexes', async function () {
73-
await collection.insert({ a: 1 });
73+
await collection.insertOne({ a: 1 });
7474

7575
const indexName = await db.createIndex(
7676
collection.collectionName,
@@ -99,6 +99,124 @@ describe('Indexes', function () {
9999
);
100100
});
101101

102+
describe('Collection.indexes()', function () {
103+
beforeEach(() => collection.createIndex({ age: 1 }));
104+
afterEach(() => collection.dropIndexes());
105+
106+
context('when `full` is not provided', () => {
107+
it('returns an array of indexes', async function () {
108+
const indexes = await collection.indexes();
109+
expect(indexes).to.be.a('array');
110+
});
111+
});
112+
113+
context('when `full` is set to `true`', () => {
114+
it('returns an array of indexes', async function () {
115+
const indexes = await collection.indexes({ full: true });
116+
expect(indexes).to.be.a('array');
117+
});
118+
});
119+
120+
context('when `full` is set to `false`', () => {
121+
it('returns a document mapping key to index definition', async function () {
122+
const indexes = await collection.indexes({ full: false });
123+
expect(indexes).to.be.a('object');
124+
expect(indexes)
125+
.to.have.property('age_1')
126+
.to.deep.equal([['age', 1]]);
127+
});
128+
});
129+
});
130+
131+
describe('Collection.indexInformation()', function () {
132+
beforeEach(() => collection.createIndex({ age: 1 }));
133+
afterEach(() => collection.dropIndexes());
134+
135+
context('when `full` is not provided', () => {
136+
it('defaults to `false` and returns a document', async function () {
137+
const indexes = await collection.indexInformation();
138+
expect(indexes).to.be.a('object');
139+
expect(indexes)
140+
.to.have.property('age_1')
141+
.to.deep.equal([['age', 1]]);
142+
});
143+
});
144+
145+
context('when `full` is set to `true`', () => {
146+
it('returns an array of indexes', async function () {
147+
const indexes = await collection.indexInformation({ full: true });
148+
expect(indexes).to.be.a('array');
149+
});
150+
});
151+
152+
context('when `full` is set to `false`', () => {
153+
it('returns a document mapping key to index definition', async function () {
154+
const indexes = await collection.indexInformation({ full: false });
155+
expect(indexes).to.be.a('object');
156+
expect(indexes)
157+
.to.have.property('age_1')
158+
.to.deep.equal([['age', 1]]);
159+
});
160+
});
161+
});
162+
163+
describe('Collection.indexExists()', function () {
164+
beforeEach(() => collection.createIndex({ age: 1 }));
165+
afterEach(() => collection.dropIndexes());
166+
167+
context('when provided a string index name', () => {
168+
it('returns true when the index exists', async () => {
169+
expect(await collection.indexExists('age_1')).to.be.true;
170+
});
171+
172+
it('returns false when the index does not exist', async () => {
173+
expect(await collection.indexExists('name_1')).to.be.false;
174+
});
175+
});
176+
177+
context('when provided an array of index names', () => {
178+
it('returns true when all indexes exists', async () => {
179+
expect(await collection.indexExists(['age_1'])).to.be.true;
180+
});
181+
182+
it('returns false when the none of the indexes exist', async () => {
183+
expect(await collection.indexExists(['name_1'])).to.be.false;
184+
});
185+
186+
it('returns false when only some of hte indexes exist', async () => {
187+
expect(await collection.indexExists(['name_1', 'age_1'])).to.be.false;
188+
});
189+
});
190+
context('when `full` is true', () => {
191+
// These tests are broken!
192+
it('returns true when all indexes exists', async () => {
193+
expect(await collection.indexExists(['age_1'], { full: true })).to.be.true;
194+
});
195+
196+
it('returns false when the none of the indexes exist', async () => {
197+
expect(await collection.indexExists(['name_1'], { full: true })).to.be.false;
198+
});
199+
200+
it('returns false when only some of hte indexes exist', async () => {
201+
expect(await collection.indexExists(['name_1', 'age_1'], { full: true })).to.be.false;
202+
});
203+
});
204+
205+
context('when `full` is false', () => {
206+
it('returns true when all indexes exists', async () => {
207+
expect(await collection.indexExists(['age_1'], { full: false })).to.be.true;
208+
});
209+
210+
it('returns false when the none of the indexes exist', async () => {
211+
expect(await collection.indexExists(['name_1'], { full: false })).to.be.false;
212+
});
213+
214+
it('returns false when only some of hte indexes exist', async () => {
215+
expect(await collection.indexExists(['name_1', 'age_1'], { full: false })).to.be.false;
216+
});
217+
});
218+
});
219+
102220
it('shouldCorrectlyHandleUniqueIndex', async function () {
103221
await db.createCollection('test_unique_index');
104222
await db.createIndex(collection.collectionName, 'hello', this.configuration.writeConcernMax());
@@ -529,7 +647,7 @@ describe('Indexes', function () {
529647
async function () {
530648
await collection.createIndex({ 'a.one': 1, 'a.two': 1 }, { name: 'n1', sparse: false });
531649

532-
const err = collection
650+
const err = await collection
533651
.createIndex({ 'a.one': 1, 'a.two': 1 }, { name: 'n2', sparse: true })
534652
.catch(e => e);
535653
expect(err).to.be.instanceOf(Error).to.have.property('code', 85);

0 commit comments

Comments
 (0)