Skip to content

Add isEqual and hash for aggregate classes #10261

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
Show file tree
Hide file tree
Changes from 9 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
67 changes: 67 additions & 0 deletions Firestore/Example/Tests/Integration/API/FIRCountTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ @interface FIRCountTests : FSTIntegrationTestCase

@implementation FIRCountTests

- (void)testEquals {
FIRCollectionReference* coll1 = [self collectionRefWithDocuments:@{}];
FIRCollectionReference* coll1Same = [[coll1 firestore] collectionWithPath:[coll1 path]];
FIRAggregateQuery* query1 = [coll1 count];
FIRAggregateQuery* query1Same = [coll1Same count];

FIRCollectionReference* sub = [[coll1 documentWithPath:@"bar"] collectionWithPath:@"baz"];
FIRAggregateQuery* query2 = [[[sub queryWhereField:@"a" isEqualTo:@1] queryLimitedTo:100] count];
FIRAggregateQuery* query2Same = [[[sub queryWhereField:@"a"
isEqualTo:@1] queryLimitedTo:100] count];
FIRAggregateQuery* query3 = [[[sub queryWhereField:@"b"
isEqualTo:@1] queryOrderedByField:@"c"] count];
FIRAggregateQuery* query3Same = [[[sub queryWhereField:@"b"
isEqualTo:@1] queryOrderedByField:@"c"] count];

XCTAssertEqualObjects(query1, query1Same);
XCTAssertEqualObjects(query2, query2Same);
XCTAssertEqualObjects(query3, query3Same);

XCTAssertEqual([query1 hash], [query1Same hash]);
XCTAssertEqual([query2 hash], [query2Same hash]);
XCTAssertEqual([query3 hash], [query3Same hash]);

XCTAssertFalse([query1 isEqual:nil]);
XCTAssertFalse([query1 isEqual:@"string"]);
XCTAssertFalse([query1 isEqual:query2]);
XCTAssertFalse([query2 isEqual:query3]);

XCTAssertNotEqual([query1 hash], [query2 hash]);
XCTAssertNotEqual([query2 hash], [query3 hash]);
}

- (void)testCanRunCountQuery {
// TODO(b/246758022): Remove this (and below) once COUNT is release for the backend.
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
Expand Down Expand Up @@ -77,6 +109,41 @@ - (void)testCanRunCountWithOrderBys {
XCTAssertEqual(snapshot.count, [NSNumber numberWithLong:3L]);
}

- (void)testSnapshotEquals {
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
return;
}

FIRCollectionReference* testCollection = [self collectionRefWithDocuments:@{
@"a" : @{@"k" : @"a"},
@"b" : @{@"k" : @"b"},
@"c" : @{@"k" : @"c"}
}];

FIRAggregateQuerySnapshot* snapshot1 =
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];
FIRAggregateQuerySnapshot* snapshot1Same =
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"b"] count]];

FIRAggregateQuerySnapshot* snapshot2 =
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];
[self writeDocumentRef:[testCollection documentWithPath:@"d"] data:@{@"k" : @"a"}];
FIRAggregateQuerySnapshot* snapshot2Different =
[self readSnapshotForAggregate:[[testCollection queryWhereField:@"k" isEqualTo:@"a"] count]];

XCTAssertEqualObjects(snapshot1, snapshot1Same);
XCTAssertEqual([snapshot1 hash], [snapshot1Same hash]);
XCTAssertEqualObjects([snapshot1 query], [[testCollection queryWhereField:@"k"
isEqualTo:@"b"] count]);

XCTAssertNotEqualObjects(snapshot1, nil);
XCTAssertNotEqualObjects(snapshot1, @"string");
XCTAssertNotEqualObjects(snapshot1, snapshot2);
XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
XCTAssertNotEqualObjects(snapshot2, snapshot2Different);
XCTAssertNotEqual([snapshot2 hash], [snapshot2Different hash]);
}

- (void)testTerminateDoesNotCrashWithFlyingCountQuery {
if (![FSTIntegrationTestCase isRunningAgainstEmulator]) {
return;
Expand Down
16 changes: 16 additions & 0 deletions Firestore/Source/API/FIRAggregateQuery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ - (instancetype _Nonnull)initWithQuery:(FIRQuery *)query {
return self;
}

#pragma mark - NSObject Methods

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

auto otherQuery = static_cast<FIRAggregateQuery *>(other);
return [_query isEqual:otherQuery->_query];
}

- (NSUInteger)hash {
return [_query hash];
}

#pragma mark - Public Methods

- (FIRQuery *)query {
return _query;
}
Expand Down
20 changes: 20 additions & 0 deletions Firestore/Source/API/FIRAggregateQuerySnapshot.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "FIRAggregateQuerySnapshot+Internal.h"

#import "FIRAggregateQuery.h"

@implementation FIRAggregateQuerySnapshot {
int64_t _result;
FIRAggregateQuery* _query;
Expand All @@ -29,6 +31,24 @@ - (instancetype _Nonnull)initWithCount:(int64_t)count Query:(FIRAggregateQuery*)
return self;
}

#pragma mark - NSObject Methods

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

auto otherSnap = static_cast<FIRAggregateQuerySnapshot*>(other);
return _result == otherSnap->_result && [_query isEqual:otherSnap->_query];
}

- (NSUInteger)hash {
NSUInteger result = [_query hash];
result = 31 * result + [[self count] hash];
return result;
}

#pragma mark - Public Methods

- (NSNumber*)count {
return [NSNumber numberWithLongLong:_result];
}
Expand Down