forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouch-db-integration.test.ts
90 lines (77 loc) · 2.61 KB
/
couch-db-integration.test.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
/**
* this test checks the integration with couchdb
* run 'npm run test:couchdb' to use it
* You need a running couchdb-instance on port 5984
* Run 'npm run couch:start' to spawn a docker-container
*/
import assert from 'assert';
import { waitUntil } from 'async-test-util';
import {
addPouchPlugin,
randomCouchString,
addDefaultRxPlugins
} from '../';
addDefaultRxPlugins();
addPouchPlugin(require('pouchdb-adapter-memory'));
addPouchPlugin(require('pouchdb-adapter-http'));
import request from 'request-promise-native';
import * as humansCollection from './helper/humans-collection';
import * as schemaObjects from './helper/schema-objects';
describe('couchdb-db-integration.test.js', () => {
const COUCHDB_URL = 'http://127.0.0.1:5984/';
it('reach couchdb server', async function () {
/**
* After the couchdb container is started,
* it can take some time until the replication endpoint is reachable
*/
this.timeout(1000 * 60);
await waitUntil(async () => {
try {
const gotJson = await request(COUCHDB_URL);
// ensure json is parseable
JSON.parse(gotJson);
return true;
} catch (err) {
console.error('could not reach couchdb server at ' + COUCHDB_URL);
return false;
}
}, 1000 * 60, 1000);
});
it('sync to couchdb', async () => {
const col = await humansCollection.create(0);
const couchName = COUCHDB_URL + randomCouchString(12);
const replicationState = await col.syncCouchDB({
remote: couchName,
waitForLeadership: false,
direction: {
pull: true,
push: true
}
});
replicationState.docs$.subscribe(docData => console.dir(docData));
// add 3 docs
await Promise.all(
new Array(3)
.fill(0)
.map(() => col.insert(schemaObjects.human()))
);
const docs1 = await col.find().exec();
assert.strictEqual(docs1.length, 3);
// create a new collection
const col2 = await humansCollection.create(0);
await col2.syncCouchDB({
remote: couchName,
waitForLeadership: false,
direction: {
pull: true,
push: true
}
});
await waitUntil(async () => {
const docs = await col2.find().exec();
return docs.length === 3;
});
col.database.destroy();
col2.database.destroy();
});
});