Skip to content

Commit df6c040

Browse files
authored
Make sure nano works with _local docs (#200)
* added test to fetch local docs * _local support added #167 * added local tests Co-authored-by: Malte Legenhausen <mlegenhausen@gmail.com> Thanks @mlegenhausen!
1 parent 3d23bb9 commit df6c040

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

lib/nano.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ module.exports = exports = function dbScope (cfg) {
225225
if (opts.path) {
226226
req.uri += '/' + opts.path
227227
} else if (opts.doc) {
228-
if (!/^_design/.test(opts.doc)) {
228+
if (!/^_design|_local/.test(opts.doc)) {
229229
// http://wiki.apache.org/couchdb/HTTP_Document_API#Naming.2FAddressing
230230
req.uri += '/' + encodeURIComponent(opts.doc)
231231
} else {

test/document.get.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@ test('should detect missing parameters (callback) - db.get', () => {
9191
})
9292
})
9393
})
94+
95+
test('check request can fetch local documents - db.get', async () => {
96+
// mocks
97+
const response = { _id: '_local/id', _rev: '1-123', a: 1 }
98+
const scope = nock(COUCH_URL)
99+
.get('/db/_local/id')
100+
.reply(200, response)
101+
102+
// test GET /db/_local/id
103+
const db = nano.db.use('db')
104+
const p = await db.get('_local/id')
105+
expect(p).toStrictEqual(response)
106+
expect(scope.isDone()).toBe(true)
107+
})

test/document.insert.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,35 @@ test('should be able to handle missing database - POST /db - db.insert', async (
130130
await expect(db.insert(doc)).rejects.toThrow('Database does not exist.')
131131
expect(scope.isDone()).toBe(true)
132132
})
133+
134+
test('should be able to insert document with _local id - PUT /db/_local/id - db.insert', async () => {
135+
// mocks
136+
const doc = { a: 1, b: 2 }
137+
const response = { ok: true, id: '_local/myid', rev: '1-123' }
138+
139+
const scope = nock(COUCH_URL)
140+
.put('/db/_local/myid', doc)
141+
.reply(200, response)
142+
143+
// test PUT /db
144+
const db = nano.db.use('db')
145+
const p = await db.insert(doc, '_local/myid')
146+
expect(p).toStrictEqual(response)
147+
expect(scope.isDone()).toBe(true)
148+
})
149+
150+
test('should be able to insert document with local id in object - POST /db - db.insert', async () => {
151+
// mocks
152+
const doc = { _id: '_local/myid', a: 1, b: 2 }
153+
const response = { ok: true, id: '_local/myid', rev: '1-123' }
154+
155+
const scope = nock(COUCH_URL)
156+
.post('/db', doc)
157+
.reply(200, response)
158+
159+
// test POST /db
160+
const db = nano.db.use('db')
161+
const p = await db.insert(doc)
162+
expect(p).toStrictEqual(response)
163+
expect(scope.isDone()).toBe(true)
164+
})

0 commit comments

Comments
 (0)