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 2020/02/12 11:25:53 UTC

[couchdb-nano] 05/15: finished top level tests

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

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

commit ea90dc1d52bc3e29808086d09aca9408ab9198c9
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Thu Feb 6 08:55:10 2020 +0000

    finished top level tests
---
 test/database.follow.test.js    |  22 ++++++++
 test/nano.config.test.js        |  63 +++++++++++++++++++++++
 test/nano.followUpdates.test.js |  21 ++++++++
 test/nano.request.test.js       | 110 ++++++++++++++++++++++++++++++++++++++++
 test/nano.updates.test.js       |  75 +++++++++++++++++++++++++++
 test/nano.use.test.js           |  31 +++++++++++
 6 files changed, 322 insertions(+)

diff --git a/test/database.follow.test.js b/test/database.follow.test.js
index e69de29..870a7bf 100644
--- a/test/database.follow.test.js
+++ b/test/database.follow.test.js
@@ -0,0 +1,22 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+
+test('should be able to follow changes feed - nano.db.follow', () => {
+  const db = nano.db.use('db')
+  const feed = db.follow({ since: 'now' })
+  expect(feed.constructor.name).toBe('Feed')
+  // no need to test the changes feed follower - it has its own tests
+})
diff --git a/test/nano.config.test.js b/test/nano.config.test.js
new file mode 100644
index 0000000..72de445
--- /dev/null
+++ b/test/nano.config.test.js
@@ -0,0 +1,63 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+
+test('should be able to supply HTTP url - nano.config', () => {
+  const HTTP_URL = 'http://localhost:5984'
+  const nano = Nano(HTTP_URL)
+  expect(nano.config.url).toBe(HTTP_URL)
+})
+
+test('should be able to supply URL with database name - nano.config', () => {
+  const HTTP_URL = 'http://localhost:5984/db'
+  const nano = Nano(HTTP_URL)
+  expect(nano.config.url).toBe('http://localhost:5984')
+  expect(nano.config.db).toBe('db')
+})
+
+test('should be able to supply HTTPS url - nano.config', () => {
+  const HTTPS_URL = 'https://mydomain.com'
+  const nano = Nano(HTTPS_URL)
+  expect(nano.config.url).toBe(HTTPS_URL)
+})
+
+test('should be able to supply HTTP url with cookie jar - nano.config', () => {
+  const HTTP_URL = 'http://localhost:5984'
+  const nano = Nano({ url: HTTP_URL, jar: true })
+  expect(nano.config.url).toBe(HTTP_URL)
+  expect(nano.config.jar).toBe(true)
+})
+
+test('should be able to supply HTTPS url with auth credentials - nano.config', () => {
+  const HTTPS_URL = 'https://myusername:mypassword@mydomain.com'
+  const nano = Nano(HTTPS_URL)
+  expect(nano.config.url).toBe(HTTPS_URL)
+})
+
+test('should be able to supply requestDefaults - nano.config', () => {
+  const HTTPS_URL = 'https://myusername:mypassword@mydomain.com'
+  const defaults = { proxy: 'http://localproxy.com' }
+  const nano = Nano({ url: HTTPS_URL, requestDefaults: defaults })
+  expect(nano.config.url).toBe(HTTPS_URL)
+  expect(nano.config.requestDefaults).toBe(defaults)
+})
+
+test('should be able to supply logging function - nano.config', () => {
+  const HTTPS_URL = 'https://myusername:mypassword@mydomain.com'
+  const logger = (id, args) => {
+    console.log(id, args)
+  }
+  const nano = Nano({ url: HTTPS_URL, log: logger })
+  expect(nano.config.url).toBe(HTTPS_URL)
+  expect(typeof nano.config.log).toBe('function')
+})
diff --git a/test/nano.followUpdates.test.js b/test/nano.followUpdates.test.js
new file mode 100644
index 0000000..001b3a3
--- /dev/null
+++ b/test/nano.followUpdates.test.js
@@ -0,0 +1,21 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+
+test('should be able to follow db updates- nano.followUpdates', () => {
+  const feed = nano.followUpdates()
+  expect(feed.constructor.name).toBe('Feed')
+  // no need to test the changes feed follower - it has its own tests
+})
diff --git a/test/nano.request.test.js b/test/nano.request.test.js
new file mode 100644
index 0000000..dc23da1
--- /dev/null
+++ b/test/nano.request.test.js
@@ -0,0 +1,110 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+
+test('check request can do GET requests - nano.request', async () => {
+  // mocks
+  const response = { ok: true }
+  const scope = nock(COUCH_URL)
+    .get('/mydb?a=1&b=2')
+    .reply(200, response)
+
+  // test GET /db
+  const req = {
+    method: 'get',
+    db: 'mydb',
+    qs: { a: 1, b: 2 }
+  }
+  const p = await nano.request(req)
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('check request can do POST requests - nano.request', async () => {
+  // mocks
+  const response = { ok: true }
+  const scope = nock(COUCH_URL)
+    .post('/mydb', { _id: '1', a: true })
+    .reply(200, response)
+
+  // test GET /db
+  const req = {
+    method: 'post',
+    db: 'mydb',
+    body: { _id: '1', a: true }
+  }
+  const p = await nano.request(req)
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('check request can do PUT requests - nano.request', async () => {
+  // mocks
+  const response = { ok: true }
+  const scope = nock(COUCH_URL)
+    .put('/mydb/1', { _id: '1', a: true })
+    .reply(200, response)
+
+  // test GET /db
+  const req = {
+    method: 'put',
+    db: 'mydb',
+    path: '1',
+    body: { _id: '1', a: true }
+  }
+  const p = await nano.request(req)
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('check request can do DELETE requests - nano.request', async () => {
+  // mocks
+  const response = { ok: true }
+  const scope = nock(COUCH_URL)
+    .delete('/mydb/mydoc')
+    .query({ rev: '1-123' })
+    .reply(200, response)
+
+  // test GET /db
+  const req = {
+    method: 'delete',
+    db: 'mydb',
+    path: 'mydoc',
+    qs: { rev: '1-123' }
+
+  }
+  const p = await nano.request(req)
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('check request can do HEAD requests - nano.request', async () => {
+  // mocks
+  const response = ''
+  const scope = nock(COUCH_URL)
+    .head('/mydb/mydoc')
+    .reply(200, '')
+
+  // test GET /db
+  const req = {
+    method: 'head',
+    db: 'mydb',
+    path: 'mydoc'
+  }
+  const p = await nano.request(req)
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
diff --git a/test/nano.updates.test.js b/test/nano.updates.test.js
new file mode 100644
index 0000000..127041e
--- /dev/null
+++ b/test/nano.updates.test.js
@@ -0,0 +1,75 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+const response = {
+  results: [
+    {
+      db_name: 'firehosetarget',
+      type: 'updated',
+      seq: '1678-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuDOQnIi8gFirGnmKQaJhqZY9ODx6Q8FiDJ0ACk_sMNZDGDGGhubJCWmopNaxYASRExkg'
+    },
+    {
+      db_name: 'bob',
+      type: 'created',
+      seq: '1679-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuDOQnIi8gFirGnmKQaJhqZY9ODx6Q8FiDJ0ACk_sMNZDGHGGhubJCWmopNaxYASTMxkw'
+    },
+    {
+      db_name: 'bob',
+      type: 'updated',
+      seq: '1680-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuDOQnIi8gFirGnmKQaJhqZY9ODx6Q8FiDJ0ACk_sMNZLGAGGhubJCWmopNaxYASVUxlA'
+    }
+  ],
+  last_seq: '1680-g1AAAACheJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuDOQnIi8gFirGnmKQaJhqZY9ODx6Q8FiDJ0ACk_sMNZLGAGGhubJCWmopNaxYASVUxlA'
+}
+const errResponse = {
+  error: 'not_found',
+  reason: 'Database does not exist.'
+}
+
+test('should be able to fetch db updates - GET /_db_updates - nano.updates', async () => {
+  // mocks
+  const scope = nock(COUCH_URL)
+    .get('/_db_updates')
+    .reply(200, response)
+
+  // test GET /db
+  const p = await nano.updates()
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('should be able to fetch db updates with options - GET /_db_updates - nano.updates', async () => {
+  // mocks
+  const scope = nock(COUCH_URL)
+    .get('/_db_updates?timeout=10000')
+    .reply(200, response)
+
+  // test GET /db
+  const p = await nano.updates({ timeout: 10000 })
+  expect(p).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
+})
+
+test('should handle 404 - GET /_db_updates - nano.updates', async () => {
+  // mocks
+  const scope = nock(COUCH_URL)
+    .get('/_db_updates')
+    .reply(404, errResponse)
+
+  // test GET /db
+  await expect(nano.db.updates()).rejects.toThrow('Database does not exist.')
+  expect(scope.isDone()).toBe(true)
+})
diff --git a/test/nano.use.test.js b/test/nano.use.test.js
new file mode 100644
index 0000000..ba73044
--- /dev/null
+++ b/test/nano.use.test.js
@@ -0,0 +1,31 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+
+test('should be able to use a database - nano.db.use', () => {
+  const db = nano.db.use('db1')
+  expect(typeof db).toBe('object')
+  expect(typeof db.get).toBe('function')
+  expect(typeof db.replication).toBe('object')
+  expect(db.config.db).toBe('db1')
+})
+
+test('should be able to use a database - nano.use', () => {
+  const db = nano.use('db2')
+  expect(typeof db).toBe('object')
+  expect(typeof db.get).toBe('function')
+  expect(typeof db.replication).toBe('object')
+  expect(db.config.db).toBe('db2')
+})