|
| 1 | +import { ResponseJSON } from '@clickhouse/client-web'; |
| 2 | + |
| 3 | +import { computeRatio, computeResultSetRatio } from '../useChartConfig'; |
| 4 | + |
| 5 | +describe('computeRatio', () => { |
| 6 | + it('should correctly compute ratio of two numbers', () => { |
| 7 | + expect(computeRatio('10', '2')).toBe(5); |
| 8 | + expect(computeRatio('3', '4')).toBe(0.75); |
| 9 | + expect(computeRatio('0', '5')).toBe(0); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should return NaN when denominator is zero', () => { |
| 13 | + expect(isNaN(computeRatio('10', '0'))).toBe(true); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return NaN for non-numeric inputs', () => { |
| 17 | + expect(isNaN(computeRatio('abc', '2'))).toBe(true); |
| 18 | + expect(isNaN(computeRatio('10', 'xyz'))).toBe(true); |
| 19 | + expect(isNaN(computeRatio('abc', 'xyz'))).toBe(true); |
| 20 | + expect(isNaN(computeRatio('', '5'))).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should handle string representations of numbers', () => { |
| 24 | + expect(computeRatio('10.5', '2')).toBe(5.25); |
| 25 | + expect(computeRatio('-10', '5')).toBe(-2); |
| 26 | + expect(computeRatio('10', '-5')).toBe(-2); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle number input types', () => { |
| 30 | + expect(computeRatio(10, 2)).toBe(5); |
| 31 | + expect(computeRatio(3, 4)).toBe(0.75); |
| 32 | + expect(computeRatio(10.5, 2)).toBe(5.25); |
| 33 | + expect(computeRatio(0, 5)).toBe(0); |
| 34 | + expect(isNaN(computeRatio(10, 0))).toBe(true); |
| 35 | + expect(computeRatio(-10, 5)).toBe(-2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle mixed string and number inputs', () => { |
| 39 | + expect(computeRatio('10', 2)).toBe(5); |
| 40 | + expect(computeRatio(10, '2')).toBe(5); |
| 41 | + expect(computeRatio(3, '4')).toBe(0.75); |
| 42 | + expect(isNaN(computeRatio(10, ''))).toBe(true); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('computeResultSetRatio', () => { |
| 47 | + it('should compute ratio for a valid result set with timestamp column', () => { |
| 48 | + const mockResultSet: ResponseJSON<any> = { |
| 49 | + meta: [ |
| 50 | + { name: 'timestamp', type: 'DateTime' }, |
| 51 | + { name: 'requests', type: 'UInt64' }, |
| 52 | + { name: 'errors', type: 'UInt64' }, |
| 53 | + ], |
| 54 | + data: [ |
| 55 | + { timestamp: '2025-04-15 10:00:00', requests: '100', errors: '10' }, |
| 56 | + { timestamp: '2025-04-15 11:00:00', requests: '200', errors: '20' }, |
| 57 | + ], |
| 58 | + rows: 2, |
| 59 | + statistics: { elapsed: 0.1, rows_read: 2, bytes_read: 100 }, |
| 60 | + }; |
| 61 | + |
| 62 | + const result = computeResultSetRatio(mockResultSet); |
| 63 | + |
| 64 | + expect(result.meta.length).toBe(2); |
| 65 | + expect(result.meta[0].name).toBe('requests/errors'); |
| 66 | + expect(result.meta[0].type).toBe('Float64'); |
| 67 | + expect(result.meta[1].name).toBe('timestamp'); |
| 68 | + |
| 69 | + expect(result.data.length).toBe(2); |
| 70 | + expect(result.data[0]['requests/errors']).toBe(10); |
| 71 | + expect(result.data[0].timestamp).toBe('2025-04-15 10:00:00'); |
| 72 | + expect(result.data[1]['requests/errors']).toBe(10); |
| 73 | + expect(result.data[1].timestamp).toBe('2025-04-15 11:00:00'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should compute ratio for a valid result set without timestamp column', () => { |
| 77 | + const mockResultSet: ResponseJSON<any> = { |
| 78 | + meta: [ |
| 79 | + { name: 'requests', type: 'UInt64' }, |
| 80 | + { name: 'errors', type: 'UInt64' }, |
| 81 | + ], |
| 82 | + data: [{ requests: '100', errors: '10' }], |
| 83 | + rows: 1, |
| 84 | + statistics: { elapsed: 0.1, rows_read: 1, bytes_read: 50 }, |
| 85 | + }; |
| 86 | + |
| 87 | + const result = computeResultSetRatio(mockResultSet); |
| 88 | + |
| 89 | + expect(result.meta.length).toBe(1); |
| 90 | + expect(result.meta[0].name).toBe('requests/errors'); |
| 91 | + expect(result.meta[0].type).toBe('Float64'); |
| 92 | + |
| 93 | + expect(result.data.length).toBe(1); |
| 94 | + expect(result.data[0]['requests/errors']).toBe(10); |
| 95 | + expect(result.data[0].timestamp).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle NaN values in ratio computation', () => { |
| 99 | + const mockResultSet: ResponseJSON<any> = { |
| 100 | + meta: [ |
| 101 | + { name: 'timestamp', type: 'DateTime' }, |
| 102 | + { name: 'requests', type: 'UInt64' }, |
| 103 | + { name: 'errors', type: 'UInt64' }, |
| 104 | + ], |
| 105 | + data: [ |
| 106 | + { timestamp: '2025-04-15 10:00:00', requests: '100', errors: '0' }, |
| 107 | + { timestamp: '2025-04-15 11:00:00', requests: 'invalid', errors: '20' }, |
| 108 | + ], |
| 109 | + rows: 2, |
| 110 | + statistics: { elapsed: 0.1, rows_read: 2, bytes_read: 100 }, |
| 111 | + }; |
| 112 | + |
| 113 | + const result = computeResultSetRatio(mockResultSet); |
| 114 | + |
| 115 | + expect(result.data.length).toBe(2); |
| 116 | + expect(isNaN(result.data[0]['requests/errors'])).toBe(true); |
| 117 | + expect(isNaN(result.data[1]['requests/errors'])).toBe(true); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should throw error when result set has insufficient columns', () => { |
| 121 | + const mockResultSet: ResponseJSON<any> = { |
| 122 | + meta: [ |
| 123 | + { name: 'timestamp', type: 'DateTime' }, |
| 124 | + { name: 'requests', type: 'UInt64' }, |
| 125 | + ], |
| 126 | + data: [{ timestamp: '2025-04-15 10:00:00', requests: '100' }], |
| 127 | + rows: 1, |
| 128 | + statistics: { elapsed: 0.1, rows_read: 1, bytes_read: 50 }, |
| 129 | + }; |
| 130 | + |
| 131 | + expect(() => computeResultSetRatio(mockResultSet)).toThrow( |
| 132 | + /Unable to compute ratio/, |
| 133 | + ); |
| 134 | + }); |
| 135 | +}); |
0 commit comments