-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-index.spec.js
103 lines (76 loc) · 2.52 KB
/
add-index.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
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
/*eslint-env es6*/
const Bluebird = require('bluebird')
const debug = require('debug')('got-couch')
const CONFIG = require('./config/_index.js')
const { initCouchDb, initTest } = require('./_test-base.js')
const DB_NAME = 'couch-add-index-test'
const CREATED = 201
const couchdb = initCouchDb(CONFIG)
const test = initTest(couchdb, DB_NAME)
test('::addIndex should create an index that is used when a query is ' +
'executed by the ::query (find) function', t =>
couchdb.then((connection) => {
let index = null
Bluebird.all([
connection.create(DB_NAME, 'index1', { test: 'INDEX', moo: 'Elsie' })
, connection.create(DB_NAME, 'index2', { test: 'INDEX', moo: 'Clara' })
, connection.create(DB_NAME, 'index3', { test: 'INDEX', moo: 'Bessie' })
, connection.create(DB_NAME, 'index4', { test: 'INDEX', moo: 'Babe' })
])
.then(() => connection.ensureFullCommit(DB_NAME))
.tap ((response) => {
t.is(
CREATED, response.statusCode,
'ensure documents created for index test'
)
})
.then(() => connection.addIndex(DB_NAME, {
index: { fields: ["test", "moo"] }
, name: "test-moo-index"
}, {}))
.tap ((response) => {
index = response.body
})
.then(() => connection.ensureFullCommit(DB_NAME))
.tap ((response) => {
t.is(
CREATED, response.statusCode,
'ensure full commit after index create is 201'
)
})
.then(() => connection.query(DB_NAME, {
selector: { test: 'INDEX', moo: 'Babe' }
}, {}))
.then((result) => {
const expected_count = 1
const expected_rows = [
{ id: 'index4', test: 'INDEX', moo: 'Babe' }
]
const _rowMap = (x) => ({ id: x._id, test: x.test, moo: x.moo })
const actual_rows = result
.body
.docs
.map(_rowMap)
const has_warning = result.body.hasOwnProperty('warning')
t.is(actual_rows.length, expected_count, 'list count is not 1')
t.deepEqual(actual_rows, expected_rows, 'list not what I expected')
t.falsy(has_warning, 'query generated an unexpected warning message')
})
.then(() => {
index.type = 'json'
index.ddoc = index.id
return connection.deleteIndex(DB_NAME, index)
})
.then((result) => {
t.is(result.ok, true, "deleting index failed miserably")
})
.catch((err) => {
t.fail(err)
})
})
.catch((err) => {
debug("ADD INDEX: %o", err)
t.fail(err)
throw err
})
)