@@ -420,6 +420,55 @@ - (void)testQueryCannotBeCreatedFromDocumentsMissingSortValues {
420
420
FSTAssertThrows ([query queryEndingAtDocument: snapshot], reason);
421
421
}
422
422
423
+ - (void )testQueriesCannotBeSortedByAnUncommittedServerTimestamp {
424
+ __weak FIRCollectionReference *collection = [self collectionRef ];
425
+ FIRFirestore *db = [self firestore ];
426
+
427
+ [db disableNetworkWithCompletion: [self completionForExpectationWithName: @" Disable network" ]];
428
+ [self awaitExpectations ];
429
+
430
+ XCTestExpectation *offlineCallbackDone =
431
+ [self expectationWithDescription: @" offline callback done" ];
432
+ XCTestExpectation *onlineCallbackDone = [self expectationWithDescription: @" online callback done" ];
433
+
434
+ [collection addSnapshotListener: ^(FIRQuerySnapshot *snapshot, NSError *error) {
435
+ XCTAssertNil (error);
436
+
437
+ // Skip the initial empty snapshot.
438
+ if (snapshot.empty ) return ;
439
+
440
+ XCTAssertEqual (snapshot.count , 1 );
441
+ FIRQueryDocumentSnapshot *docSnap = [snapshot documents ][0 ];
442
+
443
+ if ([snapshot metadata ].pendingWrites ) {
444
+ // Offline snapshot. Since the server timestamp is uncommitted, we
445
+ // shouldn't be able to query by it.
446
+ NSString *reason =
447
+ @" Invalid query. Your are trying to start or end a query using a document for which the "
448
+ @" field 'timestamp' is an uncommitted server timestamp. (Since the value of this field "
449
+ @" is unknown, you cannot start/end a query with it.)" ;
450
+ FSTAssertThrows ([[[collection queryOrderedByField: @" timestamp" ] queryEndingAtDocument: docSnap]
451
+ addSnapshotListener: ^(FIRQuerySnapshot *, NSError *){
452
+ }],
453
+ reason);
454
+ [offlineCallbackDone fulfill ];
455
+ } else {
456
+ // Online snapshot. Since the server timestamp is committed, we should be able to query by it.
457
+ [[[collection queryOrderedByField: @" timestamp" ] queryEndingAtDocument: docSnap]
458
+ addSnapshotListener: ^(FIRQuerySnapshot *, NSError *){
459
+ }];
460
+ [onlineCallbackDone fulfill ];
461
+ }
462
+ }];
463
+
464
+ FIRDocumentReference *document = [collection documentWithAutoID ];
465
+ [document setData: @{@" timestamp" : [FIRFieldValue fieldValueForServerTimestamp ]}];
466
+ [self awaitExpectations ];
467
+
468
+ [db enableNetworkWithCompletion: [self completionForExpectationWithName: @" Enable network" ]];
469
+ [self awaitExpectations ];
470
+ }
471
+
423
472
- (void )testQueryBoundMustNotHaveMoreComponentsThanSortOrders {
424
473
FIRCollectionReference *testCollection = [self collectionRef ];
425
474
FIRQuery *query = [testCollection queryOrderedByField: @" foo" ];
0 commit comments