module.exports = bulkAddDriverTest;
/*
[{
"ok": true,
"id": "100",
"rev": "1-5190881ca746eca2ce205be647a4f87f"
}, {
"ok": true,
"id": "200",
"rev": "1-eb2b4d10bd0c17043509e8b7faeaa180"
}, {
"ok": true,
"id": "300",
"rev": "1-cf1c5d4190efc543698efa71eddb5b6e"
}, {
"ok": true,
"id": "400",
"rev": "1-5ed61ab3bd832d566f2ac4bf88f43870"
}, {
"ok": true,
"id": "500",
"rev": "1-7accb86cf6c167dc71e89f75e3a2ea25"
}]
*/
//{ ok: true, index: 2, id: '300' }
function findProblemRecords(record) {
return record.ok === false;
}
function verifySchema(res, db) {
var missingitems = [];
db.allDocs({
include_docs: true,
attachments: false,
keys: res.id
}, function(err, result_alldocs) {
if (err) {
return console.log(err);
}
// handle result
if (result_alldocs) {
console.log("Returing the all the docs that were originally added...");
if (result_alldocs.rows[2].doc.hasOwnProperty('firstname') !== true) {
missingitems.push('firstName');
}
if (result_alldocs.rows[2].doc.hasOwnProperty('suspended') !== true) {
missingitems.push('suspended');
}
if (result_alldocs.rows[2].doc.hasOwnProperty('violations') !== true) {
missingitems.push('violations');
}
if (result_alldocs.rows[2].doc.hasOwnProperty('plates') !== true) {
missingitems.push('plates');
}
console.log('Problem! The third document is missing the following information: ' + JSON.stringify(missingitems));
}
});
}
function bulkAddDriverTest(a, b) {
return function(err, res) {
// what a and b are without having to have a global
var ProblemRecords = res.find(findProblemRecords);
if (err === a &&
ProblemRecords !== undefined) {
console.log('Problem! At least one of your documents didn\'t save.: ');
console.log(JSON.stringify(res));
} else if (err === a &&
ProblemRecords === undefined &&
res[b.index].id !== b.id) {
console.log('Problem! All documents saved but we found as issue.');
console.log('The third document\'s id does not equal ' + b.id);
} else if (err === a && ProblemRecords === undefined) {
verifySchema(res, b.db);
} else if (err === a &&
ProblemRecords === undefined &&
res[b.index].id === b.id) {
console.log('Success! Done.');
console.log('response: ' + JSON.stringify(res));
}
else {
console.log('There was an error: ' + JSON.stringify(err));
}
};
}