mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Add dataset library, extract-to-dataset, and render-time reference resolution
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { profileData } from './profile';
|
||||
|
||||
describe('profileData', () => {
|
||||
test('profiles a JSON-array dataset fully', () => {
|
||||
const rows = [
|
||||
{ city: 'Kyiv', pop: 2900000, capital: 'true' },
|
||||
{ city: 'Lviv', pop: 720000, capital: 'false' },
|
||||
];
|
||||
const profile = profileData(rows, 123);
|
||||
expect(profile.rowCount).toBe(2);
|
||||
expect(profile.columnCount).toBe(3);
|
||||
expect(profile.columns).toEqual(['city', 'pop', 'capital']);
|
||||
expect(profile.columnTypes).toEqual([
|
||||
{ name: 'city', type: 'string' },
|
||||
{ name: 'pop', type: 'number' },
|
||||
{ name: 'capital', type: 'boolean' },
|
||||
]);
|
||||
expect(profile.size).toBe(123);
|
||||
});
|
||||
|
||||
test('null rows (URL) return the N/A profile but keep size', () => {
|
||||
const profile = profileData(null, 42);
|
||||
expect(profile.rowCount).toBeNull();
|
||||
expect(profile.columnCount).toBeNull();
|
||||
expect(profile.columns).toEqual([]);
|
||||
expect(profile.columnTypes).toEqual([]);
|
||||
expect(profile.size).toBe(42);
|
||||
});
|
||||
|
||||
test('an empty array returns the N/A profile but keeps size', () => {
|
||||
const profile = profileData([], 7);
|
||||
expect(profile.rowCount).toBeNull();
|
||||
expect(profile.columnCount).toBeNull();
|
||||
expect(profile.columns).toEqual([]);
|
||||
expect(profile.size).toBe(7);
|
||||
});
|
||||
|
||||
test('column order follows first-seen key order across ragged rows', () => {
|
||||
const rows = [{ a: 1 }, { b: 2, a: 3 }, { c: 4 }];
|
||||
const profile = profileData(rows, 0);
|
||||
expect(profile.columns).toEqual(['a', 'b', 'c']);
|
||||
});
|
||||
|
||||
test('respects the sampling cap: rows beyond the head do not affect type', () => {
|
||||
// First 200 rows numeric; row 201 is text. With head-only sampling the
|
||||
// column still reads as number (an accepted, documented trade-off).
|
||||
const rows: Array<Record<string, unknown>> = [];
|
||||
for (let i = 0; i < 200; i++) rows.push({ v: i });
|
||||
rows.push({ v: 'tail-text' });
|
||||
const profile = profileData(rows, 0);
|
||||
expect(profile.rowCount).toBe(201); // count reflects the whole payload
|
||||
expect(profile.columnTypes).toEqual([{ name: 'v', type: 'number' }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user