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>
|
|
|
|
|
|
2018-11-13 20:04:14 -05:00
|
|
|
#include <string>
|
2019-02-19 18:30:12 -05:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2018-11-13 20:04:14 -05:00
|
|
|
|
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"
|
2018-03-14 08:57:31 -04:00
|
|
|
|
2019-02-19 18:30:12 -05:00
|
|
|
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
|
2019-03-25 11:04:21 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
#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;
|
2019-02-19 18:30:12 -05:00
|
|
|
using firebase::firestore::core::DocumentViewChange;
|
2019-03-01 14:16:41 -05:00
|
|
|
using firebase::firestore::core::ViewSnapshot;
|
2019-02-19 18:30:12 -05:00
|
|
|
using firebase::firestore::model::DocumentKeySet;
|
2019-03-25 11:04:21 -07:00
|
|
|
using firebase::firestore::model::DocumentSet;
|
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, ^{
|
2018-02-22 08:12:54 -08:00
|
|
|
sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
|
|
|
|
|
database:"abc"
|
2019-03-15 19:10:32 -04:00
|
|
|
persistenceKey:"db123"
|
|
|
|
|
credentialsProvider:nullptr
|
|
|
|
|
workerQueue:nullptr
|
2017-12-19 13:47:04 -05:00
|
|
|
firebaseApp:nil];
|
|
|
|
|
});
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
return sharedInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 15:42:05 -04:00
|
|
|
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;
|
2019-03-15 19:10:32 -04:00
|
|
|
return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
|
|
|
|
|
documentKey:testutil::Key(path)
|
|
|
|
|
document:doc
|
|
|
|
|
fromCache:fromCache
|
|
|
|
|
hasPendingWrites:hasMutations];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-14 08:57:31 -04:00
|
|
|
FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
|
|
|
|
|
return [FIRCollectionReference referenceWithPath:testutil::Resource(path)
|
|
|
|
|
firestore:FSTTestFirestore()];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
2018-03-14 08:57:31 -04:00
|
|
|
FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
|
2019-03-15 19:10:32 -04:00
|
|
|
return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
|
|
|
|
|
firestore:FSTTestFirestore().wrapped];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A convenience method for creating a query snapshots for tests. */
|
|
|
|
|
FIRQuerySnapshot *FSTTestQuerySnapshot(
|
2018-03-16 15:42:05 -04:00
|
|
|
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,
|
2019-03-12 15:58:52 -07:00
|
|
|
bool hasPendingWrites,
|
|
|
|
|
bool fromCache) {
|
2019-03-20 08:04:57 -07:00
|
|
|
SnapshotMetadata metadata(hasPendingWrites, fromCache);
|
2019-03-25 11:04:21 -07:00
|
|
|
DocumentSet 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) {
|
2019-03-25 11:04:21 -07:00
|
|
|
oldDocuments = oldDocuments.insert(
|
|
|
|
|
FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, oldDocs[key],
|
|
|
|
|
hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced));
|
2018-11-01 09:44:30 -07:00
|
|
|
if (hasPendingWrites) {
|
2018-11-13 20:04:14 -05:00
|
|
|
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-25 11:04:21 -07:00
|
|
|
DocumentSet newDocuments = oldDocuments;
|
2019-02-19 18:30:12 -05:00
|
|
|
std::vector<DocumentViewChange> documentChanges;
|
2017-12-19 13:47:04 -05:00
|
|
|
for (NSString *key in docsToAdd) {
|
2018-03-16 15:42:05 -04:00
|
|
|
FSTDocument *docToAdd =
|
2018-11-01 09:44:30 -07:00
|
|
|
FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, docsToAdd[key],
|
|
|
|
|
hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced);
|
2019-03-25 11:04:21 -07:00
|
|
|
newDocuments = newDocuments.insert(docToAdd);
|
2019-02-19 18:30:12 -05:00
|
|
|
documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::kAdded);
|
2018-11-01 09:44:30 -07:00
|
|
|
if (hasPendingWrites) {
|
2018-11-13 20:04:14 -05:00
|
|
|
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};
|
2019-03-20 08:04:57 -07:00
|
|
|
return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
|
|
|
|
|
originalQuery:FSTTestQuery(path)
|
|
|
|
|
snapshot:std::move(viewSnapshot)
|
|
|
|
|
metadata:std::move(metadata)];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|