@@ -398,7 +398,10 @@ class LocalStoreTester {
398
398
return this ;
399
399
}
400
400
401
- toReturnChanged ( ...docs : Document [ ] ) : LocalStoreTester {
401
+ toReturnChangedInternal (
402
+ docs : Document [ ] ,
403
+ isEqual ?: ( lhs : Document | null , rhs : Document | null ) => boolean
404
+ ) : LocalStoreTester {
402
405
this . promiseChain = this . promiseChain . then ( ( ) => {
403
406
debugAssert (
404
407
this . lastChanges !== null ,
@@ -407,13 +410,29 @@ class LocalStoreTester {
407
410
expect ( this . lastChanges . size ) . to . equal ( docs . length , 'number of changes' ) ;
408
411
for ( const doc of docs ) {
409
412
const returned = this . lastChanges . get ( doc . key ) ;
410
- expectEqual ( doc , returned , `Expected '${ returned } ' to equal '${ doc } '.` ) ;
413
+ const message = `Expected '${ returned } ' to equal '${ doc } '.` ;
414
+ if ( isEqual ) {
415
+ expect ( isEqual ( doc , returned ) ) . to . equal ( true , message ) ;
416
+ } else {
417
+ expectEqual ( doc , returned , message ) ;
418
+ }
411
419
}
412
420
this . lastChanges = null ;
413
421
} ) ;
414
422
return this ;
415
423
}
416
424
425
+ toReturnChanged ( ...docs : Document [ ] ) : LocalStoreTester {
426
+ return this . toReturnChangedInternal ( docs ) ;
427
+ }
428
+
429
+ toReturnChangedWithDocComparator (
430
+ isEqual : ( lhs : Document | null , rhs : Document | null ) => boolean ,
431
+ ...docs : Document [ ]
432
+ ) : LocalStoreTester {
433
+ return this . toReturnChangedInternal ( docs , isEqual ) ;
434
+ }
435
+
417
436
toReturnRemoved ( ...keyStrings : string [ ] ) : LocalStoreTester {
418
437
this . promiseChain = this . promiseChain . then ( ( ) => {
419
438
debugAssert (
@@ -433,16 +452,20 @@ class LocalStoreTester {
433
452
return this ;
434
453
}
435
454
436
- toContain ( doc : Document ) : LocalStoreTester {
455
+ toContain (
456
+ doc : Document ,
457
+ isEqual ?: ( lhs : Document , rhs : Document ) => boolean
458
+ ) : LocalStoreTester {
437
459
this . promiseChain = this . promiseChain . then ( ( ) => {
438
460
return localStoreReadDocument ( this . localStore , doc . key ) . then ( result => {
439
- expectEqual (
440
- result ,
441
- doc ,
442
- `Expected ${
443
- result ? result . toString ( ) : null
444
- } to match ${ doc . toString ( ) } .`
445
- ) ;
461
+ const message = `Expected ${
462
+ result ? result . toString ( ) : null
463
+ } to match ${ doc . toString ( ) } .`;
464
+ if ( isEqual ) {
465
+ expect ( isEqual ( result , doc ) ) . to . equal ( true , message ) ;
466
+ } else {
467
+ expectEqual ( result , doc , message ) ;
468
+ }
446
469
} ) ;
447
470
} ) ;
448
471
return this ;
@@ -539,22 +562,38 @@ class LocalStoreTester {
539
562
}
540
563
}
541
564
542
- describe ( 'LocalStore w/ Memory Persistence' , ( ) => {
543
- async function initialize ( ) : Promise < LocalStoreComponents > {
544
- const queryEngine = new CountingQueryEngine ( ) ;
545
- const persistence = await persistenceHelpers . testMemoryEagerPersistence ( ) ;
546
- const localStore = newLocalStore (
547
- persistence ,
548
- queryEngine ,
549
- User . UNAUTHENTICATED ,
550
- JSON_SERIALIZER
551
- ) ;
552
- return { queryEngine, persistence, localStore } ;
553
- }
565
+ // The `isEqual` method for the Document class does not compare createTime and
566
+ // readTime. For some tests, we'd like to verify that a certain createTime has
567
+ // been calculated for documents. In such cases we can use this comparator.
568
+ function compareDocsWithCreateTime (
569
+ lhs : Document | null ,
570
+ rhs : Document | null
571
+ ) : boolean {
572
+ return (
573
+ ( lhs === null && rhs === null ) ||
574
+ ( lhs !== null &&
575
+ rhs !== null &&
576
+ lhs . isEqual ( rhs ) &&
577
+ lhs . createTime . isEqual ( rhs . createTime ) )
578
+ ) ;
579
+ }
554
580
555
- addEqualityMatcher ( ) ;
556
- genericLocalStoreTests ( initialize , /* gcIsEager= */ true ) ;
557
- } ) ;
581
+ // describe('LocalStore w/ Memory Persistence', () => {
582
+ // async function initialize(): Promise<LocalStoreComponents> {
583
+ // const queryEngine = new CountingQueryEngine();
584
+ // const persistence = await persistenceHelpers.testMemoryEagerPersistence();
585
+ // const localStore = newLocalStore(
586
+ // persistence,
587
+ // queryEngine,
588
+ // User.UNAUTHENTICATED,
589
+ // JSON_SERIALIZER
590
+ // );
591
+ // return { queryEngine, persistence, localStore };
592
+ // }
593
+ //
594
+ // addEqualityMatcher();
595
+ // genericLocalStoreTests(initialize, /* gcIsEager= */ true);
596
+ // });
558
597
559
598
describe ( 'LocalStore w/ IndexedDB Persistence' , ( ) => {
560
599
if ( ! IndexedDbPersistence . isAvailable ( ) ) {
@@ -1964,28 +2003,58 @@ function genericLocalStoreTests(
1964
2003
. afterAllocatingQuery ( query ( 'col' ) )
1965
2004
. toReturnTargetId ( 2 )
1966
2005
. after ( docAddedRemoteEvent ( doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) , [ 2 ] ) )
1967
- . toReturnChanged ( doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) )
1968
- . toContain ( doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) )
2006
+ . toReturnChangedWithDocComparator (
2007
+ compareDocsWithCreateTime ,
2008
+ doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 )
2009
+ )
2010
+ . toContain (
2011
+ doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) ,
2012
+ compareDocsWithCreateTime
2013
+ )
1969
2014
. after ( setMutation ( 'col/doc1' , { foo : 'newBar' } ) )
1970
- . toReturnChanged (
2015
+ . toReturnChangedWithDocComparator (
2016
+ compareDocsWithCreateTime ,
1971
2017
doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 5 ) . setHasLocalMutations ( )
1972
2018
)
1973
2019
. toContain (
1974
- doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 5 ) . setHasLocalMutations ( )
2020
+ doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 5 ) . setHasLocalMutations ( ) ,
2021
+ compareDocsWithCreateTime
1975
2022
)
1976
2023
. afterAcknowledgingMutation ( { documentVersion : 13 } )
1977
2024
// We haven't seen the remote event yet
1978
- . toReturnChanged (
2025
+ . toReturnChangedWithDocComparator (
2026
+ compareDocsWithCreateTime ,
1979
2027
doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 5 ) . setHasCommittedMutations ( )
1980
2028
)
1981
2029
. toContain (
1982
- doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 5 ) . setHasCommittedMutations ( )
2030
+ doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 5 ) . setHasCommittedMutations ( ) ,
2031
+ compareDocsWithCreateTime
1983
2032
)
1984
2033
. finish ( )
1985
2034
) ;
1986
2035
} ) ;
1987
2036
1988
- it ( 'saves updateTime as createTime when creating new doc' , ( ) => {
2037
+ it ( 'saves updateTime as createTime when receives ack for creating a new doc' , ( ) => {
2038
+ if ( gcIsEager ) {
2039
+ return ;
2040
+ }
2041
+
2042
+ return expectLocalStore ( )
2043
+ . after ( setMutation ( 'col/doc1' , { foo : 'newBar' } ) )
2044
+ . afterAcknowledgingMutation ( { documentVersion : 13 } )
2045
+ . afterExecutingQuery ( query ( 'col' ) )
2046
+ . toReturnChangedWithDocComparator (
2047
+ compareDocsWithCreateTime ,
2048
+ doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( )
2049
+ )
2050
+ . toContain (
2051
+ doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( ) ,
2052
+ compareDocsWithCreateTime
2053
+ )
2054
+ . finish ( ) ;
2055
+ } ) ;
2056
+
2057
+ it ( 'saves updateTime as createTime when recreating a deleted doc' , async ( ) => {
1989
2058
if ( gcIsEager ) {
1990
2059
return ;
1991
2060
}
@@ -1998,28 +2067,29 @@ function genericLocalStoreTests(
1998
2067
. toReturnChanged ( deletedDoc ( 'col/doc1' , 12 ) )
1999
2068
. toContain ( deletedDoc ( 'col/doc1' , 12 ) )
2000
2069
. after ( setMutation ( 'col/doc1' , { foo : 'newBar' } ) )
2001
- . toReturnChanged (
2070
+ . toReturnChangedWithDocComparator (
2071
+ compareDocsWithCreateTime ,
2002
2072
doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 12 ) . setHasLocalMutations ( )
2003
2073
)
2004
- // TODO(COUNT): Below has createTime=0 due to an optimization that uses invalid doc as base doc for
2005
- // set mutations. This is OK because it has no impact on aggregation's heuristic logic. But it feels
2006
- // "wrong" to have createTime 0 here. We should revisit this.
2007
2074
. toContain (
2008
- doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 0 ) . setHasLocalMutations ( )
2075
+ doc ( 'col/doc1' , 12 , { foo : 'newBar' } , 12 ) . setHasLocalMutations ( ) ,
2076
+ compareDocsWithCreateTime
2009
2077
)
2010
2078
. afterAcknowledgingMutation ( { documentVersion : 13 } )
2011
2079
// We haven't seen the remote event yet
2012
- . toReturnChanged (
2080
+ . toReturnChangedWithDocComparator (
2081
+ compareDocsWithCreateTime ,
2013
2082
doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( )
2014
2083
)
2015
2084
. toContain (
2016
- doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( )
2085
+ doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( ) ,
2086
+ compareDocsWithCreateTime
2017
2087
)
2018
2088
. finish ( )
2019
2089
) ;
2020
2090
} ) ;
2021
2091
2022
- it ( 'saves updateTime as createTime when creating new doc ' , ( ) => {
2092
+ it ( 'document createTime is preserved through Set -> Ack -> Patch -> Ack ' , ( ) => {
2023
2093
if ( gcIsEager ) {
2024
2094
return ;
2025
2095
}
@@ -2028,11 +2098,98 @@ function genericLocalStoreTests(
2028
2098
. after ( setMutation ( 'col/doc1' , { foo : 'newBar' } ) )
2029
2099
. afterAcknowledgingMutation ( { documentVersion : 13 } )
2030
2100
. afterExecutingQuery ( query ( 'col' ) )
2031
- . toReturnChanged (
2101
+ . toReturnChangedWithDocComparator (
2102
+ compareDocsWithCreateTime ,
2032
2103
doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( )
2033
2104
)
2034
2105
. toContain (
2035
- doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( )
2106
+ doc ( 'col/doc1' , 13 , { foo : 'newBar' } , 13 ) . setHasCommittedMutations ( ) ,
2107
+ compareDocsWithCreateTime
2108
+ )
2109
+ . afterMutations ( [ patchMutation ( 'col/doc1' , { 'likes' : 1 } ) ] )
2110
+ . toReturnChangedWithDocComparator (
2111
+ compareDocsWithCreateTime ,
2112
+ doc (
2113
+ 'col/doc1' ,
2114
+ 13 ,
2115
+ { foo : 'newBar' , likes : 1 } ,
2116
+ 13
2117
+ ) . setHasLocalMutations ( )
2118
+ )
2119
+ . toContain (
2120
+ doc (
2121
+ 'col/doc1' ,
2122
+ 13 ,
2123
+ { foo : 'newBar' , likes : 1 } ,
2124
+ 13
2125
+ ) . setHasLocalMutations ( ) ,
2126
+ compareDocsWithCreateTime
2127
+ )
2128
+ . afterAcknowledgingMutation ( { documentVersion : 14 } )
2129
+ . toReturnChangedWithDocComparator (
2130
+ compareDocsWithCreateTime ,
2131
+ doc (
2132
+ 'col/doc1' ,
2133
+ 14 ,
2134
+ { foo : 'newBar' , likes : 1 } ,
2135
+ 13
2136
+ ) . setHasCommittedMutations ( )
2137
+ )
2138
+ . toContain (
2139
+ doc (
2140
+ 'col/doc1' ,
2141
+ 14 ,
2142
+ { foo : 'newBar' , likes : 1 } ,
2143
+ 13
2144
+ ) . setHasCommittedMutations ( ) ,
2145
+ compareDocsWithCreateTime
2146
+ )
2147
+ . finish ( ) ;
2148
+ } ) ;
2149
+
2150
+ it ( 'document createTime is preserved through Doc Added -> Patch -> Ack' , ( ) => {
2151
+ if ( gcIsEager ) {
2152
+ return ;
2153
+ }
2154
+ return expectLocalStore ( )
2155
+ . afterAllocatingQuery ( query ( 'col' ) )
2156
+ . toReturnTargetId ( 2 )
2157
+ . after ( docAddedRemoteEvent ( doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) , [ 2 ] ) )
2158
+ . toReturnChangedWithDocComparator (
2159
+ compareDocsWithCreateTime ,
2160
+ doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 )
2161
+ )
2162
+ . toContain (
2163
+ doc ( 'col/doc1' , 12 , { foo : 'bar' } , 5 ) ,
2164
+ compareDocsWithCreateTime
2165
+ )
2166
+ . afterMutations ( [ patchMutation ( 'col/doc1' , { 'likes' : 1 } ) ] )
2167
+ . toReturnChangedWithDocComparator (
2168
+ compareDocsWithCreateTime ,
2169
+ doc ( 'col/doc1' , 13 , { foo : 'bar' , likes : 1 } , 5 ) . setHasLocalMutations ( )
2170
+ )
2171
+ . toContain (
2172
+ doc ( 'col/doc1' , 13 , { foo : 'bar' , likes : 1 } , 5 ) . setHasLocalMutations ( ) ,
2173
+ compareDocsWithCreateTime
2174
+ )
2175
+ . afterAcknowledgingMutation ( { documentVersion : 14 } )
2176
+ . toReturnChangedWithDocComparator (
2177
+ compareDocsWithCreateTime ,
2178
+ doc (
2179
+ 'col/doc1' ,
2180
+ 14 ,
2181
+ { foo : 'bar' , likes : 1 } ,
2182
+ 5
2183
+ ) . setHasCommittedMutations ( )
2184
+ )
2185
+ . toContain (
2186
+ doc (
2187
+ 'col/doc1' ,
2188
+ 14 ,
2189
+ { foo : 'bar' , likes : 1 } ,
2190
+ 5
2191
+ ) . setHasCommittedMutations ( ) ,
2192
+ compareDocsWithCreateTime
2036
2193
)
2037
2194
. finish ( ) ;
2038
2195
} ) ;
0 commit comments