forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.test.ts
42 lines (38 loc) · 1.7 KB
/
csv.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { describe, it, vi, beforeEach } from 'vitest';
import { expect } from 'vitest';
import CSVNode, { csv_node_data } from '$lib/compute/csv_v0';
import deepCopy from 'deep-copy';
import * as utils from '$lib/utils';
describe('CSVNode class', () => {
beforeEach(() => {
// Reset module mocking before each test
vi.restoreAllMocks();
});
it('should process CSV data correctly from GCS', async () => {
vi.spyOn(utils, 'readFileFromGCS').mockResolvedValue('name,age\nAlice,30\nBob,');
const node = new CSVNode(deepCopy(csv_node_data));
node.data.gcs_path = 'path/to/file.csv';
const result = await node.compute(null, null, vi.fn(), vi.fn(), vi.fn(), 'test_slug', vi.fn());
expect(result).toEqual([
{ name: 'Alice', age: '30' },
{ name: 'Bob', age: '' }
]);
expect(node.data.dirty).toBe(false);
});
it('should handle empty CSV data from GCS', async () => {
vi.spyOn(utils, 'readFileFromGCS').mockResolvedValue('');
const node = new CSVNode(deepCopy(csv_node_data));
node.data.gcs_path = 'path/to/file.csv';
const result = await node.compute(null, null, vi.fn(), vi.fn(), vi.fn(), 'test_slug', vi.fn());
expect(result).toEqual([]);
expect(node.data.dirty).toBe(false);
});
it('should handle rows with uneven columns from GCS', async () => {
vi.spyOn(utils, 'readFileFromGCS').mockResolvedValue('name,age\nAlice,30\nBob');
const node = new CSVNode(deepCopy(csv_node_data));
node.data.gcs_path = 'path/to/file.csv';
const result = await node.compute(null, null, vi.fn(), vi.fn(), vi.fn(), 'test_slug', vi.fn());
expect(result).toEqual([{ name: 'Alice', age: '30' }, { name: 'Bob' }]);
expect(node.data.dirty).toBe(false);
});
});