Skip to content

Commit

Permalink
Refactor drills unit tests structure, split huge file by drill type
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskaber committed Nov 17, 2023
1 parent aa10916 commit 381ec0f
Show file tree
Hide file tree
Showing 15 changed files with 2,653 additions and 2,261 deletions.
2,338 changes: 131 additions & 2,207 deletions frontend/src/metabase-lib/drills.unit.spec.ts

Large diffs are not rendered by default.

130 changes: 129 additions & 1 deletion frontend/src/metabase-lib/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/* istanbul ignore file */

import { createMockMetadata } from "__support__/metadata";
import type { DatabaseId, DatasetQuery } from "metabase-types/api";
import type {
DatabaseId,
DatasetQuery,
DatasetColumn,
RowValue,
} from "metabase-types/api";
import {
createSampleDatabase,
ORDERS_ID,
} from "metabase-types/api/mocks/presets";
import Question from "metabase-lib/Question";
import type StructuredQuery from "metabase-lib/queries/StructuredQuery";
import type Metadata from "./metadata/Metadata";
import * as ML from "./v2";

Expand Down Expand Up @@ -123,3 +130,124 @@ export const findAggregationOperator = (
}
return operator;
};

export const getAvailableDrills = ({
question = Question.create({
metadata: SAMPLE_METADATA,
dataset_query: DEFAULT_QUERY,
}),
clickedColumnName,
stageIndex = -1,
columns,
rowValues,
clickType,
}: {
question?: Question;
clickedColumnName: string;
stageIndex?: number;
columns: Record<string, DatasetColumn>;
rowValues: Record<string, RowValue>;
clickType: "cell" | "header";
}) => {
const query = question._getMLv2Query();
const legacyQuery = question.query() as StructuredQuery;

const legacyColumns = legacyQuery.columns();
const column = columns[clickedColumnName];

const clickedCellValue = rowValues[clickedColumnName];

const row = legacyColumns.map(({ name }) => ({
col: columns[name],
value: rowValues[name],
}));

const dimensions =
legacyQuery.aggregations().length > 0
? row
.filter(
({ col }) =>
col?.source === "breakout" && col?.name !== clickedColumnName,
)
.map(({ value, col }) => ({ value, column: col }))
: undefined;

const drills =
clickType === "cell"
? ML.availableDrillThrus(
query,
stageIndex,
column,
clickedCellValue,
row,
dimensions,
)
: ML.availableDrillThrus(
query,
stageIndex,
column,
undefined,
undefined,
undefined,
);

const drillsDisplayInfo = drills.map(drill =>
ML.displayInfo(query, stageIndex, drill),
);

return {
drills,
drillsDisplayInfo,

query,
column,
stageIndex,
};
};

export const getAvailableDrillByType = ({
question = Question.create({
metadata: SAMPLE_METADATA,
dataset_query: DEFAULT_QUERY,
}),
clickedColumnName,
stageIndex = -1,
columns,
rowValues,
clickType,
drillType,
}: {
question?: Question;
clickedColumnName: string;
stageIndex?: number;
columns: Record<string, DatasetColumn>;
rowValues: Record<string, RowValue>;
clickType: "cell" | "header";
drillType: ML.DrillThruType;
}) => {
const { drills, drillsDisplayInfo, query } = getAvailableDrills({
question,
clickedColumnName,
stageIndex,
columns,
rowValues,
clickType,
});

const drillIndex = drillsDisplayInfo.findIndex(
({ type }) => type === drillType,
);
const drill = drills[drillIndex];
const drillDisplayInfo = drillsDisplayInfo[drillIndex];

if (!drill) {
throw new TypeError(`Failed to find ${drillType} drill`);
}

return {
drill,
drillDisplayInfo,
query,
stageIndex,
};
};
131 changes: 131 additions & 0 deletions frontend/src/metabase-lib/tests/drills-column-filter.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { DrillThruType } from "metabase-lib";
import type { DrillDisplayInfoTestCase } from "metabase-lib/tests/drills-common";
import { getDrillsQueryParameters } from "metabase-lib/tests/drills-common";
import { getAvailableDrillByType } from "metabase-lib/test-helpers";

const DRILL_TYPE: DrillThruType = "drill-thru/column-filter";

describe("drill-thru/column-filter", () => {
describe("availableDrillThrus", () => {
it.each<DrillDisplayInfoTestCase>([
{
clickType: "header",
queryType: "unaggregated",
columnName: "ID",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "unaggregated",
columnName: "USER_ID",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "unaggregated",
columnName: "TAX",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "unaggregated",
columnName: "DISCOUNT",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "unaggregated",
columnName: "CREATED_AT",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: null,
},
},
{
clickType: "header",
queryType: "unaggregated",
columnName: "QUANTITY",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "aggregated",
columnName: "PRODUCT_ID",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "aggregated",
columnName: "PRODUCT_ID",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "aggregated",
columnName: "CREATED_AT",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: null,
},
},
{
clickType: "header",
queryType: "aggregated",
columnName: "count",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
{
clickType: "header",
queryType: "aggregated",
columnName: "max",
expectedParameters: {
type: "drill-thru/column-filter",
initialOp: expect.objectContaining({ short: "=" }),
},
},
])(
`should return "${DRILL_TYPE}" drill config for $columnName $clickType in $queryType query`,
({
columnName,
clickType,
queryType,
queryTable = "ORDERS",
customQuestion,
expectedParameters,
}) => {
const { drillDisplayInfo } = getAvailableDrillByType({
drillType: DRILL_TYPE,
clickType,
clickedColumnName: columnName,
...getDrillsQueryParameters(queryType, queryTable, customQuestion),
});

expect(drillDisplayInfo).toEqual(expectedParameters);
},
);
});
});

0 comments on commit 381ec0f

Please sign in to comment.