-
Notifications
You must be signed in to change notification settings - Fork 12
/
pouchdb-tests.ts
427 lines (366 loc) · 16.5 KB
/
pouchdb-tests.ts
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Type definitions for pouchdb v3.4.0
// Project: https://pouchdb.com/, https://github.com/pouchdb/pouchdb
// Definitions by: Andy Brown <https://github.com/AGBrown> (https://github.com/AGBrown/pouchdb.d.ts)
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// THIS FILE:
// This file provides "compilation" tests for pouchdb.d.ts as per the DefinitelyTyped best practices
/// <reference path="pouchdb.d.ts" />
/// <reference path="pouchdb-loose.d.ts" />
/// <reference path="pouchdb-plugins.d.ts" />
module PouchDBTest {
module promise {
class Foo {
foo: string;
}
class fakePromise<T> implements Promise<T> {
new() { }
then<R>(onFulfilled?: (value: T) => Promise<R> | R, onRejected?: (error: any) => Promise<R> | R) {
return new fakePromise<R>();
}
catch<R>(onRejected: (error: any) => Promise<R> | R) {
return undefined;
}
[Symbol.toStringTag]: string;
}
function chainedThen() {
var fooOut: string;
new PouchDB("dbname")
.then((db) => new fakePromise<Foo>())
.then((value: Foo) => { fooOut = value.foo; });
}
}
module localDb {
// common variables
var dbt: pouchdb.thenable.PouchDB;
var dbp: pouchdb.promise.PouchDB;
var dbc: pouchdb.callback.PouchDB;
// common then, err and callback methods
var myThen = (db: pouchdb.promise.PouchDB) => { dbp = db };
var myErr = (err: any) => { };
var myCb = (err: any, db: pouchdb.callback.PouchDB) => { };
module localDbGeneral {
function nameOnly() {
// create local db just using name
dbt = new PouchDB("dbname");
dbc = new PouchDB("dbname", undefined);
new PouchDB("dbname").then(myThen, myErr);
dbc = new PouchDB("dbname", myCb);
}
function nameAndLocalDbOptions() {
// create local db using name and options
var opts1: pouchdb.options.ctor.LocalDb = {
adapter: "idb",
auto_compaction: true
};
dbt = new PouchDB("dbname", opts1);
new PouchDB("dbname", opts1).then(myThen, myErr);
dbc = new PouchDB("dbname", opts1, myCb);
// create local db using name and an extra option
var opts2: pouchdb.options.ctor.LocalDb = {
adapter: "idb",
auto_compaction: true,
foo: "bar"
};
dbt = new PouchDB("dbname", opts2);
new PouchDB("dbname", opts2).then(myThen, myErr);
dbc = new PouchDB("dbname", opts2, myCb);
}
function nameAndLocalDbOptionsNoType() {
// create local db using name and options
new PouchDB("dbname", {
auto_compaction: true
});
// create local db using name and options
new PouchDB("dbname", {
adapter: "idb"
});
// create local db using name and options
new PouchDB("dbname", {
adapter: "idb",
auto_compaction: true
});
// create local db using name and an extra option
new PouchDB("dbname", {
adapter: "idb",
auto_compaction: true,
foo: "bar"
});
}
function LocalDbWithName() {
// create local db using options with name
var opts1: pouchdb.options.ctor.LocalDbWithName = {
name: "dbname",
adapter: "idb",
auto_compaction: false
};
dbt = new PouchDB(opts1);
new PouchDB(opts1).then(myThen, myErr);
dbc = new PouchDB(opts1, myCb);
}
function LocalDbWithNameNoType() {
// create local db using options with name
new PouchDB({
name: "dbname",
adapter: "idb",
auto_compaction: false
});
}
}
module sqLite {
function nameAndLocalSQLiteDbOptions() {
// create local db using name and options
var sqliteOpts: pouchdb.options.ctor.LocalSQLiteDb = {
adapter: "websql",
auto_compaction: true,
createFromLocation: 0,
location: 2,
size: 5
};
dbt = new PouchDB("dbname", sqliteOpts);
new PouchDB("dbname", sqliteOpts).then(myThen, myErr);
dbc = new PouchDB("dbname", sqliteOpts, myCb);
}
function nameAndLocalSQLiteDbNoType() {
// create local db using name and options
new PouchDB("dbname", {
adapter: "websql",
auto_compaction: true,
createFromLocation: 0,
location: 2,
size: 5
});
}
function LocalSQLiteDbWithName() {
// create local db using name and options
var sqliteOpts: pouchdb.options.ctor.LocalSQLiteDbWithName = {
name: "dbname",
adapter: "websql",
auto_compaction: true,
createFromLocation: 0,
location: 2,
size: 5
};
dbt = new PouchDB(sqliteOpts);
new PouchDB(sqliteOpts).then(myThen, myErr);
dbc = new PouchDB(sqliteOpts, myCb);
}
function LocalSQLiteDbWithNameNoType() {
// create local db using name and options
new PouchDB({
name: "dbname",
adapter: "websql",
auto_compaction: true,
createFromLocation: 0,
location: 2,
size: 5
});
}
}
module websql {
function nameAndLocalWebSQLOptions() {
// create local db using name and options
var sqliteOpts: pouchdb.options.ctor.LocalWebSQLDb = {
adapter: "websql",
auto_compaction: false,
size: 5
};
dbt = new PouchDB("dbname", sqliteOpts);
new PouchDB("dbname", sqliteOpts).then(myThen, myErr);
dbc = new PouchDB("dbname", sqliteOpts, myCb);
}
function nameAndLocalWebSQLNoType() {
// create local db using name and options
new PouchDB("dbname", {
adapter: "websql",
auto_compaction: false,
size: 5
});
}
function LocalWebSQLWithName() {
// create local db using name and options
var sqliteOpts: pouchdb.options.ctor.LocalWebSQLDbWithName = {
name: "dbname",
adapter: "websql",
auto_compaction: false,
size: 5
};
dbt = new PouchDB(sqliteOpts);
new PouchDB(sqliteOpts).then(myThen, myErr);
dbc = new PouchDB(sqliteOpts, myCb);
}
function LocalWebSQLWithNameNoType() {
// create local db using name and options
new PouchDB({
name: "dbname",
adapter: "websql",
auto_compaction: false,
size: 5
});
}
}
}
module methods {
module close {
function callback() {
var db: pouchdb.callback.PouchDB = new PouchDB("dbname", (e, v) => { });
db.close();
db.close((err, msg) => { });
}
function promise() {
var db1: pouchdb.thenable.PouchDB = new PouchDB("dbname");
var db2: pouchdb.promise.PouchDB = new PouchDB("dbname");
db1.close();
db1.close().then(msg => { }, err => { });
}
}
module destroy {
var destroyOpts = { ajax: "full" };
function callback() {
var db: pouchdb.callback.PouchDB = new PouchDB("dbname", (e, v) => { });
var myCb1 = (err: any, info: pouchdb.api.methods.destroy.Info) => { };
var myCb2 = () => { };
// As per the 3.4.0 https://pouchdb.com/api.html#delete_database
db.destroy(destroyOpts, myCb1);
db.destroy(myCb1);
db.destroy(destroyOpts, myCb2);
db.destroy(myCb2);
db.destroy(destroyOpts);
db.destroy();
}
function promise() {
var myThen = (db: pouchdb.api.methods.destroy.Info) => { };
var myErr = (err: any) => { };
var db1: pouchdb.thenable.PouchDB = new PouchDB("dbname");
var db2: pouchdb.promise.PouchDB = new PouchDB("dbname");
db1.destroy(destroyOpts).then(myThen, myErr);
db1.destroy().then(myThen, myErr);
db2.destroy(destroyOpts).then(myThen, myErr);
db2.destroy().then(myThen, myErr);
}
}
module get {
interface TestDoc extends pouchdb.api.methods.ExistingDoc { test: string; }
var id = "1";
var test: string;
function callback() {
var db: pouchdb.callback.PouchDB = new PouchDB("dbname", (e, v) => { });
db.get<TestDoc>(id);
db.get<TestDoc>(id, (err, doc) => { test = doc.test; });
db.get<TestDoc>(id, {});
db.get<TestDoc>(id, {}, (err, doc) => { test = doc.test; });
}
function promise() {
var db1: pouchdb.thenable.PouchDB = new PouchDB("dbname");
var db2: pouchdb.promise.PouchDB = new PouchDB("dbname");
db1.get<TestDoc>(id);
db1.get<TestDoc>(id).then(doc => { test = doc.test; }, err => { });
db2.get<TestDoc>(id);
db2.get<TestDoc>(id).then(doc => { test = doc.test; }, err => { });
}
}
module put {
var oe = {};
var of = { foo: "test" };
var eDoc: pouchdb.api.methods.ExistingDoc = { foo: "test", _id: "1", _rev: "1" };
var nDoc: pouchdb.api.methods.NewDoc = { foo: "test", _id: "1" };
var bDoc: pouchdb.api.methods.BaseDoc = { foo: "test" };
var id = "1";
var rv = "1";
function callback() {
var db: pouchdb.callback.PouchDB = new PouchDB("dbname", (e, v) => { });
// Overload tests for the callback versions of put
db.put(eDoc);
db.put(eDoc, (err, val) => { });
db.put(eDoc, (err) => { });
db.put(eDoc, oe);
db.put(eDoc, of);
db.put(eDoc, oe, (err, val) => { val.ok === true; });
db.put(eDoc, of, (err, val) => { val.ok === true; });
db.put(eDoc, of, (err) => { err.error; });
db.put(nDoc);
db.put(nDoc, (err, val) => { });
db.put(nDoc, oe);
db.put(nDoc, of);
db.put(nDoc, oe, (err, val) => { val.ok === true; });
db.put(nDoc, of, (err, val) => { val.ok === true; });
db.put(nDoc, of, (err) => { err.error; });
db.put(bDoc, id, rv);
db.put(bDoc, id, rv, (err, val) => { val.ok === true; });
db.put(bDoc, id, rv, oe);
db.put(bDoc, id, rv, of);
db.put(bDoc, id, rv, oe, (err, val) => { val.ok === true; });
db.put(bDoc, id, rv, of, (err, val) => { val.ok === true; });
db.put(bDoc, id, rv, of, (err) => { err.error; });
db.put(bDoc, id);
db.put(bDoc, id, (err, val) => { val.ok === true; });
db.put(bDoc, id, oe);
db.put(bDoc, id, of);
db.put(bDoc, id, oe, (err, val) => { val.ok === true; });
db.put(bDoc, id, of, (err, val) => { val.ok === true; });
db.put(bDoc, id, of, (err) => { err.error; });
}
function promise() {
var db1: pouchdb.thenable.PouchDB = new PouchDB("dbname");
var db2: pouchdb.promise.PouchDB = new PouchDB("dbname");
db1.put(eDoc).then(resp => { }, err => { }).catch(err => { });
db2.put(eDoc).then(resp => { }, err => { }).catch(err => { });
db2.put(eDoc).then(resp => { }).catch(err => { });
db2.put(eDoc).catch(err => { });
db1.put(eDoc, {}).then(resp => { }, err => { }).catch(err => { });
db2.put(eDoc, {}).then(resp => { }, err => { }).catch(err => { });
db1.put(nDoc).then(resp => { }, err => { }).catch(err => { });
db2.put(nDoc).then(resp => { }, err => { }).catch(err => { });
db1.put(nDoc, {}).then(resp => { }, err => { }).catch(err => { });
db2.put(nDoc, {}).then(resp => { }, err => { }).catch(err => { });
db1.put(bDoc, id, rv).then(resp => { }, err => { }).catch(err => { });
db2.put(bDoc, id, rv).then(resp => { }, err => { }).catch(err => { });
db1.put(bDoc, id, rv, {}).then(resp => { }, err => { }).catch(err => { });
db2.put(bDoc, id, rv, {}).then(resp => { }, err => { }).catch(err => { });
db1.put(bDoc, id).then(resp => { }, err => { }).catch(err => { });
db2.put(bDoc, id).then(resp => { }, err => { }).catch(err => { });
db1.put(bDoc, id, {}).then(resp => { }, err => { }).catch(err => { });
db2.put(bDoc, id, {}).then(resp => { }, err => { }).catch(err => { });
}
}
module remove {
var oe = {};
var of = { foo: "test" };
var or = { rev: "1" };
var eDoc: pouchdb.api.methods.ExistingDoc = { foo: "test", _id: "1", _rev: "1" };
var nDoc: pouchdb.api.methods.NewDoc = { foo: "test", _id: "1" };
var bDoc: pouchdb.api.methods.BaseDoc = { foo: "test" };
var id = "1";
var rv = "1";
function callback() {
var db: pouchdb.callback.PouchDB = new PouchDB("dbname", (e, v) => { });
db.remove(id, rv);
db.remove(id, rv, (err, val) => { });
db.remove(id, rv, {});
db.remove(id, rv, {}, (err, val) => { });
db.remove(eDoc);
db.remove(eDoc, (err, val) => { });
db.remove(eDoc, {});
db.remove(eDoc, {}, (err, val) => { });
db.remove(nDoc, or, (err, val) => { });
}
function promise() {
var db1: pouchdb.thenable.PouchDB = new PouchDB("dbname");
var db2: pouchdb.promise.PouchDB = new PouchDB("dbname");
db1.remove(id, rv);
db2.remove(id, rv);
db1.remove(id, rv, {});
db2.remove(id, rv, {});
db1.remove(id, rv, of);
db2.remove(id, rv, of);
db1.remove(eDoc);
db2.remove(eDoc);
db1.remove(eDoc, {});
db2.remove(eDoc, {});
db1.remove(eDoc, of);
db2.remove(eDoc, of);
db1.remove(nDoc, or);
db2.remove(nDoc, or);
}
}
}
}