Skip to content
Snippets Groups Projects
Commit 0abfa3ca authored by Tom Robinson's avatar Tom Robinson
Browse files

Add some tests for SortAction and fieldRefForColumn

parent cbc424d3
No related branches found
No related tags found
No related merge requests found
......@@ -1436,6 +1436,13 @@ export const orders_count_by_id_card = {
},
};
export const clickedCreatedAtHeader = {
column: {
...metadata.field(ORDERS_CREATED_DATE_FIELD_ID),
source: "fields",
},
};
export const clickedFloatHeader = {
column: {
...metadata.field(ORDERS_TOTAL_FIELD_ID),
......@@ -1511,6 +1518,31 @@ const NoFieldsMetadata = getMetadata(
);
export const questionNoFields = new Question(NoFieldsMetadata, card);
// COUNT BY CREATED AT
export const countByCreatedAtQuestion = question
.query()
.addAggregation(["count"])
.addBreakout(["field-id", ORDERS_CREATED_DATE_FIELD_ID])
.question();
export const clickedCountAggregationHeader = {
column: {
name: "count",
display_name: "count",
base_type: "type/Integer",
special_type: "type/Number",
source: "aggregation",
},
};
export const clickedCreatedAtBreakoutHeader = {
column: {
...metadata.field(ORDERS_CREATED_DATE_FIELD_ID),
source: "breakout",
},
};
export const orders_past_300_days_segment = {
id: null,
name: "Past 300 days",
......
......@@ -567,7 +567,11 @@ describe("StructuredQuery unit tests", () => {
describe("FIELD REFERENCE METHODS", () => {
describe("fieldReferenceForColumn", () => {
pending();
it('should return `["field-id", 1]` for a normal column', () => {
expect(
query.fieldReferenceForColumn({ id: ORDERS_TOTAL_FIELD_ID }),
).toEqual(["field-id", ORDERS_TOTAL_FIELD_ID]);
});
});
describe("parseFieldReference", () => {
......
import { fieldRefForColumn } from "metabase/lib/dataset";
const FIELD_COLUMN = { id: 1 };
const FK_COLUMN = { id: 1, fk_field_id: 2 };
const EXPRESSION_COLUMN = { expression_name: "foo" };
const AGGREGATION_COLUMN = { source: "aggregation" };
describe("metabase/util/dataset", () => {
describe("fieldRefForColumn", () => {
it('should return `["field-id", 1]` for a normal column', () => {
expect(fieldRefForColumn(FIELD_COLUMN)).toEqual(["field-id", 1]);
});
it('should return `["fk->", 2, 1]` for a fk column', () => {
expect(fieldRefForColumn(FK_COLUMN)).toEqual(["fk->", 2, 1]);
});
it('should return `["expression", 2, 1]` for a fk column', () => {
expect(fieldRefForColumn(EXPRESSION_COLUMN)).toEqual([
"expression",
"foo",
]);
});
describe("aggregation column", () => {
// this is an unfortunate effect of the backend not returning enough information to determine the aggregation index from the column
it("should return `null` for aggregation column if list of columns was provided", () => {
expect(fieldRefForColumn(AGGREGATION_COLUMN)).toEqual(null);
});
it('should return `["aggregation", 0]` for aggregation column if list of columns was provided', () => {
expect(
fieldRefForColumn(AGGREGATION_COLUMN, [AGGREGATION_COLUMN]),
).toEqual(["aggregation", 0]);
});
it('should return `["aggregation", 1]` for second aggregation column if list of columns was provided', () => {
expect(
fieldRefForColumn(AGGREGATION_COLUMN, [
{ source: "aggregation" },
AGGREGATION_COLUMN,
]),
).toEqual(["aggregation", 1]);
});
});
// NOTE: sometimes id is an MBQL clause itself, e.x. nested queries
it("should return `id` if is an MBQL clause", () => {
expect(fieldRefForColumn({ id: ["field-id", 3] })).toEqual([
"field-id",
3,
]);
});
});
});
/* eslint-disable flowtype/require-valid-file-annotation */
import {
question,
clickedCreatedAtHeader,
countByCreatedAtQuestion,
clickedCountAggregationHeader,
clickedCreatedAtBreakoutHeader,
ORDERS_TABLE_ID,
ORDERS_CREATED_DATE_FIELD_ID,
} from "__support__/sample_dataset_fixture";
import SortAction from "metabase/modes/components/drill/SortAction";
// shortcut get the dataset_query's query from an action
const query = action =>
action
.question()
.query()
.query();
describe("SortAction", () => {
it("should not be valid for top level actions", () => {
expect(SortAction({ question })).toHaveLength(0);
});
it("should return ascending and descending for unsorted column", () => {
const actions = SortAction({
question,
clicked: clickedCreatedAtHeader,
});
expect(actions).toHaveLength(2);
expect(actions[0].title).toEqual("Ascending");
expect(actions[1].title).toEqual("Descending");
expect(query(actions[0])).toEqual({
"order-by": [["asc", ["field-id", 1]]],
"source-table": 1,
});
expect(query(actions[1])).toEqual({
"order-by": [["desc", ["field-id", 1]]],
"source-table": 1,
});
});
it("should return ascending for an already sorted column", () => {
const actions = SortAction({
question: question
.query()
.addSort(["asc", ["field-id", ORDERS_CREATED_DATE_FIELD_ID]])
.question(),
clicked: clickedCreatedAtHeader,
});
expect(actions).toHaveLength(1);
expect(actions[0].title).toEqual("Descending");
expect(query(actions[0])).toEqual({
"order-by": [["desc", ["field-id", 1]]],
"source-table": 1,
});
});
it("should sort by aggregation", () => {
const actions = SortAction({
question: countByCreatedAtQuestion,
clicked: clickedCountAggregationHeader,
});
expect(query(actions[0])).toEqual({
aggregation: [["count"]],
breakout: [["field-id", ORDERS_CREATED_DATE_FIELD_ID]],
"order-by": [["asc", ["aggregation", 0]]],
"source-table": ORDERS_TABLE_ID,
});
});
it("should sort by breakout", () => {
const actions = SortAction({
question: countByCreatedAtQuestion,
clicked: clickedCreatedAtBreakoutHeader,
});
expect(query(actions[0])).toEqual({
aggregation: [["count"]],
breakout: [["field-id", ORDERS_CREATED_DATE_FIELD_ID]],
"order-by": [["asc", ["field-id", 1]]],
"source-table": ORDERS_TABLE_ID,
});
});
});
......@@ -3,6 +3,7 @@ import {
getCardAfterVisualizationClick,
getColumnCardinality,
getXValues,
getFriendlyName,
} from "metabase/visualizations/lib/utils";
import _ from "underscore";
......@@ -209,4 +210,23 @@ describe("metabase/visualization/lib/utils", () => {
expect(getColumnCardinality(cols, rows, 0)).toEqual(3);
});
});
describe("getFriendlyName", () => {
it("should return friendly name for built-in aggregations", () => {
expect(getFriendlyName({ name: "avg", display_name: "avg" })).toBe(
"Average",
);
});
it("should return friendly name for duplicate built-in aggregations", () => {
expect(getFriendlyName({ name: "avg_2", display_name: "avg" })).toBe(
"Average",
);
});
it("should return display_name for non built-in aggregations", () => {
expect(getFriendlyName({ name: "foo", display_name: "Foo" })).toBe("Foo");
});
it("should return display_name for built-in aggregations", () => {
expect(getFriendlyName({ name: "avg", display_name: "Foo" })).toBe("Foo");
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment