You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2019/03/15 09:47:09 UTC

[couchdb-nano] branch issue148 created (now c5c9a00)

This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a change to branch issue148
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git.


      at c5c9a00  added tests

This branch includes the following new commits:

     new 7c906c9  fix formatting of drilldown parameter for issue #148
     new c5c9a00  added tests

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb-nano] 02/02: added tests

Posted by gl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch issue148
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git

commit c5c9a002355ca394dbfb4c1cb47f4cd5bd5c28f8
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Fri Mar 15 09:46:50 2019 +0000

    added tests
---
 package.json                     |  1 +
 tests/helpers/unit.js            |  3 +++
 tests/intercept/design/search.js | 15 +++++++++++++++
 3 files changed, 19 insertions(+)

diff --git a/package.json b/package.json
index 27cb9d1..786d31a 100644
--- a/package.json
+++ b/package.json
@@ -32,6 +32,7 @@
     "tape": "^4.6.2"
   },
   "scripts": {
+    "standard": "standard --fix",
     "test": "standard && npm run mocha",
     "mocha": "NOCK_OFF=true istanbul cover tape tests/*/*/*.js",
     "unmocked": "NOCK_OFF=true tape tests/*/*/*.js",
diff --git a/tests/helpers/unit.js b/tests/helpers/unit.js
index d5ab53f..4114fd0 100644
--- a/tests/helpers/unit.js
+++ b/tests/helpers/unit.js
@@ -94,6 +94,9 @@ helpers.unit = function (method, error) {
             delete req.httpAgent // ignore 'httpAgent' in deep equal assert
           }
 
+          // ignore qsStringifyOptions in object comparison
+          delete req.qsStringifyOptions
+
           assert.deepEqual(req, stub)
           assert.end()
         })
diff --git a/tests/intercept/design/search.js b/tests/intercept/design/search.js
index 535fae7..6079903 100644
--- a/tests/intercept/design/search.js
+++ b/tests/intercept/design/search.js
@@ -99,6 +99,21 @@ it('should not encode string drilldown parameter', function (assert) {
   })
 })
 
+it('should encode array of arrays drilldown parameter', function (assert) {
+  const p = db.search('fake', 'fake', { drilldown: [['colour', 'red'], ['category', 'cake']] })
+  assert.ok(helpers.isPromise(p), 'returns Promise')
+  p.then(function (data) {
+    assert.ok(true, 'Promise is resolved')
+    assert.equal(data.method, 'GET')
+    assert.equal(typeof data.headers, 'object')
+    console.log(data.headers)
+    assert.equal(typeof data.qs, 'object')
+    assert.end()
+  }).catch(function () {
+    assert.ok(true, 'Promise is rejected')
+  })
+})
+
 it('should encode array group_sort parameter', function (assert) {
   const p = db.search('fake', 'fake',
     { group_sort: ['-foo<number>', 'bar<string>'] })


[couchdb-nano] 01/02: fix formatting of drilldown parameter for issue #148

Posted by gl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch issue148
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git

commit 7c906c986c17561f21228b2b179e6d024edf3f80
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Fri Mar 15 09:10:28 2019 +0000

    fix formatting of drilldown parameter for issue #148
---
 lib/nano.js | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/nano.js b/lib/nano.js
index 8169b0d..6fa6784 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -275,6 +275,10 @@ module.exports = exports = function dbScope (cfg) {
       req.body = querystring.stringify(opts.form).toString('utf8')
     }
 
+    // ask request to render query string arrays as repeated values e.g.
+    // ?drilldown=["author","Dickens"]&drilldown=["publisher","Penguin"]
+    req.qsStringifyOptions = { arrayFormat: 'repeat' }
+
     log(req)
 
     if (opts.stream) {
@@ -621,7 +625,7 @@ module.exports = exports = function dbScope (cfg) {
 
       // Several search parameters must be JSON-encoded; but since this is an
       // object API, several parameters need JSON endoding.
-      const paramsToEncode = ['counts', 'drilldown', 'group_sort', 'ranges', 'sort']
+      const paramsToEncode = ['counts', 'group_sort', 'ranges', 'sort']
       paramsToEncode.forEach(function (param) {
         if (param in qs1) {
           if (typeof qs1[param] !== 'string') {
@@ -637,6 +641,25 @@ module.exports = exports = function dbScope (cfg) {
         }
       })
 
+      // the drilldown parameter needs special treatment. It can either be:
+      // - a single array of strings e.g. ['author', 'Dickens']
+      // - an array of arrays e.g. [['author','Dickens']['publisher','Penguin']]
+      // The former should be JSON.stringified the latter should be an array of
+      // JSON.stringified arrays(!).
+      if (qs1['drilldown']) {
+        // if this is an array of arrays
+        if (Array.isArray(qs1['drilldown']) &&
+            qs1['drilldown'].length > 0 &&
+            Array.isArray(qs1['drilldown'][0])) {
+          // JSON stringify each element of the array
+          qs1['drilldown'] = qs1['drilldown'].map(function (val) {
+            return JSON.stringify(val)
+          })
+        } else if (Array.isArray(qs1['drilldown'])) {
+          qs1['drilldown'] = JSON.stringify(qs1['drilldown'])
+        }
+      }
+
       if (qs1 && qs1.keys) {
         const body = {keys: qs1.keys}
         delete qs1.keys