forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimit_csv.test.ts
86 lines (78 loc) · 2.61 KB
/
limit_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { describe, it, vi } from 'vitest';
import { expect } from 'vitest';
import LimitCSVNode, { limit_csv_node_data } from '$lib/compute/limit_csv_v0';
describe('limit_csv function', () => {
it('should let all data pass through if number is left blank', async () => {
const node = new LimitCSVNode(limit_csv_node_data);
node.data.number = '';
const inputData = { csv: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }] };
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn()
);
expect(result).toEqual([{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }]);
});
it('should limit the number of rows correctly, for an object', async () => {
const node = new LimitCSVNode(limit_csv_node_data);
node.data.number = '1';
const inputData = {
csv: { 1: { name: 'Alice' }, 2: { name: 'Bob' }, 3: { name: 'Charlie' } }
};
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn()
);
expect(result).toEqual({ 1: { name: 'Alice' } });
});
it('should return all rows if limit is greater than number of rows', async () => {
const node = new LimitCSVNode(limit_csv_node_data);
node.data.number = '5';
const inputData = { csv: [{ name: 'Alice' }, { name: 'Bob' }] };
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn()
);
expect(result).toEqual([{ name: 'Alice' }, { name: 'Bob' }]);
});
it('should return an empty array if input is empty', async () => {
const node = new LimitCSVNode(limit_csv_node_data);
const inputData = { csv: [] };
const result = await node.compute(
inputData,
'run',
vi.fn(),
vi.fn(),
vi.fn(),
'test_slug',
vi.fn()
);
expect(result).toEqual([]);
});
it('should not mutate the input node', async () => {
const originalNode = new LimitCSVNode(limit_csv_node_data);
const nodeCopy = new LimitCSVNode(limit_csv_node_data);
const inputData = { csv: [{ name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' }] };
await nodeCopy.compute(inputData, 'run', vi.fn(), vi.fn(), vi.fn(), 'test_slug', vi.fn());
// Check if dirty flag is updated
expect(nodeCopy.data.dirty).toBe(false);
// Ensure other properties remain unchanged
expect(nodeCopy.id).toEqual(originalNode.id);
expect(nodeCopy.type).toEqual(originalNode.type);
expect(nodeCopy.position).toEqual(originalNode.position);
});
});