forked from pouchdb/pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.bulk_docs.js
167 lines (151 loc) · 5.18 KB
/
test.bulk_docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*globals initTestDB: false, emit: true, generateAdapterUrl: false */
/*globals PERSIST_DATABASES: false, initDBPair: false, utils: true */
/*globals ajax: true, LevelPouch: true, makeDocs: false */
"use strict";
// Porting tests from Apache CouchDB bulk docs tests
// https://github.com/apache/couchdb/blob/master/share/www/script/test/bulk_docs.js
var adapters = ['local-1', 'http-1'];
var qunit = module;
if (typeof module !== undefined && module.exports) {
Pouch = require('../src/pouch.js');
LevelPouch = require('../src/adapters/pouch.leveldb.js');
utils = require('./test.utils.js');
for (var k in utils) {
global[k] = global[k] || utils[k];
}
qunit = QUnit.module;
}
adapters.map(function(adapter) {
qunit('bulk_docs: ' + adapter, {
setup : function () {
this.name = generateAdapterUrl(adapter);
},
teardown: function() {
if (!PERSIST_DATABASES) {
Pouch.destroy(this.name);
}
}
});
var authors = [
{name: 'Dale Harvey', commits: 253},
{name: 'Mikeal Rogers', commits: 42},
{name: 'Johannes J. Schmidt', commits: 13},
{name: 'Randall Leeds', commits: 9}
];
asyncTest('Testing bulk docs', function() {
initTestDB(this.name, function(err, db) {
var docs = makeDocs(5);
db.bulkDocs({docs: docs}, function(err, results) {
ok(results.length === 5, 'results length matches');
for (var i = 0; i < 5; i++) {
ok(results[i].id === docs[i]._id, 'id matches');
ok(results[i].rev, 'rev is set');
// Update the doc
docs[i]._rev = results[i].rev;
docs[i].string = docs[i].string + ".00";
}
db.bulkDocs({docs: docs}, function(err, results) {
ok(results.length === 5, 'results length matches');
for (i = 0; i < 5; i++) {
ok(results[i].id === i.toString(), 'id matches again');
// set the delete flag to delete the docs in the next step
docs[i]._rev = results[i].rev;
docs[i]._deleted = true;
}
db.put(docs[0], function(err, doc) {
db.bulkDocs({docs: docs}, function(err, results) {
ok(results[0].error === 'conflict', 'First doc should be in conflict');
ok(typeof results[0].rev === "undefined", 'no rev in conflict');
for (i = 1; i < 5; i++) {
ok(results[i].id === i.toString());
ok(results[i].rev);
}
start();
});
});
});
});
});
});
asyncTest('No id in bulk docs', function() {
initTestDB(this.name, function(err, db) {
var newdoc = {"_id": "foobar", "body": "baz"};
db.put(newdoc, function(err, doc) {
ok(doc.ok);
var docs = [
{"_id": newdoc._id, "_rev": newdoc._rev, "body": "blam"},
{"_id": newdoc._id, "_rev": newdoc._rev, "_deleted": true}
];
db.bulkDocs({docs: docs}, function(err, results) {
ok(results[0].error === 'conflict' || results[1].error === 'conflict');
start();
});
});
});
});
asyncTest("Test errors on invalid doc id", function() {
var docs = [
{'_id': '_invalid', foo: 'bar'}
];
initTestDB(this.name, function(err, db) {
db.bulkDocs({docs: docs}, function(err, info) {
equal(err.error, 'bad_request', 'correct error returned');
ok(!info, 'info is empty');
start();
});
});
});
asyncTest("Test two errors on invalid doc id", function() {
var docs = [
{'_id': '_invalid', foo: 'bar'},
{'_id': 123, foo: 'bar'}
];
initTestDB(this.name, function(err, db) {
db.bulkDocs({docs: docs}, function(err, info) {
equal(err.error, 'bad_request', 'correct error returned');
equal(err.reason, Pouch.Errors.RESERVED_ID.reason, 'correct error message returned');
ok(!info, 'info is empty');
start();
});
});
});
asyncTest('No docs', function() {
initTestDB(this.name, function(err, db) {
db.bulkDocs({"doc": [{"foo":"bar"}]}, function(err, result) {
ok(err.status === 400);
ok(err.error === 'bad_request');
ok(err.reason === "Missing JSON list of 'docs'");
start();
});
});
});
asyncTest('Jira 911', function() {
initTestDB(this.name, function(err, db) {
var docs = [
{"_id":"0", "a" : 0},
{"_id":"1", "a" : 1},
{"_id":"1", "a" : 1},
{"_id":"3", "a" : 3}
];
db.bulkDocs({docs: docs}, function(err, results) {
ok(results[1].id === "1", 'check ordering');
ok(results[1].error === undefined, 'first id succeded');
ok(results[2].error === "conflict", 'second conflicted');
ok(results.length === 4, 'got right amount of results');
start();
});
});
});
asyncTest('Test multiple bulkdocs', function() {
initTestDB(this.name, function(err, db) {
db.bulkDocs({docs: authors}, function (err, res) {
db.bulkDocs({docs: authors}, function (err, res) {
db.allDocs(function(err, result) {
ok(result.total_rows === 8, 'correct number of results');
start();
});
});
});
});
});
});