Skip to content

Commit

Permalink
fix(database): allow null values for equalTo, etc. (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant authored and davideast committed Feb 15, 2017
1 parent 334d89e commit 561e7b7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 10 deletions.
57 changes: 57 additions & 0 deletions src/database/firebase_list_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,63 @@ describe('FirebaseListFactory', () => {
});


it('should support null for equalTo queries', (done: any) => {

questions.$ref.ref.set({
val1,
val2: Object.assign({}, val2, { extra: true }),
val3: Object.assign({}, val3, { extra: true }),
})
.then(() => {

var query = FirebaseListFactory(questions.$ref.ref, {
query: {
orderByChild: "extra",
equalTo: null
}
});

take.call(query, 1).subscribe(
(list) => {
expect(list.length).toEqual(1);
expect(list[0].$key).toEqual("val1");
done();
},
done.fail
);
});
});


it('should support null for startAt/endAt queries', (done: any) => {

questions.$ref.ref.set({
val1,
val2: Object.assign({}, val2, { extra: true }),
val3: Object.assign({}, val3, { extra: true }),
})
.then(() => {

var query = FirebaseListFactory(questions.$ref.ref, {
query: {
orderByChild: "extra",
startAt: null,
endAt: null
}
});

take.call(query, 1).subscribe(
(list) => {
expect(list.length).toEqual(1);
expect(list[0].$key).toEqual("val1");
done();
},
done.fail
);
});
});


it('should call off on all events when disposed', (done: any) => {
const questionRef = firebase.database().ref().child('questions');
var firebaseSpy = spyOn(questionRef, 'off').and.callThrough();
Expand Down
49 changes: 49 additions & 0 deletions src/database/query_observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,55 @@ describe('query combinations', () => {
});


describe('null values', () => {

it('should build an equalTo() query with a null scalar value', (done: any) => {
scalarQueryTest({
orderByChild: 'height',
equalTo: null
}, done);
});

it('should build a startAt() query with a null scalar value', (done: any) => {
scalarQueryTest({
orderByChild: 'height',
startAt: null
}, done);
});

it('should build an endAt() query with a null scalar value', (done: any) => {
scalarQueryTest({
orderByChild: 'height',
endAt: null
}, done);
});

it('should build an equalTo() query with a null observable value', (done: any) => {
const query = {
orderByChild: 'height',
equalTo: new Subject()
};
observableQueryTest(query, { equalTo: null }, done);
});

it('should build a startAt() query with a null observable value', (done: any) => {
const query = {
orderByChild: 'height',
startAt: new Subject()
};
observableQueryTest(query, { startAt: null }, done);
});

it('should build an endAt() query with a null observable value', (done: any) => {
const query = {
orderByChild: 'height',
endAt: new Subject()
};
observableQueryTest(query, { endAt: null }, done);
});

});

describe('audited queries', () => {

it('should immediately emit if not audited', () => {
Expand Down
20 changes: 10 additions & 10 deletions src/database/query_observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
LimitToSelection,
Primitive
} from '../interfaces';
import { isNil } from '../utils';
import { hasKey, isNil } from '../utils';

export function observeQuery(query: Query, audit: boolean = true): Observable<ScalarQuery> {
if (isNil(query)) {
Expand Down Expand Up @@ -68,15 +68,15 @@ export function observeQuery(query: Query, audit: boolean = true): Observable<Sc
}
}

if (!isNil(startAt)) {
if (startAt !== undefined) {
serializedOrder.startAt = startAt;
}

if (!isNil(endAt)) {
if (endAt !== undefined) {
serializedOrder.endAt = endAt;
}

if (!isNil(equalTo)) {
if (equalTo !== undefined) {
serializedOrder.equalTo = equalTo;
}

Expand Down Expand Up @@ -127,41 +127,41 @@ export function getLimitToObservables(query: Query): Observable<LimitToSelection
export function getStartAtObservable(query: Query): Observable<Primitive> {
if (query.startAt instanceof Observable) {
return query.startAt;
} else if (typeof query.startAt !== 'undefined') {
} else if (hasKey(query, 'startAt')) {
return new Observable<Primitive>(subscriber => {
subscriber.next(query.startAt);
});
} else {
return new Observable<Primitive>(subscriber => {
subscriber.next(null);
subscriber.next(undefined);
});
}
}

export function getEndAtObservable(query: Query): Observable<Primitive> {
if (query.endAt instanceof Observable) {
return query.endAt;
} else if (typeof query.endAt !== 'undefined') {
} else if (hasKey(query, 'endAt')) {
return new Observable<Primitive>(subscriber => {
subscriber.next(query.endAt);
});
} else {
return new Observable<Primitive>(subscriber => {
subscriber.next(null);
subscriber.next(undefined);
});
}
}

export function getEqualToObservable(query: Query): Observable<Primitive> {
if (query.equalTo instanceof Observable) {
return query.equalTo;
} else if (typeof query.equalTo !== 'undefined') {
} else if (hasKey(query, 'equalTo')) {
return new Observable<Primitive>(subscriber => {
subscriber.next(query.equalTo);
});
} else {
return new Observable<Primitive>(subscriber => {
subscriber.next(null);
subscriber.next(undefined);
});
}
}
Expand Down

0 comments on commit 561e7b7

Please sign in to comment.