Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Jan 2, 2024
1 parent 5737dda commit 6598a65
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
67 changes: 59 additions & 8 deletions Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,58 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly {
[self awaitExpectations];
}

//
- (void)Demo_addSnapshotListenerWithDefaultListenOptions {
// Document Reference
- (void)DocumentReferenceDemo_addSnapshotListenerWithDefaultListenOptions {
FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];

[doc addSnapshotListenerWithOptions:options
listener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertTrue(snapshot.exists);
}];
}

- (void)DocumentReferenceDemo_addSnapshotListenerWithMetadataChanges {
FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES];

[doc addSnapshotListenerWithOptions:optionsWithMetadata
listener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertTrue(snapshot.exists);
}];
}

- (void)DocumentReferenceDemo_addSnapshotListenerFromCache {
FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsFromCache = [options withSource:FIRListenSourceCache];

[doc addSnapshotListenerWithOptions:optionsFromCache
listener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertTrue(snapshot.exists);
}];
}

- (void)DocumentReferenceDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges {
FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES];
FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata =
[optionsWithMetadata withSource:FIRListenSourceCache];

[doc addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata
listener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertTrue(snapshot.exists);
}];
}

// Query
- (void)QueryDemo_addSnapshotListenerWithDefaultListenOptions {
FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"];
FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
Expand All @@ -714,38 +764,39 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions {
}];
}

void Demo_addSnapshotListenerWithMetadataChanges(FIRFirestore* db) {
- (void)QueryDemo_addSnapshotListenerWithMetadataChanges {
FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"];
FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES];

[query addSnapshotListenerWithOptions:options
[query addSnapshotListenerWithOptions:optionsWithMetadata
listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertEqual(snapshot.count, 0);
}];
}

void Demo_addSnapshotListenerFromCache(FIRFirestore* db) {
- (void)QueryDemo_addSnapshotListenerFromCache {
FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"];
FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsFromCache = [options withSource:FIRListenSourceCache];

[query addSnapshotListenerWithOptions:options
[query addSnapshotListenerWithOptions:optionsFromCache
listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
XCTAssertNil(error);
XCTAssertEqual(snapshot.count, 0);
}];
}

void Demo_addSnapshotListenerFromCache(FIRFirestore* db) {
- (void)QueryDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges {
FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"];
FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"];
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions];
FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES];
FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = [optionsWithMetadata withSource:FIRListenSourceCache];
FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata =
[optionsWithMetadata withSource:FIRListenSourceCache];

[query addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata
listener:^(FIRQuerySnapshot *snapshot, NSError *error) {
Expand Down
27 changes: 18 additions & 9 deletions Firestore/Source/API/FIRSnapshotListenOptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

@implementation FIRSnapshotListenOptions


// private method
- (instancetype)initPrivateWithSource:(FIRListenSource)source
includeMetadataChanges:(BOOL)includeMetadataChanges {
self = [super init];
includeMetadataChanges:(BOOL)includeMetadataChanges {
self = [self initWithDefaultOptions];
if (self) {
_source = source;
_includeMetadataChanges = includeMetadataChanges;
Expand All @@ -37,18 +37,27 @@ - (instancetype)initPrivateWithSource:(FIRListenSource)source
}

- (instancetype)initWithDefaultOptions {
return [self initPrivateWithSource:FIRListenSourceDefault includeMetadataChanges:NO];
self = [super init];
if (self) {
_source = FIRListenSourceDefault;
_includeMetadataChanges = NO;
}
return self;
}


- (FIRSnapshotListenOptions *)withIncludeMetadataChanges:(BOOL)includeMetadataChanges {
return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source
includeMetadataChanges:includeMetadataChanges];
FIRSnapshotListenOptions *newOptions =
[[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source
includeMetadataChanges:includeMetadataChanges];
return newOptions;
}

- (FIRSnapshotListenOptions *)withSource:(FIRListenSource)source {
return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:source
includeMetadataChanges:self.includeMetadataChanges];}
FIRSnapshotListenOptions *newOptions =
[[FIRSnapshotListenOptions alloc] initPrivateWithSource:source
includeMetadataChanges:self.includeMetadataChanges];
return newOptions;
}

@end

Expand Down

0 comments on commit 6598a65

Please sign in to comment.