Skip to content

Add isEqual() and port DocumentReference array test. #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,40 @@ - (void)testExposesFirestoreOnQueries {
XCTAssertEqual(q.firestore, self.db);
}

- (void)testDocumentReferenceEquality {
FIRFirestore *firestore = self.db;
FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);

XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);

FIRFirestore *otherFirestore = [self firestore];
XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
}

- (void)testQueryReferenceEquality {
FIRFirestore *firestore = self.db;
FIRQuery *query =
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
isEqualTo:@42];
FIRQuery *query2 =
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
isEqualTo:@42];
XCTAssertEqualObjects(query, query2);

FIRQuery *query3 =
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
isEqualTo:@42];
XCTAssertNotEqualObjects(query, query3);

FIRFirestore *otherFirestore = [self firestore];
FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
queryWhereField:@"baz"
isEqualTo:@42];
XCTAssertNotEqualObjects(query, query4);
}

- (void)testCanTraverseCollectionsAndDocuments {
NSString *expected = @"a/b/c/d";
// doc path from root Firestore.
Expand Down
17 changes: 6 additions & 11 deletions Firestore/Example/Tests/Integration/API/FIRTypeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,13 @@ - (void)testCanReadAndWriteTimestampFields {
}

- (void)testCanReadAndWriteDocumentReferences {
// We can't use assertSuccessfulRoundtrip since FIRDocumentReference doesn't implement isEqual.
FIRDocumentReference *docRef = [self.db documentWithPath:@"rooms/eros"];
id data = @{ @"a" : @42, @"ref" : docRef };
[self writeDocumentRef:docRef data:data];

FIRDocumentSnapshot *readDoc = [self readDocumentForRef:docRef];
XCTAssertTrue(readDoc.exists);
FIRDocumentReference *docRef = [self documentRef];
[self assertSuccessfulRoundtrip:@{ @"a" : @42, @"ref" : docRef }];
}

XCTAssertEqualObjects(readDoc[@"a"], data[@"a"]);
FIRDocumentReference *readDocRef = readDoc[@"ref"];
XCTAssertTrue([readDocRef isKindOfClass:[FIRDocumentReference class]]);
XCTAssertEqualObjects(readDocRef.path, docRef.path);
- (void)testCanReadAndWriteDocumentReferencesInArrays {
FIRDocumentReference *docRef = [self documentRef];
[self assertSuccessfulRoundtrip:@{ @"a" : @42, @"refs" : @[ docRef ] }];
}

@end
26 changes: 26 additions & 0 deletions Firestore/Source/API/FIRDocumentReference.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ - (instancetype)initWithKey:(FSTDocumentKey *)key firestore:(FIRFirestore *)fire
return self;
}

#pragma mark - NSObject Methods

- (BOOL)isEqual:(nullable id)other {
if (other == self) return YES;
if (!other || ![[other class] isEqual:[self class]]) return NO;

return [self isEqualToReference:other];
}

- (BOOL)isEqualToReference:(nullable FIRDocumentReference *)reference {
if (self == reference) return YES;
if (reference == nil) return NO;
if (self.firestore != reference.firestore && ![self.firestore isEqual:reference.firestore])
return NO;
if (self.key != reference.key && ![self.key isEqualToKey:reference.key]) return NO;
return YES;
}

- (NSUInteger)hash {
NSUInteger hash = [self.firestore hash];
hash = hash * 31u + [self.key hash];
return hash;
}

#pragma mark - Public Methods

- (NSString *)documentID {
return [self.key.path lastSegment];
}
Expand Down
27 changes: 26 additions & 1 deletion Firestore/Source/API/FIRQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ + (instancetype)referenceWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)f

@implementation FIRQuery

#pragma mark - Public Methods
#pragma mark - Constructor Methods

- (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
if (self = [super init]) {
Expand All @@ -103,6 +103,31 @@ - (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firest
return self;
}

#pragma mark - NSObject Methods

- (BOOL)isEqual:(nullable id)other {
if (other == self) return YES;
if (!other || ![[other class] isEqual:[self class]]) return NO;

return [self isEqualToQuery:other];
}

- (BOOL)isEqualToQuery:(nullable FIRQuery *)query {
if (self == query) return YES;
if (query == nil) return NO;
if (self.firestore != query.firestore && ![self.firestore isEqual:query.firestore]) return NO;
if (self.query != query.query && ![self.query isEqual:query.query]) return NO;
return YES;
}

- (NSUInteger)hash {
NSUInteger hash = [self.firestore hash];
hash = hash * 31u + [self.query hash];
return hash;
}

#pragma mark - Public Methods

- (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
NSError *_Nullable error))completion {
FSTListenOptions *options = [[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:YES
Expand Down