Repository navigation
Expand file tree
/
Copy pathstackql.test.ts
More file actions
337 lines (293 loc) · 8.66 KB
/
Copy pathstackql.test.ts
File metadata and controls
337 lines (293 loc) · 8.66 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import {
assert,
assertEquals,
assertExists,
assertSpyCall,
assertStringIncludes,
spy,
} from '../dev_deps.ts';
import { StackQL } from './stackql.ts';
import { isCsvString, startStackQLServer } from '../../testing/utils.ts';
import { Downloader } from './services/downloader.ts';
import osUtils from './utils/os.ts';
const downloader = new Downloader();
const setupStackQL = async () => {
await downloader.setupStackQL();
};
Deno.test('Query: Execute Statement and Query', async () => {
await setupStackQL();
// Arrange
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });
const pullQuery = 'REGISTRY PULL github;';
const providerQuery = 'SHOW PROVIDERS';
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql' and name='stackql'`;
// Act
await stackQL.executeStatement(pullQuery);
const result = await stackQL.executeStatement(providerQuery);
const githubResult = await stackQL.execute(githubTestQuery) as unknown[];
const githubResultObj = githubResult[0] as {
id: string;
name: string;
};
// Assert
assertStringIncludes(result, 'name');
assertStringIncludes(result, 'version');
assertStringIncludes(result, 'github');
assertEquals(githubResultObj.name, 'stackql');
});
Deno.test('Configuration: Apply Settings from Configs', async () => {
await setupStackQL();
const runCliSpy = spy(osUtils, 'runCommand');
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
maxResults: 100,
pageLimit: 10,
maxDepth: 5,
apiTimeout: 5000,
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;
await stackQL.execute(githubTestQuery);
const params = stackQL.getParams();
assertEquals(params.length, 10);
assertEquals(params, [
'--http.response.maxResults',
'100',
'--http.response.pageLimit',
'10',
'--indirect.depth.max',
'5',
'--apirequesttimeout',
'5000',
'--output',
'json',
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCliSpy, 0, {
args: [binaryPath, ['exec', githubTestQuery, ...params]],
});
runCliSpy.restore();
});
Deno.test('Configuration: Apply Proxy Settings from Configs', async () => {
await setupStackQL();
const runCommandSpy = spy(osUtils, 'runCommand');
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
proxyHost: 'localhost',
proxyPort: 8080,
proxyUser: 'user',
proxyPassword: 'password',
proxyScheme: 'https',
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;
await stackQL.execute(githubTestQuery);
const params = stackQL.getParams();
assertEquals(params, [
'--http.proxy.host',
'localhost',
'--http.proxy.port',
'8080',
'--http.proxy.user',
'user',
'--http.proxy.password',
'password',
'--http.proxy.scheme',
'https',
'--output',
'json',
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCommandSpy, 0, {
args: [binaryPath, ['exec', githubTestQuery, ...params]],
});
runCommandSpy.restore();
});
Deno.test('Query: json output', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
outputFormat: 'object',
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;
const result = await stackQL.execute(githubTestQuery) as unknown[];
const params = stackQL.getParams();
assertEquals(params, [
'--output',
'json',
]);
assert(Array.isArray(result));
});
Deno.test('Query: csv output', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
outputFormat: 'csv',
});
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql'`;
const result = await stackQL.execute(githubTestQuery) as string;
const params = stackQL.getParams();
assertEquals(params, [
'--output',
'csv',
]);
assert(!Array.isArray(result));
assert(await isCsvString(result));
});
Deno.test('Server mode: csv output throw error', async () => {
const { closeProcess } = await startStackQLServer();
const stackQL = new StackQL();
try {
await stackQL.initialize({
serverMode: true,
outputFormat: 'csv',
connectionString:
'postgres://postgres:password@localhost:5444/postgres',
});
const testQuery = "SHOW SERVICES IN github LIKE '%repos%';"; // Replace with a valid query for your context
await stackQL.execute(testQuery);
} catch (error) {
assert(error);
assert(
error.message.includes(
'csv output is not supported in server mode',
),
);
} finally {
await closeProcess();
await stackQL.closeConnection();
}
});
Deno.test('Server mode: Query', async () => {
const { closeProcess } = await startStackQLServer();
const stackQL = new StackQL();
try {
// Arrange
await stackQL.initialize({
serverMode: true,
connectionString:
'postgres://postgres:password@localhost:5444/postgres',
});
const pullQuery = 'REGISTRY PULL github;';
const showServiceQuery = "SHOW SERVICES IN github LIKE '%repos%';"; // Replace with a valid query for your context
// Act
await stackQL.executeStatement(pullQuery);
const showServiceResult = await stackQL.execute(showServiceQuery);
assertExists(showServiceResult);
assert(Array.isArray(showServiceResult));
assertEquals(showServiceResult.length, 1);
const result = showServiceResult[0] as {
name: string;
};
assertEquals(result.name, 'repos');
} finally {
// Cleanup
await closeProcess();
await stackQL.closeConnection();
}
});
Deno.test('executeQueriesAsync: object format', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });
const pullQuery = 'REGISTRY PULL github;';
const providerQuery = 'SHOW PROVIDERS';
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql' and name='stackql'`;
const results = await stackQL.executeQueriesAsync([
pullQuery,
providerQuery,
githubTestQuery,
]);
assert(Array.isArray(results));
assertEquals(results.length, 3);
const githubResult = results[2] as unknown[];
const githubResultObj = githubResult[0] as {
id: string;
name: string;
};
assertEquals(githubResultObj.name, 'stackql');
});
Deno.test('executeQueriesAsync: csv format', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({
serverMode: false,
outputFormat: 'csv',
});
const pullQuery = 'REGISTRY PULL github;';
const providerQuery = 'SHOW PROVIDERS';
const githubTestQuery =
`SELECT id, name from github.repos.repos where org='stackql' and name='stackql'`;
const results = await stackQL.executeQueriesAsync([
pullQuery,
providerQuery,
githubTestQuery,
]);
assert(Array.isArray(results));
assertEquals(results.length, 3);
const githubResult = results[2] as string;
assert(await isCsvString(githubResult));
});
Deno.test('GetVersion', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });
const versionRegex = /^v?(\d+(?:\.\d+)*)$/;
const shaRegex = /^[a-f0-9]{7}$/;
const { version, sha } = await stackQL.getVersion();
assert(version);
assert(sha);
assert(versionRegex.test(version));
assert(shaRegex.test(sha));
});
Deno.test('GetVersion: getVersion when version and sha are undefined', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false });
const versionRegex = /^v?(\d+(?:\.\d+)*)$/;
const shaRegex = /^[a-f0-9]{7}$/;
// deno-lint-ignore no-explicit-any
(stackQL as any).version = undefined;
// deno-lint-ignore no-explicit-any
(stackQL as any).sha = undefined;
// deno-lint-ignore no-explicit-any
assert((stackQL as any).version === undefined);
// deno-lint-ignore no-explicit-any
assert((stackQL as any).sha === undefined);
const { version, sha } = await stackQL.getVersion();
assert(version);
assert(sha);
assert(versionRegex.test(version));
assert(shaRegex.test(sha));
});
Deno.test('Upgrade: upgrade stackql', async () => {
await setupStackQL();
const stackQL = new StackQL();
await stackQL.initialize({ serverMode: false }) // deno-lint-ignore no-explicit-any
;
(stackQL as any).version = undefined // deno-lint-ignore no-explicit-any
;
(stackQL as any).sha = undefined;
const versionRegex = /^v?(\d+(?:\.\d+)*)$/;
const shaRegex = /^[a-f0-9]{7}$/;
// deno-lint-ignore no-explicit-any
assert((stackQL as any).version === undefined);
// deno-lint-ignore no-explicit-any
assert((stackQL as any).sha === undefined);
const { version, sha } = await stackQL.upgrade();
assert(version);
assert(sha);
assert(versionRegex.test(version));
assert(shaRegex.test(sha));
});