diff options
author | zxu <zxu@google.com> | 2017-12-19 13:47:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-19 13:47:04 -0500 |
commit | 9ddf3637ac984abfe16d4ebef6ff3947b4e946c1 (patch) | |
tree | 008d6eea9adc10675c79c7152703f4fd05af4cf9 /Firestore/Example/Tests/API | |
parent | 52cdf0b25a12b5baed8546893634dab9c1a03e56 (diff) |
Firestore `isEqual()` on Public Types (#569)
* add bone code for new isEqual and unit test for old isEqual
* add bone code for `FIRCollectionReference`, `FIRDocumentSnapshot`, `FIRFieldValue`, `FIRQuerySnapshot`, `FIRSnapshotMetadata`;
* change inconsistenciness of `FIRFieldPath.isEqual` implementation;
* add unit test (and file) for `FIRDocumentReferenceTest.m`, `FIRFieldPathTest.m`, `FIRQueryTests.testEquals`; `FIRGeoPoint` already has test and Blob is internal type.
* Implement isEqual for FIRCollectionReference
adding the working code and unit test.
* implement isEqual for FIRSnapshotMetadata
* Implement isEqual for FIRDocumentSnapshot
* Implement isEqual for FIRQuerySnapshot
* (un)implement `isEqual` for `FIRFieldValue`
Since `FIRFieldValue` both types are singleton, we do not need override `isEqual`. Add test to test the default `NSObject` `isEqual` works just fine.
* fix style with `scripts/style.sh`
* Implement hash for those with overridden isEqual without hash yet.
* refactor to use test helper functions -- FSTTestFirestore, FSTTestPath, FSTTestDocKey
* refactor using test helper `FSTTestDocSnapshot`, `FSTTestFieldPath`, `FSTTestQuery`, `FSTTestDoc`
* refactoring to use test helper method `FSTTestQuerySnapshot`,
* remove unneccessary nil-check, check isKindOfClass instead of isEqual
* refactoring: adding `FSTAPIHelpers.{h,m}`, `FSTTest{Collection,Document}Ref`, better naming and style fix
* a file forgot in last commit
* mainly clean up import and some minor refactoring
* fix style via style.sh
* minor style fix
* add pragma ignored -Wnonnull
Diffstat (limited to 'Firestore/Example/Tests/API')
-rw-r--r-- | Firestore/Example/Tests/API/FIRCollectionReferenceTests.m | 43 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRDocumentReferenceTests.m | 43 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRDocumentSnapshotTests.m | 62 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRFieldPathTests.m | 45 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRFieldValueTests.m | 48 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRGeoPointTests.m | 21 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRQuerySnapshotTests.m | 57 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRQueryTests.m | 42 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FIRSnapshotMetadataTests.m | 51 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FSTAPIHelpers.h | 73 | ||||
-rw-r--r-- | Firestore/Example/Tests/API/FSTAPIHelpers.m | 113 |
11 files changed, 570 insertions, 28 deletions
diff --git a/Firestore/Example/Tests/API/FIRCollectionReferenceTests.m b/Firestore/Example/Tests/API/FIRCollectionReferenceTests.m new file mode 100644 index 0000000..73ae38d --- /dev/null +++ b/Firestore/Example/Tests/API/FIRCollectionReferenceTests.m @@ -0,0 +1,43 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRCollectionReference.h" + +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRCollectionReferenceTests : XCTestCase +@end + +@implementation FIRCollectionReferenceTests + +- (void)testEquals { + FIRCollectionReference *referenceFoo = FSTTestCollectionRef(@"foo"); + FIRCollectionReference *referenceFooDup = FSTTestCollectionRef(@"foo"); + FIRCollectionReference *referenceBar = FSTTestCollectionRef(@"bar"); + XCTAssertEqualObjects(referenceFoo, referenceFooDup); + XCTAssertNotEqualObjects(referenceFoo, referenceBar); + + XCTAssertEqual([referenceFoo hash], [referenceFooDup hash]); + XCTAssertNotEqual([referenceFoo hash], [referenceBar hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRDocumentReferenceTests.m b/Firestore/Example/Tests/API/FIRDocumentReferenceTests.m new file mode 100644 index 0000000..4e301d0 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRDocumentReferenceTests.m @@ -0,0 +1,43 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRDocumentReference.h" + +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRDocumentReferenceTests : XCTestCase +@end + +@implementation FIRDocumentReferenceTests + +- (void)testEquals { + FIRDocumentReference *referenceFoo = FSTTestDocRef(@"rooms/foo"); + FIRDocumentReference *referenceFooDup = FSTTestDocRef(@"rooms/foo"); + FIRDocumentReference *referenceBar = FSTTestDocRef(@"rooms/bar"); + XCTAssertEqualObjects(referenceFoo, referenceFooDup); + XCTAssertNotEqualObjects(referenceFoo, referenceBar); + + XCTAssertEqual([referenceFoo hash], [referenceFooDup hash]); + XCTAssertNotEqual([referenceFoo hash], [referenceBar hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRDocumentSnapshotTests.m b/Firestore/Example/Tests/API/FIRDocumentSnapshotTests.m new file mode 100644 index 0000000..e865928 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRDocumentSnapshotTests.m @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRDocumentSnapshot.h" + +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRDocumentSnapshotTests : XCTestCase +@end + +@implementation FIRDocumentSnapshotTests + +- (void)testEquals { + FIRDocumentSnapshot *base = FSTTestDocSnapshot(@"rooms/foo", 1, @{ @"a" : @1 }, NO, NO); + FIRDocumentSnapshot *baseDup = FSTTestDocSnapshot(@"rooms/foo", 1, @{ @"a" : @1 }, NO, NO); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnonnull" + FIRDocumentSnapshot *nilData = FSTTestDocSnapshot(@"rooms/foo", 1, nil, NO, NO); + FIRDocumentSnapshot *nilDataDup = FSTTestDocSnapshot(@"rooms/foo", 1, nil, NO, NO); +#pragma clang diagnostic pop + FIRDocumentSnapshot *differentPath = FSTTestDocSnapshot(@"rooms/bar", 1, @{ @"a" : @1 }, NO, NO); + FIRDocumentSnapshot *differentData = FSTTestDocSnapshot(@"rooms/bar", 1, @{ @"b" : @1 }, NO, NO); + FIRDocumentSnapshot *hasMutations = FSTTestDocSnapshot(@"rooms/bar", 1, @{ @"a" : @1 }, YES, NO); + FIRDocumentSnapshot *fromCache = FSTTestDocSnapshot(@"rooms/bar", 1, @{ @"a" : @1 }, NO, YES); + XCTAssertEqualObjects(base, baseDup); + XCTAssertEqualObjects(nilData, nilDataDup); + XCTAssertNotEqualObjects(base, nilData); + XCTAssertNotEqualObjects(nilData, base); + XCTAssertNotEqualObjects(base, differentPath); + XCTAssertNotEqualObjects(base, differentData); + XCTAssertNotEqualObjects(base, hasMutations); + XCTAssertNotEqualObjects(base, fromCache); + + XCTAssertEqual([base hash], [baseDup hash]); + XCTAssertEqual([nilData hash], [nilDataDup hash]); + XCTAssertNotEqual([base hash], [nilData hash]); + XCTAssertNotEqual([base hash], [differentPath hash]); + XCTAssertNotEqual([base hash], [differentData hash]); + XCTAssertNotEqual([base hash], [hasMutations hash]); + XCTAssertNotEqual([base hash], [fromCache hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRFieldPathTests.m b/Firestore/Example/Tests/API/FIRFieldPathTests.m new file mode 100644 index 0000000..f8177c8 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRFieldPathTests.m @@ -0,0 +1,45 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRFieldPath.h" +#import "Firestore/Source/API/FIRFieldPath+Internal.h" +#import "Firestore/Source/Model/FSTPath.h" + +#import "Firestore/Example/Tests/Util/FSTHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRFieldPathTests : XCTestCase +@end + +@implementation FIRFieldPathTests + +- (void)testEquals { + FIRFieldPath *foo = [[FIRFieldPath alloc] initPrivate:FSTTestFieldPath(@"foo.ooo.oooo")]; + FIRFieldPath *fooDup = [[FIRFieldPath alloc] initPrivate:FSTTestFieldPath(@"foo.ooo.oooo")]; + FIRFieldPath *bar = [[FIRFieldPath alloc] initPrivate:FSTTestFieldPath(@"baa.aaa.aaar")]; + XCTAssertEqualObjects(foo, fooDup); + XCTAssertNotEqualObjects(foo, bar); + + XCTAssertEqual([foo hash], [fooDup hash]); + XCTAssertNotEqual([foo hash], [bar hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRFieldValueTests.m b/Firestore/Example/Tests/API/FIRFieldValueTests.m new file mode 100644 index 0000000..8c9db99 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRFieldValueTests.m @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@import FirebaseFirestore; + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRFieldValue.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRFieldValueTests : XCTestCase +@end + +@implementation FIRFieldValueTests + +- (void)testEquals { + FIRFieldValue *delete = [FIRFieldValue fieldValueForDelete]; + FIRFieldValue *deleteDup = [FIRFieldValue fieldValueForDelete]; + FIRFieldValue *serverTimestamp = [FIRFieldValue fieldValueForServerTimestamp]; + FIRFieldValue *serverTimestampDup = [FIRFieldValue fieldValueForServerTimestamp]; + XCTAssertEqualObjects(delete, deleteDup); + XCTAssertNotEqualObjects(delete, nil); + XCTAssertEqualObjects(serverTimestamp, serverTimestampDup); + XCTAssertNotEqualObjects(serverTimestamp, nil); + XCTAssertNotEqualObjects(delete, serverTimestamp); + + XCTAssertEqual([delete hash], [deleteDup hash]); + XCTAssertEqual([serverTimestamp hash], [serverTimestamp hash]); + XCTAssertNotEqual([delete hash], [serverTimestamp hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRGeoPointTests.m b/Firestore/Example/Tests/API/FIRGeoPointTests.m index b505de0..8abda10 100644 --- a/Firestore/Example/Tests/API/FIRGeoPointTests.m +++ b/Firestore/Example/Tests/API/FIRGeoPointTests.m @@ -28,16 +28,17 @@ NS_ASSUME_NONNULL_BEGIN @implementation FIRGeoPointTests - (void)testEquals { - XCTAssertEqualObjects([[FIRGeoPoint alloc] initWithLatitude:0 longitude:0], - [[FIRGeoPoint alloc] initWithLatitude:0 longitude:0]); - XCTAssertEqualObjects([[FIRGeoPoint alloc] initWithLatitude:1.23 longitude:4.56], - [[FIRGeoPoint alloc] initWithLatitude:1.23 longitude:4.56]); - XCTAssertNotEqualObjects([[FIRGeoPoint alloc] initWithLatitude:0 longitude:0], - [[FIRGeoPoint alloc] initWithLatitude:1 longitude:0]); - XCTAssertNotEqualObjects([[FIRGeoPoint alloc] initWithLatitude:0 longitude:0], - [[FIRGeoPoint alloc] initWithLatitude:0 longitude:1]); - XCTAssertNotEqualObjects([[FIRGeoPoint alloc] initWithLatitude:0 longitude:0], - [[NSObject alloc] init]); + FIRGeoPoint *foo = FSTTestGeoPoint(1.23, 4.56); + FIRGeoPoint *fooDup = FSTTestGeoPoint(1.23, 4.56); + FIRGeoPoint *differentLatitude = FSTTestGeoPoint(1.23, 0); + FIRGeoPoint *differentLongitude = FSTTestGeoPoint(0, 4.56); + XCTAssertEqualObjects(foo, fooDup); + XCTAssertNotEqualObjects(foo, differentLatitude); + XCTAssertNotEqualObjects(foo, differentLongitude); + + XCTAssertEqual([foo hash], [fooDup hash]); + XCTAssertNotEqual([foo hash], [differentLatitude hash]); + XCTAssertNotEqual([foo hash], [differentLongitude hash]); } - (void)testComparison { diff --git a/Firestore/Example/Tests/API/FIRQuerySnapshotTests.m b/Firestore/Example/Tests/API/FIRQuerySnapshotTests.m new file mode 100644 index 0000000..4637c49 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRQuerySnapshotTests.m @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRQuerySnapshot.h" +#import "Firestore/Source/Model/FSTPath.h" + +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRQuerySnapshotTests : XCTestCase +@end + +@implementation FIRQuerySnapshotTests + +- (void)testEquals { + FIRQuerySnapshot *foo = FSTTestQuerySnapshot(@"foo", @{}, @{ @"a" : @{@"a" : @1} }, YES, NO); + FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot(@"foo", @{}, @{ @"a" : @{@"a" : @1} }, YES, NO); + FIRQuerySnapshot *differentPath = FSTTestQuerySnapshot(@"bar", @{}, + @{ @"a" : @{@"a" : @1} }, YES, NO); + FIRQuerySnapshot *differentDoc = FSTTestQuerySnapshot(@"foo", + @{ @"a" : @{@"b" : @1} }, @{}, YES, NO); + FIRQuerySnapshot *noPendingWrites = FSTTestQuerySnapshot(@"foo", @{}, + @{ @"a" : @{@"a" : @1} }, NO, NO); + FIRQuerySnapshot *fromCache = FSTTestQuerySnapshot(@"foo", @{}, + @{ @"a" : @{@"a" : @1} }, YES, YES); + XCTAssertEqualObjects(foo, fooDup); + XCTAssertNotEqualObjects(foo, differentPath); + XCTAssertNotEqualObjects(foo, differentDoc); + XCTAssertNotEqualObjects(foo, noPendingWrites); + XCTAssertNotEqualObjects(foo, fromCache); + + XCTAssertEqual([foo hash], [fooDup hash]); + XCTAssertNotEqual([foo hash], [differentPath hash]); + XCTAssertNotEqual([foo hash], [differentDoc hash]); + XCTAssertNotEqual([foo hash], [noPendingWrites hash]); + XCTAssertNotEqual([foo hash], [fromCache hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FIRQueryTests.m b/Firestore/Example/Tests/API/FIRQueryTests.m index cac9f2a..1b5236d 100644 --- a/Firestore/Example/Tests/API/FIRQueryTests.m +++ b/Firestore/Example/Tests/API/FIRQueryTests.m @@ -14,16 +14,14 @@ * limitations under the License. */ -@import FirebaseFirestore; - #import <XCTest/XCTest.h> -#import "Firestore/Source/API/FIRFirestore+Internal.h" +#import "FirebaseFirestore/FIRQuery.h" #import "Firestore/Source/API/FIRQuery+Internal.h" #import "Firestore/Source/Core/FSTQuery.h" #import "Firestore/Source/Model/FSTPath.h" -#import "Firestore/Example/Tests/Util/FSTHelpers.h" +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" NS_ASSUME_NONNULL_BEGIN @@ -32,21 +30,29 @@ NS_ASSUME_NONNULL_BEGIN @implementation FIRQueryTests -- (void)testFilteringWithPredicate { -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wnonnull" - // Everything is dummy for unit test here. Filtering does not require any app - // specific setting as far as we do not fetch data. - FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:@"abc" - database:@"abc" - persistenceKey:@"db123" - credentialsProvider:nil - workerDispatchQueue:nil - firebaseApp:nil]; -#pragma clang diagnostic pop +- (void)testEquals { + FIRFirestore *firestore = FSTTestFirestore(); + FIRQuery *queryFoo = [FIRQuery referenceWithQuery:FSTTestQuery(@"foo") firestore:firestore]; + FIRQuery *queryFooDup = [FIRQuery referenceWithQuery:FSTTestQuery(@"foo") firestore:firestore]; + FIRQuery *queryBar = [FIRQuery referenceWithQuery:FSTTestQuery(@"bar") firestore:firestore]; + XCTAssertEqualObjects(queryFoo, queryFooDup); + XCTAssertNotEqualObjects(queryFoo, queryBar); + XCTAssertEqualObjects([queryFoo queryWhereField:@"f" isEqualTo:@1], + [queryFoo queryWhereField:@"f" isEqualTo:@1]); + XCTAssertNotEqualObjects([queryFoo queryWhereField:@"f" isEqualTo:@1], + [queryFoo queryWhereField:@"f" isEqualTo:@2]); - FSTResourcePath *path = [FSTResourcePath pathWithString:@"foo"]; - FIRQuery *query = [FIRQuery referenceWithQuery:[FSTQuery queryWithPath:path] firestore:firestore]; + XCTAssertEqual([queryFoo hash], [queryFooDup hash]); + XCTAssertNotEqual([queryFoo hash], [queryBar hash]); + XCTAssertEqual([[queryFoo queryWhereField:@"f" isEqualTo:@1] hash], + [[queryFoo queryWhereField:@"f" isEqualTo:@1] hash]); + XCTAssertNotEqual([[queryFoo queryWhereField:@"f" isEqualTo:@1] hash], + [[queryFoo queryWhereField:@"f" isEqualTo:@2] hash]); +} + +- (void)testFilteringWithPredicate { + FIRFirestore *firestore = FSTTestFirestore(); + FIRQuery *query = [FIRQuery referenceWithQuery:FSTTestQuery(@"foo") firestore:firestore]; FIRQuery *query1 = [query queryWhereField:@"f" isLessThanOrEqualTo:@1]; FIRQuery *query2 = [query queryFilteredUsingPredicate:[NSPredicate predicateWithFormat:@"f<=1"]]; FIRQuery *query3 = diff --git a/Firestore/Example/Tests/API/FIRSnapshotMetadataTests.m b/Firestore/Example/Tests/API/FIRSnapshotMetadataTests.m new file mode 100644 index 0000000..cf50765 --- /dev/null +++ b/Firestore/Example/Tests/API/FIRSnapshotMetadataTests.m @@ -0,0 +1,51 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <XCTest/XCTest.h> + +#import "FirebaseFirestore/FIRSnapshotMetadata.h" +#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface FIRSnapshotMetadataTests : XCTestCase +@end + +@implementation FIRSnapshotMetadataTests + +- (void)testEquals { + FIRSnapshotMetadata *foo = + [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES fromCache:YES]; + FIRSnapshotMetadata *fooDup = + [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES fromCache:YES]; + FIRSnapshotMetadata *bar = + [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES fromCache:NO]; + FIRSnapshotMetadata *baz = + [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO fromCache:YES]; + XCTAssertEqualObjects(foo, fooDup); + XCTAssertNotEqualObjects(foo, bar); + XCTAssertNotEqualObjects(foo, baz); + XCTAssertNotEqualObjects(bar, baz); + + XCTAssertEqual([foo hash], [fooDup hash]); + XCTAssertNotEqual([foo hash], [bar hash]); + XCTAssertNotEqual([foo hash], [baz hash]); + XCTAssertNotEqual([bar hash], [baz hash]); +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FSTAPIHelpers.h b/Firestore/Example/Tests/API/FSTAPIHelpers.h new file mode 100644 index 0000000..dcd8209 --- /dev/null +++ b/Firestore/Example/Tests/API/FSTAPIHelpers.h @@ -0,0 +1,73 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import <Foundation/Foundation.h> + +#import "FirebaseFirestore/FIRCollectionReference.h" +#import "FirebaseFirestore/FIRDocumentSnapshot.h" +#import "FirebaseFirestore/FIRFirestore.h" +#import "FirebaseFirestore/FIRQuerySnapshot.h" + +#import "Firestore/Example/Tests/Util/FSTHelpers.h" + +NS_ASSUME_NONNULL_BEGIN + +#if __cplusplus +extern "C" { +#endif + +/** A convenience method for creating dummy singleton FIRFirestore for tests. */ +FIRFirestore *FSTTestFirestore(); + +/** A convenience method for creating a doc snapshot for tests. */ +FIRDocumentSnapshot *FSTTestDocSnapshot(NSString *path, + FSTTestSnapshotVersion version, + NSDictionary<NSString *, id> *data, + BOOL hasMutations, + BOOL fromCache); + +/** A convenience method for creating a collection reference from a path string. */ +FIRCollectionReference *FSTTestCollectionRef(NSString *path); + +/** A convenience method for creating a document reference from a path string. */ +FIRDocumentReference *FSTTestDocRef(NSString *path); + +/** + * A convenience method for creating a particular query snapshot for tests. + * + * @param path To be used in constructing the query. + * @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each dictionary entry + * maps to a document, with the key being the document id, and the value being the document + * contents. + * @param docsToAdd Specifies data to be added into the query snapshot as of now. Each dictionary + * entry maps to a document, with the key being the document id, and the value being the document + * contents. + * @param hasPendingWrites Whether the query snapshot has pending writes to the server. + * @param fromCache Whether the query snapshot is cache result. + * @returns A query snapshot that consists of both sets of documents. + */ +FIRQuerySnapshot *FSTTestQuerySnapshot( + NSString *path, + NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs, + NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd, + BOOL hasPendingWrites, + BOOL fromCache); + +#if __cplusplus +} // extern "C" +#endif + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Example/Tests/API/FSTAPIHelpers.m b/Firestore/Example/Tests/API/FSTAPIHelpers.m new file mode 100644 index 0000000..507e2ff --- /dev/null +++ b/Firestore/Example/Tests/API/FSTAPIHelpers.m @@ -0,0 +1,113 @@ +/* + * Copyright 2017 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import "Firestore/Example/Tests/API/FSTAPIHelpers.h" + +#import "FirebaseFirestore/FIRDocumentReference.h" +#import "FirebaseFirestore/FIRSnapshotMetadata.h" +#import "Firestore/Source/API/FIRCollectionReference+Internal.h" +#import "Firestore/Source/API/FIRDocumentReference+Internal.h" +#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h" +#import "Firestore/Source/API/FIRFirestore+Internal.h" +#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h" +#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h" +#import "Firestore/Source/Core/FSTQuery.h" +#import "Firestore/Source/Core/FSTViewSnapshot.h" +#import "Firestore/Source/Model/FSTDocument.h" +#import "Firestore/Source/Model/FSTDocumentKey.h" +#import "Firestore/Source/Model/FSTDocumentSet.h" +#import "Firestore/Source/Model/FSTPath.h" + +NS_ASSUME_NONNULL_BEGIN + +FIRFirestore *FSTTestFirestore() { + static FIRFirestore *sharedInstance = nil; + static dispatch_once_t onceToken; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnonnull" + dispatch_once(&onceToken, ^{ + sharedInstance = [[FIRFirestore alloc] initWithProjectID:@"abc" + database:@"abc" + persistenceKey:@"db123" + credentialsProvider:nil + workerDispatchQueue:nil + firebaseApp:nil]; + }); +#pragma clang diagnostic pop + return sharedInstance; +} + +FIRDocumentSnapshot *FSTTestDocSnapshot(NSString *path, + FSTTestSnapshotVersion version, + NSDictionary<NSString *, id> *data, + BOOL hasMutations, + BOOL fromCache) { + FSTDocument *doc = data ? FSTTestDoc(path, version, data, hasMutations) : nil; + return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore() + documentKey:FSTTestDocKey(path) + document:doc + fromCache:fromCache]; +} + +FIRCollectionReference *FSTTestCollectionRef(NSString *path) { + return [FIRCollectionReference referenceWithPath:FSTTestPath(path) firestore:FSTTestFirestore()]; +} + +FIRDocumentReference *FSTTestDocRef(NSString *path) { + return [FIRDocumentReference referenceWithPath:FSTTestPath(path) firestore:FSTTestFirestore()]; +} + +/** A convenience method for creating a query snapshots for tests. */ +FIRQuerySnapshot *FSTTestQuerySnapshot( + NSString *path, + NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs, + NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd, + BOOL hasPendingWrites, + BOOL fromCache) { + FIRSnapshotMetadata *metadata = + [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:hasPendingWrites fromCache:fromCache]; + FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]); + for (NSString *key in oldDocs) { + oldDocuments = [oldDocuments + documentSetByAddingDocument:FSTTestDoc([NSString stringWithFormat:@"%@/%@", path, key], 1, + oldDocs[key], hasPendingWrites)]; + } + FSTDocumentSet *newDocuments = oldDocuments; + NSArray<FSTDocumentViewChange *> *documentChanges = [NSArray array]; + for (NSString *key in docsToAdd) { + FSTDocument *docToAdd = FSTTestDoc([NSString stringWithFormat:@"%@/%@", path, key], 1, + docsToAdd[key], hasPendingWrites); + newDocuments = [newDocuments documentSetByAddingDocument:docToAdd]; + documentChanges = [documentChanges + arrayByAddingObject:[FSTDocumentViewChange + changeWithDocument:docToAdd + type:FSTDocumentViewChangeTypeAdded]]; + } + FSTViewSnapshot *viewSnapshot = [[FSTViewSnapshot alloc] initWithQuery:FSTTestQuery(path) + documents:newDocuments + oldDocuments:oldDocuments + documentChanges:documentChanges + fromCache:fromCache + hasPendingWrites:hasPendingWrites + syncStateChanged:YES]; + return [FIRQuerySnapshot snapshotWithFirestore:FSTTestFirestore() + originalQuery:FSTTestQuery(path) + snapshot:viewSnapshot + metadata:metadata]; +} + +NS_ASSUME_NONNULL_END |