Skip to content

Port FSTDocumentSet to C++ #2608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 25, 2019
Merged
7 changes: 4 additions & 3 deletions Firestore/Example/Tests/API/FIRQuerySnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.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/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::core::DocumentViewChange;
using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentSet;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -87,8 +88,8 @@ - (void)testIncludeMetadataChanges {
FSTDocument *doc2Old = FSTTestDoc("foo/baz", 1, @{@"a" : @"b"}, FSTDocumentStateSynced);
FSTDocument *doc2New = FSTTestDoc("foo/baz", 1, @{@"a" : @"c"}, FSTDocumentStateSynced);

FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc1Old, doc2Old ]);
FSTDocumentSet *newDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc2New, doc2New ]);
DocumentSet oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc1Old, doc2Old ]);
DocumentSet newDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[ doc2New, doc2New ]);
std::vector<DocumentViewChange> documentChanges{
DocumentViewChange{doc1New, DocumentViewChange::Type::kMetadata},
DocumentViewChange{doc2New, DocumentViewChange::Type::kModified},
Expand Down
17 changes: 8 additions & 9 deletions Firestore/Example/Tests/API/FSTAPIHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
#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/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"

Expand All @@ -43,6 +43,7 @@
using firebase::firestore::core::DocumentViewChange;
using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentSet;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -98,26 +99,24 @@
bool hasPendingWrites,
bool fromCache) {
SnapshotMetadata metadata(hasPendingWrites, fromCache);
FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
DocumentSet oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
DocumentKeySet mutatedKeys;
for (NSString *key in oldDocs) {
oldDocuments = [oldDocuments
documentSetByAddingDocument:FSTTestDoc(util::StringFormat("%s/%s", path, key), 1,
oldDocs[key],
hasPendingWrites ? FSTDocumentStateLocalMutations
: FSTDocumentStateSynced)];
oldDocuments = oldDocuments.insert(
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);
mutatedKeys = mutatedKeys.insert(testutil::Key(documentKey));
}
}
FSTDocumentSet *newDocuments = oldDocuments;
DocumentSet newDocuments = oldDocuments;
std::vector<DocumentViewChange> documentChanges;
for (NSString *key in docsToAdd) {
FSTDocument *docToAdd =
FSTTestDoc(util::StringFormat("%s/%s", path, key), 1, docsToAdd[key],
hasPendingWrites ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced);
newDocuments = [newDocuments documentSetByAddingDocument:docToAdd];
newDocuments = newDocuments.insert(docToAdd);
documentChanges.emplace_back(docToAdd, DocumentViewChange::Type::kAdded);
if (hasPendingWrites) {
const std::string documentKey = util::StringFormat("%s/%s", path, key);
Expand Down
5 changes: 3 additions & 2 deletions Firestore/Example/Tests/Core/FSTEventManagerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSyncEngine.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/model/types.h"
#include "Firestore/core/src/firebase/firestore/util/statusor.h"

using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::core::ViewSnapshotHandler;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentSet;
using firebase::firestore::model::OnlineState;
using firebase::firestore::util::StatusOr;

Expand Down Expand Up @@ -106,7 +107,7 @@ - (FSTQueryListener *)queryListenerForQuery:(FSTQuery *)query
}

- (ViewSnapshot)makeEmptyViewSnapshotWithQuery:(FSTQuery *)query {
FSTDocumentSet *emptyDocs = [FSTDocumentSet documentSetWithComparator:query.comparator];
DocumentSet emptyDocs{query.comparator};
// sync_state_changed has to be `true` to prevent an assertion about a meaningless view snapshot.
return ViewSnapshot{
query, emptyDocs, emptyDocs, {}, DocumentKeySet{}, false, /*sync_state_changed=*/true, false};
Expand Down
88 changes: 42 additions & 46 deletions Firestore/Example/Tests/Core/FSTQueryListenerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTView.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Util/FSTAsyncQueryListener.h"

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/include/firebase/firestore/firestore_errors.h"
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/model/types.h"
#include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
#include "Firestore/core/src/firebase/firestore/util/delayed_constructor.h"
Expand All @@ -44,6 +44,7 @@
using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::core::ViewSnapshotHandler;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentSet;
using firebase::firestore::model::OnlineState;
using firebase::firestore::remote::TargetChange;
using firebase::firestore::util::DelayedConstructor;
Expand Down Expand Up @@ -119,15 +120,14 @@ - (void)testRaisesCollectionEvents {
XC_ASSERT_THAT(accum[0].document_changes(), ElementsAre(change1, change2));
XC_ASSERT_THAT(accum[1].document_changes(), ElementsAre(change3));

ViewSnapshot expectedSnap2{
snap2.query(),
snap2.documents(),
/*old_documents=*/[FSTDocumentSet documentSetWithComparator : snap2.query().comparator],
/*document_changes=*/{ change1, change4 },
snap2.mutated_keys(),
snap2.from_cache(),
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
ViewSnapshot expectedSnap2{snap2.query(),
snap2.documents(),
/*old_documents=*/DocumentSet{snap2.query().comparator},
/*document_changes=*/{change1, change4},
snap2.mutated_keys(),
snap2.from_cache(),
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
XC_ASSERT_THAT(otherAccum, ElementsAre(expectedSnap2));
}

Expand Down Expand Up @@ -395,15 +395,14 @@ - (void)testWillWaitForSyncIfOnline {

DocumentViewChange change1{doc1, DocumentViewChange::Type::kAdded};
DocumentViewChange change2{doc2, DocumentViewChange::Type::kAdded};
ViewSnapshot expectedSnap{
snap3.query(),
snap3.documents(),
/*old_documents=*/[FSTDocumentSet documentSetWithComparator : snap3.query().comparator],
/*document_changes=*/{ change1, change2 },
snap3.mutated_keys(),
/*from_cache=*/false,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
ViewSnapshot expectedSnap{snap3.query(),
snap3.documents(),
/*old_documents=*/DocumentSet{snap3.query().comparator},
/*document_changes=*/{change1, change2},
snap3.mutated_keys(),
/*from_cache=*/false,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
XC_ASSERT_THAT(events, ElementsAre(expectedSnap));
}

Expand Down Expand Up @@ -433,15 +432,14 @@ - (void)testWillRaiseInitialEventWhenGoingOffline {

DocumentViewChange change1{doc1, DocumentViewChange::Type::kAdded};
DocumentViewChange change2{doc2, DocumentViewChange::Type::kAdded};
ViewSnapshot expectedSnap1{
query,
/*documents=*/snap1.documents(),
/*old_documents=*/[FSTDocumentSet documentSetWithComparator : snap1.query().comparator],
/*document_changes=*/{ change1 },
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
ViewSnapshot expectedSnap1{query,
/*documents=*/snap1.documents(),
/*old_documents=*/DocumentSet{snap1.query().comparator},
/*document_changes=*/{change1},
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};

ViewSnapshot expectedSnap2{query,
/*documents=*/snap2.documents(),
Expand Down Expand Up @@ -469,15 +467,14 @@ - (void)testWillRaiseInitialEventWhenGoingOfflineAndThereAreNoDocs {
[listener queryDidChangeViewSnapshot:snap1]; // no event
[listener applyChangedOnlineState:OnlineState::Offline]; // event

ViewSnapshot expectedSnap{
query,
/*documents=*/snap1.documents(),
/*old_documents=*/[FSTDocumentSet documentSetWithComparator : snap1.query().comparator],
/*document_changes=*/{},
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
ViewSnapshot expectedSnap{query,
/*documents=*/snap1.documents(),
/*old_documents=*/DocumentSet{snap1.query().comparator},
/*document_changes=*/{},
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
XC_ASSERT_THAT(events, ElementsAre(expectedSnap));
}

Expand All @@ -495,15 +492,14 @@ - (void)testWillRaiseInitialEventWhenStartingOfflineAndThereAreNoDocs {
[listener applyChangedOnlineState:OnlineState::Offline]; // no event
[listener queryDidChangeViewSnapshot:snap1]; // event

ViewSnapshot expectedSnap{
query,
/*documents=*/snap1.documents(),
/*old_documents=*/[FSTDocumentSet documentSetWithComparator : snap1.query().comparator],
/*document_changes=*/{},
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
ViewSnapshot expectedSnap{query,
/*documents=*/snap1.documents(),
/*old_documents=*/DocumentSet{snap1.query().comparator},
/*document_changes=*/{},
snap1.mutated_keys(),
/*from_cache=*/true,
/*sync_state_changed=*/true,
/*excludes_metadata_changes=*/true};
XC_ASSERT_THAT(events, ElementsAre(expectedSnap));
}

Expand Down
10 changes: 5 additions & 5 deletions Firestore/Example/Tests/Core/FSTViewSnapshotTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@

#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
#include "Firestore/core/src/firebase/firestore/model/document_set.h"

using firebase::firestore::core::DocumentViewChange;
using firebase::firestore::core::DocumentViewChangeSet;
using firebase::firestore::core::ViewSnapshot;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::DocumentSet;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -100,10 +101,9 @@ - (void)testTrack {

- (void)testViewSnapshotConstructor {
FSTQuery *query = FSTTestQuery("a");
FSTDocumentSet *documents = [FSTDocumentSet documentSetWithComparator:FSTDocumentComparatorByKey];
FSTDocumentSet *oldDocuments = documents;
documents =
[documents documentSetByAddingDocument:FSTTestDoc("c/a", 1, @{}, FSTDocumentStateSynced)];
DocumentSet documents = DocumentSet{FSTDocumentComparatorByKey};
DocumentSet oldDocuments = documents;
documents = documents.insert(FSTTestDoc("c/a", 1, @{}, FSTDocumentStateSynced));
std::vector<DocumentViewChange> documentChanges{DocumentViewChange{
FSTTestDoc("c/a", 1, @{}, FSTDocumentStateSynced), DocumentViewChange::Type::kAdded}};

Expand Down
Loading