forked from googleapis/nodejs-bigtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.ts
629 lines (545 loc) · 18.2 KB
/
filter.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright 2016 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as assert from 'assert';
import {afterEach, before, beforeEach, describe, it} from 'mocha';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import * as fr from '../src/filter';
import {Row} from '../src/row';
const sandbox = sinon.createSandbox();
const FakeMutation = {
convertToBytes: sandbox.spy(value => {
return value;
}),
createTimeRange: sandbox.stub(),
};
describe('Bigtable/Filter', () => {
let Filter: typeof fr.Filter;
let FilterError: typeof fr.FilterError;
let filter: fr.Filter;
before(() => {
const Fake = proxyquire('../src/filter', {
'./mutation.js': {Mutation: FakeMutation},
});
Filter = Fake.Filter;
FilterError = Fake.FilterError;
});
beforeEach(() => {
filter = new Filter();
});
afterEach(() => {
sandbox.restore();
});
describe('instantiation', () => {
it('should create an empty array of filters', () => {
assert.deepStrictEqual(filter.filters_, []);
});
});
describe('convertToRegExpString', () => {
it('should convert a RegExp to a string', () => {
const str = Filter.convertToRegExpString(/\d+/);
assert.strictEqual(str, '\\d+');
});
it('should convert an Array of strings to a single string', () => {
const things = ['a', 'b', 'c'];
const str = Filter.convertToRegExpString(things);
assert.strictEqual(str, '(a|b|c)');
});
it('should convert an Array of buffers to a single string', () => {
const faces = [Buffer.from('.|.'), Buffer.from('=|=')];
const str = Filter.convertToRegExpString(faces);
assert.strictEqual(str, '(\\.\\|\\.|=\\|=)');
});
it('should not do anything to a string', () => {
const str1 = 'hello';
const str2 = Filter.convertToRegExpString(str1);
assert.strictEqual(str1, str2);
});
it('should convert a number to a string', () => {
const str = Filter.convertToRegExpString(1);
assert.strictEqual(str, '1');
});
it('should not do anything to a buffer', () => {
const str1 = 'hello';
const buffer = Buffer.from(str1);
const str2 = Filter.convertToRegExpString(buffer);
assert.deepStrictEqual(buffer, str2);
});
it('should use a binary encoding on a non utf8 buffer', () => {
const str1 = 'æ';
const buffer = Buffer.from('æ', 'binary');
const str2 = Filter.convertToRegExpString(buffer).toString('binary');
assert.strictEqual(str1, str2);
});
it('should throw an error for unknown types', () => {
const errorReg = /Can't convert to RegExp String from unknown type\./;
assert.throws(() => {
Filter.convertToRegExpString(true as {} as string);
}, errorReg);
});
});
describe('createRange', () => {
it('should create a range object', () => {
const start = 'a' as fr.BoundData;
const end = 'b' as fr.BoundData;
const key = 'Key';
const range = Filter.createRange(start, end, key);
assert.deepStrictEqual(range, {
startKeyClosed: start,
endKeyClosed: end,
});
});
it('should only create start bound', () => {
const start = 'a' as fr.BoundData;
const key = 'Key';
const range = Filter.createRange(start, null, key);
assert(FakeMutation.convertToBytes.calledWithExactly(start));
assert.deepStrictEqual(range, {
startKeyClosed: start,
});
});
it('should only create an end bound', () => {
const end = 'b' as fr.BoundData;
const key = 'Key';
const range = Filter.createRange(null, end, key);
assert(FakeMutation.convertToBytes.calledWithExactly(end));
assert.deepStrictEqual(range, {
endKeyClosed: end,
});
});
it('should optionally accept inclusive flags', () => {
const start = {
value: 'a',
inclusive: false,
};
const end = {
value: 'b',
inclusive: false,
};
const key = 'Key';
const range = Filter.createRange(start, end, key);
assert.deepStrictEqual(range, {
startKeyOpen: start.value,
endKeyOpen: end.value,
});
});
});
describe('parse', () => {
it('should call each individual filter method', () => {
sandbox.spy(Filter.prototype, 'row');
sandbox.spy(Filter.prototype, 'value');
const fakeFilter = [
{
row: 'a',
},
{
value: 'b',
},
];
Filter.parse(fakeFilter);
assert.strictEqual((Filter.prototype.row as sinon.SinonSpy).callCount, 1);
assert((Filter.prototype.row as sinon.SinonSpy).calledWithExactly('a'));
assert.strictEqual(
(Filter.prototype.value as sinon.SinonSpy).callCount,
1
);
assert((Filter.prototype.value as sinon.SinonSpy).calledWithExactly('b'));
});
it('should throw an error for unknown filters', () => {
const fakeFilter = [
{
wat: 'a',
},
];
assert.throws(Filter.parse.bind(null, fakeFilter), FilterError);
});
it('should return the filter in JSON form', () => {
const fakeProto = {a: 'a'};
const fakeFilter = [
{
column: 'a',
},
];
const stub = sandbox.stub(Filter.prototype, 'toProto').returns(fakeProto);
const parsedFilter = Filter.parse(fakeFilter);
assert.strictEqual(parsedFilter, fakeProto);
assert.strictEqual(
(Filter.prototype.toProto as sinon.SinonSpy).callCount,
1
);
stub.restore();
});
});
describe('all', () => {
it('should create a pass all filter when set to true', done => {
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'passAllFilter');
assert.strictEqual(value, true);
done();
};
filter.all(true);
});
it('should create a block all filter when set to false', done => {
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'blockAllFilter');
assert.strictEqual(value, true);
done();
};
filter.all(false);
});
});
describe('column', () => {
it('should set the column qualifier regex filter', done => {
const column = {
name: 'fake-column',
};
const spy = sandbox.stub(Filter, 'convertToRegExpString').returnsArg(0);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'columnQualifierRegexFilter');
assert.strictEqual(value, column.name);
assert(spy.calledWithExactly(column.name));
assert(FakeMutation.convertToBytes.calledWithExactly(column.name));
spy.restore();
done();
};
filter.column(column);
});
it('should handle a binary encoded buffer regex filter', done => {
const column = {
name: Buffer.from('æ', 'binary'),
};
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'columnQualifierRegexFilter');
assert.deepStrictEqual(value, column.name);
assert(FakeMutation.convertToBytes.calledWithExactly(column.name));
done();
};
filter.column(column);
});
it('should accept the short-hand version of column', done => {
const column = 'fake-column';
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'columnQualifierRegexFilter');
assert.strictEqual(value, column);
done();
};
filter.column(column);
});
it('should accept the cells per column limit filter', done => {
const column = {
cellLimit: 10,
};
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'cellsPerColumnLimitFilter');
assert.strictEqual(value, column.cellLimit);
done();
};
filter.column(column);
});
it('should accept the column range filter', done => {
const fakeRange = {
a: 'a',
b: 'b',
};
const column = {
start: 'a' as fr.BoundData,
end: 'b' as fr.BoundData,
};
const spy = sandbox.stub(Filter, 'createRange').returns(fakeRange);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'columnRangeFilter');
assert.strictEqual(value, fakeRange);
assert(spy.calledWithExactly(column.start, column.end, 'Qualifier'));
spy.restore();
done();
};
filter.column(column);
});
});
describe('condition', () => {
it('should create a condition filter', done => {
const condition = {
test: {a: 'a'},
pass: {b: 'b'},
fail: {c: 'c'},
};
const spy = sandbox.stub(Filter, 'parse').returnsArg(0);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'condition');
assert.deepStrictEqual(value, {
predicateFilter: condition.test,
trueFilter: condition.pass,
falseFilter: condition.fail,
});
assert.strictEqual(spy.getCall(0).args[0], condition.test);
assert.strictEqual(spy.getCall(1).args[0], condition.pass);
assert.strictEqual(spy.getCall(2).args[0], condition.fail);
spy.restore();
done();
};
filter.condition(condition);
});
});
describe('family', () => {
it('should create a family name regex filter', done => {
const familyName = 'fake-family';
const spy = sandbox.stub(Filter, 'convertToRegExpString').returnsArg(0);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'familyNameRegexFilter');
assert.strictEqual(value, familyName);
assert(spy.calledWithExactly(familyName));
spy.restore();
done();
};
filter.family(familyName);
});
});
describe('interleave', () => {
it('should create an interleave filter', done => {
const fakeFilters = [{}, {}, {}];
const spy = sandbox.stub(Filter, 'parse').returnsArg(0);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filter.set = (filterName, value: any) => {
assert.strictEqual(filterName, 'interleave');
assert.strictEqual(value.filters[0], fakeFilters[0]);
assert.strictEqual(value.filters[1], fakeFilters[1]);
assert.strictEqual(value.filters[2], fakeFilters[2]);
assert.strictEqual(spy.getCall(0).args[0], fakeFilters[0]);
assert.strictEqual(spy.getCall(1).args[0], fakeFilters[1]);
assert.strictEqual(spy.getCall(2).args[0], fakeFilters[2]);
spy.restore();
done();
};
filter.interleave(fakeFilters);
});
});
describe('label', () => {
it('should apply the label transformer', done => {
const label = 'label';
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'applyLabelTransformer');
assert.strictEqual(value, label);
done();
};
filter.label(label);
});
});
describe('row', () => {
it('should apply the row key regex filter', done => {
const row = {
key: 'gwashinton',
};
const convertedKey = 'abcd';
const spy = sandbox
.stub(Filter, 'convertToRegExpString')
.returns(convertedKey);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'rowKeyRegexFilter');
assert.strictEqual(value, convertedKey);
assert(spy.calledWithExactly(row.key));
assert(FakeMutation.convertToBytes.calledWithExactly(convertedKey));
spy.restore();
done();
};
filter.row(row);
});
it('should accept the short-hand version of row key', done => {
const rowKey = 'gwashington';
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'rowKeyRegexFilter');
assert.strictEqual(value, rowKey);
done();
};
filter.row(rowKey);
});
it('should set the row sample filter', done => {
const row = {
sample: 10,
};
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'rowSampleFilter');
assert.strictEqual(value, row.sample);
done();
};
filter.row(row as {} as Row);
});
it('should set the cells per row offset filter', done => {
const row = {
cellOffset: 10,
};
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'cellsPerRowOffsetFilter');
assert.strictEqual(value, row.cellOffset);
done();
};
filter.row(row);
});
it('should set the cells per row limit filter', done => {
const row = {
cellLimit: 10,
};
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'cellsPerRowLimitFilter');
assert.strictEqual(value, row.cellLimit);
done();
};
filter.row(row);
});
});
describe('set', () => {
it('should create a filter object', () => {
const key = 'notARealFilter';
const value = {a: 'b'};
filter.set(key, value);
assert.strictEqual(filter.filters_[0][key], value);
});
});
describe('sink', () => {
it('should set the sink filter', done => {
const sink = true;
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'sink');
assert.strictEqual(value, sink);
done();
};
filter.sink(sink);
});
});
describe('time', () => {
it('should set the timestamp range filter', done => {
const fakeTimeRange = {
start: 10,
end: 10,
};
const spy = FakeMutation.createTimeRange.returns(fakeTimeRange);
filter.set = (filterName, value) => {
assert.strictEqual(filterName, 'timestampRangeFilter');
assert.strictEqual(value, fakeTimeRange);
assert(spy.calledWithExactly(fakeTimeRange.start, fakeTimeRange.end));
done();
};
filter.time(fakeTimeRange);
});
});
describe('toProto', () => {
it('should return null when no filters are present', () => {
const filter = new Filter();
const filterProto = filter.toProto();
assert.strictEqual(filterProto, null);
});
it('should return a plain filter if there is only 1', () => {
filter.filters_ = [{}];
const filterProto = filter.toProto();
assert.strictEqual(filterProto, filter.filters_[0]);
});
it('should create a chain filter if there are multiple', () => {
filter.filters_ = [{}, {}];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const filterProto = filter.toProto() as any;
assert.strictEqual(filterProto!.chain.filters, filter.filters_);
});
});
describe('value', () => {
it('should set the value regex filter', done => {
const value = {
value: 'fake-value',
};
const fakeRegExValue = 'abcd';
const fakeConvertedValue = 'dcba';
const regSpy = sandbox
.stub(Filter, 'convertToRegExpString')
.returns(fakeRegExValue);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bytesSpy = ((FakeMutation as any).convertToBytes = sandbox.spy(
() => {
return fakeConvertedValue;
}
));
filter.set = (filterName, val) => {
assert.strictEqual(filterName, 'valueRegexFilter');
assert.strictEqual(fakeConvertedValue, val);
assert(regSpy.calledWithExactly(value.value));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert((bytesSpy as any).calledWithExactly(fakeRegExValue));
regSpy.restore();
done();
};
filter.value(value);
});
it('should accept the short-hand version of value', done => {
const value = 'fake-value';
const fakeRegExValue = 'abcd';
const fakeConvertedValue = 'dcba';
const regSpy = sandbox
.stub(Filter, 'convertToRegExpString')
.returns(fakeRegExValue);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bytesSpy = ((FakeMutation.convertToBytes as any) = sandbox.spy(
() => {
return fakeConvertedValue;
}
));
filter.set = (filterName, val) => {
assert.strictEqual(filterName, 'valueRegexFilter');
assert.strictEqual(fakeConvertedValue, val);
assert(regSpy.calledWithExactly(value));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert((bytesSpy as any).calledWithExactly(fakeRegExValue));
regSpy.restore();
done();
};
filter.value(value);
});
it('should accept the value range filter', done => {
const fakeRange = {
a: 'a',
b: 'b',
};
const value = {
start: 'a' as fr.BoundData,
end: 'b' as fr.BoundData,
};
const spy = sandbox.stub(Filter, 'createRange').returns(fakeRange);
filter.set = (filterName, val) => {
assert.strictEqual(filterName, 'valueRangeFilter');
assert.strictEqual(val, fakeRange);
assert(spy.calledWithExactly(value.start, value.end, 'Value'));
spy.restore();
done();
};
filter.value(value);
});
it('should apply the strip label transformer', done => {
const value = {
strip: true,
};
filter.set = (filterName, val) => {
assert.strictEqual(filterName, 'stripValueTransformer');
assert.strictEqual(val, value.strip);
done();
};
filter.value(value);
});
});
describe('FilterError', () => {
it('should set the correct message', () => {
const err = new FilterError('test');
assert.strictEqual(err.message, 'Unknown filter: test.');
});
it('should set the correct name', () => {
const err = new FilterError('test');
assert.strictEqual(err.name, 'FilterError');
});
});
});