Skip to content

Commit

Permalink
Update query unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tohhsinpei committed Oct 19, 2022
1 parent 015dcf2 commit 724336d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
62 changes: 52 additions & 10 deletions packages/database-compat/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,34 +375,76 @@ describe('Query Tests', () => {
expect(queryId(path)).to.equal('default');

expect(queryId(path.startAt('pri', 'name'))).to.equal(
'{"sn":"name","sp":"pri"}'
'{"sin":true,"sn":"name","sp":"pri"}'
);
expect(queryId(path.startAfter('pri', 'name'))).to.equal(
'{"sn":"name-","sp":"pri"}'
'{"sin":false,"sn":"name","sp":"pri"}'
);
expect(queryId(path.endAt('pri', 'name'))).to.equal(
'{"ein":true,"en":"name","ep":"pri"}'
);
expect(queryId(path.endBefore('pri', 'name'))).to.equal(
'{"ein":false,"en":"name","ep":"pri"}'
);

expect(queryId(path.startAt('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAt('spri').endBefore('epri'))).to.equal(
'{"ein":false,"ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sn":"[MAX_NAME]","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":false,"sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endBefore('epri'))).to.equal(
'{"ein":false,"ep":"epri","sin":false,"sp":"spri"}'
);

expect(
queryId(path.startAt('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAt('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname-","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);

expect(queryId(path.startAt('pri').limitToFirst(100))).to.equal(
'{"l":100,"sp":"pri","vf":"l"}'
'{"l":100,"sin":true,"sp":"pri","vf":"l"}'
);
expect(queryId(path.startAfter('pri').limitToFirst(100))).to.equal(
'{"l":100,"sn":"[MAX_NAME]","sp":"pri","vf":"l"}'
'{"l":100,"sin":false,"sp":"pri","vf":"l"}'
);
expect(queryId(path.endAt('pri').limitToLast(100))).to.equal(
'{"ein":true,"ep":"pri","l":100,"vf":"r"}'
);
expect(queryId(path.endBefore('pri').limitToLast(100))).to.equal(
'{"ein":false,"ep":"pri","l":100,"vf":"r"}'
);

expect(queryId(path.startAt('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sp":"bar"}'
'{"i":"foo","sin":true,"sp":"bar"}'
);
expect(queryId(path.startAfter('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sn":"[MAX_NAME]","sp":"bar"}'
'{"i":"foo","sin":false,"sp":"bar"}'
);
expect(queryId(path.endAt('bar').orderByChild('foo'))).to.equal(
'{"ein":true,"ep":"bar","i":"foo"}'
);
expect(queryId(path.endBefore('bar').orderByChild('foo'))).to.equal(
'{"ein":false,"ep":"bar","i":"foo"}'
);
});

Expand Down
35 changes: 4 additions & 31 deletions packages/database/src/core/view/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { KEY_INDEX } from '../snap/indexes/KeyIndex';
import { PathIndex } from '../snap/indexes/PathIndex';
import { PRIORITY_INDEX, PriorityIndex } from '../snap/indexes/PriorityIndex';
import { VALUE_INDEX } from '../snap/indexes/ValueIndex';
import { predecessor, successor } from '../util/NextPushId';
import { MAX_NAME, MIN_NAME } from '../util/util';

import { IndexedFilter } from './filter/IndexedFilter';
Expand Down Expand Up @@ -195,10 +194,12 @@ export class QueryParams {
copy.limitSet_ = this.limitSet_;
copy.limit_ = this.limit_;
copy.startSet_ = this.startSet_;
copy.startAfterSet_ = this.startAfterSet_;
copy.indexStartValue_ = this.indexStartValue_;
copy.startNameSet_ = this.startNameSet_;
copy.indexStartName_ = this.indexStartName_;
copy.endSet_ = this.endSet_;
copy.endBeforeSet_ = this.endBeforeSet_;
copy.indexEndValue_ = this.indexEndValue_;
copy.endNameSet_ = this.endNameSet_;
copy.indexEndName_ = this.indexEndName_;
Expand Down Expand Up @@ -277,21 +278,7 @@ export function queryParamsStartAfter(
indexValue: unknown,
key?: string | null
): QueryParams {
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = successor(indexValue as string);
}
params = queryParamsStartAt(queryParams, indexValue, key);
} else {
let childKey: string;
if (key == null) {
childKey = MAX_NAME;
} else {
childKey = successor(key);
}
params = queryParamsStartAt(queryParams, indexValue, childKey);
}
const params: QueryParams = queryParamsStartAt(queryParams, indexValue, key);
params.startAfterSet_ = true;
return params;
}
Expand Down Expand Up @@ -322,21 +309,7 @@ export function queryParamsEndBefore(
indexValue: unknown,
key?: string | null
): QueryParams {
let childKey: string;
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = predecessor(indexValue as string);
}
params = queryParamsEndAt(queryParams, indexValue, key);
} else {
if (key == null) {
childKey = MIN_NAME;
} else {
childKey = predecessor(key);
}
params = queryParamsEndAt(queryParams, indexValue, childKey);
}
const params: QueryParams = queryParamsEndAt(queryParams, indexValue, key);
params.endBeforeSet_ = true;
return params;
}
Expand Down

0 comments on commit 724336d

Please sign in to comment.