FSTViewSnapshotTest.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "Firestore/Source/Core/FSTViewSnapshot.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Core/FSTQuery.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. #import "Firestore/Source/Model/FSTDocumentSet.h"
  21. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface FSTViewSnapshotTests : XCTestCase
  24. @end
  25. @implementation FSTViewSnapshotTests
  26. - (void)testDocumentChangeConstructor {
  27. FSTDocument *doc = FSTTestDoc("a/b", 0, @{}, NO);
  28. FSTDocumentViewChangeType type = FSTDocumentViewChangeTypeModified;
  29. FSTDocumentViewChange *change = [FSTDocumentViewChange changeWithDocument:doc type:type];
  30. XCTAssertEqual(change.document, doc);
  31. XCTAssertEqual(change.type, type);
  32. }
  33. - (void)testTrack {
  34. FSTDocumentViewChangeSet *set = [FSTDocumentViewChangeSet changeSet];
  35. FSTDocument *docAdded = FSTTestDoc("a/1", 0, @{}, NO);
  36. FSTDocument *docRemoved = FSTTestDoc("a/2", 0, @{}, NO);
  37. FSTDocument *docModified = FSTTestDoc("a/3", 0, @{}, NO);
  38. FSTDocument *docAddedThenModified = FSTTestDoc("b/1", 0, @{}, NO);
  39. FSTDocument *docAddedThenRemoved = FSTTestDoc("b/2", 0, @{}, NO);
  40. FSTDocument *docRemovedThenAdded = FSTTestDoc("b/3", 0, @{}, NO);
  41. FSTDocument *docModifiedThenRemoved = FSTTestDoc("b/4", 0, @{}, NO);
  42. FSTDocument *docModifiedThenModified = FSTTestDoc("b/5", 0, @{}, NO);
  43. [set addChange:[FSTDocumentViewChange changeWithDocument:docAdded
  44. type:FSTDocumentViewChangeTypeAdded]];
  45. [set addChange:[FSTDocumentViewChange changeWithDocument:docRemoved
  46. type:FSTDocumentViewChangeTypeRemoved]];
  47. [set addChange:[FSTDocumentViewChange changeWithDocument:docModified
  48. type:FSTDocumentViewChangeTypeModified]];
  49. [set addChange:[FSTDocumentViewChange changeWithDocument:docAddedThenModified
  50. type:FSTDocumentViewChangeTypeAdded]];
  51. [set addChange:[FSTDocumentViewChange changeWithDocument:docAddedThenModified
  52. type:FSTDocumentViewChangeTypeModified]];
  53. [set addChange:[FSTDocumentViewChange changeWithDocument:docAddedThenRemoved
  54. type:FSTDocumentViewChangeTypeAdded]];
  55. [set addChange:[FSTDocumentViewChange changeWithDocument:docAddedThenRemoved
  56. type:FSTDocumentViewChangeTypeRemoved]];
  57. [set addChange:[FSTDocumentViewChange changeWithDocument:docRemovedThenAdded
  58. type:FSTDocumentViewChangeTypeRemoved]];
  59. [set addChange:[FSTDocumentViewChange changeWithDocument:docRemovedThenAdded
  60. type:FSTDocumentViewChangeTypeAdded]];
  61. [set addChange:[FSTDocumentViewChange changeWithDocument:docModifiedThenRemoved
  62. type:FSTDocumentViewChangeTypeModified]];
  63. [set addChange:[FSTDocumentViewChange changeWithDocument:docModifiedThenRemoved
  64. type:FSTDocumentViewChangeTypeRemoved]];
  65. [set addChange:[FSTDocumentViewChange changeWithDocument:docModifiedThenModified
  66. type:FSTDocumentViewChangeTypeModified]];
  67. [set addChange:[FSTDocumentViewChange changeWithDocument:docModifiedThenModified
  68. type:FSTDocumentViewChangeTypeModified]];
  69. NSArray<FSTDocumentViewChange *> *changes = [set changes];
  70. XCTAssertEqual(changes.count, 7);
  71. XCTAssertEqual(changes[0].document, docAdded);
  72. XCTAssertEqual(changes[0].type, FSTDocumentViewChangeTypeAdded);
  73. XCTAssertEqual(changes[1].document, docRemoved);
  74. XCTAssertEqual(changes[1].type, FSTDocumentViewChangeTypeRemoved);
  75. XCTAssertEqual(changes[2].document, docModified);
  76. XCTAssertEqual(changes[2].type, FSTDocumentViewChangeTypeModified);
  77. XCTAssertEqual(changes[3].document, docAddedThenModified);
  78. XCTAssertEqual(changes[3].type, FSTDocumentViewChangeTypeAdded);
  79. XCTAssertEqual(changes[4].document, docRemovedThenAdded);
  80. XCTAssertEqual(changes[4].type, FSTDocumentViewChangeTypeModified);
  81. XCTAssertEqual(changes[5].document, docModifiedThenRemoved);
  82. XCTAssertEqual(changes[5].type, FSTDocumentViewChangeTypeRemoved);
  83. XCTAssertEqual(changes[6].document, docModifiedThenModified);
  84. XCTAssertEqual(changes[6].type, FSTDocumentViewChangeTypeModified);
  85. }
  86. - (void)testViewSnapshotConstructor {
  87. FSTQuery *query = FSTTestQuery("a");
  88. FSTDocumentSet *documents = [FSTDocumentSet documentSetWithComparator:FSTDocumentComparatorByKey];
  89. FSTDocumentSet *oldDocuments = documents;
  90. documents = [documents documentSetByAddingDocument:FSTTestDoc("c/a", 1, @{}, NO)];
  91. NSArray<FSTDocumentViewChange *> *documentChanges =
  92. @[ [FSTDocumentViewChange changeWithDocument:FSTTestDoc("c/a", 1, @{}, NO)
  93. type:FSTDocumentViewChangeTypeAdded] ];
  94. BOOL fromCache = YES;
  95. BOOL hasPendingWrites = NO;
  96. BOOL syncStateChanged = YES;
  97. FSTViewSnapshot *snapshot = [[FSTViewSnapshot alloc] initWithQuery:query
  98. documents:documents
  99. oldDocuments:oldDocuments
  100. documentChanges:documentChanges
  101. fromCache:fromCache
  102. hasPendingWrites:hasPendingWrites
  103. syncStateChanged:syncStateChanged];
  104. XCTAssertEqual(snapshot.query, query);
  105. XCTAssertEqual(snapshot.documents, documents);
  106. XCTAssertEqual(snapshot.oldDocuments, oldDocuments);
  107. XCTAssertEqual(snapshot.documentChanges, documentChanges);
  108. XCTAssertEqual(snapshot.fromCache, fromCache);
  109. XCTAssertEqual(snapshot.hasPendingWrites, hasPendingWrites);
  110. XCTAssertEqual(snapshot.syncStateChanged, syncStateChanged);
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END