FSTViewSnapshotTest.mm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #include <vector>
  18. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  20. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  21. namespace core = firebase::firestore::core;
  22. namespace testutil = firebase::firestore::testutil;
  23. using firebase::firestore::core::DocumentViewChange;
  24. using firebase::firestore::core::DocumentViewChangeSet;
  25. using firebase::firestore::core::ViewSnapshot;
  26. using firebase::firestore::model::Document;
  27. using firebase::firestore::model::DocumentComparator;
  28. using firebase::firestore::model::DocumentKeySet;
  29. using firebase::firestore::model::DocumentSet;
  30. using firebase::firestore::model::DocumentState;
  31. using firebase::firestore::testutil::Query;
  32. using testutil::Doc;
  33. using testutil::Map;
  34. NS_ASSUME_NONNULL_BEGIN
  35. @interface FSTViewSnapshotTests : XCTestCase
  36. @end
  37. @implementation FSTViewSnapshotTests
  38. - (void)testDocumentChangeConstructor {
  39. Document doc = Doc("a/b", 0, Map());
  40. DocumentViewChange::Type type = DocumentViewChange::Type::Modified;
  41. DocumentViewChange change{doc, type};
  42. XCTAssertEqual(change.document(), doc);
  43. XCTAssertEqual(change.type(), type);
  44. }
  45. - (void)testTrack {
  46. DocumentViewChangeSet set;
  47. Document docAdded = Doc("a/1", 0, Map());
  48. Document docRemoved = Doc("a/2", 0, Map());
  49. Document docModified = Doc("a/3", 0, Map());
  50. Document docAddedThenModified = Doc("b/1", 0, Map());
  51. Document docAddedThenRemoved = Doc("b/2", 0, Map());
  52. Document docRemovedThenAdded = Doc("b/3", 0, Map());
  53. Document docModifiedThenRemoved = Doc("b/4", 0, Map());
  54. Document docModifiedThenModified = Doc("b/5", 0, Map());
  55. set.AddChange(DocumentViewChange{docAdded, DocumentViewChange::Type::Added});
  56. set.AddChange(DocumentViewChange{docRemoved, DocumentViewChange::Type::Removed});
  57. set.AddChange(DocumentViewChange{docModified, DocumentViewChange::Type::Modified});
  58. set.AddChange(DocumentViewChange{docAddedThenModified, DocumentViewChange::Type::Added});
  59. set.AddChange(DocumentViewChange{docAddedThenModified, DocumentViewChange::Type::Modified});
  60. set.AddChange(DocumentViewChange{docAddedThenRemoved, DocumentViewChange::Type::Added});
  61. set.AddChange(DocumentViewChange{docAddedThenRemoved, DocumentViewChange::Type::Removed});
  62. set.AddChange(DocumentViewChange{docRemovedThenAdded, DocumentViewChange::Type::Removed});
  63. set.AddChange(DocumentViewChange{docRemovedThenAdded, DocumentViewChange::Type::Added});
  64. set.AddChange(DocumentViewChange{docModifiedThenRemoved, DocumentViewChange::Type::Modified});
  65. set.AddChange(DocumentViewChange{docModifiedThenRemoved, DocumentViewChange::Type::Removed});
  66. set.AddChange(DocumentViewChange{docModifiedThenModified, DocumentViewChange::Type::Modified});
  67. set.AddChange(DocumentViewChange{docModifiedThenModified, DocumentViewChange::Type::Modified});
  68. std::vector<DocumentViewChange> changes = set.GetChanges();
  69. XCTAssertEqual(changes.size(), 7);
  70. XCTAssertEqual(changes[0].document(), docAdded);
  71. XCTAssertEqual(changes[0].type(), DocumentViewChange::Type::Added);
  72. XCTAssertEqual(changes[1].document(), docRemoved);
  73. XCTAssertEqual(changes[1].type(), DocumentViewChange::Type::Removed);
  74. XCTAssertEqual(changes[2].document(), docModified);
  75. XCTAssertEqual(changes[2].type(), DocumentViewChange::Type::Modified);
  76. XCTAssertEqual(changes[3].document(), docAddedThenModified);
  77. XCTAssertEqual(changes[3].type(), DocumentViewChange::Type::Added);
  78. XCTAssertEqual(changes[4].document(), docRemovedThenAdded);
  79. XCTAssertEqual(changes[4].type(), DocumentViewChange::Type::Modified);
  80. XCTAssertEqual(changes[5].document(), docModifiedThenRemoved);
  81. XCTAssertEqual(changes[5].type(), DocumentViewChange::Type::Removed);
  82. XCTAssertEqual(changes[6].document(), docModifiedThenModified);
  83. XCTAssertEqual(changes[6].type(), DocumentViewChange::Type::Modified);
  84. }
  85. - (void)testViewSnapshotConstructor {
  86. core::Query query = Query("a");
  87. DocumentSet documents = DocumentSet{DocumentComparator::ByKey()};
  88. DocumentSet oldDocuments = documents;
  89. documents = documents.insert(Doc("c/a", 1, Map()));
  90. std::vector<DocumentViewChange> documentChanges{
  91. DocumentViewChange{Doc("c/a", 1, Map()), DocumentViewChange::Type::Added}};
  92. bool fromCache = true;
  93. DocumentKeySet mutatedKeys;
  94. bool syncStateChanged = true;
  95. ViewSnapshot snapshot{query,
  96. documents,
  97. oldDocuments,
  98. documentChanges,
  99. mutatedKeys,
  100. fromCache,
  101. syncStateChanged,
  102. /*excludes_metadata_changes=*/false};
  103. XCTAssertEqual(snapshot.query(), query);
  104. XCTAssertEqual(snapshot.documents(), documents);
  105. XCTAssertEqual(snapshot.old_documents(), oldDocuments);
  106. XCTAssertEqual(snapshot.document_changes(), documentChanges);
  107. XCTAssertEqual(snapshot.from_cache(), fromCache);
  108. XCTAssertEqual(snapshot.mutated_keys(), mutatedKeys);
  109. XCTAssertEqual(snapshot.sync_state_changed(), syncStateChanged);
  110. }
  111. @end
  112. NS_ASSUME_NONNULL_END