feat: 🎸 Add custom sort support to sql parser (#2959)
* feat: 🎸 Add custom sort support to sql parser * test: 💍 fix column numner * refactor: 💡 apply pr feedback * fix: 🐛 use every method for checking attrs * refactor: 💡 PR feedback + ensure default sort * refactor: 💡 Consolidate attributes field when sorting ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17441 --------- Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
This commit is contained in:
1 parent
eab93cf6c5
commit
d414df4ca4
29 files changed
+333
-260
No files matched your search
@@ -37,7 +37,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
]);
|
||||
|
||||
const querySort = { attribute: 'name' };
|
||||
const querySort = { attributes: ['name'] };
|
||||
|
||||
// Note: `name` is not defined as a schema attribute but exists on the record
|
||||
const schema = { attributes: new Map() };
|
||||
@@ -55,7 +55,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
});
|
||||
|
||||
const querySort = {
|
||||
attribute: 'name',
|
||||
attributes: ['name'],
|
||||
direction: 'invalid sort direction',
|
||||
};
|
||||
const schema = { attributes: new Map() };
|
||||
@@ -119,7 +119,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySort = { attribute: 'id' };
|
||||
const querySort = { attributes: ['id'] };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('id', { type: 'string' });
|
||||
const sortedResults = sortResults(shuffledResults, { querySort, schema });
|
||||
@@ -152,7 +152,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySort = { attribute: 'name' };
|
||||
const querySort = { attributes: ['name'] };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('name', { type: 'string' });
|
||||
const sortedResults = sortResults(shuffledResults, { querySort, schema });
|
||||
@@ -182,8 +182,8 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySortAsc = { attribute: 'name', direction: 'asc' };
|
||||
const querySortDesc = { attribute: 'name', direction: 'desc' };
|
||||
const querySortAsc = { attributes: ['name'], direction: 'asc' };
|
||||
const querySortDesc = { attributes: ['name'], direction: 'desc' };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('name', { type: 'string' });
|
||||
const sortedResultsAsc = sortResults(shuffledResults, {
|
||||
@@ -227,8 +227,8 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result04,
|
||||
result05,
|
||||
]);
|
||||
const querySortAsc = { attribute: 'name', direction: 'asc' };
|
||||
const querySortDesc = { attribute: 'name', direction: 'desc' };
|
||||
const querySortAsc = { attributes: ['name'], direction: 'asc' };
|
||||
const querySortDesc = { attributes: ['name'], direction: 'desc' };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('name', { type: 'string' });
|
||||
|
||||
@@ -272,7 +272,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySort = { attribute: 'date' };
|
||||
const querySort = { attributes: ['date'] };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('date', { type: 'date' });
|
||||
|
||||
@@ -304,8 +304,8 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySortAsc = { attribute: 'date', direction: 'asc' };
|
||||
const querySortDesc = { attribute: 'date', direction: 'desc' };
|
||||
const querySortAsc = { attributes: ['date'], direction: 'asc' };
|
||||
const querySortDesc = { attributes: ['date'], direction: 'desc' };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('date', { type: 'date' });
|
||||
|
||||
@@ -350,7 +350,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySort = { attribute: 'sessions' };
|
||||
const querySort = { attributes: ['sessions'] };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('sessions', { type: 'number' });
|
||||
|
||||
@@ -382,8 +382,8 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySortAsc = { attribute: 'sessions', direction: 'asc' };
|
||||
const querySortDesc = { attribute: 'sessions', direction: 'desc' };
|
||||
const querySortAsc = { attributes: ['sessions'], direction: 'asc' };
|
||||
const querySortDesc = { attributes: ['sessions'], direction: 'desc' };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('sessions', { type: 'number' });
|
||||
|
||||
@@ -419,7 +419,7 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySort = { attribute: 'active' };
|
||||
const querySort = { attributes: ['active'] };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('active', { type: 'boolean' });
|
||||
|
||||
@@ -442,8 +442,8 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
result03,
|
||||
result04,
|
||||
]);
|
||||
const querySortAsc = { attribute: 'active', direction: 'asc' };
|
||||
const querySortDesc = { attribute: 'active', direction: 'desc' };
|
||||
const querySortAsc = { attributes: ['active'], direction: 'asc' };
|
||||
const querySortDesc = { attributes: ['active'], direction: 'desc' };
|
||||
const schema = { attributes: new Map() };
|
||||
schema.attributes.set('active', { type: 'boolean' });
|
||||
|
||||
@@ -482,32 +482,39 @@ module('Unit | Utility | sortResults', function (hooks) {
|
||||
name: 'target4',
|
||||
});
|
||||
|
||||
const sortFunction = (recordA, recordB) => {
|
||||
const nameMap = {
|
||||
target3: 'Alpha',
|
||||
target4: 'Beta',
|
||||
target1: 'Delta',
|
||||
target2: 'Epsilon',
|
||||
};
|
||||
return String(nameMap[recordA.attributes.name]).localeCompare(
|
||||
String(nameMap[recordB.attributes.name]),
|
||||
);
|
||||
const nameMap = {
|
||||
target3: 'Alpha',
|
||||
target4: 'Beta',
|
||||
target1: 'Delta',
|
||||
target2: 'Epsilon',
|
||||
};
|
||||
|
||||
const schema = { attributes: new Map() };
|
||||
|
||||
test.each(
|
||||
'it sorts by `name` using custom sorting function',
|
||||
{
|
||||
'ascending (default)': {
|
||||
querySort: { sortFunction },
|
||||
querySort: {
|
||||
attributes: ['name'],
|
||||
customSort: { attributeMap: nameMap },
|
||||
},
|
||||
expectedResults: ['target3', 'target4', 'target1', 'target2'],
|
||||
},
|
||||
descending: {
|
||||
querySort: { sortFunction, direction: 'desc' },
|
||||
querySort: {
|
||||
attributes: ['name'],
|
||||
customSort: { attributeMap: nameMap },
|
||||
direction: 'desc',
|
||||
},
|
||||
expectedResults: ['target2', 'target1', 'target4', 'target3'],
|
||||
},
|
||||
ascending: {
|
||||
querySort: { sortFunction, direction: 'asc' },
|
||||
querySort: {
|
||||
attributes: ['name'],
|
||||
customSort: { attributeMap: nameMap },
|
||||
direction: 'asc',
|
||||
},
|
||||
expectedResults: ['target3', 'target4', 'target1', 'target2'],
|
||||
},
|
||||
},
|
||||
|
||||
@@ -28,7 +28,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
SELECT * FROM "target"
|
||||
WHERE rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
);
|
||||
@@ -44,7 +44,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM token
|
||||
SELECT * FROM "token"
|
||||
WHERE (id = ?)`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(parameters, ['tokenKey']);
|
||||
@@ -59,8 +59,8 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT count(*) as total FROM target
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
SELECT count(*) as total FROM "target"
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(parameters, []);
|
||||
});
|
||||
@@ -160,7 +160,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
SELECT * FROM "target"
|
||||
${expectedWhereClause}
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
);
|
||||
@@ -176,15 +176,55 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
],
|
||||
function (assert, [direction, expectedDirection]) {
|
||||
const query = {
|
||||
sort: { attribute: 'name', direction },
|
||||
sort: { attributes: ['name'], direction },
|
||||
};
|
||||
|
||||
const { sql, parameters } = generateSQLExpressions('target', query);
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
ORDER BY name ${expectedDirection}`.removeExtraWhiteSpace(),
|
||||
SELECT * FROM "target"
|
||||
ORDER BY name COLLATE NOCASE ${expectedDirection}, name ${expectedDirection}`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(parameters, []);
|
||||
},
|
||||
);
|
||||
|
||||
test.each(
|
||||
'it generates sort order clause correctly',
|
||||
{
|
||||
'sort on multiple attributes and coalesced': {
|
||||
sort: {
|
||||
attributes: ['name', 'id'],
|
||||
isCoalesced: true,
|
||||
},
|
||||
expectedOrderByClause: `ORDER BY COALESCE(name, id) COLLATE NOCASE DESC, COALESCE(name, id) DESC`,
|
||||
},
|
||||
'sort on multiple attributes': {
|
||||
sort: {
|
||||
attributes: ['name', 'id'],
|
||||
},
|
||||
expectedOrderByClause: `ORDER BY name COLLATE NOCASE DESC, id COLLATE NOCASE DESC, name DESC, id DESC`,
|
||||
},
|
||||
'sort on mapped attributes': {
|
||||
sort: {
|
||||
attributes: ['type'],
|
||||
customSort: { attributeMap: { ssh: 'SSH', tcp: 'Generic TCP' } },
|
||||
},
|
||||
expectedOrderByClause: `ORDER BY CASE type WHEN 'ssh' THEN 'SSH' WHEN 'tcp' THEN 'Generic TCP' END DESC`,
|
||||
},
|
||||
},
|
||||
function (assert, { sort, expectedOrderByClause }) {
|
||||
const query = {
|
||||
sort: { ...sort, direction: 'desc' },
|
||||
};
|
||||
|
||||
const { sql, parameters } = generateSQLExpressions('target', query);
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM "target"
|
||||
${expectedOrderByClause}`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(parameters, []);
|
||||
},
|
||||
@@ -206,7 +246,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
SELECT * FROM "target"
|
||||
ORDER BY created_time DESC
|
||||
LIMIT ? OFFSET ?`.removeExtraWhiteSpace(),
|
||||
);
|
||||
@@ -230,7 +270,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
SELECT * FROM "target"
|
||||
WHERE (id != ? AND id != ?) AND (status = ? OR status = ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
);
|
||||
@@ -254,7 +294,7 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
},
|
||||
created_time: [{ gte: date }],
|
||||
},
|
||||
sort: { attribute: 'name', direction: 'desc' },
|
||||
sort: { attributes: ['name'], direction: 'desc' },
|
||||
};
|
||||
|
||||
const { sql, parameters } = generateSQLExpressions('target', query, {
|
||||
@@ -266,9 +306,9 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
`
|
||||
SELECT data FROM target
|
||||
SELECT data FROM "target"
|
||||
WHERE (type = ?) AND (status = ? OR status = ?) AND (created_time >= ?) AND rowid IN (SELECT rowid FROM target_fts WHERE target_fts MATCH ?)
|
||||
ORDER BY name DESC
|
||||
ORDER BY name COLLATE NOCASE DESC, name DESC
|
||||
LIMIT ? OFFSET ?`.removeExtraWhiteSpace(),
|
||||
);
|
||||
|
||||
@@ -290,19 +330,38 @@ module('Unit | Utility | sqlite-query', function (hooks) {
|
||||
emptySearch: { search: '' },
|
||||
emptyFilter: { filters: {} },
|
||||
emptyFilterAttributes: { filters: { type: [] } },
|
||||
invalidSortAttribute: { attribute: 'test', direction: 'asc' },
|
||||
invalidSortAttribute: {
|
||||
sort: { attributes: ['test'], direction: 'asc' },
|
||||
},
|
||||
invalidCoalescedSortAttribute: {
|
||||
sort: {
|
||||
attributes: ['id', 'test'],
|
||||
direction: 'asc',
|
||||
isCoalesced: true,
|
||||
},
|
||||
},
|
||||
onlyPage: { page: 10 },
|
||||
onlyPageSize: { pageSize: 5 },
|
||||
},
|
||||
function (assert, query) {
|
||||
const { sql, parameters } = generateSQLExpressions('target', query);
|
||||
const { sql: targetSql, parameters: targetParameters } =
|
||||
generateSQLExpressions('target', query);
|
||||
assert.strictEqual(
|
||||
sql,
|
||||
targetSql,
|
||||
`
|
||||
SELECT * FROM target
|
||||
SELECT * FROM "target"
|
||||
ORDER BY created_time DESC`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(parameters, []);
|
||||
assert.deepEqual(targetParameters, []);
|
||||
|
||||
const { sql: resourceSql, parameters: resourceParameters } =
|
||||
generateSQLExpressions('resource', query);
|
||||
assert.strictEqual(
|
||||
resourceSql,
|
||||
`
|
||||
SELECT * FROM "resource"`.removeExtraWhiteSpace(),
|
||||
);
|
||||
assert.deepEqual(resourceParameters, []);
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user