|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import * as sourceModule from '@/source'; |
| 4 | + |
| 5 | +import * as metadataModule from '../useMetadata'; |
| 6 | +import { |
| 7 | + optimizeTimestampValueExpression, |
| 8 | + useOptimizedTimestampValueExpression, |
| 9 | +} from '../useOptimizedTimestampValueExpression'; |
| 10 | + |
| 11 | +// Mock the dependencies |
| 12 | +jest.mock('@/layout', () => ({ |
| 13 | + withAppNav: (component: any) => component, |
| 14 | +})); |
| 15 | + |
| 16 | +describe('useOptimizedTimestampValueExpression', () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('optimizeTimestampValueExpression', () => { |
| 22 | + const testCases = [ |
| 23 | + { |
| 24 | + timestampValueExpression: 'Timestamp', |
| 25 | + primaryKey: 'Timestamp', |
| 26 | + expected: 'Timestamp', |
| 27 | + }, |
| 28 | + { |
| 29 | + timestampValueExpression: 'Timestamp', |
| 30 | + primaryKey: undefined, |
| 31 | + expected: 'Timestamp', |
| 32 | + }, |
| 33 | + { |
| 34 | + timestampValueExpression: 'Timestamp', |
| 35 | + primaryKey: '', |
| 36 | + expected: 'Timestamp', |
| 37 | + }, |
| 38 | + { |
| 39 | + // Traces Table |
| 40 | + timestampValueExpression: 'Timestamp', |
| 41 | + primaryKey: 'ServiceName, SpanName, toDateTime(Timestamp)', |
| 42 | + expected: 'Timestamp', |
| 43 | + }, |
| 44 | + { |
| 45 | + // Optimized Traces Table |
| 46 | + timestampValueExpression: 'Timestamp', |
| 47 | + primaryKey: |
| 48 | + 'toStartOfHour(Timestamp), ServiceName, SpanName, toDateTime(Timestamp)', |
| 49 | + expected: 'Timestamp, toStartOfHour(Timestamp)', |
| 50 | + }, |
| 51 | + { |
| 52 | + // Unsupported for now as it's not a great primary key, want to just |
| 53 | + // use default behavior for this |
| 54 | + timestampValueExpression: 'Timestamp', |
| 55 | + primaryKey: 'toDateTime(Timestamp), ServiceName, SpanName, Timestamp', |
| 56 | + expected: 'Timestamp', |
| 57 | + }, |
| 58 | + { |
| 59 | + // Inverted primary key order, we should not try to optimize this |
| 60 | + timestampValueExpression: 'Timestamp', |
| 61 | + primaryKey: |
| 62 | + 'ServiceName, toDateTime(Timestamp), SeverityText, toStartOfHour(Timestamp)', |
| 63 | + expected: 'Timestamp', |
| 64 | + }, |
| 65 | + { |
| 66 | + timestampValueExpression: 'Timestamp', |
| 67 | + primaryKey: 'toStartOfHour(Timestamp), other_column, Timestamp', |
| 68 | + expected: 'Timestamp, toStartOfHour(Timestamp)', |
| 69 | + }, |
| 70 | + { |
| 71 | + // When the user has already manually configured an optimized timestamp value expression |
| 72 | + timestampValueExpression: ' toStartOfHour(Timestamp), Timestamp', |
| 73 | + primaryKey: 'toStartOfHour(Timestamp), other_column, Timestamp', |
| 74 | + expected: ' toStartOfHour(Timestamp), Timestamp', |
| 75 | + }, |
| 76 | + { |
| 77 | + timestampValueExpression: 'Timestamp', |
| 78 | + primaryKey: |
| 79 | + 'toStartOfInterval(Timestamp, INTERVAL 1 HOUR), other_column, Timestamp', |
| 80 | + expected: 'Timestamp, toStartOfInterval(Timestamp, INTERVAL 1 HOUR)', |
| 81 | + }, |
| 82 | + { |
| 83 | + // test variation of toUnixTimestamp |
| 84 | + timestampValueExpression: 'Timestamp', |
| 85 | + primaryKey: |
| 86 | + 'toStartOfMinute(Timestamp), user_id, status, toUnixTimestamp64Nano(Timestamp)', |
| 87 | + expected: 'Timestamp, toStartOfMinute(Timestamp)', |
| 88 | + }, |
| 89 | + { |
| 90 | + // TODO TimestampTime is not matched since it is not in the timestampValueExpression |
| 91 | + timestampValueExpression: 'Timestamp', |
| 92 | + primaryKey: |
| 93 | + 'toStartOfMinute(TimestampTime), user_id, status, Timestamp', |
| 94 | + expected: 'Timestamp', |
| 95 | + }, |
| 96 | + ] as const; |
| 97 | + |
| 98 | + it.each(testCases)( |
| 99 | + 'should return optimized expression $expected for original expression $timestampValueExpression and primary key $primaryKey', |
| 100 | + ({ timestampValueExpression, primaryKey, expected }) => { |
| 101 | + const actual = optimizeTimestampValueExpression( |
| 102 | + timestampValueExpression, |
| 103 | + primaryKey, |
| 104 | + ); |
| 105 | + |
| 106 | + expect(actual).toBe(expected); |
| 107 | + }, |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('useOptimizedTimestampValueExpression', () => { |
| 112 | + it('should handle null source ungracefully', () => { |
| 113 | + jest.spyOn(sourceModule, 'useSource').mockReturnValue({ |
| 114 | + data: null, |
| 115 | + isLoading: false, |
| 116 | + error: null, |
| 117 | + } as any); |
| 118 | + |
| 119 | + jest.spyOn(metadataModule, 'useTableMetadata').mockReturnValue({ |
| 120 | + data: null, |
| 121 | + isLoading: false, |
| 122 | + error: null, |
| 123 | + } as any); |
| 124 | + |
| 125 | + const { result } = renderHook(() => |
| 126 | + useOptimizedTimestampValueExpression(null), |
| 127 | + ); |
| 128 | + |
| 129 | + expect(result.current).toBe(undefined); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle undefined sourceID ungracefully', () => { |
| 133 | + jest.spyOn(sourceModule, 'useSource').mockReturnValue({ |
| 134 | + data: null, |
| 135 | + isLoading: false, |
| 136 | + error: null, |
| 137 | + } as any); |
| 138 | + |
| 139 | + jest.spyOn(metadataModule, 'useTableMetadata').mockReturnValue({ |
| 140 | + data: null, |
| 141 | + isLoading: false, |
| 142 | + error: null, |
| 143 | + } as any); |
| 144 | + |
| 145 | + const { result } = renderHook(() => |
| 146 | + useOptimizedTimestampValueExpression(undefined), |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result.current).toBe(undefined); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle complex Timestamp expressions', () => { |
| 153 | + const mockSource = { |
| 154 | + timestampValueExpression: 'toDateTime(timestamp_ms / 1000)', |
| 155 | + }; |
| 156 | + |
| 157 | + const mockTableMetadata = { |
| 158 | + primary_key: |
| 159 | + 'toStartOfHour(toDateTime(timestamp_ms / 1000)), toDateTime(timestamp_ms / 1000)', |
| 160 | + }; |
| 161 | + |
| 162 | + jest.spyOn(sourceModule, 'useSource').mockReturnValue({ |
| 163 | + data: mockSource, |
| 164 | + isLoading: false, |
| 165 | + error: null, |
| 166 | + } as any); |
| 167 | + |
| 168 | + jest.spyOn(metadataModule, 'useTableMetadata').mockReturnValue({ |
| 169 | + data: mockTableMetadata, |
| 170 | + isLoading: false, |
| 171 | + error: null, |
| 172 | + } as any); |
| 173 | + |
| 174 | + const { result } = renderHook(() => |
| 175 | + useOptimizedTimestampValueExpression('source-id'), |
| 176 | + ); |
| 177 | + |
| 178 | + expect(result.current).toBe( |
| 179 | + 'toDateTime(timestamp_ms / 1000), toStartOfHour(toDateTime(timestamp_ms / 1000))', |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should memoize result correctly when dependencies change', () => { |
| 184 | + const mockSource1 = { |
| 185 | + timestampValueExpression: 'timestamp1', |
| 186 | + }; |
| 187 | + |
| 188 | + const mockSource2 = { |
| 189 | + timestampValueExpression: 'timestamp2', |
| 190 | + }; |
| 191 | + |
| 192 | + const useSourceSpy = jest |
| 193 | + .spyOn(sourceModule, 'useSource') |
| 194 | + .mockReturnValue({ |
| 195 | + data: mockSource1, |
| 196 | + isLoading: false, |
| 197 | + error: null, |
| 198 | + } as any); |
| 199 | + |
| 200 | + jest.spyOn(metadataModule, 'useTableMetadata').mockReturnValue({ |
| 201 | + data: undefined, |
| 202 | + isLoading: false, |
| 203 | + error: null, |
| 204 | + } as any); |
| 205 | + |
| 206 | + const { result, rerender } = renderHook(() => |
| 207 | + useOptimizedTimestampValueExpression('source-id'), |
| 208 | + ); |
| 209 | + |
| 210 | + expect(result.current).toBe('timestamp1'); |
| 211 | + |
| 212 | + // Update the mock to return different data |
| 213 | + useSourceSpy.mockReturnValue({ |
| 214 | + data: mockSource2, |
| 215 | + isLoading: false, |
| 216 | + error: null, |
| 217 | + } as any); |
| 218 | + |
| 219 | + rerender(); |
| 220 | + |
| 221 | + expect(result.current).toBe('timestamp2'); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments