Files
firebase-ios-sdk/Firestore/Example/Tests/Local/FSTLocalStoreTests.mm
T

1149 lines
47 KiB
Plaintext
Raw Normal View History

2017-10-03 08:55:22 -07: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.
*/
2019-09-18 14:00:32 -04:00
// TODO(wuandy): Move `local_store.h` here once this test is ported to C++.
2017-10-03 08:55:22 -07:00
#import <FirebaseFirestore/FIRTimestamp.h>
2017-10-03 08:55:22 -07:00
#import <XCTest/XCTest.h>
#include <string>
2019-02-07 22:03:17 -05:00
#include <utility>
#include <vector>
2019-03-06 14:51:30 -08:00
#import "Firestore/Source/API/FIRFieldValue+Internal.h"
#import "Firestore/Source/Util/FSTClasses.h"
2017-10-03 08:55:22 -07:00
#import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2017-10-03 08:55:22 -07:00
#include "Firestore/core/include/firebase/firestore/timestamp.h"
#include "Firestore/core/src/firebase/firestore/auth/user.h"
2019-09-18 14:00:32 -04:00
#include "Firestore/core/src/firebase/firestore/local/local_store.h"
2019-07-25 21:57:22 -04:00
#include "Firestore/core/src/firebase/firestore/local/local_view_changes.h"
2019-07-21 22:12:02 -04:00
#include "Firestore/core/src/firebase/firestore/local/local_write_result.h"
2019-09-12 14:28:10 -07:00
#include "Firestore/core/src/firebase/firestore/local/persistence.h"
#include "Firestore/core/src/firebase/firestore/local/query_data.h"
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
2019-03-25 11:04:21 -07:00
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/model/mutation_batch_result.h"
#include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
2019-01-23 17:26:49 -05:00
#include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
#include "Firestore/core/src/firebase/firestore/util/status.h"
2019-10-04 11:55:20 -07:00
#include "Firestore/core/test/firebase/firestore/remote/fake_target_metadata_provider.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
namespace testutil = firebase::firestore::testutil;
using firebase::Timestamp;
using firebase::firestore::auth::User;
2019-09-18 14:00:32 -04:00
using firebase::firestore::local::LocalStore;
2019-07-25 21:57:22 -04:00
using firebase::firestore::local::LocalViewChanges;
2019-07-21 22:12:02 -04:00
using firebase::firestore::local::LocalWriteResult;
2019-09-12 14:28:10 -07:00
using firebase::firestore::local::Persistence;
using firebase::firestore::local::QueryData;
using firebase::firestore::model::Document;
using firebase::firestore::model::DocumentKey;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentState;
using firebase::firestore::model::FieldValue;
using firebase::firestore::model::ListenSequenceNumber;
using firebase::firestore::model::MaybeDocument;
using firebase::firestore::model::Mutation;
using firebase::firestore::model::MutationBatch;
using firebase::firestore::model::MutationBatchResult;
using firebase::firestore::model::MutationResult;
using firebase::firestore::model::DocumentMap;
using firebase::firestore::model::MaybeDocumentMap;
using firebase::firestore::model::SnapshotVersion;
2018-08-15 15:56:45 -07:00
using firebase::firestore::model::TargetId;
using firebase::firestore::nanopb::ByteString;
2019-10-04 11:55:20 -07:00
using firebase::firestore::remote::FakeTargetMetadataProvider;
2019-01-29 18:55:25 -05:00
using firebase::firestore::remote::RemoteEvent;
using firebase::firestore::remote::WatchChangeAggregator;
2019-01-23 17:26:49 -05:00
using firebase::firestore::remote::WatchTargetChange;
using firebase::firestore::remote::WatchTargetChangeState;
using firebase::firestore::util::Status;
using testutil::Array;
using testutil::DeletedDoc;
using testutil::Doc;
using testutil::Key;
using testutil::Map;
2019-08-02 11:35:33 -07:00
using testutil::Query;
using testutil::UnknownDoc;
using testutil::Vector;
2019-08-02 11:35:33 -07:00
namespace {
std::vector<MaybeDocument> DocMapToArray(const MaybeDocumentMap &docs) {
std::vector<MaybeDocument> result;
for (const auto &kv : docs) {
result.push_back(kv.second);
}
return result;
}
std::vector<Document> DocMapToArray(const DocumentMap &docs) {
std::vector<Document> result;
for (const auto &kv : docs.underlying_map()) {
result.push_back(Document(kv.second));
}
return result;
}
} // namespace
2017-10-03 08:55:22 -07:00
NS_ASSUME_NONNULL_BEGIN
@interface FSTLocalStoreTests ()
2018-08-15 15:56:45 -07:00
@property(nonatomic, assign, readwrite) TargetId lastTargetID;
2017-10-03 08:55:22 -07:00
@end
@implementation FSTLocalStoreTests {
2019-09-18 14:00:32 -04:00
std::unique_ptr<LocalStore> _localStore;
2019-09-12 14:28:10 -07:00
std::unique_ptr<Persistence> _localStorePersistence;
std::vector<MutationBatch> _batches;
MaybeDocumentMap _lastChanges;
}
2017-10-03 08:55:22 -07:00
- (void)setUp {
[super setUp];
if ([self isTestBaseClass]) {
return;
}
2019-09-12 14:28:10 -07:00
std::unique_ptr<Persistence> persistence = [self persistence];
_localStorePersistence = std::move(persistence);
2019-09-18 14:00:32 -04:00
_localStore =
absl::make_unique<LocalStore>(_localStorePersistence.get(), User::Unauthenticated());
_localStore->Start();
2017-10-03 08:55:22 -07:00
_lastTargetID = 0;
}
- (void)tearDown {
2019-09-12 14:28:10 -07:00
if (_localStorePersistence) {
_localStorePersistence->Shutdown();
}
2017-10-03 08:55:22 -07:00
[super tearDown];
}
2019-09-12 14:28:10 -07:00
- (std::unique_ptr<Persistence>)persistence {
2017-10-03 08:55:22 -07:00
@throw FSTAbstractMethodException(); // NOLINT
}
2018-07-27 13:58:11 -07:00
- (BOOL)gcIsEager {
@throw FSTAbstractMethodException(); // NOLINT
}
2017-10-03 08:55:22 -07:00
/**
* Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
* FSTLocalStoreTests since it is incomplete without the implementations supplied by its
* subclasses.
*/
- (BOOL)isTestBaseClass {
return [self class] == [FSTLocalStoreTests class];
}
- (void)writeMutation:(Mutation)mutation {
[self writeMutations:{std::move(mutation)}];
2017-10-03 08:55:22 -07:00
}
- (void)writeMutations:(std::vector<Mutation> &&)mutations {
2019-02-07 22:03:17 -05:00
auto mutationsCopy = mutations;
2019-09-18 14:00:32 -04:00
LocalWriteResult result = _localStore->WriteLocally(std::move(mutationsCopy));
_batches.emplace_back(result.batch_id(), Timestamp::Now(), std::vector<Mutation>{},
std::move(mutations));
2019-07-21 22:12:02 -04:00
_lastChanges = result.changes();
2017-10-03 08:55:22 -07:00
}
2019-01-29 18:55:25 -05:00
- (void)applyRemoteEvent:(const RemoteEvent &)event {
2019-09-18 14:00:32 -04:00
_lastChanges = _localStore->ApplyRemoteEvent(event);
2017-10-03 08:55:22 -07:00
}
2019-07-25 21:57:22 -04:00
- (void)notifyLocalViewChanges:(LocalViewChanges)changes {
2019-09-18 14:00:32 -04:00
_localStore->NotifyLocalViewChanges(std::vector<LocalViewChanges>{std::move(changes)});
2017-10-03 08:55:22 -07:00
}
2019-03-06 14:51:30 -08:00
- (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion
transformResult:(id _Nullable)transformResult {
XCTAssertGreaterThan(_batches.size(), 0, @"Missing batch to acknowledge.");
MutationBatch batch = _batches.front();
_batches.erase(_batches.begin());
XCTAssertEqual(batch.mutations().size(), 1,
@"Acknowledging more than one mutation not supported.");
SnapshotVersion version = testutil::Version(documentVersion);
absl::optional<std::vector<FieldValue>> mutationTransformResult;
if (transformResult) {
mutationTransformResult = std::vector<FieldValue>{FSTTestFieldValue(transformResult)};
}
MutationResult mutationResult(version, mutationTransformResult);
MutationBatchResult result(batch, version, {mutationResult}, {});
2019-09-18 14:00:32 -04:00
_lastChanges = _localStore->AcknowledgeBatch(result);
2017-10-03 08:55:22 -07:00
}
2019-03-06 14:51:30 -08:00
- (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
[self acknowledgeMutationWithVersion:documentVersion transformResult:nil];
}
2017-10-03 08:55:22 -07:00
- (void)rejectMutation {
MutationBatch batch = _batches.front();
_batches.erase(_batches.begin());
2019-09-18 14:00:32 -04:00
_lastChanges = _localStore->RejectBatch(batch.batch_id());
2017-10-03 08:55:22 -07:00
}
2019-08-02 11:35:33 -07:00
- (TargetId)allocateQuery:(core::Query)query {
2019-09-18 14:00:32 -04:00
QueryData queryData = _localStore->AllocateQuery(std::move(query));
self.lastTargetID = queryData.target_id();
return queryData.target_id();
2017-10-03 08:55:22 -07:00
}
/** Asserts that the last target ID is the given number. */
#define FSTAssertTargetID(targetID) \
do { \
XCTAssertEqual(self.lastTargetID, targetID); \
} while (0)
/** Asserts that a the lastChanges contain the docs in the given array. */
#define FSTAssertChanged(...) \
do { \
std::vector<MaybeDocument> expected = {__VA_ARGS__}; \
XCTAssertEqual(_lastChanges.size(), expected.size()); \
auto lastChangesList = DocMapToArray(_lastChanges); \
XCTAssertEqual(lastChangesList, expected); \
_lastChanges = MaybeDocumentMap{}; \
2017-10-03 08:55:22 -07:00
} while (0)
/** Asserts that the given keys were removed. */
#define FSTAssertRemoved(...) \
do { \
std::vector<std::string> keyPaths = {__VA_ARGS__}; \
XCTAssertEqual(_lastChanges.size(), keyPaths.size()); \
auto keyPathIterator = keyPaths.begin(); \
for (const auto &kv : _lastChanges) { \
const DocumentKey &actualKey = kv.first; \
const MaybeDocument &value = kv.second; \
DocumentKey expectedKey = Key(*keyPathIterator); \
XCTAssertEqual(actualKey, expectedKey); \
XCTAssertTrue(value.is_no_document()); \
++keyPathIterator; \
} \
_lastChanges = MaybeDocumentMap{}; \
2017-10-03 08:55:22 -07:00
} while (0)
/** Asserts that the given local store contains the given document. */
2019-09-18 14:00:32 -04:00
#define FSTAssertContains(document) \
do { \
MaybeDocument expected = (document); \
absl::optional<MaybeDocument> actual = _localStore->ReadDocument(expected.key()); \
XCTAssertEqual(actual, expected); \
2017-10-03 08:55:22 -07:00
} while (0)
/** Asserts that the given local store does not contain the given document. */
2019-09-18 14:00:32 -04:00
#define FSTAssertNotContains(keyPathString) \
do { \
DocumentKey key = Key(keyPathString); \
absl::optional<MaybeDocument> actual = _localStore->ReadDocument(key); \
XCTAssertEqual(actual, absl::nullopt); \
2017-10-03 08:55:22 -07:00
} while (0)
- (void)testMutationBatchKeys {
if ([self isTestBaseClass]) return;
Mutation base = FSTTestSetMutation(@"foo/ignore", @{@"foo" : @"bar"});
Mutation set1 = FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"});
Mutation set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
MutationBatch batch = MutationBatch(1, Timestamp::Now(), {base}, {set1, set2});
DocumentKeySet keys = batch.keys();
XCTAssertEqual(keys.size(), 2u);
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutation {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:0];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) {
// Nothing is pinning this anymore, as it has been acknowledged and there are no targets active.
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
} else {
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationThenDocument {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
TargetId targetID = [self allocateQuery:Query("foo")];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 2, Map("it", "changed")),
2019-01-23 17:26:49 -05:00
{targetID}, {})];
FSTAssertChanged(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesAckThenRejectThenRemoteEvent {
if ([self isTestBaseClass]) return;
// Start a query that requires acks to be held.
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
// The last seen version is zero, so this ack must be held.
[self acknowledgeMutationWithVersion:1];
FSTAssertChanged(Doc("foo/bar", 1, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-11-01 09:44:30 -07:00
// Under eager GC, there is no longer a reference for the document, and it should be
// deleted.
if ([self gcIsEager]) {
FSTAssertNotContains("foo/bar");
2018-11-01 09:44:30 -07:00
} else {
FSTAssertContains(Doc("foo/bar", 1, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-11-01 09:44:30 -07:00
}
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"})];
FSTAssertChanged(Doc("bar/baz", 0, Map("bar", "baz"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("bar/baz", 0, Map("bar", "baz"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self rejectMutation];
FSTAssertRemoved("bar/baz");
FSTAssertNotContains("bar/baz");
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 2, Map("it", "changed")),
2019-01-23 17:26:49 -05:00
{targetID})];
FSTAssertChanged(Doc("foo/bar", 2, Map("it", "changed")));
FSTAssertContains(Doc("foo/bar", 2, Map("it", "changed")));
FSTAssertNotContains("bar/baz");
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDeletedDocumentThenSetMutationThenAck {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(DeletedDoc("foo/bar", 2), {targetID}, {})];
FSTAssertRemoved("foo/bar");
2018-07-27 13:58:11 -07:00
// Under eager GC, there is no longer a reference for the document, and it should be
// deleted.
if (![self gcIsEager]) {
FSTAssertContains(DeletedDoc("foo/bar", 2, NO));
2018-07-27 13:58:11 -07:00
} else {
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2018-04-16 12:18:35 -07:00
// Can now remove the target, since we have a mutation pinning the document
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2018-07-27 13:58:11 -07:00
// Verify we didn't lose anything
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:3];
FSTAssertChanged(Doc("foo/bar", 3, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-07-27 13:58:11 -07:00
// It has been acknowledged, and should no longer be retained as there is no target and mutation
if ([self gcIsEager]) {
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationThenDeletedDocument {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(DeletedDoc("foo/bar", 2), {targetID}, {})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDocumentThenSetMutationThenAckThenDocument {
if ([self isTestBaseClass]) return;
2018-04-16 12:18:35 -07:00
// Start a query that requires acks to be held.
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 2, Map("it", "base")), {targetID})];
FSTAssertChanged(Doc("foo/bar", 2, Map("it", "base")));
FSTAssertContains(Doc("foo/bar", 2, Map("it", "base")));
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"})];
FSTAssertChanged(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:3];
2018-04-16 12:18:35 -07:00
// we haven't seen the remote event yet, so the write is still held.
FSTAssertChanged(Doc("foo/bar", 3, Map("foo", "bar"), DocumentState::kCommittedMutations));
FSTAssertContains(Doc("foo/bar", 3, Map("foo", "bar"), DocumentState::kCommittedMutations));
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 3, Map("it", "changed")),
2019-01-23 17:26:49 -05:00
{targetID}, {})];
FSTAssertChanged(Doc("foo/bar", 3, Map("it", "changed")));
FSTAssertContains(Doc("foo/bar", 3, Map("it", "changed")));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesPatchWithoutPriorDocument {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
FSTAssertRemoved("foo/bar");
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:1];
FSTAssertChanged(UnknownDoc("foo/bar", 1));
2018-11-01 09:44:30 -07:00
if ([self gcIsEager]) {
FSTAssertNotContains("foo/bar");
2018-11-01 09:44:30 -07:00
} else {
FSTAssertContains(UnknownDoc("foo/bar", 1));
2018-11-01 09:44:30 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesPatchMutationThenDocumentThenAck {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
FSTAssertRemoved("foo/bar");
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID})];
FSTAssertChanged(
Doc("foo/bar", 1, Map("foo", "bar", "it", "base"), DocumentState::kLocalMutations));
FSTAssertContains(
Doc("foo/bar", 1, Map("foo", "bar", "it", "base"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2];
2018-04-16 12:18:35 -07:00
// We still haven't seen the remote events for the patch, so the local changes remain, and there
// are no changes
FSTAssertChanged(
Doc("foo/bar", 2, Map("foo", "bar", "it", "base"), DocumentState::kCommittedMutations));
FSTAssertContains(
Doc("foo/bar", 2, Map("foo", "bar", "it", "base"), DocumentState::kCommittedMutations));
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(
Doc("foo/bar", 2, Map("foo", "bar", "it", "base")), {targetID}, {})];
2018-04-16 12:18:35 -07:00
FSTAssertChanged(Doc("foo/bar", 2, Map("foo", "bar", "it", "base")));
FSTAssertContains(Doc("foo/bar", 2, Map("foo", "bar", "it", "base")));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesPatchMutationThenAckThenDocument {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
FSTAssertRemoved("foo/bar");
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:1];
FSTAssertChanged(UnknownDoc("foo/bar", 1));
2018-11-01 09:44:30 -07:00
// There's no target pinning the doc, and we've ack'd the mutation.
if ([self gcIsEager]) {
FSTAssertNotContains("foo/bar");
2018-11-01 09:44:30 -07:00
} else {
FSTAssertContains(UnknownDoc("foo/bar", 1));
2018-11-01 09:44:30 -07:00
}
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID},
{})];
FSTAssertChanged(Doc("foo/bar", 1, Map("it", "base")));
FSTAssertContains(Doc("foo/bar", 1, Map("it", "base")));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDeleteMutationThenAck {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:1];
FSTAssertRemoved("foo/bar");
2018-07-27 13:58:11 -07:00
// There's no target pinning the doc, and we've ack'd the mutation.
if ([self gcIsEager]) {
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDocumentThenDeleteMutationThenAck {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID},
{})];
FSTAssertChanged(Doc("foo/bar", 1, Map("it", "base")));
FSTAssertContains(Doc("foo/bar", 1, Map("it", "base")));
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
2018-04-16 12:18:35 -07:00
// Remove the target so only the mutation is pinning the document
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2];
FSTAssertRemoved("foo/bar");
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) {
// Neither the target nor the mutation pin the document, it should be gone.
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDeleteMutationThenDocumentThenAck {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
2018-07-27 13:58:11 -07:00
// Add the document to a target so it will remain in persistence even when ack'd
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID},
{})];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
2018-04-16 12:18:35 -07:00
// Don't need to keep it pinned anymore
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2];
FSTAssertRemoved("foo/bar");
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) {
// The doc is not pinned in a target and we've acknowledged the mutation. It shouldn't exist
// anymore.
FSTAssertNotContains("foo/bar");
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDocumentThenDeletedDocumentThenDocument {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID},
2019-01-23 17:26:49 -05:00
{})];
FSTAssertChanged(Doc("foo/bar", 1, Map("it", "base")));
FSTAssertContains(Doc("foo/bar", 1, Map("it", "base")));
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(DeletedDoc("foo/bar", 2), {targetID}, {})];
FSTAssertRemoved("foo/bar");
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) {
FSTAssertContains(DeletedDoc("foo/bar", 2));
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 3, Map("it", "changed")),
2019-01-23 17:26:49 -05:00
{targetID}, {})];
FSTAssertChanged(Doc("foo/bar", 3, Map("it", "changed")));
FSTAssertContains(Doc("foo/bar", 3, Map("it", "changed")));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationThenPatchMutationThenDocumentThenAckThenAck {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "old"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "old"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("it", "base")), {targetID},
{})];
FSTAssertChanged(Doc("foo/bar", 1, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 1, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2]; // delete mutation
FSTAssertChanged(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 2, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:3]; // patch mutation
FSTAssertChanged(Doc("foo/bar", 3, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) {
// we've ack'd all of the mutations, nothing is keeping this pinned anymore
FSTAssertNotContains("foo/bar");
2018-11-01 09:44:30 -07:00
} else {
FSTAssertContains(Doc("foo/bar", 3, Map("foo", "bar"), DocumentState::kCommittedMutations));
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationAndPatchMutationTogether {
if ([self isTestBaseClass]) return;
2019-02-07 22:03:17 -05:00
[self writeMutations:{
2017-10-03 08:55:22 -07:00
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
2019-02-07 22:03:17 -05:00
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
}];
2017-10-03 08:55:22 -07:00
FSTAssertChanged(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationThenPatchMutationThenReject {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"})];
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "old"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:1];
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
2018-07-27 13:58:11 -07:00
// A blind patch is not visible in the cache
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
[self rejectMutation];
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesSetMutationsAndPatchMutationOfJustOneTogether {
if ([self isTestBaseClass]) return;
2019-02-07 22:03:17 -05:00
[self writeMutations:{
2017-10-03 08:55:22 -07:00
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
2019-02-07 22:03:17 -05:00
FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
}];
2017-10-03 08:55:22 -07:00
FSTAssertChanged(Doc("bar/baz", 0, Map("bar", "baz"), DocumentState::kLocalMutations),
Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("bar/baz", 0, Map("bar", "baz"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
}
- (void)testHandlesDeleteMutationThenPatchMutationThenAckThenAck {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestDeleteMutation(@"foo/bar")];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar"));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2]; // delete mutation
FSTAssertRemoved("foo/bar");
FSTAssertContains(DeletedDoc("foo/bar", 2, /* has_committed_mutations= */ true));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:3]; // patch mutation
FSTAssertChanged(UnknownDoc("foo/bar", 3));
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) {
// There are no more pending mutations, the doc has been dropped
FSTAssertNotContains("foo/bar");
2018-11-01 09:44:30 -07:00
} else {
FSTAssertContains(UnknownDoc("foo/bar", 3));
2018-07-27 13:58:11 -07:00
}
2017-10-03 08:55:22 -07:00
}
- (void)testCollectsGarbageAfterChangeBatchWithNoTargetIDs {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(DeletedDoc("foo/bar", 2), {}, {},
{1})];
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
2018-07-27 13:58:11 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(
Doc("foo/bar", 2, Map("foo", "bar")), {}, {}, {1})];
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
}
- (void)testCollectsGarbageAfterChangeBatch {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 2, Map("foo", "bar")), {targetID})];
FSTAssertContains(Doc("foo/bar", 2, Map("foo", "bar")));
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 2, Map("foo", "baz")), {},
{targetID})];
2017-10-03 08:55:22 -07:00
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
}
- (void)testCollectsGarbageAfterAcknowledgedMutation {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 0, Map("foo", "old")), {targetID},
{})];
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
2018-04-16 12:18:35 -07:00
// Release the query so that our target count goes back to 0 and we are considered up-to-date.
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
[self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bah", 0, Map("foo", "bah"), DocumentState::kLocalMutations));
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:3];
FSTAssertNotContains("foo/bar");
FSTAssertContains(Doc("foo/bah", 0, Map("foo", "bah"), DocumentState::kLocalMutations));
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:4];
FSTAssertNotContains("foo/bar");
FSTAssertNotContains("foo/bah");
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:5];
FSTAssertNotContains("foo/bar");
FSTAssertNotContains("foo/bah");
FSTAssertNotContains("foo/baz");
2017-10-03 08:55:22 -07:00
}
- (void)testCollectsGarbageAfterRejectedMutation {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2018-04-16 12:18:35 -07:00
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 0, Map("foo", "old")), {targetID},
{})];
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})];
2018-04-16 12:18:35 -07:00
// Release the query so that our target count goes back to 0 and we are considered up-to-date.
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2018-04-16 12:18:35 -07:00
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/bah", @{@"foo" : @"bah"})];
[self writeMutation:FSTTestDeleteMutation(@"foo/baz")];
FSTAssertContains(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations));
FSTAssertContains(Doc("foo/bah", 0, Map("foo", "bah"), DocumentState::kLocalMutations));
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self rejectMutation]; // patch mutation
FSTAssertNotContains("foo/bar");
FSTAssertContains(Doc("foo/bah", 0, Map("foo", "bah"), DocumentState::kLocalMutations));
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self rejectMutation]; // set mutation
FSTAssertNotContains("foo/bar");
FSTAssertNotContains("foo/bah");
FSTAssertContains(DeletedDoc("foo/baz"));
2017-10-03 08:55:22 -07:00
[self rejectMutation]; // delete mutation
FSTAssertNotContains("foo/bar");
FSTAssertNotContains("foo/bah");
FSTAssertNotContains("foo/baz");
2017-10-03 08:55:22 -07:00
}
- (void)testPinsDocumentsInTheLocalView {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2018-08-15 15:56:45 -07:00
TargetId targetID = [self allocateQuery:query];
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("foo", "bar")), {targetID})];
2017-10-03 08:55:22 -07:00
[self writeMutation:FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"})];
FSTAssertContains(Doc("foo/bar", 1, Map("foo", "bar")));
FSTAssertContains(Doc("foo/baz", 0, Map("foo", "baz"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
2019-07-25 21:57:22 -04:00
[self notifyLocalViewChanges:TestViewChanges(targetID, @[ @"foo/bar", @"foo/baz" ], @[])];
FSTAssertContains(Doc("foo/bar", 1, Map("foo", "bar")));
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 1, Map("foo", "bar")), {},
{targetID})];
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/baz", 2, Map("foo", "baz")), {targetID},
{})];
FSTAssertContains(Doc("foo/baz", 2, Map("foo", "baz"), DocumentState::kLocalMutations));
2017-10-03 08:55:22 -07:00
[self acknowledgeMutationWithVersion:2];
FSTAssertContains(Doc("foo/baz", 2, Map("foo", "baz")));
FSTAssertContains(Doc("foo/bar", 1, Map("foo", "bar")));
FSTAssertContains(Doc("foo/baz", 2, Map("foo", "baz")));
2017-10-03 08:55:22 -07:00
2019-07-25 21:57:22 -04:00
[self notifyLocalViewChanges:TestViewChanges(targetID, @[], @[ @"foo/bar", @"foo/baz" ])];
FSTAssertNotContains("foo/bar");
FSTAssertNotContains("foo/baz");
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2017-10-03 08:55:22 -07:00
}
- (void)testThrowsAwayDocumentsWithUnknownTargetIDsImmediately {
if ([self isTestBaseClass]) return;
2018-07-27 13:58:11 -07:00
if (![self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2018-08-15 15:56:45 -07:00
TargetId targetID = 321;
[self applyRemoteEvent:FSTTestUpdateRemoteEventWithLimboTargets(Doc("foo/bar", 1, Map()), {}, {},
{targetID})];
2017-10-03 08:55:22 -07:00
FSTAssertNotContains("foo/bar");
2017-10-03 08:55:22 -07:00
}
- (void)testCanExecuteDocumentQueries {
if ([self isTestBaseClass]) return;
2019-09-18 14:00:32 -04:00
_localStore->WriteLocally({
2017-10-03 08:55:22 -07:00
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
2019-02-07 22:03:17 -05:00
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
2019-09-18 14:00:32 -04:00
});
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo/bar");
2019-09-18 14:00:32 -04:00
DocumentMap docs = _localStore->ExecuteQuery(query);
XCTAssertEqual(DocMapToArray(docs),
Vector(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations)));
2017-10-03 08:55:22 -07:00
}
- (void)testCanExecuteCollectionQueries {
if ([self isTestBaseClass]) return;
2019-09-18 14:00:32 -04:00
_localStore->WriteLocally({
2017-10-03 08:55:22 -07:00
FSTTestSetMutation(@"fo/bar", @{@"fo" : @"bar"}),
2019-02-07 22:03:17 -05:00
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
2019-09-18 14:00:32 -04:00
});
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2019-09-18 14:00:32 -04:00
DocumentMap docs = _localStore->ExecuteQuery(query);
XCTAssertEqual(DocMapToArray(docs),
Vector(Doc("foo/bar", 0, Map("foo", "bar"), DocumentState::kLocalMutations),
Doc("foo/baz", 0, Map("foo", "baz"), DocumentState::kLocalMutations)));
2017-10-03 08:55:22 -07:00
}
- (void)testCanExecuteMixedCollectionQueries {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2017-10-03 08:55:22 -07:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/baz", 10, Map("a", "b")), {2}, {})];
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 20, Map("a", "b")), {2}, {})];
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
_localStore->WriteLocally({ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) });
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
DocumentMap docs = _localStore->ExecuteQuery(query);
XCTAssertEqual(DocMapToArray(docs),
Vector(Doc("foo/bar", 20, Map("a", "b")), Doc("foo/baz", 10, Map("a", "b")),
Doc("foo/bonk", 0, Map("a", "b"), DocumentState::kLocalMutations)));
2017-10-03 08:55:22 -07:00
}
- (void)testPersistsResumeTokens {
if ([self isTestBaseClass]) return;
// This test only works in the absence of the FSTEagerGarbageCollector.
2018-07-27 13:58:11 -07:00
if ([self gcIsEager]) return;
2017-10-03 08:55:22 -07:00
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo/bar");
2019-09-18 14:00:32 -04:00
QueryData queryData = _localStore->AllocateQuery(query);
ListenSequenceNumber initialSequenceNumber = queryData.sequence_number();
TargetId targetID = queryData.target_id();
ByteString resumeToken = testutil::ResumeToken(1000);
2017-10-03 08:55:22 -07:00
2019-01-23 17:26:49 -05:00
WatchTargetChange watchChange{WatchTargetChangeState::Current, {targetID}, resumeToken};
2019-10-04 11:55:20 -07:00
auto metadataProvider = FakeTargetMetadataProvider::CreateSingleResultProvider(
testutil::Key("foo/bar"), std::vector<TargetId>{targetID});
WatchChangeAggregator aggregator{&metadataProvider};
aggregator.HandleTargetChange(watchChange);
2019-01-29 18:55:25 -05:00
RemoteEvent remoteEvent = aggregator.CreateRemoteEvent(testutil::Version(1000));
2017-10-03 08:55:22 -07:00
[self applyRemoteEvent:remoteEvent];
// Stop listening so that the query should become inactive (but persistent)
2019-09-18 14:00:32 -04:00
_localStore->ReleaseQuery(query);
2017-10-03 08:55:22 -07:00
// Should come back with the same resume token
2019-09-18 14:00:32 -04:00
QueryData queryData2 = _localStore->AllocateQuery(query);
XCTAssertEqual(queryData2.resume_token(), resumeToken);
// The sequence number should have been bumped when we saved the new resume token.
ListenSequenceNumber newSequenceNumber = queryData2.sequence_number();
XCTAssertGreaterThan(newSequenceNumber, initialSequenceNumber);
2017-10-03 08:55:22 -07:00
}
- (void)testRemoteDocumentKeysForTarget {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2017-10-03 08:55:22 -07:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/baz", 10, Map("a", "b")), {2})];
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 20, Map("a", "b")), {2})];
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
_localStore->WriteLocally({ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) });
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
DocumentKeySet keys = _localStore->GetRemoteDocumentKeys(2);
DocumentKeySet expected{testutil::Key("foo/bar"), testutil::Key("foo/baz")};
XCTAssertEqual(keys, expected);
2017-10-03 08:55:22 -07:00
2019-09-18 14:00:32 -04:00
keys = _localStore->GetRemoteDocumentKeys(2);
XCTAssertEqual(keys, (DocumentKeySet{testutil::Key("foo/bar"), testutil::Key("foo/baz")}));
2017-10-03 08:55:22 -07:00
}
2019-03-06 14:51:30 -08:00
// TODO(mrschmidt): The FieldValue.increment() field transform tests below would probably be
// better implemented as spec tests but currently they don't support transforms.
- (void)testHandlesSetMutationThenTransformMutationThenTransformMutation {
if ([self isTestBaseClass]) return;
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 3), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 3), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
}
- (void)testHandlesSetMutationThenAckThenTransformMutationThenAckThenTransformMutation {
if ([self isTestBaseClass]) return;
// Since this test doesn't start a listen, Eager GC removes the documents from the cache as
// soon as the mutation is applied. This creates a lot of special casing in this unit test but
// does not expand its test coverage.
if ([self gcIsEager]) return;
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:1];
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 0), DocumentState::kCommittedMutations));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 0), DocumentState::kCommittedMutations));
2019-03-06 14:51:30 -08:00
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:2 transformResult:@1];
FSTAssertContains(Doc("foo/bar", 2, Map("sum", 1), DocumentState::kCommittedMutations));
FSTAssertChanged(Doc("foo/bar", 2, Map("sum", 1), DocumentState::kCommittedMutations));
2019-03-06 14:51:30 -08:00
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
FSTAssertContains(Doc("foo/bar", 2, Map("sum", 3), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 2, Map("sum", 3), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
}
- (void)testHandlesSetMutationThenTransformMutationThenRemoteEventThenTransformMutation {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2019-03-06 14:51:30 -08:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0})];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 0), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("sum", 0)), {2})];
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:1];
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 0)));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 0)));
2019-03-06 14:51:30 -08:00
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})];
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
// The value in this remote event gets ignored since we still have a pending transform mutation.
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(Doc("foo/bar", 2, Map("sum", 0)), {2}, {})];
FSTAssertContains(Doc("foo/bar", 2, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 2, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
// Add another increment. Note that we still compute the increment based on the local value.
[self writeMutation:FSTTestTransformMutation(
@"foo/bar", @{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:2]})];
FSTAssertContains(Doc("foo/bar", 2, Map("sum", 3), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 2, Map("sum", 3), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:3 transformResult:@1];
FSTAssertContains(Doc("foo/bar", 3, Map("sum", 3), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 3, Map("sum", 3), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:4 transformResult:@1339];
FSTAssertContains(Doc("foo/bar", 4, Map("sum", 1339), DocumentState::kCommittedMutations));
FSTAssertChanged(Doc("foo/bar", 4, Map("sum", 1339), DocumentState::kCommittedMutations));
2019-03-06 14:51:30 -08:00
}
- (void)testHoldsBackOnlyNonIdempotentTransforms {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2019-03-06 14:51:30 -08:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"sum" : @0, @"array_union" : @[]})];
FSTAssertChanged(
Doc("foo/bar", 0, Map("sum", 0, "array_union", Array()), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self acknowledgeMutationWithVersion:1];
FSTAssertChanged(
Doc("foo/bar", 1, Map("sum", 0, "array_union", Array()), DocumentState::kCommittedMutations));
2019-03-06 14:51:30 -08:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(
Doc("foo/bar", 1, Map("sum", 0, "array_union", Array())), {2})];
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 0, "array_union", Array())));
2019-03-06 14:51:30 -08:00
[self writeMutations:{
FSTTestTransformMutation(@"foo/bar",
@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]}),
FSTTestTransformMutation(
@"foo/bar",
@{@"array_union" : [FIRFieldValue fieldValueForArrayUnion:@[ @"foo" ]]})
}];
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 1, "array_union", Array("foo")),
DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
// The sum transform is not idempotent and the backend's updated value is ignored. The
// ArrayUnion transform is recomputed and includes the backend value.
[self applyRemoteEvent:FSTTestUpdateRemoteEvent(
Doc("foo/bar", 2, Map("sum", 1337, "array_union", Array("bar"))), {2},
{})];
FSTAssertChanged(Doc("foo/bar", 2, Map("sum", 1, "array_union", Array("bar", "foo")),
DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
}
- (void)testHandlesMergeMutationWithTransformThenRemoteEvent {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2019-03-06 14:51:30 -08:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self writeMutations:{
FSTTestPatchMutation("foo/bar", @{}, {firebase::firestore::testutil::Field("sum")}),
FSTTestTransformMutation(@"foo/bar",
@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
}];
FSTAssertContains(Doc("foo/bar", 0, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 0, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("sum", 1337)), {2})];
2019-03-06 14:51:30 -08:00
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
}
- (void)testHandlesPatchMutationWithTransformThenRemoteEvent {
if ([self isTestBaseClass]) return;
2019-08-02 11:35:33 -07:00
core::Query query = Query("foo");
2019-03-06 14:51:30 -08:00
[self allocateQuery:query];
FSTAssertTargetID(2);
[self writeMutations:{
FSTTestPatchMutation("foo/bar", @{}, {}),
FSTTestTransformMutation(@"foo/bar",
@{@"sum" : [FIRFieldValue fieldValueForIntegerIncrement:1]})
}];
FSTAssertNotContains("foo/bar");
FSTAssertChanged(DeletedDoc("foo/bar"));
2019-03-06 14:51:30 -08:00
// Note: This test reflects the current behavior, but it may be preferable to replay the
// mutation once we receive the first value from the remote event.
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("sum", 1337)), {2})];
2019-03-06 14:51:30 -08:00
FSTAssertContains(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
FSTAssertChanged(Doc("foo/bar", 1, Map("sum", 1), DocumentState::kLocalMutations));
2019-03-06 14:51:30 -08:00
}
2019-08-20 16:18:02 -04:00
- (void)testGetHighestUnacknowledgeBatchId {
if ([self isTestBaseClass]) return;
2019-09-18 14:00:32 -04:00
XCTAssertEqual(-1, _localStore->GetHighestUnacknowledgedBatchId());
2019-08-20 16:18:02 -04:00
[self writeMutation:FSTTestSetMutation(@"foo/bar", @{@"abc" : @123})];
2019-09-18 14:00:32 -04:00
XCTAssertEqual(1, _localStore->GetHighestUnacknowledgedBatchId());
2019-08-20 16:18:02 -04:00
[self writeMutation:FSTTestPatchMutation("foo/bar", @{@"abc" : @321}, {})];
2019-09-18 14:00:32 -04:00
XCTAssertEqual(2, _localStore->GetHighestUnacknowledgedBatchId());
2019-08-20 16:18:02 -04:00
[self acknowledgeMutationWithVersion:1];
2019-09-18 14:00:32 -04:00
XCTAssertEqual(2, _localStore->GetHighestUnacknowledgedBatchId());
2019-08-20 16:18:02 -04:00
[self rejectMutation];
2019-09-18 14:00:32 -04:00
XCTAssertEqual(-1, _localStore->GetHighestUnacknowledgedBatchId());
2019-08-20 16:18:02 -04:00
}
- (void)testOnlyPersistsUpdatesForDocumentsWhenVersionChanges {
if ([self isTestBaseClass]) return;
core::Query query = Query("foo");
[self allocateQuery:query];
FSTAssertTargetID(2);
[self applyRemoteEvent:FSTTestAddedRemoteEvent(Doc("foo/bar", 1, Map("val", "old")), {2})];
FSTAssertContains(Doc("foo/bar", 1, Map("val", "old")));
FSTAssertChanged(Doc("foo/bar", 1, Map("val", "old")));
[self applyRemoteEvent:FSTTestAddedRemoteEvent({Doc("foo/bar", 1, Map("val", "new")),
Doc("foo/baz", 2, Map("val", "new"))},
{2})];
// The update to foo/bar is ignored.
FSTAssertContains(Doc("foo/bar", 1, Map("val", "old")));
FSTAssertContains(Doc("foo/baz", 2, Map("val", "new")));
FSTAssertChanged(Doc("foo/baz", 2, Map("val", "new")));
}
2017-10-03 08:55:22 -07:00
@end
NS_ASSUME_NONNULL_END