/
Client.it.ts
329 lines (295 loc) · 11.2 KB
/
Client.it.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
Copyright 2017 Yuce Tekol
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
import {expect} from 'chai';
import {Client, Cluster, Index, URI, TimeQuantum} from '../src/index';
import * as nock from 'nock';
const SERVER_ADDRESS = "http://localhost:10101";
class Util {
private static counter = 0;
static getClient() {
return new Client(SERVER_ADDRESS);
}
static getRandomIndex() {
return new Index(`testdb-${Util.counter++}`);
}
}
describe('Client', () => {
let db = Util.getRandomIndex();
const frameNames = [
"foo", "query-test", "another-frame", "test",
"count-test", "importframe", "topn_test"
];
let frame = db.frame("test");
const client = Util.getClient();
beforeEach(done => {
client.ensureIndex(db).then(() => {
let ensureFramesList = [];
for (let name of frameNames) {
ensureFramesList.push(client.ensureFrame(db.frame(name)));
}
Promise.all(ensureFramesList).
then(_value => done(), done);
}).catch(done);
});
afterEach(done => {
nock.cleanAll();
const client = Util.getClient()
client.deleteIndex(db)
.then(done)
.catch(done);
});
it('should return a response', done => {
client.query(db.frame("query-test").setBit(155, 10)).then(r => {
expect(r).not.equal(null);
done();
}).catch(done);
});
it('should create and delete an index', done => {
const tempdb = new Index("to-be-deleted-" + db.name);
client.createIndex(tempdb).then(() =>
client.createFrame(tempdb.frame("delframe"))).then(() =>
client.query(tempdb.frame("delframe").setBit(1, 2))).then(() =>
client.deleteIndex(tempdb))
.then(done)
.catch(done);
});
it('should throw an error on connection failure', done => {
const client = new Client("http://non-existent-sub.pilosa.com:22222");
client.query(frame.setBit(15, 10))
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail on unknown scheme', done => {
const client = new Client("notknown://:15555");
client.query(frame.setBit(15, 10))
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail on parsing errors', done => {
client.query(db.rawQuery("SetBit(id=15, frame='test', col_id:=10)"))
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('can receive count responses', done => {
const f = db.frame("count-test");
client.query(db.batchQuery(
f.setBit(10, 20),
f.setBit(10, 21),
f.setBit(15, 25))).then(_ =>
client.query(db.count(f.bitmap(10)))).then(r => {
expect(r.result).not.null;
if (r.result) {
expect(r.result.count).equal(2);
}
done();
}).catch(done);
});
it('should fail when creating existing index', done => {
client.createIndex(db)
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail when creating existing frame', done => {
client.createFrame(frame)
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail if it cannot delete an index', done => {
const client = new Client("http://non-existent-sub.pilosa.com:22222");
client.deleteIndex(new Index("non-existent"))
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail if it cannot ensure index existence', done => {
const client = new Client("http://non-existent-sub.pilosa.com:22222");
client.ensureIndex(db)
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('should fail if it cannot ensure frame existence', done => {
const client = new Client("http://non-existent-sub.pilosa.com:22222");
client.ensureFrame(db.frame("foo"))
.then(() => done(new Error("should have failed")))
.catch(_e => done());
});
it('can ensure index exists', done => {
const tempdb = new Index(db.name + "-ensure");
client.ensureIndex(tempdb).then(() =>
client.ensureFrame(tempdb.frame("frm"))).then(() =>
client.ensureIndex(tempdb)).then(() =>
client.deleteIndex(tempdb))
.then(done)
.catch(done);
});
it('can ensure frame exists', done => {
const frameName = "ensure-frame";
const frame = db.frame(frameName);
client.ensureFrame(frame).then(() =>
client.query(frame.bitmap(1))).then(() =>
client.ensureFrame(frame))
.then(done)
.catch(done);
});
it('can create an index with time quantum', done => {
const db = new Index("db-with-timequantum", {
timeQuantum: TimeQuantum.YEAR
});
client.ensureIndex(db).then(() =>
client.deleteIndex(db))
.then(done)
.catch(done);
});
it('can create a frame with time quantum', done => {
const frame = db.frame("frame-with-timequantum", {
timeQuantum: TimeQuantum.YEAR_MONTH_DAY
});
client.ensureFrame(frame)
.then(done)
.catch(done);
});
it('can delete a frame', done => {
const frame = db.frame("to-be-deleted");
client.ensureFrame(frame).then(() =>
client.deleteFrame(frame))
.then(done)
.catch(done);
});
it('can query bitmap', done => {
client.query(db.batchQuery(
frame.setBit(5, 10),
frame.setBit(5, 15),
frame.setBit(10, 20))).then(() =>
client.query(db.batchQuery(
frame.bitmap(5),
frame.bitmap(10)))).then(r => {
expect(r.results.length).equal(2);
expect(r.result).not.null;
if (r.result) {
expect(r.result.bitmap.attributes).eql({});
expect(r.result.bitmap.bits).eql([10, 15]);
expect(r.results[0].bitmap.bits).eql([10, 15]);
expect(r.results[1].bitmap.bits).eql([20]);
}
done();
}).catch(done);
});
it('can query topn', done => {
const sleep = (ms: number) => {
return new Promise((resolve, _) => {
setTimeout(_ => resolve(), ms);
});
}
let frame = db.frame("topn_test");
client.query(db.batchQuery(
frame.setBit(10, 5),
frame.setBit(10, 10),
frame.setBit(10, 15),
frame.setBit(20, 5),
frame.setBit(30, 5))).then(_ =>
// XXX: The following is required to make this test pass. See: https://github.com/pilosa/pilosa/issues/625
sleep(10000)).then(() =>
client.query(frame.topN(2))).then(r => {
expect(r.result).not.null;
if (r.result) {
expect(r.result.countItems.length).equal(2);
expect(r.result.countItems[0].id).equal(10);
expect(r.result.countItems[0].count).equal(3);
}
done();
}).catch(done);
});
it('can retrieve bitmap attributes', done => {
const attrs = {
name: "some string",
age: 95,
height: 1.83,
registered: true
};
client.query(db.batchQuery(
frame.setBit(10, 44),
frame.setRowAttrs(10, attrs))).then(_ =>
client.query(frame.bitmap(10))).then(r => {
expect(r.result).not.null;
if (r.result) {
expect(r.result.bitmap.attributes).eql({
name: "some string",
age: 95,
height: 1.83,
registered: true
});
}
done();
}).catch(done);
});
it('can query with columns', done => {
const frame = db.frame("query-test");
client.ensureFrame(frame).then(() =>
client.query(frame.setBit(100, 1000))).then(() =>
client.query(frame.setBit(100, 1000))).then(() =>
client.query(db.setColumnAttrs(1000, {name: "bombo"}))).then(() =>
client.query(frame.bitmap(100), {columns: true})).then(r => {
expect(r).not.null;
expect(r.column).not.null;
if (r.column) {
expect(r.column.id).equal(1000);
expect(r.column.attributes).eql({name: "bombo"});
}
}).then(() =>
client.query(frame.bitmap(300))).then(r => {
expect(r).not.null;
expect(r.column).null;
done();
}).catch(done);
});
it('should throw an expection if the response was not decoded', done => {
nock(SERVER_ADDRESS).
post(`/index/${db.name}/query`).
reply(200, () => "bad-reply");
client.query(db.frame("foo").bitmap(1))
.then(_ => done(new Error("should have failed")))
.catch(_e => done());
});
it('can execute httpRequest with no data', done => {
nock(SERVER_ADDRESS).
get("/schema").
reply(200, () => "{}");
class C extends Client {
static defaultClient() {
return new C(new Cluster(URI.address(SERVER_ADDRESS)));
}
httpSchema() {
return this.httpRequest("GET", "/schema");
}
}
const client = C.defaultClient();
client.httpSchema()
.then(_ => done())
.catch(done);
});
});