Files
firebase-ios-sdk/Firestore/Example/Tests/API/FSTAPIHelpers.mm
T

142 lines
6.2 KiB
Plaintext
Raw Normal View History

2017-12-19 13:47:04 -05:00
/*
* 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"
2018-01-21 21:14:58 -08:00
#import <FirebaseFirestore/FIRDocumentChange.h>
#import <FirebaseFirestore/FIRDocumentReference.h>
#import <FirebaseFirestore/FIRSnapshotMetadata.h>
#include <string>
#include <utility>
#include <vector>
2017-12-19 13:47:04 -05:00
#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/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
namespace testutil = firebase::firestore::testutil;
namespace util = firebase::firestore::util;
using firebase::firestore::core::DocumentViewChange;
2019-03-01 14:16:41 -05:00
using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::model::DocumentKeySet;
2017-12-19 13:47:04 -05:00
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"
2017-12-19 13:47:04 -05:00
persistenceKey:@"db123"
credentialsProvider:nil
workerQueue:nil
2017-12-19 13:47:04 -05:00
firebaseApp:nil];
});
#pragma clang diagnostic pop
return sharedInstance;
}
FIRDocumentSnapshot *FSTTestDocSnapshot(const absl::string_view path,
2017-12-19 13:47:04 -05:00
FSTTestSnapshotVersion version,
2018-01-21 21:14:58 -08:00
NSDictionary<NSString *, id> *_Nullable data,
2017-12-19 13:47:04 -05:00
BOOL hasMutations,
BOOL fromCache) {
2018-11-01 09:44:30 -07:00
FSTDocument *doc =
data ? FSTTestDoc(path, version, data,
hasMutations ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced)
: nil;
2017-12-19 13:47:04 -05:00
return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore()
documentKey:testutil::Key(path)
2017-12-19 13:47:04 -05:00
document:doc
2018-11-01 09:44:30 -07:00
fromCache:fromCache
hasPendingWrites:hasMutations];
2017-12-19 13:47:04 -05:00
}
FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
firestore:FSTTestFirestore()];
2017-12-19 13:47:04 -05:00
}
FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
return [FIRDocumentReference referenceWithPath:testutil::Resource(path)
firestore:FSTTestFirestore()];
2017-12-19 13:47:04 -05:00
}
/** A convenience method for creating a query snapshots for tests. */
FIRQuerySnapshot *FSTTestQuerySnapshot(
const absl::string_view path,
2017-12-19 13:47:04 -05:00
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, @[]);
2018-11-01 09:44:30 -07:00
DocumentKeySet mutatedKeys;
2017-12-19 13:47:04 -05:00
for (NSString *key in oldDocs) {
2018-11-01 09:44:30 -07:00
oldDocuments = [oldDocuments
documentSetByAddingDocument:FSTTestDoc(util::StringFormat("%s/%s", path, key), 1,
oldDocs[key],
hasPendingWrites ? FSTDocumentStateLocalMutations
: FSTDocumentStateSynced)];
if (hasPendingWrites) {
const std::string documentKey = util::StringFormat("%s/%s", path, key);
2018-11-01 09:44:30 -07:00
mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
}
2017-12-19 13:47:04 -05:00
}
FSTDocumentSet *newDocuments = oldDocuments;
std::vector<DocumentViewChange> documentChanges;
2017-12-19 13:47:04 -05:00
for (NSString *key in docsToAdd) {
FSTDocument *docToAdd =
2018-11-01 09:44:30 -07:00
FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, docsToAdd[key],
hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced);
2017-12-19 13:47:04 -05:00
newDocuments = [newDocuments documentSetByAddingDocument:docToAdd];
documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::kAdded);
2018-11-01 09:44:30 -07:00
if (hasPendingWrites) {
const std::string documentKey = util::StringFormat("%s/%s", path, key);
2018-11-01 09:44:30 -07:00
mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
}
2017-12-19 13:47:04 -05:00
}
2019-03-01 14:16:41 -05:00
ViewSnapshot viewSnapshot{FSTTestQuery(path),
newDocuments,
oldDocuments,
std::move(documentChanges),
mutatedKeys,
fromCache,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/false};
2017-12-19 13:47:04 -05:00
return [FIRQuerySnapshot snapshotWithFirestore:FSTTestFirestore()
originalQuery:FSTTestQuery(path)
2019-03-01 14:16:41 -05:00
snapshot:std::move(viewSnapshot)
2017-12-19 13:47:04 -05:00
metadata:metadata];
}
NS_ASSUME_NONNULL_END