Browse Source

Fix test

pull/122/head
Jeremy Nagel 4 years ago
parent
commit
31b27b56ca
  1. 43
      lib/xsql.js
  2. 17
      tests/tests.js

43
lib/xsql.js

@ -348,13 +348,16 @@ class Xsql {
if (i) { if (i) {
queryParamsObj.query += ", "; queryParamsObj.query += ", ";
} }
if (orderByCols[i][0] === "-") { const aggregationFunction = this.getAggregationFunction(orderByCols[i]);
let len = orderByCols[i].length; const columnName = this.getColumnNameWithoutAggregationFunctions(orderByCols[i]);
queryParamsObj.query += " ?? DESC"; const orderByDirection = orderByCols[i][0] === "-" ? 'DESC' : 'ASC';
queryParamsObj.params.push(orderByCols[i].substring(1, len));
if (aggregationFunction) {
queryParamsObj.query += `${aggregationFunction}(??) ${orderByDirection}`;
queryParamsObj.params.push(columnName);
} else { } else {
queryParamsObj.query += " ?? ASC"; queryParamsObj.query += `?? ${orderByDirection}`;
queryParamsObj.params.push(orderByCols[i]); queryParamsObj.params.push(columnName);
} }
} }
} }
@ -376,7 +379,7 @@ class Xsql {
getColumnsForGroupBy(tableName, reqQueryParams, queryParamsObj) { getColumnsForGroupBy(tableName, reqQueryParams, queryParamsObj) {
const updatedQueryParams = Object.assign({}, reqQueryParams); const updatedQueryParams = Object.assign({}, reqQueryParams);
if ("_groupbyfields" in updatedQueryParams) { if ('_groupbyfields' in updatedQueryParams) {
// allows you to group by different fields than you have in the select // allows you to group by different fields than you have in the select
updatedQueryParams['_fields'] = updatedQueryParams['_groupbyfields']; updatedQueryParams['_fields'] = updatedQueryParams['_groupbyfields'];
} }
@ -424,21 +427,39 @@ class Xsql {
if (i) { if (i) {
queryParamsObj.query += ","; queryParamsObj.query += ",";
} }
queryParamsObj.query += "??"; const aggregationFunction = this.getAggregationFunction(cols[i]);
queryParamsObj.params.push(cols[i]);
if (aggregationFunction) {
queryParamsObj.query += `${aggregationFunction}(??)`;
const columnName = this.getColumnNameWithoutAggregationFunctions(cols[i]);
queryParamsObj.params.push(columnName);
} else {
queryParamsObj.query += "??";
queryParamsObj.params.push(cols[i]);
}
} }
return cols.join(","); return cols.join(",");
} }
getAggregationFunction(rawColumnName) {
const AGGREGATION_FUNCTION_REGEX = /^[-]?(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/i;
const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX);
if (aggFuncMatch && aggFuncMatch.length === 3) {
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
return aggFuncMatch[1];
}
return null;
}
getColumnNameWithoutAggregationFunctions(rawColumnName) { getColumnNameWithoutAggregationFunctions(rawColumnName) {
const AGGREGATION_FUNCTION_REGEX = /^(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/; const AGGREGATION_FUNCTION_REGEX = /^[-]?(AVG|BIT_AND|BIT_OR|BIT_XOR|COUNT|COUNTDISTINCT|GROUP_CONCAT|JSON_ARRAYAGG|JSON_OBJECTAGG|MAX|MIN|STD|STDDEV|STDDEV_POP|STDDEV_SAMP|SUM|VAR_POP|VAR_SAMP|VARIANCE)\((.*)\)$/i;
const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX); const aggFuncMatch = rawColumnName.match(AGGREGATION_FUNCTION_REGEX);
if (aggFuncMatch && aggFuncMatch.length === 3) { if (aggFuncMatch && aggFuncMatch.length === 3) {
// match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined] // match will look like (3) ["AVG(timestamp)", "AVG", "timestamp", index: 0, input: "AVG(timestamp)", groups: undefined]
return aggFuncMatch[2]; return aggFuncMatch[2];
} }
return rawColumnName; return rawColumnName.replace(/-/, '');
} }
removeUnknownColumns(inputColumns, tableName) { removeUnknownColumns(inputColumns, tableName) {

17
tests/tests.js

@ -1178,23 +1178,20 @@ describe("xmysql : tests", function() {
} }
); );
it( it(`GET ${apiPrefix}customers/groupby?_fields=city&_sort=city&_groupbyfields=country should PASS`,
"GET " +
apiPrefix +
"customers/groupby?_fields=city&_sort=city&_groupbyfields=country should PASS",
function(done) { function(done) {
//post to an url with data
agent agent
.get(apiPrefix + "customers/groupby?_fields=avg(creditLimit),country,city&_sort=city&_groupbyfields=country,city") //enter url .get(apiPrefix + "customers/groupby?_fields=avg(creditLimit),country,city&_sort=-avg(creditLimit),city&_groupbyfields=country,city")
.expect(200) //200 for success 4xx for failure .expect(200)
.end(function(err, res) { .end(function(err, res) {
// Handle /api/v error
if (err) { if (err) {
return done(err); return done(err);
} }
//validate response res.body[0]['avg(`creditLimit`)'].should.be.equals(210500);
res.body[0]["city"].should.be.equals("Aachen"); res.body[0]["country"].should.be.equals("USA");
res.body[0]["city"].should.be.equals("San Rafael");
res.body[0]["_count"].should.be.equals(1);
res.body.length.should.be.equals(95); res.body.length.should.be.equals(95);
return done(); return done();

Loading…
Cancel
Save