-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest.defaults.js
More file actions
185 lines (160 loc) · 6.02 KB
/
test.defaults.js
File metadata and controls
185 lines (160 loc) · 6.02 KB
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
'use strict';
if (!process.env.LEVEL_ADAPTER &&
!process.env.LEVEL_PREFIX &&
!process.env.AUTO_COMPACTION &&
!process.env.ADAPTERS) {
// these tests don't make sense for anything other than default leveldown
const path = require('path');
const { mkdirSync } = require('fs');
const rimraf = require('rimraf');
describe('defaults', () => {
beforeEach(async () => {
await new PouchDB('mydb').destroy();
await new PouchDB('mydb', {db: require('memdown')}).destroy();
});
afterEach(() => {
rimraf.sync('./tmp/_pouch_.');
rimraf.sync('./tmp/path');
});
it('should allow prefixes', async () => {
const prefix = './tmp/path/to/db/1/';
const dir = path.join(prefix, '/tmp/');
const dir2 = path.join('./tmp/_pouch_./', prefix);
const dir3 = path.join(dir2, './tmp/_pouch_mydb');
mkdirSync(dir, { recursive:true });
mkdirSync(dir2, { recursive:true });
mkdirSync(dir3, { recursive:true });
const db = new PouchDB('mydb', {prefix});
const info1 = await db.info();
info1.db_name.should.equal('mydb');
await db.destroy();
});
it('Defaults leaks eventEmitters', () => {
PouchDB.defaults({db: require('memdown') });
PouchDB.defaults({db: require('memdown') });
PouchDB.defaults({db: require('memdown') });
PouchDB.defaults({db: require('memdown') });
});
it('should allow us to set a prefix by default', async () => {
const prefix = './tmp/path/to/db/2/';
const dir = path.join(prefix, '/tmp/');
const dir2 = path.join('./tmp/_pouch_./', prefix);
const dir3 = path.join(dir2, './tmp/_pouch_mydb');
mkdirSync(dir, { recursive:true });
mkdirSync(dir2, { recursive:true });
mkdirSync(dir3, { recursive:true });
const CustomPouch = PouchDB.defaults({
prefix
});
const db = CustomPouch({name: 'mydb'});
const info1 = await db.info();
info1.db_name.should.equal('mydb');
await db.destroy();
});
it('should allow us to use memdown', async () => {
const opts = { name: 'mydb', db: require('memdown') };
const db = new PouchDB(opts);
await db.put({_id: 'foo'});
const otherDB = new PouchDB('mydb');
const info1 = await db.info();
const info2 = await otherDB.info();
info1.doc_count.should.not.equal(info2.doc_count);
await otherDB.destroy();
await db.destroy();
});
it('should allow us to destroy memdown', async () => {
const opts = {db: require('memdown') };
const db = new PouchDB('mydb', opts);
await db.put({_id: 'foo'});
const otherDB = new PouchDB('mydb', opts);
const info1 = await db.info();
const info2 = await otherDB.info();
info1.doc_count.should.equal(info2.doc_count);
await otherDB.destroy();
const db3 = new PouchDB('mydb', opts);
const info = await db3.info();
info.doc_count.should.equal(0);
await db3.destroy();
});
it('should allow us to use memdown by default', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const db = new CustomPouch('mydb');
await db.put({_id: 'foo'});
const otherDB = new PouchDB('mydb');
const info1 = await db.info();
const info2 = await otherDB.info();
info1.doc_count.should.not.equal(info2.doc_count);
await otherDB.destroy();
await db.destroy();
});
it('should inform us when using memdown', async () => {
const opts = { name: 'mydb', db: require('memdown') };
const db = new PouchDB(opts);
const info = await db.info();
info.backend_adapter.should.equal('MemDOWN');
});
it('constructor emits destroyed when using defaults', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const db = new CustomPouch('mydb');
await new Promise((resolve) => {
CustomPouch.once('destroyed', (name) => {
name.should.equal('mydb');
resolve();
});
db.destroy();
});
});
it('db emits destroyed when using defaults', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const db = new CustomPouch('mydb');
const destroyedPromise = new Promise((resolve) => db.once('destroyed', resolve));
db.destroy();
await destroyedPromise;
});
it('constructor emits creation event', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const eventPromise = new Promise((resolve) => {
CustomPouch.once('created', (name) => {
name.should.equal('mydb', 'should be same thing');
resolve();
});
});
new PouchDB('mydb');
await eventPromise;
});
// somewhat odd behavior (CustomPouch constructor always mirrors PouchDB),
// but better to test it explicitly
it('PouchDB emits destroyed when using defaults', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const db = new CustomPouch('mydb');
await new Promise((resolve) => {
PouchDB.once('destroyed', (name) => {
name.should.equal('mydb');
resolve();
});
db.destroy();
});
});
// somewhat odd behavior (CustomPouch constructor always mirrors PouchDB),
// but better to test it explicitly
it('PouchDB emits created when using defaults', async () => {
const CustomPouch = PouchDB.defaults({db: require('memdown')});
const eventPromise = new Promise((resolve) => {
PouchDB.once('created', (name) => {
name.should.equal('mydb', 'should be same thing');
resolve();
});
});
new CustomPouch('mydb');
await eventPromise;
});
it('should be transitive (#5922)', async () => {
const CustomPouch = PouchDB
.defaults({db: require('memdown')})
.defaults({});
const db = new CustomPouch('mydb');
const info = await db.info();
info.backend_adapter.should.equal('MemDOWN');
});
});
}