|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + DataFrame, Row, arrayDataFrame, awaitRows, resolvablePromise, sortableDataFrame, wrapPromise, |
| 4 | +} from '../src/dataframe.js' |
| 5 | + |
| 6 | +function wrapObject(obj: Record<string, any>): Row { |
| 7 | + return Object.fromEntries( |
| 8 | + Object.entries(obj).map(([key, value]) => [key, wrapPromise(value)]) |
| 9 | + ) |
| 10 | +} |
| 11 | + |
| 12 | +describe('resolvablePromise', () => { |
| 13 | + it('should resolve with a value', async () => { |
| 14 | + const promise = resolvablePromise<number>() |
| 15 | + promise.resolve(42) |
| 16 | + expect(await promise).toBe(42) |
| 17 | + }) |
| 18 | + it('should reject with an error', async () => { |
| 19 | + const promise = resolvablePromise<number>() |
| 20 | + promise.reject(new Error('Failed')) |
| 21 | + await expect(promise).rejects.toThrow('Failed') |
| 22 | + }) |
| 23 | +}) |
| 24 | + |
| 25 | +describe('awaitRows', () => { |
| 26 | + it('should resolve with a row', async () => { |
| 27 | + const row = wrapObject({ id: 1, name: 'Alice', age: 30, __index__: 0 }) |
| 28 | + const result = await awaitRows([row]) |
| 29 | + expect(result).toEqual([{ id: 1, name: 'Alice', age: 30, __index__: 0 }]) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('sortableDataFrame', () => { |
| 34 | + const data = [ |
| 35 | + { id: 3, name: 'Charlie', age: 25 }, |
| 36 | + { id: 1, name: 'Alice', age: 30 }, |
| 37 | + { id: 2, name: 'Bob', age: 20 }, |
| 38 | + { id: 4, name: 'Dani', age: 20 }, |
| 39 | + ] |
| 40 | + |
| 41 | + const dataFrame: DataFrame = { |
| 42 | + header: ['id', 'name', 'age'], |
| 43 | + numRows: data.length, |
| 44 | + rows(start: number, end: number): Row[] { |
| 45 | + // Return the slice of data between start and end indices |
| 46 | + return data.slice(start, end).map(wrapObject) |
| 47 | + }, |
| 48 | + sortable: false, |
| 49 | + } |
| 50 | + |
| 51 | + const sortableDf = sortableDataFrame(dataFrame) |
| 52 | + |
| 53 | + it('should set sortable to true', () => { |
| 54 | + expect(sortableDf.sortable).toBe(true) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should preserve header and numRows', () => { |
| 58 | + expect(sortableDf.header).toEqual(dataFrame.header) |
| 59 | + expect(sortableDf.numRows).toBe(dataFrame.numRows) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should return unsorted data when orderBy is not provided', async () => { |
| 63 | + const rows = await awaitRows(sortableDf.rows(0, 3)) |
| 64 | + expect(rows).toEqual([ |
| 65 | + { id: 3, name: 'Charlie', age: 25 }, |
| 66 | + { id: 1, name: 'Alice', age: 30 }, |
| 67 | + { id: 2, name: 'Bob', age: 20 }, |
| 68 | + ]) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should return data sorted by column "age"', async () => { |
| 72 | + const rows = await awaitRows(sortableDf.rows(0, 4, 'age')) |
| 73 | + expect(rows).toEqual([ |
| 74 | + { id: 2, name: 'Bob', age: 20, __index__: 2 }, |
| 75 | + { id: 4, name: 'Dani', age: 20, __index__: 3 }, |
| 76 | + { id: 3, name: 'Charlie', age: 25, __index__: 0 }, |
| 77 | + { id: 1, name: 'Alice', age: 30, __index__: 1 }, |
| 78 | + ]) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should slice the sorted data correctly', async () => { |
| 82 | + const rows = await awaitRows(sortableDf.rows(1, 3, 'id')) |
| 83 | + expect(rows).toEqual([ |
| 84 | + { id: 2, name: 'Bob', age: 20, __index__: 2 }, |
| 85 | + { id: 3, name: 'Charlie', age: 25, __index__: 0 }, |
| 86 | + ]) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns self if already sortable', () => { |
| 90 | + const sortableDf2 = sortableDataFrame(sortableDf) |
| 91 | + expect(sortableDf2).toBe(sortableDf) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should throw for invalid orderBy field', () => { |
| 95 | + expect(() => sortableDf.rows(0, 3, 'invalid')) |
| 96 | + .toThrowError('Invalid orderBy field: invalid') |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +describe('arrayDataFrame', () => { |
| 101 | + const testData = [ |
| 102 | + { id: 1, name: 'Alice', age: 30 }, |
| 103 | + { id: 2, name: 'Bob', age: 25 }, |
| 104 | + { id: 3, name: 'Charlie', age: 35 }, |
| 105 | + ] |
| 106 | + |
| 107 | + it('should create a DataFrame with correct header and numRows', () => { |
| 108 | + const df = arrayDataFrame(testData) |
| 109 | + expect(df.header).toEqual(['id', 'name', 'age']) |
| 110 | + expect(df.numRows).toBe(3) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should handle empty data array', () => { |
| 114 | + const df = arrayDataFrame([]) |
| 115 | + expect(df.header).toEqual([]) |
| 116 | + expect(df.numRows).toBe(0) |
| 117 | + expect(df.rows(0, 1)).resolves.toEqual([]) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should return correct rows for given range', async () => { |
| 121 | + const df = arrayDataFrame(testData) |
| 122 | + const rows = await df.rows(0, 2) |
| 123 | + expect(rows).toEqual([ |
| 124 | + { id: 1, name: 'Alice', age: 30 }, |
| 125 | + { id: 2, name: 'Bob', age: 25 }, |
| 126 | + ]) |
| 127 | + }) |
| 128 | + |
| 129 | + it('should handle start index equal to end index', async () => { |
| 130 | + const df = arrayDataFrame(testData) |
| 131 | + const rows = await df.rows(1, 1) |
| 132 | + expect(rows).toEqual([]) |
| 133 | + }) |
| 134 | + |
| 135 | + it('should return all rows when end index exceeds array length', async () => { |
| 136 | + const df = arrayDataFrame(testData) |
| 137 | + const rows = await df.rows(0, 10) |
| 138 | + expect(rows).toEqual(testData) |
| 139 | + }) |
| 140 | +}) |
0 commit comments