Skip to content

Commit b8ec847

Browse files
committed
Add tests for bqrsToResultSet
1 parent a0c85b7 commit b8ec847

3 files changed

Lines changed: 316 additions & 16 deletions

File tree

extensions/ql-vscode/src/common/bqrs-cli-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type BqrsColumnKind =
2121
| typeof BqrsColumnKindCode.DATE
2222
| typeof BqrsColumnKindCode.ENTITY;
2323

24-
interface BqrsSchemaColumn {
24+
export interface BqrsSchemaColumn {
2525
name?: string;
2626
kind: BqrsColumnKind;
2727
}

extensions/ql-vscode/src/common/bqrs-raw-results-mapper.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
BqrsResultSetSchema,
99
BqrsUrlValue as BqrsUrlValue,
1010
BqrsWholeFileLocation,
11+
BqrsSchemaColumn,
1112
} from "./bqrs-cli-types";
1213
import {
1314
CellValue,
@@ -28,26 +29,37 @@ export function bqrsToResultSet(
2829
): RawResultSet {
2930
const name = schema.name;
3031
const totalRowCount = schema.rows;
31-
const nextPageOffset = chunk.next;
3232

33-
const columns = schema.columns.map(
34-
(column): Column => ({
35-
kind: mapColumnKind(column.kind),
36-
name: column.name,
37-
}),
38-
);
33+
const columns = schema.columns.map(mapColumn);
3934

4035
const rows = chunk.tuples.map(
4136
(tuple): Row => tuple.map((cell): CellValue => mapCellValue(cell)),
4237
);
4338

44-
return {
39+
const resultSet: RawResultSet = {
4540
name,
4641
totalRowCount,
4742
columns,
4843
rows,
49-
nextPageOffset,
5044
};
45+
46+
if (chunk.next) {
47+
resultSet.nextPageOffset = chunk.next;
48+
}
49+
50+
return resultSet;
51+
}
52+
53+
function mapColumn(column: BqrsSchemaColumn): Column {
54+
const result: Column = {
55+
kind: mapColumnKind(column.kind),
56+
};
57+
58+
if (column.name) {
59+
result.name = column.name;
60+
}
61+
62+
return result;
5163
}
5264

5365
function mapColumnKind(kind: BqrsColumnKind): ColumnKind {
@@ -95,11 +107,19 @@ function mapCellValue(cellValue: BqrsCellValue): CellValue {
95107
}
96108

97109
function mapEntityValue(cellValue: BqrsEntityValue): EntityValue {
98-
return {
99-
url: cellValue.url === undefined ? undefined : mapUrlValue(cellValue.url),
100-
label: cellValue.label,
101-
id: cellValue.id,
102-
};
110+
const result: EntityValue = {};
111+
112+
if (cellValue.id) {
113+
result.id = cellValue.id;
114+
}
115+
if (cellValue.label) {
116+
result.label = cellValue.label;
117+
}
118+
if (cellValue.url) {
119+
result.url = mapUrlValue(cellValue.url);
120+
}
121+
122+
return result;
103123
}
104124

105125
export function mapUrlValue(urlValue: BqrsUrlValue): UrlValue | undefined {

extensions/ql-vscode/test/unit-tests/common/bqrs-raw-results-mapper.test.ts

Lines changed: 281 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,284 @@
1-
import { mapUrlValue } from "../../../src/common/bqrs-raw-results-mapper";
1+
import {
2+
bqrsToResultSet,
3+
mapUrlValue,
4+
} from "../../../src/common/bqrs-raw-results-mapper";
5+
import {
6+
BqrsResultSetSchema,
7+
DecodedBqrsChunk,
8+
} from "../../../src/common/bqrs-cli-types";
9+
import { ColumnKind } from "../../../src/common/raw-result-types";
10+
11+
describe("bqrsToResultSet", () => {
12+
const schema: BqrsResultSetSchema = {
13+
name: "#select",
14+
columns: [
15+
{
16+
name: "endpoint",
17+
kind: "e",
18+
},
19+
{
20+
kind: "s",
21+
},
22+
{
23+
kind: "s",
24+
},
25+
{
26+
kind: "s",
27+
},
28+
{
29+
kind: "s",
30+
},
31+
{
32+
name: "supported",
33+
kind: "b",
34+
},
35+
{
36+
kind: "s",
37+
},
38+
{
39+
name: "type",
40+
kind: "s",
41+
},
42+
],
43+
rows: 2,
44+
};
45+
46+
const chunk: DecodedBqrsChunk = {
47+
columns: [
48+
{ name: "endpoint", kind: "Entity" },
49+
{ kind: "String" },
50+
{ kind: "String" },
51+
{ kind: "String" },
52+
{ kind: "String" },
53+
{ name: "supported", kind: "Boolean" },
54+
{ kind: "String" },
55+
{ name: "type", kind: "String" },
56+
],
57+
tuples: [
58+
[
59+
{
60+
label: "connect",
61+
url: {
62+
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
63+
startLine: 22,
64+
startColumn: 19,
65+
endLine: 22,
66+
endColumn: 25,
67+
},
68+
},
69+
"org.example",
70+
"HelloController",
71+
"connect",
72+
"(String)",
73+
false,
74+
"example",
75+
"",
76+
],
77+
[
78+
{
79+
label: "index",
80+
url: {
81+
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
82+
startLine: 13,
83+
startColumn: 19,
84+
endLine: 13,
85+
endColumn: 23,
86+
},
87+
},
88+
"org.example",
89+
"HelloController",
90+
"index",
91+
"(String)",
92+
true,
93+
"example",
94+
"summary",
95+
],
96+
[
97+
{
98+
label: "index",
99+
url: "file://home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java:15:19:15:23",
100+
},
101+
"org.example",
102+
"HelloController",
103+
"index",
104+
"(String)",
105+
true,
106+
"example",
107+
"summary",
108+
],
109+
],
110+
};
111+
112+
it("creates a result set", () => {
113+
expect(bqrsToResultSet(schema, chunk)).toEqual({
114+
name: "#select",
115+
totalRowCount: 2,
116+
columns: [
117+
{
118+
name: "endpoint",
119+
kind: ColumnKind.Entity,
120+
},
121+
{
122+
kind: ColumnKind.String,
123+
},
124+
{
125+
kind: ColumnKind.String,
126+
},
127+
{
128+
kind: ColumnKind.String,
129+
},
130+
{
131+
kind: ColumnKind.String,
132+
},
133+
{
134+
name: "supported",
135+
kind: ColumnKind.Boolean,
136+
},
137+
{
138+
kind: ColumnKind.String,
139+
},
140+
{
141+
name: "type",
142+
kind: ColumnKind.String,
143+
},
144+
],
145+
rows: [
146+
[
147+
{
148+
type: "entity",
149+
value: {
150+
label: "connect",
151+
url: {
152+
type: "lineColumnLocation",
153+
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
154+
startLine: 22,
155+
startColumn: 19,
156+
endLine: 22,
157+
endColumn: 25,
158+
},
159+
},
160+
},
161+
{
162+
type: "string",
163+
value: "org.example",
164+
},
165+
{
166+
type: "string",
167+
value: "HelloController",
168+
},
169+
{
170+
type: "string",
171+
value: "connect",
172+
},
173+
{
174+
type: "string",
175+
value: "(String)",
176+
},
177+
{
178+
type: "boolean",
179+
value: false,
180+
},
181+
{
182+
type: "string",
183+
value: "example",
184+
},
185+
{
186+
type: "string",
187+
value: "",
188+
},
189+
],
190+
[
191+
{
192+
type: "entity",
193+
value: {
194+
label: "index",
195+
url: {
196+
type: "lineColumnLocation",
197+
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
198+
startLine: 13,
199+
startColumn: 19,
200+
endLine: 13,
201+
endColumn: 23,
202+
},
203+
},
204+
},
205+
{
206+
type: "string",
207+
value: "org.example",
208+
},
209+
{
210+
type: "string",
211+
value: "HelloController",
212+
},
213+
{
214+
type: "string",
215+
value: "index",
216+
},
217+
{
218+
type: "string",
219+
value: "(String)",
220+
},
221+
{
222+
type: "boolean",
223+
value: true,
224+
},
225+
{
226+
type: "string",
227+
value: "example",
228+
},
229+
{
230+
type: "string",
231+
value: "summary",
232+
},
233+
],
234+
[
235+
{
236+
type: "entity",
237+
value: {
238+
label: "index",
239+
url: {
240+
type: "lineColumnLocation",
241+
uri: "home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
242+
startLine: 15,
243+
startColumn: 19,
244+
endLine: 15,
245+
endColumn: 23,
246+
},
247+
},
248+
},
249+
{
250+
type: "string",
251+
value: "org.example",
252+
},
253+
{
254+
type: "string",
255+
value: "HelloController",
256+
},
257+
{
258+
type: "string",
259+
value: "index",
260+
},
261+
{
262+
type: "string",
263+
value: "(String)",
264+
},
265+
{
266+
type: "boolean",
267+
value: true,
268+
},
269+
{
270+
type: "string",
271+
value: "example",
272+
},
273+
{
274+
type: "string",
275+
value: "summary",
276+
},
277+
],
278+
],
279+
});
280+
});
281+
});
2282

3283
describe("mapUrlValue", () => {
4284
it("should detect Windows whole-file locations", () => {

0 commit comments

Comments
 (0)