clerk 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,520 +0,0 @@
1
- "use strict";
2
-
3
- var clerk = require("../clerk");
4
- var expect = require("expect.js");
5
- var shared = require("./shared");
6
-
7
- describe("DB", function () {
8
- before(shared.clerkFactory);
9
-
10
- describe("#create", function () {
11
- it("should create database", shared.createDB("db"));
12
- });
13
-
14
- describe("#replicate", function () {
15
-
16
- before(function () {
17
- this.replica = this.client.db(this.dbname + "-replica");
18
- });
19
-
20
- before(shared.createDB("replica"));
21
- after(shared.destroyDB("replica"));
22
-
23
- it("should be ok", function (done) {
24
- var options = {
25
- target: this.replica.name
26
- };
27
- this.db.replicate(options, function (err, body, status, headers, res) {
28
- if (!err) {
29
- shared.shouldBeOk(body);
30
- shared.shouldHave2xxStatus(status);
31
- }
32
- done(err);
33
- });
34
- });
35
-
36
- });
37
-
38
- describe("utils", function () {
39
-
40
- describe("#exists", function () {
41
- it("should be true", function (done) {
42
- this.db.exists(function (err, exists, status, headers, res) {
43
- if (!err) {
44
- expect(exists).to.be(true);
45
- shared.shouldHave2xxStatus(status);
46
- }
47
- done(err);
48
- });
49
- });
50
-
51
- it("should be false", function (done) {
52
- var db = this.client.db(this.dbname + "-404-" + Date.now());
53
- db.exists(function (err, exists, status, headers, res) {
54
- if (err) return done(err);
55
- expect(exists).to.be(false);
56
- expect(status).to.be(404);
57
- done();
58
- });
59
- });
60
- });
61
-
62
- describe("#info", function () {
63
- it("should return database info", function (done) {
64
- var self = this;
65
- this.db.info(function (err, body, status, headers, res) {
66
- if (!err) {
67
- expect(body).to.have.property("db_name", self.dbname);
68
- expect(body).to.have.property("doc_count", 0);
69
- expect(body).to.have.property("doc_del_count", 0);
70
- shared.shouldHave2xxStatus(status);
71
- }
72
- done(err);
73
- });
74
- });
75
- });
76
-
77
- describe("#commit", function () {
78
- it("should be ok", function (done) {
79
- this.db.commit(function (err, body, status, headers, res) {
80
- if (!err) {
81
- shared.shouldBeOk(body);
82
- shared.shouldHave2xxStatus(status);
83
- }
84
- done(err);
85
- });
86
- });
87
- });
88
-
89
- describe("#purge", function () {
90
- it("should be ok", function (done) {
91
- this.db.purge({}, function (err, body, status, headers, res) {
92
- if (!err) {
93
- expect(body).to.have.property("purged");
94
- shared.shouldHave2xxStatus(status);
95
- }
96
- done(err);
97
- });
98
- });
99
- });
100
-
101
- describe("#compact", function () {
102
- it("should be ok", function (done) {
103
- this.db.compact(function (err, body, status, headers, res) {
104
- if (!err) {
105
- shared.shouldBeOk(body);
106
- shared.shouldHave2xxStatus(status);
107
- }
108
- done(err);
109
- });
110
- });
111
- });
112
-
113
- describe("#vacuum", function () {
114
- it("should be ok", function (done) {
115
- this.db.vacuum(function (err, body, status, headers, res) {
116
- if (!err) {
117
- shared.shouldBeOk(body);
118
- shared.shouldHave2xxStatus(status);
119
- }
120
- done(err);
121
- });
122
- });
123
- });
124
-
125
- });
126
-
127
- describe("putting documents", function () {
128
- beforeEach(shared.docFactory);
129
-
130
- describe("#post", function () {
131
- it("should store document", function (done) {
132
- this.db.post({}, function (err, body, status, headers, res) {
133
- if (!err) {
134
- shared.shouldBeOk(body);
135
- shouldHaveIdRev(body, body._id, body._rev);
136
- shared.shouldHave2xxStatus(status);
137
- }
138
- done(err);
139
- });
140
- });
141
- });
142
-
143
- describe("#put", function () {
144
- it("should store document", function (done) {
145
- var doc = this.doc;
146
- this.db.put(doc, function (err, body, status, headers, res) {
147
- if (!err) {
148
- shared.shouldBeOk(body);
149
- shouldHaveIdRev(body, doc._id, "1-15f65339921e497348be384867bb940f");
150
- shared.shouldHave2xxStatus(status);
151
- }
152
- done(err);
153
- });
154
- });
155
- });
156
-
157
- });
158
-
159
- describe("getting documents", function () {
160
- before(shared.docFactory);
161
- before(putDocument);
162
-
163
- describe("#get", function () {
164
-
165
- it("should return document", function (done) {
166
- var doc = this.doc;
167
- this.db.get(doc._id, function (err, body, status, headers, res) {
168
- if (!err) {
169
- shouldHaveIdRev(body, doc._id, doc._rev);
170
- shouldBeDocument(body, doc);
171
- shared.shouldHave2xxStatus(status);
172
- }
173
- done(err);
174
- });
175
- });
176
-
177
- it("should return not found", function (done) {
178
- this.db.get("notfound" + Math.random(), function (err, body, status, headers, res) {
179
- expect(err).to.be.an(Error);
180
- expect(err.status).to.be(404);
181
- expect(status).to.be(404);
182
- done();
183
- });
184
- });
185
-
186
- });
187
-
188
- describe("#head", function () {
189
- it("should return document metadata", function (done) {
190
- var doc = this.doc;
191
- this.db.head(doc._id, function (err, body, status, headers, res) {
192
- if (!err) {
193
- shouldHaveIdRev(body, doc._id, doc._rev);
194
- shared.shouldHave2xxStatus(status);
195
- }
196
- done(err);
197
- });
198
- });
199
- });
200
-
201
- });
202
-
203
- describe("updating documents", function () {
204
- beforeEach(shared.docFactory);
205
- beforeEach(putDocument);
206
-
207
- describe("#post", function () {
208
- it("should return document metadata", function (done) {
209
- var doc = this.doc;
210
- this.db.post(doc, function (err, body, status, headers, res) {
211
- if (!err) {
212
- shared.shouldBeOk(body);
213
- shouldHaveIdRev(body, doc._id, "2-47661acbb62a2a63704c803bc0152f2b");
214
- shared.shouldHave2xxStatus(status);
215
- }
216
- done(err);
217
- });
218
- });
219
- });
220
-
221
- describe("#del", function () {
222
- it("should be ok", function (done) {
223
- var doc = this.doc;
224
- this.db.del(doc, function (err, body, status, headers, res) {
225
- if (!err) {
226
- shared.shouldBeOk(body);
227
- shared.shouldHave2xxStatus(status);
228
- }
229
- done(err);
230
- });
231
- });
232
- });
233
-
234
- describe("#copy", function () {
235
-
236
- it("should copy id to id", function (done) {
237
- var id = shared.randomId();
238
- shouldCopy.call(this, done, this.doc._id, id, id, "1-");
239
- });
240
-
241
- it("should copy doc to id", function (done) {
242
- var id = shared.randomId();
243
- shouldCopy.call(this, done, this.doc, id, id, "1-");
244
- });
245
-
246
- it("should copy doc to doc", function (done) {
247
- var id = shared.randomId();
248
- var target = { _id: id };
249
- shouldCopy.call(this, done, this.doc, target, target._id, "1-");
250
- });
251
-
252
- it("should copy doc to doc with rev", function (done) {
253
- var id = shared.randomId();
254
- var target = { _id: id, _rev: this.doc._rev };
255
- shouldCopy.call(this, done, this.doc, target, target._id, "2-");
256
- });
257
-
258
- function shouldCopy(done, source, target, id, rev) {
259
- this.db.copy(source, target, function (err, body, status, headers, res) {
260
- if (!err) {
261
- // CouchDB 1.2 changes the rev on COPY
262
- // https://issues.apache.org/jira/browse/COUCHDB-1485
263
- var proto = body.__proto__ || body;
264
- expect(proto._id).to.be(id);
265
- // shouldHaveIdRev(body, id, rev);
266
- shared.shouldHave2xxStatus(status);
267
- }
268
- done(err);
269
- });
270
- }
271
-
272
- });
273
-
274
- });
275
-
276
- describe("batch", function () {
277
- before(shared.docFactory);
278
-
279
- describe("#post", function () {
280
- it("should be ok", function (done) {
281
- var docs = this.docs;
282
- this.db.post(docs, function (err, body, status, headers, res) {
283
- var i = 0, j, len, item, doc;
284
- if (!err) {
285
- for (len = body.length; i < len; i++) {
286
- item = body[i], doc = docs[i];
287
- expect(item).to.have.property("id", doc._id);
288
- expect(item).to.have.property("rev");
289
- doc._rev = item.rev;
290
- }
291
- shared.shouldBeOk(item);
292
- shared.shouldHave2xxStatus(status);
293
- }
294
- done(err);
295
- });
296
- });
297
- });
298
-
299
- describe("#del", function () {
300
- it("should be ok", function (done) {
301
- var docs = this.docs;
302
- this.db.del(docs, function (err, body, status, headers, res) {
303
- var i = 0,
304
- len, item, doc;
305
- if (!err) {
306
- for (len = body.length; i < len; i++) {
307
- item = body[i], doc = docs[i];
308
- shared.shouldBeOk(item);
309
- shouldHaveIdRev(item, doc._id, item._rev);
310
- }
311
- }
312
- shared.shouldHave2xxStatus(status);
313
- done(err);
314
- });
315
- });
316
- });
317
-
318
- });
319
-
320
- describe("querying documents", function () {
321
-
322
- describe("#all", function () {
323
-
324
- it("should return metadata", function (done) {
325
- this.db.all(function (err, body, status, headers, res) {
326
- var i = 0, len, item;
327
- if (!err) {
328
- expect(body).to.have.property("rows");
329
- expect(body).to.have.property("total_rows");
330
- expect(body.total_rows).greaterThan(0);
331
- expect(body).to.have.property("offset", 0);
332
- for (len = body.length; i < len; i++) {
333
- item = body[i];
334
- expect(item).to.have.property("id");
335
- expect(item).to.have.property("key");
336
- expect(item.value).to.have.property("rev");
337
- }
338
- shared.shouldHave2xxStatus(status);
339
- }
340
- done(err);
341
- });
342
- });
343
-
344
- it("should return documents", function (done) {
345
- this.db.all({
346
- include_docs: true
347
- }, function (err, body, status, headers, res) {
348
- var i = 0, len, item;
349
- if (!err) {
350
- expect(body).to.have.property("rows");
351
- expect(body).to.have.property("total_rows");
352
- expect(body.total_rows).greaterThan(0);
353
- expect(body).to.have.property("offset", 0);
354
- for (len = body.length; i < len; i++) {
355
- item = body[i];
356
- expect(item).to.have.property("id");
357
- expect(item).to.have.property("key", item.id);
358
- expect(item.value).to.have.property("rev");
359
- expect(item.doc).to.have.property("_id");
360
- expect(item.doc).to.have.property("_rev");
361
- if (/^clerk-test-/.test(item.id)) {
362
- expect(item.doc).to.have.property("hello");
363
- }
364
- }
365
- shared.shouldHave2xxStatus(status);
366
- }
367
- done(err);
368
- });
369
- });
370
-
371
- });
372
-
373
- describe("#find", function () {
374
-
375
- });
376
-
377
- });
378
-
379
- describe("realtime", function () {
380
- beforeEach(shared.docFactory);
381
-
382
- describe("#changes", function () {
383
- it("should get changes", function (done) {
384
- var db = this.db,
385
- doc = this.doc;
386
- putDocument.call(this, function (err) {
387
- if (err) return done(err);
388
- db.changes(function (err, body, status) {
389
- if (!err) {
390
- expect(body).to.have.property("results");
391
- expect(body).to.be.an("array");
392
- expect(body.length).to.be.greaterThan(0);
393
- var changes = body[body.length - 1];
394
- expect(changes).to.have.property("changes");
395
- expect(changes.changes).to.be.an("array");
396
- expect(changes.changes[0]).to.have.property("rev", doc._rev);
397
- shared.shouldHave2xxStatus(status);
398
- }
399
- done(err);
400
- });
401
- });
402
- });
403
- });
404
-
405
- describe("#follow", function () {
406
- it("should follow changes", function (done) {
407
- var db = this.db;
408
- var docs = this.docs;
409
-
410
- db.follow(function (err, body, status) {
411
- if (err) return done(err);
412
- if (body.id != docs[0]._id) return;
413
-
414
- var doc = docs.shift();
415
-
416
- expect(body).to.have.property("id", doc._id);
417
- expect(body.changes).to.be.an("array");
418
- expect(body.changes[0]).to.have.property("rev", doc._rev);
419
- shared.shouldHave2xxStatus(status);
420
-
421
- if (!docs.length) {
422
- done();
423
- return false;
424
- }
425
- });
426
-
427
- bulkDocuments.call(this, function (err) {
428
- if (err) return done(err);
429
- });
430
- });
431
- });
432
-
433
- });
434
-
435
- describe("#update", function () {
436
-
437
- it("should create an update handler", function (done) {
438
- this.db.put("_design/test", require("./design"), done);
439
- });
440
-
441
- it("should use an update handler", function (done) {
442
- this.db.update("test/test", "myrandomid", {Hello: "World"}, function (err, body) {
443
- if (!err) {
444
- expect(body).to.have.property("_id", "myrandomid");
445
- expect(body).to.have.property("Hello", "World");
446
- expect(body).to.have.property("dtcreated");
447
- expect(body).to.have.property("dtupdated");
448
- }
449
- done(err);
450
- });
451
- });
452
-
453
- it("should not set the path from the request body", function (done) {
454
- this.db.update("test/test", {_id: "foo", Hello: "World"}, function (err) {
455
- expect(err).to.be.an(Error);
456
- expect(err.status).to.be(400);
457
- expect(err.url).to.not.match(/\bfoo\b/)
458
- done();
459
- });
460
- });
461
-
462
- });
463
-
464
-
465
- describe("#destroy", function () {
466
-
467
- it("should destroy database", function (done) {
468
- this.db.destroy(function (err, body, status, headers, res) {
469
- if (!err) {
470
- shared.shouldBeOk(body);
471
- }
472
- done(err);
473
- });
474
- });
475
-
476
- });
477
-
478
- function putDocument(done) {
479
- var doc = this.doc;
480
- this.db.put(doc, function (err, body, status, headers, res) {
481
- if (!err) {
482
- shouldHaveIdRev(body, doc._id, body._rev);
483
- shared.shouldHave2xxStatus(status);
484
- doc._rev = body._rev;
485
- }
486
- done(err);
487
- });
488
- }
489
-
490
- function bulkDocuments(done) {
491
- var docs = this.docs;
492
- this.db.post(docs, function (err, body, status, headers, res) {
493
- var i = 0,
494
- j, len, item, doc;
495
- if (!err) {
496
- for (len = body.length; i < len; i++) {
497
- item = body[i], doc = docs[i];
498
- expect(item).to.have.property("id", doc._id);
499
- expect(item).to.have.property("rev");
500
- doc._rev = item.rev;
501
- }
502
- shared.shouldBeOk(item);
503
- shared.shouldHave2xxStatus(status);
504
- }
505
- done(err);
506
- });
507
- }
508
-
509
- function shouldHaveIdRev(body, id, rev) {
510
- expect(body._id).to.be(id);
511
- expect(body._rev).to.be(rev);
512
- }
513
-
514
- function shouldBeDocument(body, doc) {
515
- for (var key in doc) {
516
- expect(body).to.have.property(key, doc[key]);
517
- }
518
- }
519
-
520
- });
package/test/design.js DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- module.exports = {
4
-
5
- updates: {
6
- test: function (doc, req) {
7
- var now = new Date().toISOString();
8
-
9
- doc = {
10
- _id: req.id,
11
- dtcreated: doc && doc.dtcreated || now,
12
- dtupdated: now
13
- }
14
-
15
- var data = JSON.parse(req.body);
16
- Object.keys(data).forEach(function (key) {
17
- if (key in doc) return;
18
- doc[key] = data[key];
19
- });
20
-
21
- var res = {
22
- headers: {
23
- "Content-Type": "application/json"
24
- },
25
- body: toJSON(doc)
26
- };
27
-
28
- return [doc, res];
29
- }
30
- }
31
-
32
- };
package/test/shared.js DELETED
@@ -1,58 +0,0 @@
1
- "use strict";
2
-
3
- var clerk = require("../clerk");
4
- var expect = require("expect.js");
5
-
6
- exports.clerkFactory = function () {
7
- this.baseURL = "http://127.0.0.1:5984";
8
- this.dbname = "clerk-test-" + (1000 * Math.random() | 0);
9
- this.client = clerk(this.baseURL);
10
- this.db = this.client.db(this.dbname);
11
- };
12
-
13
- exports.randomId = function () {
14
- return "clerk-test-" + Date.now() + (Math.random() * 1000000 | 0);
15
- };
16
-
17
- exports.docFactory = function () {
18
- this.doc = {
19
- _id: exports.randomId(),
20
- hello: "world"
21
- };
22
- this.docs = [];
23
- var i, j;
24
- for (i = 1; i < 10; i++) {
25
- this.docs.push({
26
- _id: exports.randomId(),
27
- hello: "world" + i
28
- });
29
- }
30
- };
31
-
32
- exports.createDB = function (key) {
33
- return function (done) {
34
- this[key].create(function (err, body, status, headers, res) {
35
- if (!err) {
36
- exports.shouldBeOk(body);
37
- } else {
38
- expect(status).to.be(412); // database exists
39
- err = null;
40
- }
41
- done(err);
42
- });
43
- };
44
- };
45
-
46
- exports.destroyDB = function (key) {
47
- return function (done) {
48
- this[key].destroy(done);
49
- };
50
- };
51
-
52
- exports.shouldBeOk = function (body) {
53
- expect(body).to.have.property("ok", true);
54
- };
55
-
56
- exports.shouldHave2xxStatus = function (status) {
57
- expect(status).to.be.within(200, 299);
58
- };
package/webpack.config.js DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- /**
4
- * Webpack configuration.
5
- */
6
-
7
- exports = module.exports = {
8
- output: {
9
- library: "clerk",
10
- libraryTarget: "umd",
11
- sourcePrefix: ""
12
- },
13
- externals: [{
14
- "sinon": "sinon",
15
- "es6-promise": "window"
16
- }],
17
- devtool: "source-map",
18
- node: {
19
- Buffer: false
20
- }
21
- };