From 964964cdd4219732184a865e06731fe63096ea7a Mon Sep 17 00:00:00 2001 From: XWang1024 Date: Thu, 9 Mar 2017 18:44:18 +0800 Subject: [PATCH] fix: `order` with empty array --- lib/operator.js | 2 +- test/operator.test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/operator.js b/lib/operator.js index 9aff1b0..343d595 100644 --- a/lib/operator.js +++ b/lib/operator.js @@ -242,7 +242,7 @@ proto._orders = function (orders) { } } } - return ' ORDER BY ' + values.join(', '); + return values.length ? ' ORDER BY ' + values.join(', ') : ''; }; proto._limit = function (limit, offset) { diff --git a/test/operator.test.js b/test/operator.test.js index f10e67b..89a9780 100644 --- a/test/operator.test.js +++ b/test/operator.test.js @@ -68,4 +68,23 @@ describe('operator.test.js', function () { } }); }); + + describe('_orders()', function () { + it('should get order sql', function* () { + let op = new Operator(); + assert.equal(op._orders(), ''); + assert.equal(op._orders([]), ''); + assert.equal(op._orders([null]), ''); + assert.equal(op._orders([function() {}]), ''); + assert.equal(op._orders(['id']), ' ORDER BY `id`'); + assert.equal(op._orders(['test.id']), ' ORDER BY `test`.`id`'); + assert.equal(op._orders(['id', 'name']), ' ORDER BY `id`, `name`'); + assert.equal(op._orders(['id', null]), ' ORDER BY `id`'); + assert.equal(op._orders(['id', ['name', 'desc']]), ' ORDER BY `id`, `name` DESC'); + assert.equal(op._orders(['id', ['name', 'ASC']]), ' ORDER BY `id`, `name` ASC'); + assert.equal(op._orders(['id', ['name', 'other']]), ' ORDER BY `id`, `name`'); + assert.equal(op._orders([['id', 'asc'], ['name', 'desc']]), ' ORDER BY `id` ASC, `name` DESC'); + assert.equal(op._orders([['id', 'asc'], ['name'], 'type']), ' ORDER BY `id` ASC, `name`, `type`'); + }); + }); });