|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Config = require('../lib/Config'); |
| 4 | +const TestUtils = require('../lib/TestUtils'); |
| 5 | +const request = require('../lib/request'); |
| 6 | + |
| 7 | +let config; |
| 8 | + |
| 9 | +const masterKeyHeaders = { |
| 10 | + 'X-Parse-Application-Id': 'test', |
| 11 | + 'X-Parse-Rest-API-Key': 'rest', |
| 12 | + 'X-Parse-Master-Key': 'test', |
| 13 | + 'Content-Type': 'application/json', |
| 14 | +}; |
| 15 | + |
| 16 | +const masterKeyOptions = { |
| 17 | + headers: masterKeyHeaders, |
| 18 | + json: true, |
| 19 | +}; |
| 20 | + |
| 21 | +describe_only_db('mongo')('Parse.Query hint', () => { |
| 22 | + beforeEach(() => { |
| 23 | + config = Config.get('test'); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(async () => { |
| 27 | + await config.database.schemaCache.clear(); |
| 28 | + await TestUtils.destroyAllDataPermanently(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it('query find with hint string', async () => { |
| 32 | + const object = new TestObject(); |
| 33 | + await object.save(); |
| 34 | + |
| 35 | + const collection = await config.database.adapter._adaptiveCollection( |
| 36 | + 'TestObject' |
| 37 | + ); |
| 38 | + let explain = await collection._rawFind( |
| 39 | + { _id: object.id }, |
| 40 | + { explain: true } |
| 41 | + ); |
| 42 | + expect(explain.queryPlanner.winningPlan.stage).toBe('IDHACK'); |
| 43 | + explain = await collection._rawFind( |
| 44 | + { _id: object.id }, |
| 45 | + { hint: '_id_', explain: true } |
| 46 | + ); |
| 47 | + expect(explain.queryPlanner.winningPlan.stage).toBe('FETCH'); |
| 48 | + expect(explain.queryPlanner.winningPlan.inputStage.indexName).toBe('_id_'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('query find with hint object', async () => { |
| 52 | + const object = new TestObject(); |
| 53 | + await object.save(); |
| 54 | + |
| 55 | + const collection = await config.database.adapter._adaptiveCollection( |
| 56 | + 'TestObject' |
| 57 | + ); |
| 58 | + let explain = await collection._rawFind( |
| 59 | + { _id: object.id }, |
| 60 | + { explain: true } |
| 61 | + ); |
| 62 | + expect(explain.queryPlanner.winningPlan.stage).toBe('IDHACK'); |
| 63 | + explain = await collection._rawFind( |
| 64 | + { _id: object.id }, |
| 65 | + { hint: { _id: 1 }, explain: true } |
| 66 | + ); |
| 67 | + expect(explain.queryPlanner.winningPlan.stage).toBe('FETCH'); |
| 68 | + expect(explain.queryPlanner.winningPlan.inputStage.keyPattern).toEqual({ |
| 69 | + _id: 1, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('query aggregate with hint string', async () => { |
| 74 | + const object = new TestObject({ foo: 'bar' }); |
| 75 | + await object.save(); |
| 76 | + |
| 77 | + const collection = await config.database.adapter._adaptiveCollection( |
| 78 | + 'TestObject' |
| 79 | + ); |
| 80 | + let result = await collection.aggregate([{ $group: { _id: '$foo' } }], { |
| 81 | + explain: true, |
| 82 | + }); |
| 83 | + let { queryPlanner } = result[0].stages[0].$cursor; |
| 84 | + expect(queryPlanner.winningPlan.stage).toBe('COLLSCAN'); |
| 85 | + |
| 86 | + result = await collection.aggregate([{ $group: { _id: '$foo' } }], { |
| 87 | + hint: '_id_', |
| 88 | + explain: true, |
| 89 | + }); |
| 90 | + queryPlanner = result[0].stages[0].$cursor.queryPlanner; |
| 91 | + expect(queryPlanner.winningPlan.stage).toBe('FETCH'); |
| 92 | + expect(queryPlanner.winningPlan.inputStage.indexName).toBe('_id_'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('query aggregate with hint object', async () => { |
| 96 | + const object = new TestObject({ foo: 'bar' }); |
| 97 | + await object.save(); |
| 98 | + |
| 99 | + const collection = await config.database.adapter._adaptiveCollection( |
| 100 | + 'TestObject' |
| 101 | + ); |
| 102 | + let result = await collection.aggregate([{ $group: { _id: '$foo' } }], { |
| 103 | + explain: true, |
| 104 | + }); |
| 105 | + let { queryPlanner } = result[0].stages[0].$cursor; |
| 106 | + expect(queryPlanner.winningPlan.stage).toBe('COLLSCAN'); |
| 107 | + |
| 108 | + result = await collection.aggregate([{ $group: { _id: '$foo' } }], { |
| 109 | + hint: { _id: 1 }, |
| 110 | + explain: true, |
| 111 | + }); |
| 112 | + queryPlanner = result[0].stages[0].$cursor.queryPlanner; |
| 113 | + expect(queryPlanner.winningPlan.stage).toBe('FETCH'); |
| 114 | + expect(queryPlanner.winningPlan.inputStage.keyPattern).toEqual({ _id: 1 }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('query find with hint (rest)', async () => { |
| 118 | + const object = new TestObject(); |
| 119 | + await object.save(); |
| 120 | + let options = Object.assign({}, masterKeyOptions, { |
| 121 | + url: Parse.serverURL + '/classes/TestObject', |
| 122 | + qs: { |
| 123 | + explain: true, |
| 124 | + }, |
| 125 | + }); |
| 126 | + let response = await request(options); |
| 127 | + let explain = response.data.results; |
| 128 | + expect(explain.queryPlanner.winningPlan.inputStage.stage).toBe('COLLSCAN'); |
| 129 | + |
| 130 | + options = Object.assign({}, masterKeyOptions, { |
| 131 | + url: Parse.serverURL + '/classes/TestObject', |
| 132 | + qs: { |
| 133 | + explain: true, |
| 134 | + hint: '_id_', |
| 135 | + }, |
| 136 | + }); |
| 137 | + response = await request(options); |
| 138 | + explain = response.data.results; |
| 139 | + expect( |
| 140 | + explain.queryPlanner.winningPlan.inputStage.inputStage.indexName |
| 141 | + ).toBe('_id_'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('query aggregate with hint (rest)', async () => { |
| 145 | + const object = new TestObject({ foo: 'bar' }); |
| 146 | + await object.save(); |
| 147 | + let options = Object.assign({}, masterKeyOptions, { |
| 148 | + url: Parse.serverURL + '/aggregate/TestObject', |
| 149 | + qs: { |
| 150 | + explain: true, |
| 151 | + group: JSON.stringify({ objectId: '$foo' }), |
| 152 | + }, |
| 153 | + }); |
| 154 | + let response = await request(options); |
| 155 | + let { queryPlanner } = response.data.results[0].stages[0].$cursor; |
| 156 | + expect(queryPlanner.winningPlan.stage).toBe('COLLSCAN'); |
| 157 | + |
| 158 | + options = Object.assign({}, masterKeyOptions, { |
| 159 | + url: Parse.serverURL + '/aggregate/TestObject', |
| 160 | + qs: { |
| 161 | + explain: true, |
| 162 | + hint: '_id_', |
| 163 | + group: JSON.stringify({ objectId: '$foo' }), |
| 164 | + }, |
| 165 | + }); |
| 166 | + response = await request(options); |
| 167 | + queryPlanner = response.data.results[0].stages[0].$cursor.queryPlanner; |
| 168 | + expect(queryPlanner.winningPlan.inputStage.keyPattern).toEqual({ _id: 1 }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments