From cfa70c459ccab83c55575f8cc3a199f2b6b2a8a6 Mon Sep 17 00:00:00 2001 From: o1lab Date: Sun, 20 May 2018 12:40:09 +0100 Subject: [PATCH] Fix #33: _size to return right number of records back. --- README.md | 3 ++ lib/xsql.js | 11 +++++-- tests/tests.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 39391ccc83..c0b4498d7e 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,9 @@ By default 20 records and max of 100 are returned per GET request on a table. /api/payments?_p=2&_size=50 ``` +When _size is greater than 100 - number of records defaults to 100 (i.e maximum) + +When _size is less than or equal to 0 - number of records defaults to 20 (i.e minimum) ## Order by / Sorting diff --git a/lib/xsql.js b/lib/xsql.js index e590380cb9..fd31a505f3 100644 --- a/lib/xsql.js +++ b/lib/xsql.js @@ -193,15 +193,20 @@ class Xsql { reqParams._index = 0; reqParams._len = 20; - if ('_size' in reqParams && parseInt(reqParams._size) < 100) { - reqParams._len = parseInt(reqParams._size) + if('_size' in reqParams) { + if (parseInt(reqParams._size) > 0 && parseInt(reqParams._size) <= 100) { + reqParams._len = parseInt(reqParams._size) + } else if (parseInt(reqParams._size) > 100) { + reqParams._len = 100 + } } + if ('_p' in reqParams && parseInt(reqParams._p) > 0) { reqParams._index = (parseInt(reqParams._p) - 1) * reqParams._len; } - //console.log(reqParams._index, reqParams._len); + console.log(reqParams._index, reqParams._len); return [reqParams._index, reqParams._len] diff --git a/tests/tests.js b/tests/tests.js index a97259ac5c..7f5cf1137b 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -241,6 +241,89 @@ describe('xmysql : tests', function () { }); + + it('GET ' + apiPrefix + 'customers should PASS', function (done) { + + //http get an url + agent.get(apiPrefix + 'customers') // api url + .expect(200) // 2xx for success and 4xx for failure + .end(function (err, res) { + // Handle /api/tables error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equal(20); + + return done(); + + }); + + }); + + it('GET ' + apiPrefix + 'customers?_size=100 should PASS', function (done) { + + //http get an url + agent.get(apiPrefix + 'customers?_size=100') // api url + .expect(200) // 2xx for success and 4xx for failure + .end(function (err, res) { + // Handle /api/tables error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equal(100); + + return done(); + + }); + + }); + + it('GET ' + apiPrefix + 'customers?_size=1000 should PASS', function (done) { + + //http get an url + agent.get(apiPrefix + 'customers?_size=1000') // api url + .expect(200) // 2xx for success and 4xx for failure + .end(function (err, res) { + // Handle /api/tables error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equal(100); + + return done(); + + }); + + }); + + it('GET ' + apiPrefix + 'customers?_size=-1 should PASS', function (done) { + + //http get an url + agent.get(apiPrefix + 'customers?_size=-1') // api url + .expect(200) // 2xx for success and 4xx for failure + .end(function (err, res) { + // Handle /api/tables error + if (err) { + return done(err); + } + + //validate response + res.body.length.should.be.equal(20); + + return done(); + + }); + + }); + + + it('GET ' + apiPrefix + 'payments?_p=2&_size=10 should PASS', function (done) { //http get an url