forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjq_v1.test.ts
68 lines (61 loc) · 2.52 KB
/
jq_v1.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
import { describe, it, vi } from 'vitest';
import { expect } from 'vitest';
import JqNodeV1, { jq_node_data } from '$lib/compute/jq_v1';
import _ from 'lodash';
const log = console.log;
const input = { input: { x: [{ y: 2 }, { y: 4 }] } };
// Please note these tests do not cover backend node-jq functionality
// They only cover the frontend jq_web functionality
// todo: please make sure to test backend functionality as well
describe('jq function', () => {
it('should process data correctly with JQ filter', async () => {
const node = new JqNodeV1(_.cloneDeep(jq_node_data));
node.data.text = '.x[].y';
const result = await node.compute(input, 'run', log, log, log, '/', null);
try {
expect(result).toEqual([2, 4]);
} catch (e) {
expect(result).toEqual(['2', '4']);
}
vi.restoreAllMocks();
});
it('should handle invalid JQ filter', async () => {
const node = new JqNodeV1(_.cloneDeep(jq_node_data));
node.data.text = 'invalid filter';
const result = await node.compute(input, 'run', log, log, log, '/', null);
expect(result).toBeUndefined();
vi.restoreAllMocks();
});
it('should return an empty array when no matches found', async () => {
const node = new JqNodeV1(_.cloneDeep(jq_node_data));
node.data.text = '.x[].z';
const result = await node.compute(input, 'run', log, log, log, '/', null);
expect(result).toEqual([null, null]);
vi.restoreAllMocks();
});
it('should process data correctly with a complex JQ filter', async () => {
const complexInput = {
input: {
items: [
{ id: 1, name: 'apple', category: 'fruits' },
{ id: 2, name: 'banana', category: 'fruits' },
{ id: 3, name: 'broccoli', category: 'vegetables' }
]
}
};
const node = new JqNodeV1(_.cloneDeep(jq_node_data));
node.data.text = '.items[] | select(.category == "fruits") | .name';
const result = await node.compute(complexInput, 'run', log, log, log, '/', null);
expect(result).toEqual(['apple', 'banana']);
vi.restoreAllMocks();
});
it('should return undefined if the input is null or undefined', async () => {
const node = new JqNodeV1(_.cloneDeep(jq_node_data));
node.data.text = '.x[].y';
const resultNullInput = await node.compute(null, 'run', log, log, log, '/', null);
const resultUndefinedInput = await node.compute(undefined, 'run', log, log, log, '/', null);
expect(resultNullInput).toBeUndefined();
expect(resultUndefinedInput).toBeUndefined();
vi.restoreAllMocks();
});
});