|
3 | 3 | /** |
4 | 4 | * Test dependencies. |
5 | 5 | */ |
6 | | -const sinon = require('sinon'); |
7 | 6 | const start = require('./common'); |
8 | 7 |
|
9 | 8 | const CastError = require('../lib/error/cast'); |
10 | 9 | const assert = require('assert'); |
| 10 | +const model = require('../lib/model'); |
11 | 11 | const { once } = require('events'); |
12 | 12 | const random = require('./util').random; |
13 | 13 | const util = require('./util'); |
14 | | -const model = require('../lib/model'); |
| 14 | +const sinon = require('sinon'); |
15 | 15 |
|
16 | 16 | const mongoose = start.mongoose; |
17 | 17 | const Schema = mongoose.Schema; |
@@ -8686,6 +8686,33 @@ describe('Model', function() { |
8686 | 8686 | assert.equal(doc.name, undefined); |
8687 | 8687 | }); |
8688 | 8688 | }); |
| 8689 | + |
| 8690 | + it('createSearchIndexes creates an index for each search index in schema (gh-15465)', async function() { |
| 8691 | + const sinon = require('sinon'); |
| 8692 | + const schema = new mongoose.Schema({ |
| 8693 | + name: String, |
| 8694 | + description: String |
| 8695 | + }); |
| 8696 | + |
| 8697 | + schema.searchIndex({ name: 'test', definition: { mappings: { dynamic: true } } }); |
| 8698 | + |
| 8699 | + const TestModel = db.model('Test', schema); |
| 8700 | + |
| 8701 | + const createSearchIndexStub = sinon.stub(TestModel, 'createSearchIndex').resolves({ acknowledged: true }); |
| 8702 | + |
| 8703 | + try { |
| 8704 | + const results = await TestModel.createSearchIndexes(); |
| 8705 | + |
| 8706 | + assert.equal(createSearchIndexStub.callCount, 1); |
| 8707 | + assert.equal(results.length, 1); |
| 8708 | + assert.deepEqual(results, [{ acknowledged: true }]); |
| 8709 | + |
| 8710 | + // Verify that createSearchIndex was called with the correct arguments |
| 8711 | + assert.ok(createSearchIndexStub.firstCall.calledWithMatch({ name: 'test', definition: { mappings: { dynamic: true } } })); |
| 8712 | + } finally { |
| 8713 | + sinon.restore(); |
| 8714 | + } |
| 8715 | + }); |
8689 | 8716 | }); |
8690 | 8717 |
|
8691 | 8718 |
|
|
0 commit comments