Add tests for bqrsToResultSet

This commit is contained in:
Koen Vlaswinkel
2023-11-27 14:25:59 +01:00
parent a0c85b78c5
commit b8ec847253
3 changed files with 316 additions and 16 deletions

View File

@@ -21,7 +21,7 @@ export type BqrsColumnKind =
| typeof BqrsColumnKindCode.DATE
| typeof BqrsColumnKindCode.ENTITY;
interface BqrsSchemaColumn {
export interface BqrsSchemaColumn {
name?: string;
kind: BqrsColumnKind;
}

View File

@@ -8,6 +8,7 @@ import {
BqrsResultSetSchema,
BqrsUrlValue as BqrsUrlValue,
BqrsWholeFileLocation,
BqrsSchemaColumn,
} from "./bqrs-cli-types";
import {
CellValue,
@@ -28,26 +29,37 @@ export function bqrsToResultSet(
): RawResultSet {
const name = schema.name;
const totalRowCount = schema.rows;
const nextPageOffset = chunk.next;
const columns = schema.columns.map(
(column): Column => ({
kind: mapColumnKind(column.kind),
name: column.name,
}),
);
const columns = schema.columns.map(mapColumn);
const rows = chunk.tuples.map(
(tuple): Row => tuple.map((cell): CellValue => mapCellValue(cell)),
);
return {
const resultSet: RawResultSet = {
name,
totalRowCount,
columns,
rows,
nextPageOffset,
};
if (chunk.next) {
resultSet.nextPageOffset = chunk.next;
}
return resultSet;
}
function mapColumn(column: BqrsSchemaColumn): Column {
const result: Column = {
kind: mapColumnKind(column.kind),
};
if (column.name) {
result.name = column.name;
}
return result;
}
function mapColumnKind(kind: BqrsColumnKind): ColumnKind {
@@ -95,11 +107,19 @@ function mapCellValue(cellValue: BqrsCellValue): CellValue {
}
function mapEntityValue(cellValue: BqrsEntityValue): EntityValue {
return {
url: cellValue.url === undefined ? undefined : mapUrlValue(cellValue.url),
label: cellValue.label,
id: cellValue.id,
};
const result: EntityValue = {};
if (cellValue.id) {
result.id = cellValue.id;
}
if (cellValue.label) {
result.label = cellValue.label;
}
if (cellValue.url) {
result.url = mapUrlValue(cellValue.url);
}
return result;
}
export function mapUrlValue(urlValue: BqrsUrlValue): UrlValue | undefined {

View File

@@ -1,4 +1,284 @@
import { mapUrlValue } from "../../../src/common/bqrs-raw-results-mapper";
import {
bqrsToResultSet,
mapUrlValue,
} from "../../../src/common/bqrs-raw-results-mapper";
import {
BqrsResultSetSchema,
DecodedBqrsChunk,
} from "../../../src/common/bqrs-cli-types";
import { ColumnKind } from "../../../src/common/raw-result-types";
describe("bqrsToResultSet", () => {
const schema: BqrsResultSetSchema = {
name: "#select",
columns: [
{
name: "endpoint",
kind: "e",
},
{
kind: "s",
},
{
kind: "s",
},
{
kind: "s",
},
{
kind: "s",
},
{
name: "supported",
kind: "b",
},
{
kind: "s",
},
{
name: "type",
kind: "s",
},
],
rows: 2,
};
const chunk: DecodedBqrsChunk = {
columns: [
{ name: "endpoint", kind: "Entity" },
{ kind: "String" },
{ kind: "String" },
{ kind: "String" },
{ kind: "String" },
{ name: "supported", kind: "Boolean" },
{ kind: "String" },
{ name: "type", kind: "String" },
],
tuples: [
[
{
label: "connect",
url: {
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
startLine: 22,
startColumn: 19,
endLine: 22,
endColumn: 25,
},
},
"org.example",
"HelloController",
"connect",
"(String)",
false,
"example",
"",
],
[
{
label: "index",
url: {
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
startLine: 13,
startColumn: 19,
endLine: 13,
endColumn: 23,
},
},
"org.example",
"HelloController",
"index",
"(String)",
true,
"example",
"summary",
],
[
{
label: "index",
url: "file://home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java:15:19:15:23",
},
"org.example",
"HelloController",
"index",
"(String)",
true,
"example",
"summary",
],
],
};
it("creates a result set", () => {
expect(bqrsToResultSet(schema, chunk)).toEqual({
name: "#select",
totalRowCount: 2,
columns: [
{
name: "endpoint",
kind: ColumnKind.Entity,
},
{
kind: ColumnKind.String,
},
{
kind: ColumnKind.String,
},
{
kind: ColumnKind.String,
},
{
kind: ColumnKind.String,
},
{
name: "supported",
kind: ColumnKind.Boolean,
},
{
kind: ColumnKind.String,
},
{
name: "type",
kind: ColumnKind.String,
},
],
rows: [
[
{
type: "entity",
value: {
label: "connect",
url: {
type: "lineColumnLocation",
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
startLine: 22,
startColumn: 19,
endLine: 22,
endColumn: 25,
},
},
},
{
type: "string",
value: "org.example",
},
{
type: "string",
value: "HelloController",
},
{
type: "string",
value: "connect",
},
{
type: "string",
value: "(String)",
},
{
type: "boolean",
value: false,
},
{
type: "string",
value: "example",
},
{
type: "string",
value: "",
},
],
[
{
type: "entity",
value: {
label: "index",
url: {
type: "lineColumnLocation",
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
startLine: 13,
startColumn: 19,
endLine: 13,
endColumn: 23,
},
},
},
{
type: "string",
value: "org.example",
},
{
type: "string",
value: "HelloController",
},
{
type: "string",
value: "index",
},
{
type: "string",
value: "(String)",
},
{
type: "boolean",
value: true,
},
{
type: "string",
value: "example",
},
{
type: "string",
value: "summary",
},
],
[
{
type: "entity",
value: {
label: "index",
url: {
type: "lineColumnLocation",
uri: "home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
startLine: 15,
startColumn: 19,
endLine: 15,
endColumn: 23,
},
},
},
{
type: "string",
value: "org.example",
},
{
type: "string",
value: "HelloController",
},
{
type: "string",
value: "index",
},
{
type: "string",
value: "(String)",
},
{
type: "boolean",
value: true,
},
{
type: "string",
value: "example",
},
{
type: "string",
value: "summary",
},
],
],
});
});
});
describe("mapUrlValue", () => {
it("should detect Windows whole-file locations", () => {