Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Add simple partition support (get from partition view). Allow _delete… #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function MockCouch(server, options) {
server.get('/:db/_design/:doc/_view/:name', get_view);
server.post('/:db/_design/:doc/_view/:name', get_view);

// GET from a certain partition view
server.get('/:db/_partition/:partition/_design/:doc/_view/:name', get_view);

// GET and HEAD a certain document or _design document
get_doc = require('./lib/get_doc')(self);
server.get('/:db/_design/:designdoc/', get_doc);
Expand Down
8 changes: 7 additions & 1 deletion lib/bulk_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function (self) {
// Save the doc
db[id] = doc;

return {id: id, rev: doc._rev};
return {ok: true, id: id, rev: doc._rev};
}

if (!req.body || !req.body.docs) {
Expand All @@ -43,6 +43,12 @@ module.exports = function (self) {

docs = req.body.docs.map(function (doc) {

// Is a document to delete
if (doc._deleted && db[doc._id]._rev === doc._rev) {
delete db[doc._id];
return {ok: true, id: doc._id, rev: doc._rev};
}

// is a new document without id
if (!doc._id) {
doc._id = createMD5();
Expand Down
5 changes: 3 additions & 2 deletions lib/get_uuids.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*jslint node: true, indent: 2, nomen : true */
'use strict';
var createMD5 = require('./createMD5');

module.exports = function (self) {
/**
Expand All @@ -8,11 +9,11 @@ module.exports = function (self) {
return function (req, res) {
var count, ret, i, seqPrefix;

count = req.params.count || 1;
count = (req.query && req.query.count) || 1;

ret = [];

seqPrefix = self.seqPrefix || '4e17c12963f4bee0e6ec90da54';
seqPrefix = self.seqPrefix || createMD5();
for (i = 0; i < count; i += 1) {
ret.push(seqPrefix + ('000000' + i).substr(-6, 6));
}
Expand Down
15 changes: 12 additions & 3 deletions lib/get_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ module.exports = function (self) {

db = formatDB(self.databases[req.params.db]);

view = self.databases[req.params.db]['_design/' + req.params.doc].views[req.params.name];
try {
view = self.databases[req.params.db]['_design/' + req.params.doc].views[req.params.name];
} catch (e) {
res.send(404, {
error: 'not_found',
reason: 'missing'
});
return false;
}
req.query = req.query || {};

// Create flags
Expand All @@ -47,7 +55,9 @@ module.exports = function (self) {
result.doc = doc;
}
}
rows.push(result);
if (!req.params.partition || result.id.startsWith(req.params.partition + ':')) {
rows.push(result);
}
}
};
view.map(local);
Expand Down Expand Up @@ -180,4 +190,3 @@ module.exports = function (self) {
next();
};
};

15 changes: 7 additions & 8 deletions test/get_uuids.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ describe('get_uuids', function () {
});

it('should get a single uuid', function () {
get({ params : {} }, res, dummy_function);
get({ query : {} }, res, dummy_function);
expect(result.uuids.length).toBe(1);
expect(result.uuids[0]).toEqual("4e17c12963f4bee0e6ec90da54000000");
expect(statusCode).toBe(200);
});

it('should get multiple uuids', function () {
get({ params : {"count": 3} }, res, dummy_function);
get({ query : {"count": 3} }, res, dummy_function);
expect(result.uuids.length).toBe(3);
expect(result.uuids[0]).toEqual("4e17c12963f4bee0e6ec90da54000000");
expect(result.uuids[1]).toEqual("4e17c12963f4bee0e6ec90da54000001");
expect(result.uuids[2]).toEqual("4e17c12963f4bee0e6ec90da54000002");
expect(result.uuids[0].slice(-6)).toEqual("000000");
expect(result.uuids[1].slice(-6)).toEqual("000001");
expect(result.uuids[2].slice(-6)).toEqual("000002");
expect(statusCode).toBe(200);
});

it('should allow a custom prefix', function () {
custom_get({ params : {} }, res, dummy_function);
custom_get({ query : {} }, res, dummy_function);
expect(result.uuids.length).toBe(1);
expect(result.uuids[0]).toEqual("4e17c12963f4bee0e6ec90da55000000");
expect(statusCode).toBe(200);
});

});
});