Skip to content

Commit

Permalink
fix: Allow filtering null values (#1067)
Browse files Browse the repository at this point in the history
* fix: Allows nulls to be passed into value

A user expressed interest in filtering with a null value. This typescript parameter change will now allow users to do so.

fixes #958

* Add tests in the system test folder

The code in the system test folder reflects the user experience more closely so we want to provide test cases there that will break when compiling typescript if new changes in src are not provided.

* Assert statements in system tests

Add assert statements to check that the value in the filter created in the system test actually equals null.

* Modify tests to assert check for null

Slight rename in system tests. Modified tests so that they test to see that the filter has a null value.

* Prefer elvis operator in assert statements

This change replaces the a longer fragment with elvis operator to make assert statements more concise.

* eliminate the need for a variable

In two tests we eliminate variables and just have an assert statement so that we can have more concise code.
  • Loading branch information
danieljbruce committed Feb 17, 2023
1 parent 0c8bc83 commit b89fa21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,13 @@ class Query {
* const keyQuery = query.filter('__key__', key);
* ```
*/
filter(property: string, value: {}): Query;
filter(property: string, operator: Operator, value: {}): Query;
filter(property: string, operatorOrValue: Operator, value?: {}): Query {
filter(property: string, value: {} | null): Query;
filter(property: string, operator: Operator, value: {} | null): Query;
filter(
property: string,
operatorOrValue: Operator,
value?: {} | null
): Query {
let operator = operatorOrValue as Operator;
if (arguments.length === 2) {
value = operatorOrValue as {};
Expand Down
14 changes: 14 additions & 0 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,20 @@ describe('Datastore', () => {
assert.strictEqual(entities.length, characters.length);
});

it('should construct filters by null status', async () => {
assert.strictEqual(
datastore.createQuery('Character').filter('status', null).filters.pop()
?.val,
null
);
assert.strictEqual(
datastore
.createQuery('Character')
.filter('status', '=', null)
.filters.pop()?.val,
null
);
});
it('should filter by key', async () => {
const key = datastore.key(['Book', 'GoT', 'Character', 'Rickard']);
const q = datastore
Expand Down
10 changes: 10 additions & 0 deletions test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ describe('Query', () => {
assert.strictEqual(filter.op, '=');
assert.strictEqual(filter.val, 'Stephen');
});
it('should accept null as value', () => {
assert.strictEqual(
new Query(['kind1']).filter('status', null).filters.pop()?.val,
null
);
assert.strictEqual(
new Query(['kind1']).filter('status', '=', null).filters.pop()?.val,
null
);
});
});

describe('hasAncestor', () => {
Expand Down

0 comments on commit b89fa21

Please sign in to comment.