Skip to content

Commit 7a3896f

Browse files
committed
refactor: use async/await, const and arrow function
1 parent f10b4fb commit 7a3896f

1 file changed

Lines changed: 58 additions & 66 deletions

File tree

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
describe('test.use-index.js', function () {
4-
beforeEach(function () {
5-
var db = context.db;
6-
return db.bulkDocs([
3+
describe('test.use-index.js', () => {
4+
beforeEach(async () => {
5+
const db = context.db;
6+
await db.bulkDocs([
77
{ name: 'mario', _id: 'mario', rank: 5, series: 'mario', debut: 1981 },
88
{ name: 'jigglypuff', _id: 'puff', rank: 8, series: 'pokemon', debut: 1996 },
99
{ name: 'link', rank: 10, _id: 'link', series: 'zelda', debut: 1986 },
@@ -16,95 +16,87 @@ describe('test.use-index.js', function () {
1616
{ name: 'samus', rank: 12, _id: 'samus', series: 'metroid', debut: 1986 },
1717
{ name: 'yoshi', _id: 'yoshi', rank: 6, series: 'mario', debut: 1990 },
1818
{ name: 'kirby', _id: 'kirby', series: 'kirby', rank: 2, debut: 1992 }
19-
])
20-
.then(function () {
21-
return db.createIndex({
22-
index: {
23-
"fields": ["name", "series"]
24-
},
25-
"ddoc": "index-1",
26-
"type": "json"
27-
});
28-
})
29-
.then(function () {
30-
return db.createIndex({
31-
index: {
32-
"fields": ["name", "debut"]
33-
},
34-
"ddoc": "index-2",
35-
"type": "json"
36-
});
37-
})
38-
.then(function () {
39-
return db.createIndex({
40-
index: {
41-
"fields": ["name", "another_field"]
42-
},
43-
"ddoc": "index-3",
44-
"name": "third-index",
45-
"type": "json"
46-
});
19+
]);
20+
await db.createIndex({
21+
index: {
22+
"fields": ["name", "series"]
23+
},
24+
"ddoc": "index-1",
25+
"type": "json"
26+
});
27+
await db.createIndex({
28+
index: {
29+
"fields": ["name", "debut"]
30+
},
31+
"ddoc": "index-2",
32+
"type": "json"
33+
});
34+
await db.createIndex({
35+
index: {
36+
"fields": ["name", "another_field"]
37+
},
38+
"ddoc": "index-3",
39+
"name": "third-index",
40+
"type": "json"
4741
});
4842
});
4943

50-
it.skip('use index based on ddoc', function () {
51-
var db = context.db;
52-
return db.explain({
44+
it.skip('use index based on ddoc', async () => {
45+
const db = context.db;
46+
const resp = await db.explain({
5347
selector: {
5448
name: 'mario'
5549
},
5650
use_index: "index-2",
5751
fields: ["_id"]
58-
}).then(function (resp) {
59-
resp.index.ddoc.should.equal("_design/index-2");
6052
});
53+
resp.index.ddoc.should.equal("_design/index-2");
6154
});
6255

6356
if (testUtils.adapterType() === 'http') {
6457
return;
6558
}
66-
it('use index based on ddoc and name', function () {
67-
var db = context.db;
68-
return db.explain({
59+
it('use index based on ddoc and name', async () => {
60+
const db = context.db;
61+
const resp = await db.explain({
6962
selector: {
7063
name: 'mario'
7164
},
7265
use_index: ["index-3", "third-index"],
7366
fields: ["_id"]
74-
}).then(function (resp) {
75-
resp.index.ddoc.should.equal("_design/index-3");
7667
});
68+
resp.index.ddoc.should.equal("_design/index-3");
7769
});
7870

79-
it('throws error if index does not exist', function () {
80-
var db = context.db;
81-
return db.explain({
82-
selector: {
83-
name: 'mario'
84-
},
85-
use_index: "index-not-found",
86-
fields: ["_id"]
87-
}).then(function () {
71+
it('throws error if index does not exist', async () => {
72+
const db = context.db;
73+
try {
74+
await db.explain({
75+
selector: {
76+
name: 'mario'
77+
},
78+
use_index: "index-not-found",
79+
fields: ["_id"]
80+
});
8881
throw "Should not get here";
89-
})
90-
.catch(function (err) {
82+
} catch (err) {
9183
err.error.should.equal("unknown_error");
92-
});
84+
}
9385
});
9486

95-
it('throws error if index cannot be used', function () {
96-
var db = context.db;
97-
return db.explain({
98-
selector: {
99-
rank : 2
100-
},
101-
use_index: "index-2",
102-
fields: ["_id"]
103-
}).then(function () {
87+
it('throws error if index cannot be used', async () => {
88+
const db = context.db;
89+
try {
90+
await db.explain({
91+
selector: {
92+
rank : 2
93+
},
94+
use_index: "index-2",
95+
fields: ["_id"]
96+
});
10497
throw "Should not get here";
105-
})
106-
.catch(function (err) {
98+
} catch (err) {
10799
err.error.should.equal("no_usable_index");
108-
});
100+
}
109101
});
110102
});

0 commit comments

Comments
 (0)