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
|
|
|
|
2019-06-24 16:18:30 -04:00
|
|
|
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
|
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"
|
2019-08-13 13:59:00 -07:00
|
|
|
#import "Firestore/Source/API/FSTUserDataConverter.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
|
2020-04-20 14:57:34 -07:00
|
|
|
#include "Firestore/core/src/core/view_snapshot.h"
|
|
|
|
|
#include "Firestore/core/src/model/document.h"
|
|
|
|
|
#include "Firestore/core/src/model/document_set.h"
|
2020-09-25 15:47:20 -04:00
|
|
|
#include "Firestore/core/src/remote/firebase_metadata_provider.h"
|
2020-04-20 14:57:34 -07:00
|
|
|
#include "Firestore/core/src/util/string_apple.h"
|
|
|
|
|
#include "Firestore/core/test/unit/testutil/testutil.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
|
|
|
|
|
namespace testutil = firebase::firestore::testutil;
|
|
|
|
|
namespace util = firebase::firestore::util;
|
2019-04-12 17:03:26 -07:00
|
|
|
using firebase::firestore::api::SnapshotMetadata;
|
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-05-21 20:28:51 -07:00
|
|
|
using firebase::firestore::model::DatabaseId;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::Document;
|
2019-04-25 11:11:38 -07:00
|
|
|
using firebase::firestore::model::DocumentComparator;
|
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;
|
2019-06-20 09:39:19 -07:00
|
|
|
using firebase::firestore::model::DocumentState;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::FieldValue;
|
|
|
|
|
using firebase::firestore::model::NoDocument;
|
2017-12-19 13:47:04 -05:00
|
|
|
|
2019-08-13 13:59:00 -07:00
|
|
|
using testutil::Doc;
|
2019-08-02 11:35:33 -07:00
|
|
|
using testutil::Query;
|
|
|
|
|
|
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, ^{
|
2019-05-21 20:28:51 -07:00
|
|
|
sharedInstance = [[FIRFirestore alloc] initWithDatabaseID:DatabaseId("abc", "abc")
|
|
|
|
|
persistenceKey:"db123"
|
|
|
|
|
credentialsProvider:nullptr
|
|
|
|
|
workerQueue:nullptr
|
2020-09-25 15:47:20 -04:00
|
|
|
firebaseMetadataProvider:nullptr
|
2019-08-06 20:33:08 -04:00
|
|
|
firebaseApp:nil
|
|
|
|
|
instanceRegistry:nil];
|
2017-12-19 13:47:04 -05:00
|
|
|
});
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
return sharedInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 16:18:30 -04:00
|
|
|
FIRDocumentSnapshot *FSTTestDocSnapshot(const char *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) {
|
2019-08-13 13:59:00 -07:00
|
|
|
absl::optional<Document> doc;
|
|
|
|
|
if (data) {
|
|
|
|
|
FSTUserDataConverter *converter = FSTTestUserDataConverter();
|
|
|
|
|
FieldValue parsed = [converter parsedQueryValue:data];
|
|
|
|
|
|
|
|
|
|
doc = Doc(path, version, parsed,
|
|
|
|
|
hasMutations ? DocumentState::kLocalMutations : DocumentState::kSynced);
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2019-06-24 16:18:30 -04:00
|
|
|
FIRCollectionReference *FSTTestCollectionRef(const char *path) {
|
2019-06-04 18:10:17 -07:00
|
|
|
return [[FIRCollectionReference alloc] initWithPath:testutil::Resource(path)
|
|
|
|
|
firestore:FSTTestFirestore().wrapped];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
2019-06-24 16:18:30 -04:00
|
|
|
FIRDocumentReference *FSTTestDocRef(const char *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(
|
2019-06-24 16:18:30 -04:00
|
|
|
const char *path,
|
2017-12-19 13:47:04 -05:00
|
|
|
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *oldDocs,
|
|
|
|
|
NSDictionary<NSString *, NSDictionary<NSString *, id> *> *docsToAdd,
|
2019-06-24 16:18:30 -04:00
|
|
|
BOOL hasPendingWrites,
|
|
|
|
|
BOOL fromCache) {
|
2019-08-13 13:59:00 -07:00
|
|
|
FSTUserDataConverter *converter = FSTTestUserDataConverter();
|
|
|
|
|
|
2019-03-20 08:04:57 -07:00
|
|
|
SnapshotMetadata metadata(hasPendingWrites, fromCache);
|
2019-08-13 13:59:00 -07:00
|
|
|
DocumentSet oldDocuments(DocumentComparator::ByKey());
|
2018-11-01 09:44:30 -07:00
|
|
|
DocumentKeySet mutatedKeys;
|
2017-12-19 13:47:04 -05:00
|
|
|
for (NSString *key in oldDocs) {
|
2019-08-13 13:59:00 -07:00
|
|
|
FieldValue doc = [converter parsedQueryValue:oldDocs[key]];
|
|
|
|
|
std::string documentKey = util::StringFormat("%s/%s", path, key);
|
2019-03-25 11:04:21 -07:00
|
|
|
oldDocuments = oldDocuments.insert(
|
2019-08-13 13:59:00 -07:00
|
|
|
Doc(documentKey, 1, doc,
|
|
|
|
|
hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced));
|
2018-11-01 09:44:30 -07:00
|
|
|
if (hasPendingWrites) {
|
|
|
|
|
mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
|
|
|
|
|
}
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
2019-08-13 13:59:00 -07: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) {
|
2019-08-13 13:59:00 -07:00
|
|
|
FieldValue doc = [converter parsedQueryValue:docsToAdd[key]];
|
|
|
|
|
std::string documentKey = util::StringFormat("%s/%s", path, key);
|
|
|
|
|
Document docToAdd =
|
|
|
|
|
Doc(documentKey, 1, doc,
|
|
|
|
|
hasPendingWrites ? DocumentState::kLocalMutations : DocumentState::kSynced);
|
2019-03-25 11:04:21 -07:00
|
|
|
newDocuments = newDocuments.insert(docToAdd);
|
2019-08-26 19:39:54 -07:00
|
|
|
documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::Added);
|
2018-11-01 09:44:30 -07:00
|
|
|
if (hasPendingWrites) {
|
|
|
|
|
mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
|
|
|
|
|
}
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
2019-08-02 11:35:33 -07:00
|
|
|
ViewSnapshot viewSnapshot{Query(path),
|
2019-03-01 14:16:41 -05:00
|
|
|
newDocuments,
|
|
|
|
|
oldDocuments,
|
|
|
|
|
std::move(documentChanges),
|
|
|
|
|
mutatedKeys,
|
2019-06-24 16:18:30 -04:00
|
|
|
static_cast<bool>(fromCache),
|
2019-03-01 14:16:41 -05:00
|
|
|
/*sync_state_changed=*/true,
|
|
|
|
|
/*excludes_metadata_changes=*/false};
|
2019-03-20 08:04:57 -07:00
|
|
|
return [[FIRQuerySnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
|
2019-08-02 11:35:33 -07:00
|
|
|
originalQuery:Query(path)
|
2019-03-20 08:04:57 -07:00
|
|
|
snapshot:std::move(viewSnapshot)
|
|
|
|
|
metadata:std::move(metadata)];
|
2017-12-19 13:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|