Skip to content

Commit 6a8344e

Browse files
committed
use stackql-exec
1 parent 70532c6 commit 6a8344e

File tree

2 files changed

+49
-94
lines changed

2 files changed

+49
-94
lines changed

.github/workflows/stackql-assert.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
#
2121
# Pull required providers
2222
#
23-
- name: Use test query string and expected rows
23+
- name: pull required providers
2424
uses: stackql/[email protected]
2525
with:
2626
is_command: true
@@ -33,7 +33,6 @@ jobs:
3333
uses: ./
3434
with:
3535
test_query: |
36-
REGISTRY PULL google;
3736
SELECT name
3837
FROM google.compute.instances
3938
WHERE project = 'stackql-demo' AND zone = 'australia-southeast1-a' AND name = 'stackql-demo-001';

lib/tests/utils.test.js

Lines changed: 48 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,79 @@
1-
const { setupAuth, getStackqlCommand } = require("../utils");
1+
const { assertResult, parseResult, getExpectedResult } = require('./assert');
2+
const fs = require('fs');
23

3-
describe("util", () => {
4+
jest.mock('fs');
5+
6+
describe('assert.js functions', () => {
47
let core;
5-
const expectedAuth = '{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}';
68

79
beforeEach(() => {
810
core = {
9-
setFailed: jest.fn(),
1011
info: jest.fn(),
11-
exportVariable: jest.fn(),
1212
error: jest.fn(),
13+
setFailed: jest.fn()
1314
};
15+
process.env.RESULT = JSON.stringify([{ id: 1, value: 'test' }]);
16+
process.env.EXPECTED_ROWS = '1';
1417
});
1518

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+
});
2623

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' });
2928
});
3029

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');
3833
});
34+
});
3935

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' });
4640
});
4741

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');
5447
});
5548

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.');
6351
});
6452
});
6553

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");
7659
});
7760

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"));
8165
});
8266

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."));
9071
});
9172

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"));
12177
});
12278
});
12379
});

0 commit comments

Comments
 (0)