Files
firebase-ios-sdk/Firestore/Example/Tests/API/FSTAPIHelpers.m
T
zxu 9ddf3637ac 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
2017-12-19 13:47:04 -05:00

114 lines
5.3 KiB
Objective-C

/*
* 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