Skip to content

Commit 024788e

Browse files
authored
Add isEqual() and port DocumentReference array test. (#380)
* Ports the test for the fix I made for Android for DocumentReference objects in arrays (bug not present in iOS). * Implements isEqual on FIRQuery and FIRDocumentReference.
1 parent b561342 commit 024788e

File tree

4 files changed

+92
-12
lines changed

4 files changed

+92
-12
lines changed

Firestore/Example/Tests/Integration/API/FIRDatabaseTests.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,40 @@ - (void)testExposesFirestoreOnQueries {
680680
XCTAssertEqual(q.firestore, self.db);
681681
}
682682

683+
- (void)testDocumentReferenceEquality {
684+
FIRFirestore *firestore = self.db;
685+
FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
686+
XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
687+
XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
688+
689+
XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
690+
691+
FIRFirestore *otherFirestore = [self firestore];
692+
XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
693+
}
694+
695+
- (void)testQueryReferenceEquality {
696+
FIRFirestore *firestore = self.db;
697+
FIRQuery *query =
698+
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
699+
isEqualTo:@42];
700+
FIRQuery *query2 =
701+
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
702+
isEqualTo:@42];
703+
XCTAssertEqualObjects(query, query2);
704+
705+
FIRQuery *query3 =
706+
[[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
707+
isEqualTo:@42];
708+
XCTAssertNotEqualObjects(query, query3);
709+
710+
FIRFirestore *otherFirestore = [self firestore];
711+
FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
712+
queryWhereField:@"baz"
713+
isEqualTo:@42];
714+
XCTAssertNotEqualObjects(query, query4);
715+
}
716+
683717
- (void)testCanTraverseCollectionsAndDocuments {
684718
NSString *expected = @"a/b/c/d";
685719
// doc path from root Firestore.

Firestore/Example/Tests/Integration/API/FIRTypeTests.m

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,13 @@ - (void)testCanReadAndWriteTimestampFields {
6262
}
6363

6464
- (void)testCanReadAndWriteDocumentReferences {
65-
// We can't use assertSuccessfulRoundtrip since FIRDocumentReference doesn't implement isEqual.
66-
FIRDocumentReference *docRef = [self.db documentWithPath:@"rooms/eros"];
67-
id data = @{ @"a" : @42, @"ref" : docRef };
68-
[self writeDocumentRef:docRef data:data];
69-
70-
FIRDocumentSnapshot *readDoc = [self readDocumentForRef:docRef];
71-
XCTAssertTrue(readDoc.exists);
65+
FIRDocumentReference *docRef = [self documentRef];
66+
[self assertSuccessfulRoundtrip:@{ @"a" : @42, @"ref" : docRef }];
67+
}
7268

73-
XCTAssertEqualObjects(readDoc[@"a"], data[@"a"]);
74-
FIRDocumentReference *readDocRef = readDoc[@"ref"];
75-
XCTAssertTrue([readDocRef isKindOfClass:[FIRDocumentReference class]]);
76-
XCTAssertEqualObjects(readDocRef.path, docRef.path);
69+
- (void)testCanReadAndWriteDocumentReferencesInArrays {
70+
FIRDocumentReference *docRef = [self documentRef];
71+
[self assertSuccessfulRoundtrip:@{ @"a" : @42, @"refs" : @[ docRef ] }];
7772
}
7873

7974
@end

Firestore/Source/API/FIRDocumentReference.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ - (instancetype)initWithKey:(FSTDocumentKey *)key firestore:(FIRFirestore *)fire
110110
return self;
111111
}
112112

113+
#pragma mark - NSObject Methods
114+
115+
- (BOOL)isEqual:(nullable id)other {
116+
if (other == self) return YES;
117+
if (!other || ![[other class] isEqual:[self class]]) return NO;
118+
119+
return [self isEqualToReference:other];
120+
}
121+
122+
- (BOOL)isEqualToReference:(nullable FIRDocumentReference *)reference {
123+
if (self == reference) return YES;
124+
if (reference == nil) return NO;
125+
if (self.firestore != reference.firestore && ![self.firestore isEqual:reference.firestore])
126+
return NO;
127+
if (self.key != reference.key && ![self.key isEqualToKey:reference.key]) return NO;
128+
return YES;
129+
}
130+
131+
- (NSUInteger)hash {
132+
NSUInteger hash = [self.firestore hash];
133+
hash = hash * 31u + [self.key hash];
134+
return hash;
135+
}
136+
137+
#pragma mark - Public Methods
138+
113139
- (NSString *)documentID {
114140
return [self.key.path lastSegment];
115141
}

Firestore/Source/API/FIRQuery.m

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ + (instancetype)referenceWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)f
9393

9494
@implementation FIRQuery
9595

96-
#pragma mark - Public Methods
96+
#pragma mark - Constructor Methods
9797

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

106+
#pragma mark - NSObject Methods
107+
108+
- (BOOL)isEqual:(nullable id)other {
109+
if (other == self) return YES;
110+
if (!other || ![[other class] isEqual:[self class]]) return NO;
111+
112+
return [self isEqualToQuery:other];
113+
}
114+
115+
- (BOOL)isEqualToQuery:(nullable FIRQuery *)query {
116+
if (self == query) return YES;
117+
if (query == nil) return NO;
118+
if (self.firestore != query.firestore && ![self.firestore isEqual:query.firestore]) return NO;
119+
if (self.query != query.query && ![self.query isEqual:query.query]) return NO;
120+
return YES;
121+
}
122+
123+
- (NSUInteger)hash {
124+
NSUInteger hash = [self.firestore hash];
125+
hash = hash * 31u + [self.query hash];
126+
return hash;
127+
}
128+
129+
#pragma mark - Public Methods
130+
106131
- (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
107132
NSError *_Nullable error))completion {
108133
FSTListenOptions *options = [[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:YES

0 commit comments

Comments
 (0)