forked from pouchdb/pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.local_docs.js
203 lines (186 loc) · 5.97 KB
/
test.local_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
var adapters = ['http', 'local'];
adapters.forEach(function (adapter) {
describe('test.local_docs.js-' + adapter, function () {
var dbs = {};
beforeEach(function (done) {
dbs.name = testUtils.adapterUrl(adapter, 'testdb');
testUtils.cleanup([dbs.name], done);
});
after(function (done) {
testUtils.cleanup([dbs.name], done);
});
it('local docs - put then get', function () {
var db = new PouchDB(dbs.name);
return db.put({_id: '_local/foo'}).then(function (res) {
res.id.should.equal('_local/foo');
res.rev.should.be.a('string');
res.ok.should.equal(true);
return db.get('_local/foo');
});
});
it('local docs - put then get w/ revisions', function () {
var db = new PouchDB(dbs.name);
var doc = {
_id: '_local/foo'
};
return db.put(doc).then(function (res) {
res.id.should.equal('_local/foo');
res.rev.should.be.a('string');
res.ok.should.equal(true);
return db.get('_local/foo');
}).then(function (doc) {
should.not.exist(doc._revisions);
doc._revisions = {start: 0, ids: ['1']};
return db.put(doc);
}).then(function () {
return db.get('_local/foo');
}).then(function (doc) {
should.not.exist(doc._revisions);
});
});
it('local docs - put then remove then get', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.remove(doc);
}).then(function (res) {
res.id.should.equal('_local/foo');
res.rev.should.equal('0-0');
res.ok.should.equal(true);
return db.get('_local/foo').then(function (doc) {
should.not.exist(doc);
}).catch(function (err) {
err.name.should.equal('not_found');
});
});
});
it('local docs - put after remove', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.remove(doc);
}).then(function (res) {
res.id.should.equal('_local/foo');
res.rev.should.equal('0-0');
res.ok.should.equal(true);
delete doc._rev;
return db.put(doc);
});
});
it('local docs - put after remove, check return vals', function () {
// as long as it starts with 0-, couch
// treats it as a new local doc
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/quux'};
return db.put(doc).then(function (res) {
res.rev.should.equal('0-1');
res.ok.should.equal(true);
doc._rev = res.rev;
return db.put(doc);
}).then(function (res) {
res.rev.should.equal('0-2');
res.ok.should.equal(true);
doc._rev = res.rev;
return db.put(doc);
}).then(function (res) {
res.rev.should.equal('0-3');
res.ok.should.equal(true);
});
});
it('local docs - put after put w/ deleted:true', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
doc._deleted = true;
return db.put(doc);
}).then(function (res) {
res.id.should.equal('_local/foo');
res.rev.should.equal('0-0');
res.ok.should.equal(true);
delete doc._deleted;
delete doc._rev;
return db.put(doc);
});
});
it('local docs - put after remove with a rev', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.remove(doc);
}).then(function (res) {
res.id.should.equal('_local/foo');
res.ok.should.equal(true);
res.rev.should.equal('0-0');
delete doc._rev;
return db.put(doc);
});
});
it('local docs - multiple removes', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.put(doc);
}).then(function (res) {
doc._rev = res.rev;
return db.remove(doc);
}).then(function (res) {
res.rev.should.equal('0-0');
delete doc._rev;
return db.put(doc);
}).then(function (res) {
doc._rev = res.rev;
return db.remove(doc);
}).then(function (res) {
res.rev.should.equal('0-0');
});
});
it('local docs - get unknown', function () {
var db = new PouchDB(dbs.name);
return db.get('_local/foo').then(function (doc) {
should.not.exist(doc);
}).catch(function (err) {
err.name.should.equal('not_found');
});
});
it('local docs - put unknown', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo', _rev: '1-fake'};
return db.put(doc).then(function (res) {
should.not.exist(res);
}).catch(function (err) {
err.name.should.be.a('string');
});
});
it('local docs - put new and conflicting', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function () {
return db.put(doc);
}).then(function (res) {
should.not.exist(res);
}).catch(function (err) {
err.name.should.be.a('string');
});
});
it('local docs - put modified and conflicting', function () {
var db = new PouchDB(dbs.name);
var doc = {_id: '_local/foo'};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.put(doc);
}).then(function () {
return db.put(doc);
}).then(function (res) {
should.not.exist(res);
}).catch(function (err) {
err.name.should.be.a('string');
});
});
});
});