|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { retrieveQueryById } from './QueryUtils'; |
| 7 | +import { mockQueries } from '../../test/mocks/mockQueries'; |
| 8 | +import { testQueryParams } from '../../test/mocks/testConstants'; |
| 9 | + |
| 10 | +jest.unmock('../../common/utils/QueryUtils'); |
| 11 | + |
| 12 | +describe('retrieveQueryById - Fetch Query Record by ID from API', () => { |
| 13 | + const mockCore = { |
| 14 | + http: { |
| 15 | + get: jest.fn(), |
| 16 | + post: jest.fn(), |
| 17 | + }, |
| 18 | + }; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + const testStart = testQueryParams?.start; |
| 25 | + const testEnd = testQueryParams?.end; |
| 26 | + const testId = testQueryParams?.id; |
| 27 | + const mockQuery = mockQueries[0]; |
| 28 | + |
| 29 | + const mockResponse = { |
| 30 | + response: { |
| 31 | + top_queries: [mockQuery], |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + it('should make three GET requests to fetch different query records', async () => { |
| 36 | + mockCore.http.get.mockResolvedValue(mockResponse); |
| 37 | + |
| 38 | + await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 39 | + |
| 40 | + expect(mockCore.http.get).toHaveBeenCalledTimes(3); |
| 41 | + expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/latency', { |
| 42 | + query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined }, |
| 43 | + }); |
| 44 | + expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/cpu', { |
| 45 | + query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined }, |
| 46 | + }); |
| 47 | + expect(mockCore.http.get).toHaveBeenCalledWith('/api/top_queries/memory', { |
| 48 | + query: { from: testStart, to: testEnd, id: testId, dataSourceId: undefined }, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return the valid query result', async () => { |
| 53 | + mockCore.http.get.mockResolvedValue(mockResponse); |
| 54 | + |
| 55 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 56 | + |
| 57 | + expect(result).toEqual(mockResponse.response.top_queries[0]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return null if no queries are found', async () => { |
| 61 | + mockCore.http.get.mockResolvedValue({ response: { top_queries: [] } }); |
| 62 | + |
| 63 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 64 | + |
| 65 | + expect(result).toBeNull(); |
| 66 | + }); |
| 67 | + it('should return null if API response is missing the response field', async () => { |
| 68 | + mockCore.http.get.mockResolvedValue({}); |
| 69 | + |
| 70 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 71 | + |
| 72 | + expect(result).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return null if API response contains an unexpected structure', async () => { |
| 76 | + mockCore.http.get.mockResolvedValue({ unexpectedKey: {} }); |
| 77 | + |
| 78 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 79 | + |
| 80 | + expect(result).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return null if API request fails', async () => { |
| 84 | + mockCore.http.get.mockRejectedValue(new Error('API error')); |
| 85 | + |
| 86 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 87 | + |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should handle cases where API returns an empty object instead of expected response structure', async () => { |
| 92 | + mockCore.http.get.mockResolvedValue({}); |
| 93 | + |
| 94 | + const result = await retrieveQueryById(mockCore, undefined, testStart, testEnd, testId); |
| 95 | + |
| 96 | + expect(result).toBeNull(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments