-
Notifications
You must be signed in to change notification settings - Fork 2
/
testutils.js
77 lines (66 loc) · 1.78 KB
/
testutils.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
require('babel-polyfill');
var fs = require('fs');
var path = require('path');
var Promise = require('pouchdb-promise');
var chai = require('chai');
exports.PouchDB = require('pouchdb').defaults({
db: require('memdown')
});
var testConfig;
var home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var confPath = path.join(home, '.pouchdb-plugin-helper-conf.json');
try {
testConfig = JSON.parse(fs.readFileSync(confPath, {encoding: 'utf8'}));
} catch (err) {
testConfig = {};
}
exports.should = chai.should();
var db;
exports.setup = function () {
db = new exports.PouchDB('test');
return db;
};
exports.setupWithDoc = function() {
exports.setup();
return db.put({
_id: 'mytest',
test: true
}).then(function (info) {
return {
db: db,
rev: info.rev
};
});
}
exports.setupWithDocAndAttachment = function () {
var res;
return exports.setupWithDoc().then(function (info) {
res = info;
var buffer = new Buffer('abcd', 'ascii');
return db.putAttachment('attachment_test', 'text', buffer, 'text/plain');
}).then(function (info) {
res.attRev = info.rev;
return res;
});
}
exports.teardown = function () {
return db.destroy();
};
exports.shouldThrowError = function (func) {
return Promise.resolve().then(function () {
return func();
}).then(function () {
'No error thrown while it should have been'.should.equal('');
}).catch(function (err) {
return err;
});
};
exports.BASE_URL = testConfig.base_url || 'http://localhost:5984';
exports.HTTP_AUTH = testConfig.username ? {
username: testConfig.username,
password: testConfig.password
} : null;
exports.setupHTTP = function () {
db = new exports.PouchDB(exports.BASE_URL + '/pouchdb-plugin-helper-db', {auth: exports.HTTP_AUTH});
return db;
};