Skip to content
Snippets Groups Projects
Unverified Commit 7c101902 authored by Dalton's avatar Dalton Committed by GitHub
Browse files

Fix sort by aggregation name generation (#22232)

parent 5eadb33d
Branches
Tags
No related merge requests found
......@@ -168,12 +168,15 @@ export function getOrderByDescription(tableMetadata, query, options) {
return [
t`Sorted by `,
joinList(
orderBy.map(
([direction, field]) =>
getFieldName(tableMetadata, field, options) +
" " +
(direction === "asc" ? "ascending" : "descending"),
),
orderBy.map(([direction, field]) => {
const name = FIELD_REF.isAggregateField(field)
? getAggregationDescription(tableMetadata, query, options)
: getFieldName(tableMetadata, field, options);
return (
name + " " + (direction === "asc" ? "ascending" : "descending")
);
}),
" and ",
),
];
......
import { generateQueryDescription } from "metabase/lib/query/description";
import {
generateQueryDescription,
getOrderByDescription,
} from "metabase/lib/query/description";
import { ORDERS } from "__support__/sample_database_fixture";
import { ORDERS, PRODUCTS } from "__support__/sample_database_fixture";
const mockTableMetadata = {
display_name: "Order",
fields: [{ id: 1, display_name: "Total" }],
};
describe("generateQueryDescription", () => {
it("should work with multiple aggregations", () => {
expect(
generateQueryDescription(mockTableMetadata, {
"source-table": ORDERS.id,
aggregation: [["count"], ["sum", ["field", 1, null]]],
}),
).toEqual("Orders, Count and Sum of Total");
});
it("should work with named aggregations", () => {
expect(
generateQueryDescription(mockTableMetadata, {
"source-table": ORDERS.id,
aggregation: [
[
"aggregation-options",
["sum", ["field", 1, null]],
{ "display-name": "Revenue" },
describe("metabase/lib/query/description", () => {
describe("generateQueryDescription", () => {
it("should work with multiple aggregations", () => {
expect(
generateQueryDescription(mockTableMetadata, {
"source-table": ORDERS.id,
aggregation: [["count"], ["sum", ["field", 1, null]]],
}),
).toEqual("Orders, Count and Sum of Total");
});
it("should work with named aggregations", () => {
expect(
generateQueryDescription(mockTableMetadata, {
"source-table": ORDERS.id,
aggregation: [
[
"aggregation-options",
["sum", ["field", 1, null]],
{ "display-name": "Revenue" },
],
],
],
}),
).toEqual("Orders, Revenue");
}),
).toEqual("Orders, Revenue");
});
});
describe("getOrderByDescription", () => {
it("should work with fields", () => {
const query = {
"source-table": PRODUCTS.id,
"order-by": [["asc", ["field", PRODUCTS.CATEGORY.id, null]]],
};
expect(getOrderByDescription(PRODUCTS, query)).toEqual([
"Sorted by ",
["Category ascending"],
]);
});
it("should work with aggregations", () => {
const query = {
"source-table": PRODUCTS.id,
aggregation: [["count"]],
breakout: [["field", PRODUCTS.CATEGORY.id, null]],
"order-by": [["asc", ["aggregation", 0, null]]],
};
expect(getOrderByDescription(PRODUCTS, query)).toEqual([
"Sorted by ",
["Count ascending"],
]);
});
it("should work with expressions", () => {
const query = {
"source-table": PRODUCTS.id,
expressions: {
Foo: ["concat", "Foo ", ["field", 4, null]],
},
"order-by": [["asc", ["expression", "Foo", null]]],
};
expect(getOrderByDescription(PRODUCTS, query)).toEqual([
"Sorted by ",
["Foo ascending"],
]);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment