Skip to content

Commit a8f96f4

Browse files
committed
added tests
1 parent f917f5a commit a8f96f4

File tree

3 files changed

+95
-47
lines changed

3 files changed

+95
-47
lines changed

src/commands/serverCommand.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -895,37 +895,43 @@ async function _executeQuery(
895895
);
896896
} else {
897897
/* istanbul ignore next */
898-
const data = resultToBase64(results);
899-
if (data) {
900-
const active = ext.activeTextEditor;
901-
if (active) {
902-
const plot = <Plot>{
903-
charts: [{ data }],
904-
};
905-
const uri = await addWorkspaceFile(
906-
active.document.uri,
907-
"plot",
908-
".plot",
909-
);
910-
if (!workspaceHas(uri)) {
911-
await workspace.openTextDocument(uri);
912-
await openWith(uri, ChartEditorProvider.viewType, ViewColumn.Beside);
898+
if (ext.isResultsTabVisible) {
899+
const data = resultToBase64(results);
900+
if (data) {
901+
const active = ext.activeTextEditor;
902+
if (active) {
903+
const plot = <Plot>{
904+
charts: [{ data }],
905+
};
906+
const uri = await addWorkspaceFile(
907+
active.document.uri,
908+
"plot",
909+
".plot",
910+
);
911+
if (!workspaceHas(uri)) {
912+
await workspace.openTextDocument(uri);
913+
await openWith(
914+
uri,
915+
ChartEditorProvider.viewType,
916+
ViewColumn.Beside,
917+
);
918+
}
919+
await setUriContent(uri, JSON.stringify(plot));
913920
}
914-
await setUriContent(uri, JSON.stringify(plot));
921+
} else {
922+
await writeQueryResultsToView(
923+
results,
924+
query,
925+
connLabel,
926+
executorName,
927+
isInsights,
928+
isWorkbook ? "WORKBOOK" : "SCRATCHPAD",
929+
isPython,
930+
duration,
931+
isFromConnTree,
932+
connVersion,
933+
);
915934
}
916-
} else if (ext.isResultsTabVisible) {
917-
await writeQueryResultsToView(
918-
results,
919-
query,
920-
connLabel,
921-
executorName,
922-
isInsights,
923-
isWorkbook ? "WORKBOOK" : "SCRATCHPAD",
924-
isPython,
925-
duration,
926-
isFromConnTree,
927-
connVersion,
928-
);
929935
} else {
930936
await writeQueryResultsToConsole(
931937
results,

src/utils/queryUtils.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,30 +396,18 @@ export function formatScratchpadStacktrace(stacktrace: ScratchpadStacktrace) {
396396
.join("\n");
397397
}
398398

399+
const PNG = ["0x89", "0x50", "0x4e", "0x47", "0x0d", "0x0a", "0x1a", "0x0a"];
400+
399401
export function resultToBase64(result: any): string | undefined {
400402
const bytes =
401-
result?.columns?.type === "bytes" ? result?.columns?.values : result;
402-
403-
if (Array.isArray(bytes) && bytes.length > 7) {
404-
const png = [
405-
"0x89",
406-
"0x50",
407-
"0x4e",
408-
"0x47",
409-
"0x0d",
410-
"0x0a",
411-
"0x1a",
412-
"0x0a",
413-
];
414-
415-
for (let i = 0; i < png.length; i++) {
416-
if (bytes[i] !== png[i] && bytes[i] !== parseInt(png[i], 16)) {
403+
result?.columns?.type === "bytes" ? result.columns.values : result;
404+
if (Array.isArray(bytes) && bytes.length > 66) {
405+
for (let i = 0; i < PNG.length; i++) {
406+
if (bytes[i] !== PNG[i] && bytes[i] !== parseInt(PNG[i], 16)) {
417407
return undefined;
418408
}
419409
}
420-
421410
return `data:image/png;base64,${Buffer.from(bytes).toString("base64")}`;
422411
}
423-
424412
return undefined;
425413
}

test/suite/utils.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,4 +1974,58 @@ describe("Utils", () => {
19741974
);
19751975
});
19761976
});
1977+
1978+
describe("resultToBase64", () => {
1979+
const png = [
1980+
"0x89",
1981+
"0x50",
1982+
"0x4e",
1983+
"0x47",
1984+
"0x0d",
1985+
"0x0a",
1986+
"0x1a",
1987+
"0x0a",
1988+
];
1989+
const img = Array.from({ length: 59 }, () => "0x00");
1990+
1991+
it("should return undefined for undefined", () => {
1992+
const result = queryUtils.resultToBase64(undefined);
1993+
assert.strictEqual(result, undefined);
1994+
});
1995+
it("should return undefined for just signature", () => {
1996+
const result = queryUtils.resultToBase64(png);
1997+
assert.strictEqual(result, undefined);
1998+
});
1999+
it("should return undefined for undefined for bad signature", () => {
2000+
const result = queryUtils.resultToBase64([
2001+
...png.map((v) => parseInt(v, 16) + 1),
2002+
,
2003+
...img,
2004+
]);
2005+
assert.strictEqual(result, undefined);
2006+
});
2007+
it("should return base64 for minimum img str", () => {
2008+
const result = queryUtils.resultToBase64([...png, ...img]);
2009+
assert.ok(result.startsWith("data:image/png;base64"));
2010+
});
2011+
it("should return base64 for minimum img num", () => {
2012+
const result = queryUtils.resultToBase64([
2013+
...png.map((v) => parseInt(v, 16)),
2014+
...img.map((v) => parseInt(v, 16)),
2015+
]);
2016+
assert.ok(result.startsWith("data:image/png;base64"));
2017+
});
2018+
it("should return base64 for minimum img str for structuredText", () => {
2019+
const result = queryUtils.resultToBase64({
2020+
columns: { type: "bytes", values: [...png, ...img] },
2021+
});
2022+
assert.ok(result.startsWith("data:image/png;base64"));
2023+
});
2024+
it("should return undefined for bogus structuredText", () => {
2025+
const result = queryUtils.resultToBase64({
2026+
columns: { type: "bytes" },
2027+
});
2028+
assert.strictEqual(result, undefined);
2029+
});
2030+
});
19772031
});

0 commit comments

Comments
 (0)