Skip to content

Commit c0ec21a

Browse files
authored
Add isEqual and hash for aggregate classes (#10261)
1 parent 1478d9c commit c0ec21a

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Firestore/Example/Tests/Integration/API/FIRCountTests.mm

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ @interface FIRCountTests : FSTIntegrationTestCase
2828

2929
@implementation FIRCountTests
3030

31+
- (void)testAggregateQueryEquals {
32+
FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
33+
FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
34+
FIRAggregateQuery* query1 = [coll1 count];
35+
FIRAggregateQuery* query1Same = [coll1Same count];
36+
37+
FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
38+
FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100] count];
39+
FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a"
40+
isEqualTo:@1] queryLimitedTo:100] count];
41+
FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b"
42+
isEqualTo:@1] queryOrderedByField:@"c"] count];
43+
FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
44+
isEqualTo:@1] queryOrderedByField:@"c"] count];
45+
46+
XCTAssertEqualObjects(query1, query1Same);
47+
XCTAssertEqualObjects(query2, query2Same);
48+
XCTAssertEqualObjects(query3, query3Same);
49+
50+
XCTAssertEqual([query1 hash], [query1Same hash]);
51+
XCTAssertEqual([query2 hash], [query2Same hash]);
52+
XCTAssertEqual([query3 hash], [query3Same hash]);
53+
54+
XCTAssertFalse([query1 isEqual:nil]);
55+
XCTAssertFalse([query1 isEqual:@"string"]);
56+
XCTAssertFalse([query1 isEqual:query2]);
57+
XCTAssertFalse([query2 isEqual:query3]);
58+
59+
XCTAssertNotEqual([query1 hash], [query2 hash]);
60+
XCTAssertNotEqual([query2 hash], [query3 hash]);
61+
}
62+
3163
- (void)testCanRunCountQuery {
3264
// TODO(b/246758022): Remove this (and below) once COUNT is release for the backend.
3365
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
@@ -77,6 +109,48 @@ - (void)testCanRunCountWithOrderBys {
77109
XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
78110
}
79111

112+
- (void)testSnapshotEquals {
113+
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
114+
return;
115+
}
116+
117+
FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
118+
@"a" : @{@"k" : @"a"},
119+
@"b" : @{@"k" : @"b"},
120+
@"c" : @{@"k" : @"c"}
121+
}];
122+
123+
FIRAggregateQuerySnapshot* snapshot1 =
124+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
125+
FIRAggregateQuerySnapshot* snapshot1Same =
126+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
127+
128+
FIRAggregateQuerySnapshot* snapshot2 =
129+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
130+
[self writeDocumentRef:[testCollection documentWithPath:@"d"] data:@{@"k" : @"a"}];
131+
FIRAggregateQuerySnapshot* snapshot2Different =
132+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
133+
134+
FIRAggregateQuerySnapshot* snapshot3 =
135+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
136+
FIRAggregateQuerySnapshot* snapshot3Different =
137+
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"c"] count]];
138+
139+
XCTAssertEqualObjects(snapshot1, snapshot1Same);
140+
XCTAssertEqual([snapshot1 hash], [snapshot1Same hash]);
141+
XCTAssertEqualObjects([snapshot1 query], [[testCollection queryWhereField:@"k"
142+
isEqualTo:@"b"] count]);
143+
144+
XCTAssertNotEqualObjects(snapshot1, nil);
145+
XCTAssertNotEqualObjects(snapshot1, @"string");
146+
XCTAssertNotEqualObjects(snapshot1, snapshot2);
147+
XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
148+
XCTAssertNotEqualObjects(snapshot2, snapshot2Different);
149+
XCTAssertNotEqual([snapshot2 hash], [snapshot2Different hash]);
150+
XCTAssertNotEqualObjects(snapshot3, snapshot3Different);
151+
XCTAssertNotEqual([snapshot3 hash], [snapshot3Different hash]);
152+
}
153+
80154
- (void)testTerminateDoesNotCrashWithFlyingCountQuery {
81155
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
82156
return;

Firestore/Source/API/FIRAggregateQuery.mm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ - (instancetype)initWithQuery:(FIRQuery *)query {
4141
return self;
4242
}
4343

44+
#pragma mark - NSObject Methods
45+
46+
- (BOOL)isEqual:(nullable id)other {
47+
if (other == self) return YES;
48+
if (![[other class] isEqual:[self class]]) return NO;
49+
50+
auto otherQuery = static_cast<FIRAggregateQuery *>(other);
51+
return [_query isEqual:otherQuery->_query];
52+
}
53+
54+
- (NSUInteger)hash {
55+
return [_query hash];
56+
}
57+
58+
#pragma mark - Public Methods
59+
4460
- (FIRQuery *)query {
4561
return _query;
4662
}

Firestore/Source/API/FIRAggregateQuerySnapshot.mm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import "FIRAggregateQuerySnapshot+Internal.h"
1818

19+
#import "FIRAggregateQuery.h"
20+
1921
NS_ASSUME_NONNULL_BEGIN
2022

2123
@implementation FIRAggregateQuerySnapshot {
@@ -31,6 +33,24 @@ - (instancetype)initWithCount:(int64_t)count query:(FIRAggregateQuery*)query {
3133
return self;
3234
}
3335

36+
#pragma mark - NSObject Methods
37+
38+
- (BOOL)isEqual:(nullable id)other {
39+
if (other == self) return YES;
40+
if (![[other class] isEqual:[self class]]) return NO;
41+
42+
auto otherSnap = static_cast<FIRAggregateQuerySnapshot*>(other);
43+
return _result == otherSnap->_result && [_query isEqual:otherSnap->_query];
44+
}
45+
46+
- (NSUInteger)hash {
47+
NSUInteger result = [_query hash];
48+
result = 31 * result + [[self count] hash];
49+
return result;
50+
}
51+
52+
#pragma mark - Public Methods
53+
3454
- (NSNumber*)count {
3555
return [NSNumber numberWithLongLong:_result];
3656
}

0 commit comments

Comments
 (0)