|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { aggregateRecords } from '../ObjectChart'; |
| 11 | + |
| 12 | +describe('aggregateRecords', () => { |
| 13 | + const records = [ |
| 14 | + { account: 'Acme Corp', amount: 100 }, |
| 15 | + { account: 'Acme Corp', amount: 200 }, |
| 16 | + { account: 'Globex', amount: 150 }, |
| 17 | + { account: 'Globex', amount: 50 }, |
| 18 | + { account: 'Initech', amount: 300 }, |
| 19 | + ]; |
| 20 | + |
| 21 | + it('should aggregate using sum', () => { |
| 22 | + const result = aggregateRecords(records, { |
| 23 | + field: 'amount', |
| 24 | + function: 'sum', |
| 25 | + groupBy: 'account', |
| 26 | + }); |
| 27 | + |
| 28 | + expect(result).toHaveLength(3); |
| 29 | + expect(result.find(r => r.account === 'Acme Corp')?.amount).toBe(300); |
| 30 | + expect(result.find(r => r.account === 'Globex')?.amount).toBe(200); |
| 31 | + expect(result.find(r => r.account === 'Initech')?.amount).toBe(300); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should aggregate using count', () => { |
| 35 | + const result = aggregateRecords(records, { |
| 36 | + field: 'amount', |
| 37 | + function: 'count', |
| 38 | + groupBy: 'account', |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result).toHaveLength(3); |
| 42 | + expect(result.find(r => r.account === 'Acme Corp')?.amount).toBe(2); |
| 43 | + expect(result.find(r => r.account === 'Globex')?.amount).toBe(2); |
| 44 | + expect(result.find(r => r.account === 'Initech')?.amount).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should aggregate using avg', () => { |
| 48 | + const result = aggregateRecords(records, { |
| 49 | + field: 'amount', |
| 50 | + function: 'avg', |
| 51 | + groupBy: 'account', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result).toHaveLength(3); |
| 55 | + expect(result.find(r => r.account === 'Acme Corp')?.amount).toBe(150); |
| 56 | + expect(result.find(r => r.account === 'Globex')?.amount).toBe(100); |
| 57 | + expect(result.find(r => r.account === 'Initech')?.amount).toBe(300); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should aggregate using min', () => { |
| 61 | + const result = aggregateRecords(records, { |
| 62 | + field: 'amount', |
| 63 | + function: 'min', |
| 64 | + groupBy: 'account', |
| 65 | + }); |
| 66 | + |
| 67 | + expect(result).toHaveLength(3); |
| 68 | + expect(result.find(r => r.account === 'Acme Corp')?.amount).toBe(100); |
| 69 | + expect(result.find(r => r.account === 'Globex')?.amount).toBe(50); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should aggregate using max', () => { |
| 73 | + const result = aggregateRecords(records, { |
| 74 | + field: 'amount', |
| 75 | + function: 'max', |
| 76 | + groupBy: 'account', |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result).toHaveLength(3); |
| 80 | + expect(result.find(r => r.account === 'Acme Corp')?.amount).toBe(200); |
| 81 | + expect(result.find(r => r.account === 'Globex')?.amount).toBe(150); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle records with missing groupBy field', () => { |
| 85 | + const input = [ |
| 86 | + { account: 'Acme', amount: 100 }, |
| 87 | + { amount: 200 }, // missing account |
| 88 | + ]; |
| 89 | + |
| 90 | + const result = aggregateRecords(input, { |
| 91 | + field: 'amount', |
| 92 | + function: 'sum', |
| 93 | + groupBy: 'account', |
| 94 | + }); |
| 95 | + |
| 96 | + expect(result).toHaveLength(2); |
| 97 | + expect(result.find(r => r.account === 'Acme')?.amount).toBe(100); |
| 98 | + expect(result.find(r => r.account === 'Unknown')?.amount).toBe(200); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle empty records', () => { |
| 102 | + const result = aggregateRecords([], { |
| 103 | + field: 'amount', |
| 104 | + function: 'sum', |
| 105 | + groupBy: 'account', |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result).toEqual([]); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should handle non-numeric values gracefully', () => { |
| 112 | + const input = [ |
| 113 | + { account: 'Acme', amount: 'not-a-number' }, |
| 114 | + { account: 'Acme', amount: 100 }, |
| 115 | + ]; |
| 116 | + |
| 117 | + const result = aggregateRecords(input, { |
| 118 | + field: 'amount', |
| 119 | + function: 'sum', |
| 120 | + groupBy: 'account', |
| 121 | + }); |
| 122 | + |
| 123 | + expect(result).toHaveLength(1); |
| 124 | + expect(result[0].amount).toBe(100); // non-numeric value coerced to 0, sum is 0 + 100 |
| 125 | + }); |
| 126 | +}); |
0 commit comments