Skip to content

Commit 532711b

Browse files
authored
fix: Change tests to use new filter and use gax warn for warning just once (#1185)
* trim operator In the legacy version of filter we trim the operator. We should do this here too. * In test cases, replace various filter calls Always pass entity filters into the filter function in tests to reflect new behavior. * accept string too A string is needed as an input parameter because we might trim it to get an operator. * Use .filter the old way Revert changes on the .filter method * Change docs to use new filter The new property filter should be used and this should be reflected in the doc comments. * Use new Property filter In entity.ts, use the new property filter constructor. * Revert changes to operator The changes cause compiler errors. Do not trim the operator for now. * Move test up and add change Add change to issue warning just once. Use google-gax so that the warning is issued just once. * Revert "Change docs to use new filter" This reverts commit c290481. * run linter * Add a test to ensure just one warning A test to make sure the warning is emitted just once might provide proper coverage.
1 parent 52adb5e commit 532711b

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/query.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {EntityFilter, isFilter, AllowedFilterValueType} from './filter';
2222
import {Transaction} from './transaction';
2323
import {CallOptions} from 'google-gax';
2424
import {RunQueryStreamOptions} from '../src/request';
25+
import * as gaxInstance from 'google-gax';
2526

2627
export type Operator =
2728
| '='
@@ -223,7 +224,8 @@ class Query {
223224
value?: AllowedFilterValueType<T>
224225
): Query {
225226
if (arguments.length > 1) {
226-
process.emitWarning(
227+
gaxInstance.warn(
228+
'filter',
227229
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
228230
);
229231
}

test/entity.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ describe('entity', () => {
19291929

19301930
const query = ds
19311931
.createQuery('Kind1')
1932-
.filter('name', 'John')
1932+
.filter(new PropertyFilter('name', '=', 'John'))
19331933
.start('start')
19341934
.end('end')
19351935
.groupBy(['name'])
@@ -1989,7 +1989,11 @@ describe('entity', () => {
19891989

19901990
const query = ds
19911991
.createQuery('Kind1')
1992-
.filter('__key__', 'IN', [new entity.Key({path: ['Kind1', 'key1']})]);
1992+
.filter(
1993+
new PropertyFilter('__key__', 'IN', [
1994+
new entity.Key({path: ['Kind1', 'key1']}),
1995+
])
1996+
);
19931997

19941998
assert.deepStrictEqual(
19951999
testEntity.queryToQueryProto(query),

test/query.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ describe('Query', () => {
206206
});
207207

208208
describe('filter', () => {
209+
it('should issue a warning when a Filter instance is not provided', done => {
210+
const onWarning = (warning: {message: unknown}) => {
211+
assert.strictEqual(
212+
warning.message,
213+
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
214+
);
215+
process.removeListener('warning', onWarning);
216+
done();
217+
};
218+
process.on('warning', onWarning);
219+
new Query(['kind1']).filter('name', 'Stephen');
220+
});
221+
it('should not issue a warning again when a Filter instance is not provided', done => {
222+
const onWarning = () => {
223+
assert.fail();
224+
};
225+
process.on('warning', onWarning);
226+
new Query(['kind1']).filter('name', 'Stephen');
227+
setImmediate(() => {
228+
process.removeListener('warning', onWarning);
229+
done();
230+
});
231+
});
209232
it('should support filtering', () => {
210233
const now = new Date();
211234
const query = new Query(['kind1']).filter('date', '<=', now);
@@ -293,18 +316,6 @@ describe('Query', () => {
293316
assert.strictEqual(filter.val, 'Stephen');
294317
});
295318
});
296-
it('should issue a warning when a Filter instance is not provided', done => {
297-
const onWarning = (warning: {message: unknown}) => {
298-
assert.strictEqual(
299-
warning.message,
300-
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
301-
);
302-
process.removeListener('warning', onWarning);
303-
done();
304-
};
305-
process.on('warning', onWarning);
306-
new Query(['kind1']).filter('name', 'Stephen');
307-
});
308319
describe('filter with Filter class', () => {
309320
it('should support filter with Filter', () => {
310321
const now = new Date();

0 commit comments

Comments
 (0)