Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change tests to use new filter and use gax warn for warning just once #1185

Merged
4 changes: 3 additions & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {EntityFilter, isFilter, AllowedFilterValueType} from './filter';
import {Transaction} from './transaction';
import {CallOptions} from 'google-gax';
import {RunQueryStreamOptions} from '../src/request';
import * as gaxInstance from 'google-gax';

export type Operator =
| '='
Expand Down Expand Up @@ -223,7 +224,8 @@ class Query {
value?: AllowedFilterValueType<T>
): Query {
if (arguments.length > 1) {
process.emitWarning(
gaxInstance.warn(
'filter',
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
);
}
Expand Down
8 changes: 6 additions & 2 deletions test/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ describe('entity', () => {

const query = ds
.createQuery('Kind1')
.filter('name', 'John')
.filter(new PropertyFilter('name', '=', 'John'))
.start('start')
.end('end')
.groupBy(['name'])
Expand Down Expand Up @@ -1989,7 +1989,11 @@ describe('entity', () => {

const query = ds
.createQuery('Kind1')
.filter('__key__', 'IN', [new entity.Key({path: ['Kind1', 'key1']})]);
.filter(
new PropertyFilter('__key__', 'IN', [
new entity.Key({path: ['Kind1', 'key1']}),
])
);

assert.deepStrictEqual(
testEntity.queryToQueryProto(query),
Expand Down
35 changes: 23 additions & 12 deletions test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ describe('Query', () => {
});

describe('filter', () => {
it('should issue a warning when a Filter instance is not provided', done => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to make this validate that the warning only happens once, but since gax probably also tests that, it's probably not a big issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const onWarning = (warning: {message: unknown}) => {
assert.strictEqual(
warning.message,
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
);
process.removeListener('warning', onWarning);
done();
};
process.on('warning', onWarning);
new Query(['kind1']).filter('name', 'Stephen');
});
it('should not issue a warning again when a Filter instance is not provided', done => {
const onWarning = () => {
assert.fail();
};
process.on('warning', onWarning);
new Query(['kind1']).filter('name', 'Stephen');
setImmediate(() => {
process.removeListener('warning', onWarning);
done();
});
});
it('should support filtering', () => {
const now = new Date();
const query = new Query(['kind1']).filter('date', '<=', now);
Expand Down Expand Up @@ -293,18 +316,6 @@ describe('Query', () => {
assert.strictEqual(filter.val, 'Stephen');
});
});
it('should issue a warning when a Filter instance is not provided', done => {
const onWarning = (warning: {message: unknown}) => {
assert.strictEqual(
warning.message,
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter'
);
process.removeListener('warning', onWarning);
done();
};
process.on('warning', onWarning);
new Query(['kind1']).filter('name', 'Stephen');
});
describe('filter with Filter class', () => {
it('should support filter with Filter', () => {
const now = new Date();
Expand Down