|
1 |
| -const { setupAuth, getStackqlCommand } = require("../utils"); |
| 1 | +const { assertResult, parseResult, getExpectedResult } = require('./assert'); |
| 2 | +const fs = require('fs'); |
2 | 3 |
|
3 |
| -describe("util", () => { |
| 4 | +jest.mock('fs'); |
| 5 | + |
| 6 | +describe('assert.js functions', () => { |
4 | 7 | let core;
|
5 |
| - const expectedAuth = '{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}'; |
6 | 8 |
|
7 | 9 | beforeEach(() => {
|
8 | 10 | core = {
|
9 |
| - setFailed: jest.fn(), |
10 | 11 | info: jest.fn(),
|
11 |
| - exportVariable: jest.fn(), |
12 | 12 | error: jest.fn(),
|
| 13 | + setFailed: jest.fn() |
13 | 14 | };
|
| 15 | + process.env.RESULT = JSON.stringify([{ id: 1, value: 'test' }]); |
| 16 | + process.env.EXPECTED_ROWS = '1'; |
14 | 17 | });
|
15 | 18 |
|
16 |
| - describe("setupAuth", () => { |
17 |
| - let AUTH_ENV = { |
18 |
| - AUTH_STR: expectedAuth, |
19 |
| - AUTH_FILE_PATH: "./lib/tests/test-auth.json", |
20 |
| - }; |
21 |
| - |
22 |
| - beforeEach(() => { |
23 |
| - jest.resetModules(); |
24 |
| - process.env = { ...AUTH_ENV }; |
25 |
| - }); |
| 19 | + afterEach(() => { |
| 20 | + jest.resetAllMocks(); |
| 21 | + jest.restoreAllMocks(); |
| 22 | + }); |
26 | 23 |
|
27 |
| - afterEach(() => { |
28 |
| - process.env = AUTH_ENV; |
| 24 | + describe('parseResult', () => { |
| 25 | + it('should correctly parse valid JSON', () => { |
| 26 | + const input = JSON.stringify({ key: 'value' }); |
| 27 | + expect(parseResult(input, 'valid JSON')).toEqual({ key: 'value' }); |
29 | 28 | });
|
30 | 29 |
|
31 |
| - it("should not throw an error when neither AUTH_STR or AUTH_FILE_PATH is set", () => { |
32 |
| - process.env.AUTH_STR = undefined; |
33 |
| - process.env.AUTH_FILE_PATH = undefined; |
34 |
| - |
35 |
| - setupAuth(core); |
36 |
| - |
37 |
| - expect(core.setFailed).not.toBeCalled(); |
| 30 | + it('should throw an error on invalid JSON', () => { |
| 31 | + const input = "invalid JSON"; |
| 32 | + expect(() => parseResult(input, 'invalid JSON')).toThrow('Failed to parse invalid JSON JSON'); |
38 | 33 | });
|
| 34 | + }); |
39 | 35 |
|
40 |
| - it("should set AUTH environment variable when AUTH_STR is set", () => { |
41 |
| - process.env.AUTH_FILE_PATH = undefined; |
42 |
| - |
43 |
| - setupAuth(core); |
44 |
| - |
45 |
| - expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth); |
| 36 | + describe('getExpectedResult', () => { |
| 37 | + it('should return parsed result from string', () => { |
| 38 | + const input = JSON.stringify({ key: 'value' }); |
| 39 | + expect(getExpectedResult(input, null)).toEqual({ key: 'value' }); |
46 | 40 | });
|
47 | 41 |
|
48 |
| - it("should set AUTH environment variable when AUTH_FILE_PATH is set", () => { |
49 |
| - process.env.AUTH_STR = undefined; |
50 |
| - |
51 |
| - setupAuth(core); |
52 |
| - |
53 |
| - expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth); |
| 42 | + it('should return parsed result from file', () => { |
| 43 | + const input = JSON.stringify({ key: 'value' }); |
| 44 | + fs.readFileSync.mockReturnValue(input); |
| 45 | + expect(getExpectedResult(null, 'path/to/file')).toEqual({ key: 'value' }); |
| 46 | + expect(fs.readFileSync).toHaveBeenCalledWith('path/to/file', 'utf-8'); |
54 | 47 | });
|
55 | 48 |
|
56 |
| - it("should throw error when AUTH_FILE_PATH is set but file does not exist", () => { |
57 |
| - process.env.AUTH_STR = undefined; |
58 |
| - process.env.AUTH_FILE_PATH = "./failed-test-auth.json"; |
59 |
| - |
60 |
| - setupAuth(core); |
61 |
| - |
62 |
| - expect(core.setFailed).toBeCalledWith(`Cannot find auth file ${process.env.AUTH_FILE_PATH}`); |
| 49 | + it('should throw an error if no input is provided', () => { |
| 50 | + expect(() => getExpectedResult(null, null)).toThrow('No expected result provided.'); |
63 | 51 | });
|
64 | 52 | });
|
65 | 53 |
|
66 |
| - describe("getStackqlCommand", () => { |
67 |
| - const EXECUTE_ENV = { |
68 |
| - QUERY: "test", |
69 |
| - QUERY_FILE_PATH: "test-query.iql", |
70 |
| - AUTH: "test-auth", |
71 |
| - }; |
72 |
| - |
73 |
| - beforeEach(() => { |
74 |
| - jest.resetModules(); |
75 |
| - process.env = { ...EXECUTE_ENV }; |
| 54 | + describe('assertResult', () => { |
| 55 | + it('should log success if the expected rows and results match', () => { |
| 56 | + process.env.EXPECTED_RESULTS_STR = process.env.RESULT; |
| 57 | + assertResult(core); |
| 58 | + expect(core.info).toHaveBeenCalledWith("✅ StackQL Assert Successful"); |
76 | 59 | });
|
77 | 60 |
|
78 |
| - afterEach(() => { |
79 |
| - process.env = EXECUTE_ENV; |
80 |
| - jest.clearAllMocks(); |
| 61 | + it('should fail if expected rows do not match', () => { |
| 62 | + process.env.EXPECTED_ROWS = '2'; // Actual result will have only one item |
| 63 | + assertResult(core); |
| 64 | + expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Expected rows: 2, got: 1")); |
81 | 65 | });
|
82 | 66 |
|
83 |
| - it("should return error when there is neither query or query file path", () => { |
84 |
| - process.env.QUERY = undefined; |
85 |
| - process.env.QUERY_FILE_PATH = undefined; |
86 |
| - |
87 |
| - getStackqlCommand(core); |
88 |
| - |
89 |
| - expect(core.setFailed).toBeCalledWith("Either test_query or test_query_file_path need to be set"); |
| 67 | + it('should fail if expected results do not match', () => { |
| 68 | + process.env.EXPECTED_RESULTS_STR = JSON.stringify([{ id: 1, value: 'wrong' }]); |
| 69 | + assertResult(core); |
| 70 | + expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Expected results do not match actual results.")); |
90 | 71 | });
|
91 | 72 |
|
92 |
| - // it("should not return error when there is no AUTH", () => { |
93 |
| - // process.env.AUTH = undefined; |
94 |
| - |
95 |
| - // getStackqlCommand(core); |
96 |
| - |
97 |
| - // expect(core.setFailed).not.toBeCalled(); |
98 |
| - // expect(core.exportVariable).toBeCalledWith( |
99 |
| - // "STACKQL_COMMAND", "stackql exec \"test\" --output='json'" |
100 |
| - // ); |
101 |
| - // }); |
102 |
| - |
103 |
| - it("should execute stackql with query file path", () => { |
104 |
| - process.env.QUERY = undefined; |
105 |
| - |
106 |
| - getStackqlCommand(core); |
107 |
| - |
108 |
| - expect(core.exportVariable).toBeCalledWith( |
109 |
| - "STACKQL_COMMAND", "stackql exec -i test-query.iql --auth='test-auth' --output='json'" |
110 |
| - ); |
111 |
| - }); |
112 |
| - |
113 |
| - it("should execute stackql with query", () => { |
114 |
| - process.env.QUERY_FILE_PATH = undefined; |
115 |
| - |
116 |
| - getStackqlCommand(core); |
117 |
| - |
118 |
| - expect(core.exportVariable).toBeCalledWith( |
119 |
| - "STACKQL_COMMAND", "stackql exec \"test\" --auth='test-auth' --output='json'" |
120 |
| - ); |
| 73 | + it('should handle errors during processing', () => { |
| 74 | + process.env.RESULT = 'invalid json'; |
| 75 | + assertResult(core); |
| 76 | + expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Assertion error")); |
121 | 77 | });
|
122 | 78 | });
|
123 | 79 | });
|
0 commit comments