FSTViewSnapshotTest.m 6.7 KB

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