Repository navigation
Expand file tree
/
Copy pathstackql.test.ts
More file actions
210 lines (184 loc) · 5.81 KB
/
Copy pathstackql.test.ts
File metadata and controls
210 lines (184 loc) · 5.81 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
import { assertStringIncludes } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { StackQL } from "./stackql.ts";
import { startStackQLServer } from "../testing/utils.ts";
import {
assertEquals,
assertExists,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
import { Downloader } from "./services/downloader.ts";
import {
assertSpyCall,
spy,
} from "https://deno.land/std@0.207.0/testing/mock.ts";
import osUtils from "./utils/os.ts";
import { assert } from "https://deno.land/std@0.133.0/_util/assert.ts";
const downloader = new Downloader();
const setupStackQL = async () => {
await downloader.setupStackQL();
};
Deno.test("StackQL CLI run 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'`;
// Act
await stackQL.runQuery(pullQuery);
const result = await stackQL.runQuery(providerQuery);
const githubResult = await stackQL.runQuery(githubTestQuery);
// Assert
assertStringIncludes(result, "name");
assertStringIncludes(result, "version");
assertStringIncludes(result, "github");
assertStringIncludes(githubResult, "stackql");
});
Deno.test("Set properties 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.runQuery(githubTestQuery);
const params = stackQL.getParams();
assertEquals(params.length, 8);
assertEquals(params, [
"--http.response.maxResults",
"100",
"--http.response.pageLimit",
"10",
"--indirect.depth.max",
"5",
"--apirequesttimeout",
"5000",
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCliSpy, 0, {
args: [binaryPath, ["exec", githubTestQuery, ...params]],
});
runCliSpy.restore();
});
Deno.test("Set proxy properties 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.runQuery(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",
]);
const binaryPath = stackQL.getBinaryPath();
assert(binaryPath);
assertSpyCall(runCommandSpy, 0, {
args: [binaryPath, ["exec", githubTestQuery, ...params]],
});
runCommandSpy.restore();
});
Deno.test("StackQL runServerQuery", 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 testQuery = "SHOW SERVICES IN github LIKE '%repos%';"; // Replace with a valid query for your context
// Act
await stackQL.runServerQuery(pullQuery);
const results = await stackQL.runServerQuery(testQuery);
assertExists(results);
assertEquals(results.length, 1);
const result = results[0] as {
name: string;
};
assertEquals(result.name, "repos");
// Assert
} finally {
// Cleanup
await closeProcess();
await stackQL.closeConnection();
}
});
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 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 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));
});