-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbulk-upsert.spec.js
64 lines (47 loc) · 1.57 KB
/
bulk-upsert.spec.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
/*eslint-env es6*/
const R = require('ramda')
const debug = require('debug')('got-couch')
const CONFIG = require('./config/_index.js')
const { initCouchDb, initTest } = require('./_test-base.js')
const DB_NAME = 'couch-bulk-upsert-test'
const couchdb = initCouchDb(CONFIG)
const test = initTest(couchdb, DB_NAME)
test('::bulk_upsert should update a list of documents in the database ' +
'based on the _id and _rev keys. If these two keys don\'t exist, create ' +
'the documents in the database', t =>
couchdb.then((connection) => {
connection.create(DB_NAME, '1', { first: 'matt', last: 'c' })
.then((res) =>
connection.bulk_upsert(DB_NAME, [
{ _id : res.body.id
, _rev : res.body.rev
, first : 'matt'
, last : 'chuang'
, test : 'bulk_upsert'
}
, { _id : '2', first: 'jon', last: 'lee', test: 'bulk_upsert' }
])
)
.then(() => connection.list(DB_NAME, {}))
.then((res) => res.body)
.then((list) => {
const expected_count = 2
const map = {}
R.compose(
R.forEach((x) => { map[x.id] = x.doc })
, R.filter((x) => 'bulk_upsert' === x.doc.test)
)(list.rows)
t.is(expected_count, Object.keys(map).length)
t.is(map['1'].last, 'chuang')
t.is(map['2'].last, 'lee')
})
.catch((err) => {
t.fail(err)
})
})
.catch((err) => {
debug("BULK UPSERT: %o", err)
t.fail(err)
throw err
})
)