|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { DataPublicPluginStart } from '../../../../../src/plugins/data/public'; |
| 7 | +import { PPLAggsAutoSuggestTask } from './ppl_aggs_auto_suggest_task'; |
| 8 | + |
| 9 | +describe('PPLAutoSuggestTask', () => { |
| 10 | + let pplAutoSuggestTask: PPLAggsAutoSuggestTask; |
| 11 | + let mockSearchClient: DataPublicPluginStart['search']; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Mock the search client |
| 15 | + mockSearchClient = { |
| 16 | + search: jest.fn(), |
| 17 | + aggs: { |
| 18 | + calculateAutoTimeExpression: jest.fn(), |
| 19 | + }, |
| 20 | + }; |
| 21 | + pplAutoSuggestTask = new PPLAggsAutoSuggestTask(mockSearchClient); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return original input if PPL is empty', async () => { |
| 25 | + const input = { |
| 26 | + ppl: '', |
| 27 | + dataSourceId: 'test-source', |
| 28 | + }; |
| 29 | + |
| 30 | + const result = await pplAutoSuggestTask.execute(input); |
| 31 | + expect(result).toEqual(input); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return original input if PPL already has aggregation', async () => { |
| 35 | + const input = { |
| 36 | + ppl: 'source = test | stats count()', |
| 37 | + dataSourceId: 'test-source', |
| 38 | + }; |
| 39 | + |
| 40 | + const result = await pplAutoSuggestTask.execute(input); |
| 41 | + expect(result).toEqual(input); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should add simple count aggregation when no time field is provided', async () => { |
| 45 | + const input = { |
| 46 | + ppl: 'source = test', |
| 47 | + dataSourceId: 'test-source', |
| 48 | + }; |
| 49 | + |
| 50 | + const expected = { |
| 51 | + ppl: 'source = test | stats count()', |
| 52 | + dataSourceId: 'test-source', |
| 53 | + }; |
| 54 | + |
| 55 | + const result = await pplAutoSuggestTask.execute(input); |
| 56 | + expect(result).toEqual(expected); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should add time-based aggregation when time field is provided', async () => { |
| 60 | + const input = { |
| 61 | + ppl: 'source = test', |
| 62 | + dataSourceId: 'test-source', |
| 63 | + timeFiledName: 'timestamp', |
| 64 | + }; |
| 65 | + |
| 66 | + // Mock successful search response |
| 67 | + const mockResponse = { |
| 68 | + rawResponse: { |
| 69 | + total: 1, |
| 70 | + jsonData: [ |
| 71 | + { |
| 72 | + min: '2023-01-01', |
| 73 | + max: '2023-12-31', |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + mockSearchClient.search.mockReturnValue({ |
| 80 | + toPromise: () => Promise.resolve(mockResponse), |
| 81 | + }); |
| 82 | + mockSearchClient.aggs.calculateAutoTimeExpression.mockReturnValue('1d'); |
| 83 | + |
| 84 | + const expected = { |
| 85 | + ppl: 'source = test | stats count() by span(timestamp, 1d)', |
| 86 | + dataSourceId: 'test-source', |
| 87 | + timeFiledName: 'timestamp', |
| 88 | + }; |
| 89 | + |
| 90 | + const result = await pplAutoSuggestTask.execute(input); |
| 91 | + expect(result).toEqual(expected); |
| 92 | + expect(mockSearchClient.search).toHaveBeenCalledWith( |
| 93 | + { |
| 94 | + params: { |
| 95 | + body: { |
| 96 | + query: 'source = test | stats min(timestamp) as min, max(timestamp) as max', |
| 97 | + }, |
| 98 | + }, |
| 99 | + dataSourceId: 'test-source', |
| 100 | + }, |
| 101 | + { strategy: 'pplraw' } |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle empty search results for time-based queries', async () => { |
| 106 | + const input = { |
| 107 | + ppl: 'source = test', |
| 108 | + dataSourceId: 'test-source', |
| 109 | + timeFiledName: 'timestamp', |
| 110 | + }; |
| 111 | + |
| 112 | + // Mock empty search response |
| 113 | + const mockResponse = { |
| 114 | + rawResponse: { |
| 115 | + total: 0, |
| 116 | + jsonData: [], |
| 117 | + }, |
| 118 | + }; |
| 119 | + |
| 120 | + mockSearchClient.search.mockReturnValue({ |
| 121 | + toPromise: () => Promise.resolve(mockResponse), |
| 122 | + }); |
| 123 | + |
| 124 | + const expected = { |
| 125 | + ppl: 'source = test | stats count()', |
| 126 | + dataSourceId: 'test-source', |
| 127 | + timeFiledName: 'timestamp', |
| 128 | + }; |
| 129 | + |
| 130 | + const result = await pplAutoSuggestTask.execute(input); |
| 131 | + expect(result).toEqual(expected); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle null search results for time-based queries', async () => { |
| 135 | + const input = { |
| 136 | + ppl: 'source = test', |
| 137 | + dataSourceId: 'test-source', |
| 138 | + timeFiledName: 'timestamp', |
| 139 | + }; |
| 140 | + |
| 141 | + // Mock null search response |
| 142 | + const mockResponse = null; |
| 143 | + |
| 144 | + mockSearchClient.search.mockReturnValue({ |
| 145 | + toPromise: () => Promise.resolve(mockResponse), |
| 146 | + }); |
| 147 | + |
| 148 | + const expected = { |
| 149 | + ppl: 'source = test | stats count()', |
| 150 | + dataSourceId: 'test-source', |
| 151 | + timeFiledName: 'timestamp', |
| 152 | + }; |
| 153 | + |
| 154 | + const result = await pplAutoSuggestTask.execute(input); |
| 155 | + expect(result).toEqual(expected); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle search errors', async () => { |
| 159 | + const input = { |
| 160 | + ppl: 'source = test', |
| 161 | + dataSourceId: 'test-source', |
| 162 | + timeFiledName: 'timestamp', |
| 163 | + }; |
| 164 | + |
| 165 | + mockSearchClient.search.mockReturnValue({ |
| 166 | + toPromise: () => Promise.reject(new Error('Search failed')), |
| 167 | + }); |
| 168 | + |
| 169 | + const expected = { |
| 170 | + ...input, |
| 171 | + ppl: 'source = test | stats count()', |
| 172 | + }; |
| 173 | + |
| 174 | + const result = await pplAutoSuggestTask.execute(input); |
| 175 | + expect(result).toEqual(expected); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('isPPLHasAggregation', () => { |
| 179 | + it('should detect stats command with various spacing', () => { |
| 180 | + const testCases = [ |
| 181 | + 'source = test | stats count()', |
| 182 | + 'source = test |stats count()', |
| 183 | + 'source = test| stats count()', |
| 184 | + 'source = test|stats count()', |
| 185 | + ]; |
| 186 | + |
| 187 | + testCases.forEach((ppl) => { |
| 188 | + expect(pplAutoSuggestTask.isPPLHasAggregation(ppl)).toBeTruthy(); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should return false for queries without stats', () => { |
| 193 | + const ppl = 'source = test | where count > 0'; |
| 194 | + expect(pplAutoSuggestTask.isPPLHasAggregation(ppl)).toBeFalsy(); |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments