@@ -30,21 +30,34 @@ import {
30
30
queryWithLimit ,
31
31
queryWithStartAt
32
32
} from '../../../src/core/query' ;
33
+ import { Timestamp } from '../../../src/lite-api/timestamp' ;
33
34
import {
34
35
displayNameForIndexType ,
35
36
IndexType
36
37
} from '../../../src/local/index_manager' ;
37
38
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence' ;
38
39
import { Persistence } from '../../../src/local/persistence' ;
39
40
import { documentMap } from '../../../src/model/collections' ;
40
- import { Document } from '../../../src/model/document' ;
41
+ import { Document , MutableDocument } from '../../../src/model/document' ;
41
42
import {
42
43
IndexKind ,
43
44
IndexOffset ,
44
45
IndexState
45
46
} from '../../../src/model/field_index' ;
46
47
import { JsonObject } from '../../../src/model/object_value' ;
47
48
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' ;
48
61
import { addEqualityMatcher } from '../../util/equality_matcher' ;
49
62
import {
50
63
bound ,
@@ -95,6 +108,11 @@ describe('IndexedDbIndexManager', async () => {
95
108
}
96
109
97
110
let persistencePromise : Promise < Persistence > ;
111
+ const serializer = new JsonProtoSerializer (
112
+ persistenceHelpers . TEST_DATABASE_ID ,
113
+ /* useProto3Json= */ true
114
+ ) ;
115
+
98
116
beforeEach ( async ( ) => {
99
117
persistencePromise = persistenceHelpers . testIndexedDbPersistence ( ) ;
100
118
} ) ;
@@ -1026,6 +1044,62 @@ describe('IndexedDbIndexManager', async () => {
1026
1044
await verifyResults ( testingQuery , 'coll/val3' ) ;
1027
1045
} ) ;
1028
1046
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
+
1029
1103
it ( 'support advances queries' , async ( ) => {
1030
1104
// This test compares local query results with those received from the Java
1031
1105
// Server SDK.
@@ -1828,6 +1902,31 @@ describe('IndexedDbIndexManager', async () => {
1828
1902
return addDocs ( doc ( key , 1 , data ) ) ;
1829
1903
}
1830
1904
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
+
1831
1930
async function verifyResults ( query : Query , ...keys : string [ ] ) : Promise < void > {
1832
1931
const target = queryToTarget ( query ) ;
1833
1932
const actualResults = await indexManager . getDocumentsMatchingTarget ( target ) ;
0 commit comments