|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { DataFrame } from '../../../src/core/DataFrame.js'; |
| 3 | +import { mode } from '../../../src/methods/aggregation/mode.js'; |
| 4 | + |
| 5 | +describe('mode method', () => { |
| 6 | + // Create test data |
| 7 | + const testData = [ |
| 8 | + { value: 30, category: 'A', mixed: '20' }, |
| 9 | + { value: 10, category: 'B', mixed: 30 }, |
| 10 | + { value: 30, category: 'A', mixed: null }, |
| 11 | + { value: 40, category: 'C', mixed: undefined }, |
| 12 | + { value: 30, category: 'B', mixed: NaN }, |
| 13 | + { value: 20, category: 'B', mixed: '20' }, |
| 14 | + ]; |
| 15 | + |
| 16 | + const df = DataFrame.create(testData); |
| 17 | + |
| 18 | + it('should find the most frequent value in a column', () => { |
| 19 | + // Call mode function directly |
| 20 | + const modeFn = mode({ validateColumn: () => {} }); |
| 21 | + const result = modeFn(df._frame, 'value'); |
| 22 | + |
| 23 | + // Check that the mode is correct |
| 24 | + expect(result).toBe(30); // 30 appears 3 times, more than any other value |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle mixed data types by treating them as distinct', () => { |
| 28 | + // Call mode function directly |
| 29 | + const modeFn = mode({ validateColumn: () => {} }); |
| 30 | + const result = modeFn(df._frame, 'mixed'); |
| 31 | + |
| 32 | + // Check that the mode is correct (only valid values are considered) |
| 33 | + expect(result).toBe('20'); // '20' appears twice (string '20', not number 20) |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return null for a column with no valid values', () => { |
| 37 | + // Create data with only invalid values |
| 38 | + const invalidData = [ |
| 39 | + { invalid: null }, |
| 40 | + { invalid: undefined }, |
| 41 | + { invalid: NaN }, |
| 42 | + ]; |
| 43 | + |
| 44 | + const invalidDf = DataFrame.create(invalidData); |
| 45 | + |
| 46 | + // Call mode function directly |
| 47 | + const modeFn = mode({ validateColumn: () => {} }); |
| 48 | + const result = modeFn(invalidDf._frame, 'invalid'); |
| 49 | + |
| 50 | + // Check that the result is null (no valid values) |
| 51 | + expect(result).toBe(null); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return the first encountered value if multiple values have the same highest frequency', () => { |
| 55 | + // Create data with multiple modes |
| 56 | + const multiModeData = [ |
| 57 | + { value: 10 }, |
| 58 | + { value: 20 }, |
| 59 | + { value: 10 }, |
| 60 | + { value: 30 }, |
| 61 | + { value: 20 }, |
| 62 | + { value: 30 }, |
| 63 | + ]; |
| 64 | + |
| 65 | + const multiModeDf = DataFrame.create(multiModeData); |
| 66 | + |
| 67 | + // Call mode function directly |
| 68 | + const modeFn = mode({ validateColumn: () => {} }); |
| 69 | + const result = modeFn(multiModeDf._frame, 'value'); |
| 70 | + |
| 71 | + // Check that one of the modes is returned (all appear twice) |
| 72 | + expect([10, 20, 30]).toContain(result); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw an error for non-existent column', () => { |
| 76 | + // Create a validator that throws an error for non-existent column |
| 77 | + const validateColumn = (frame, column) => { |
| 78 | + if (!(column in frame.columns)) { |
| 79 | + throw new Error(`Column '${column}' not found`); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // Call mode function with validator |
| 84 | + const modeFn = mode({ validateColumn }); |
| 85 | + |
| 86 | + // Check that it throws an error for non-existent column |
| 87 | + expect(() => modeFn(df._frame, 'nonexistent')).toThrow( |
| 88 | + "Column 'nonexistent' not found", |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle empty frames', () => { |
| 93 | + // Create an empty DataFrame |
| 94 | + const emptyDf = DataFrame.create([]); |
| 95 | + |
| 96 | + // Add an empty column |
| 97 | + emptyDf._frame.columns.value = []; |
| 98 | + |
| 99 | + // Call mode function directly |
| 100 | + const modeFn = mode({ validateColumn: () => {} }); |
| 101 | + const result = modeFn(emptyDf._frame, 'value'); |
| 102 | + |
| 103 | + // Check that the result is null for empty column |
| 104 | + expect(result).toBe(null); |
| 105 | + }); |
| 106 | +}); |
0 commit comments