Skip to content

Commit 555314e

Browse files
committed
add index manager test
1 parent e0e4e94 commit 555314e

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

packages/firestore/test/unit/local/index_manager.test.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,34 @@ import {
3030
queryWithLimit,
3131
queryWithStartAt
3232
} from '../../../src/core/query';
33+
import { Timestamp } from '../../../src/lite-api/timestamp';
3334
import {
3435
displayNameForIndexType,
3536
IndexType
3637
} from '../../../src/local/index_manager';
3738
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
3839
import { Persistence } from '../../../src/local/persistence';
3940
import { documentMap } from '../../../src/model/collections';
40-
import { Document } from '../../../src/model/document';
41+
import { Document, MutableDocument } from '../../../src/model/document';
4142
import {
4243
IndexKind,
4344
IndexOffset,
4445
IndexState
4546
} from '../../../src/model/field_index';
4647
import { JsonObject } from '../../../src/model/object_value';
4748
import { canonicalId } from '../../../src/model/values';
49+
import {
50+
ApiClientObjectMap as ProtoObjectMap,
51+
Document as ProtoDocument,
52+
Value as ProtoValue
53+
} from '../../../src/protos/firestore_proto_api';
54+
import {
55+
fromDocument,
56+
JsonProtoSerializer,
57+
toName,
58+
toTimestamp,
59+
toVersion
60+
} from '../../../src/remote/serializer';
4861
import { addEqualityMatcher } from '../../util/equality_matcher';
4962
import {
5063
bound,
@@ -95,6 +108,11 @@ describe('IndexedDbIndexManager', async () => {
95108
}
96109

97110
let persistencePromise: Promise<Persistence>;
111+
const serializer = new JsonProtoSerializer(
112+
persistenceHelpers.TEST_DATABASE_ID,
113+
/* useProto3Json= */ true
114+
);
115+
98116
beforeEach(async () => {
99117
persistencePromise = persistenceHelpers.testIndexedDbPersistence();
100118
});
@@ -1026,6 +1044,62 @@ describe('IndexedDbIndexManager', async () => {
10261044
await verifyResults(testingQuery, 'coll/val3');
10271045
});
10281046

1047+
it('can index timestamp fields of different format', async () => {
1048+
await indexManager.addFieldIndex(
1049+
fieldIndex('coll', { fields: [['date', IndexKind.ASCENDING]] })
1050+
);
1051+
1052+
await addDocFromProto('coll/val1', {
1053+
'date': { timestampValue: '2016-01-02T10:20:50Z' }
1054+
});
1055+
await addDocFromProto('coll/val2', {
1056+
'date': { timestampValue: '2016-01-02T10:20:50.000000000Z' }
1057+
});
1058+
await addDocFromProto('coll/val3', {
1059+
'date': { timestampValue: '2016-01-02T10:20:50.850Z' }
1060+
});
1061+
await addDocFromProto('coll/val4', {
1062+
'date': { timestampValue: '2016-01-02T10:20:50.850000000Z' }
1063+
});
1064+
await addDocFromProto('coll/val5', {
1065+
'date': { timestampValue: { seconds: 1451730050, nanos: 999999999 } }
1066+
});
1067+
await addDocFromProto('coll/val6', {
1068+
'date': {
1069+
timestampValue: toTimestamp(serializer, new Timestamp(1451730050, 1))
1070+
}
1071+
});
1072+
1073+
let q = queryWithAddedOrderBy(query('coll'), orderBy('date'));
1074+
await verifyResults(
1075+
q,
1076+
'coll/val1',
1077+
'coll/val2',
1078+
'coll/val6',
1079+
'coll/val3',
1080+
'coll/val4',
1081+
'coll/val5'
1082+
);
1083+
1084+
q = queryWithAddedFilter(
1085+
query('coll'),
1086+
filter('date', '==', new Timestamp(1451730050, 850000000))
1087+
);
1088+
await verifyResults(q, 'coll/val3', 'coll/val4');
1089+
1090+
q = queryWithAddedFilter(
1091+
query('coll'),
1092+
filter('date', '>=', new Timestamp(1451730050, 850000000))
1093+
);
1094+
await verifyResults(q, 'coll/val3', 'coll/val4', 'coll/val5');
1095+
1096+
q = queryWithAddedFilter(
1097+
query('coll'),
1098+
filter('date', '>', new Timestamp(1451730050, 0))
1099+
);
1100+
await verifyResults(q, 'coll/val6', 'coll/val3', 'coll/val4', 'coll/val5');
1101+
});
1102+
10291103
it('support advances queries', async () => {
10301104
// This test compares local query results with those received from the Java
10311105
// Server SDK.
@@ -1828,6 +1902,31 @@ describe('IndexedDbIndexManager', async () => {
18281902
return addDocs(doc(key, 1, data));
18291903
}
18301904

1905+
function addDocFromProto(
1906+
key: string,
1907+
data: ProtoObjectMap<ProtoValue> | undefined
1908+
): Promise<void> {
1909+
return addDocs(docFromProto(key, 1, data));
1910+
}
1911+
1912+
function docFromProto(
1913+
keyStr: string,
1914+
versionNumber: number,
1915+
data: ProtoObjectMap<ProtoValue> | undefined
1916+
): MutableDocument {
1917+
const proto: ProtoDocument = {
1918+
name: toName(serializer, key(keyStr)),
1919+
fields: data,
1920+
updateTime: toVersion(serializer, version(versionNumber)),
1921+
createTime: toVersion(serializer, version(versionNumber))
1922+
};
1923+
return fromDocument(
1924+
serializer,
1925+
proto,
1926+
/* hasCommittedMutations= */ undefined
1927+
);
1928+
}
1929+
18311930
async function verifyResults(query: Query, ...keys: string[]): Promise<void> {
18321931
const target = queryToTarget(query);
18331932
const actualResults = await indexManager.getDocumentsMatchingTarget(target);

0 commit comments

Comments
 (0)