FIRQuerySnapshotTests.mm 6.0 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 <FirebaseFirestore/FIRQuerySnapshot.h>
  17. #import <XCTest/XCTest.h>
  18. #include <memory>
  19. #include <utility>
  20. #include <vector>
  21. #import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
  22. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  23. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  24. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  25. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  26. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
  27. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  28. #include "Firestore/core/src/core/query.h"
  29. #include "Firestore/core/src/core/view_snapshot.h"
  30. #include "Firestore/core/src/model/document.h"
  31. #include "Firestore/core/src/model/document_set.h"
  32. #include "Firestore/core/src/util/string_apple.h"
  33. #include "Firestore/core/test/unit/testutil/testutil.h"
  34. using firebase::firestore::api::DocumentChange;
  35. using firebase::firestore::api::DocumentSnapshot;
  36. using firebase::firestore::api::Firestore;
  37. using firebase::firestore::api::SnapshotMetadata;
  38. using firebase::firestore::core::DocumentViewChange;
  39. using firebase::firestore::core::ViewSnapshot;
  40. using firebase::firestore::model::Document;
  41. using firebase::firestore::model::DocumentComparator;
  42. using firebase::firestore::model::DocumentKeySet;
  43. using firebase::firestore::model::DocumentSet;
  44. using firebase::firestore::testutil::Doc;
  45. using firebase::firestore::testutil::DocSet;
  46. using firebase::firestore::testutil::Map;
  47. using firebase::firestore::testutil::Query;
  48. NS_ASSUME_NONNULL_BEGIN
  49. @interface FIRQuerySnapshotTests : XCTestCase
  50. @end
  51. @implementation FIRQuerySnapshotTests
  52. - (void)testEquals {
  53. FIRQuerySnapshot *foo =
  54. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false, false);
  55. FIRQuerySnapshot *fooDup =
  56. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false, false);
  57. FIRQuerySnapshot *differentPath =
  58. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, true, false, false);
  59. FIRQuerySnapshot *differentDoc =
  60. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, true, false, false);
  61. FIRQuerySnapshot *noPendingWrites =
  62. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, false, false, false);
  63. FIRQuerySnapshot *fromCache =
  64. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, true, true);
  65. XCTAssertEqualObjects(foo, fooDup);
  66. XCTAssertNotEqualObjects(foo, differentPath);
  67. XCTAssertNotEqualObjects(foo, differentDoc);
  68. XCTAssertNotEqualObjects(foo, noPendingWrites);
  69. XCTAssertNotEqualObjects(foo, fromCache);
  70. XCTAssertEqual([foo hash], [fooDup hash]);
  71. XCTAssertNotEqual([foo hash], [differentPath hash]);
  72. XCTAssertNotEqual([foo hash], [differentDoc hash]);
  73. XCTAssertNotEqual([foo hash], [noPendingWrites hash]);
  74. XCTAssertNotEqual([foo hash], [fromCache hash]);
  75. }
  76. - (void)testIncludeMetadataChanges {
  77. Document doc1Old = Doc("foo/bar", 1, Map("a", "b")).SetHasLocalMutations();
  78. Document doc1New = Doc("foo/bar", 1, Map("a", "b"));
  79. Document doc2Old = Doc("foo/baz", 1, Map("a", "b"));
  80. Document doc2New = Doc("foo/baz", 1, Map("a", "c"));
  81. DocumentSet oldDocuments = DocSet(DocumentComparator::ByKey(), {doc1Old, doc2Old});
  82. DocumentSet newDocuments = DocSet(DocumentComparator::ByKey(), {doc2New, doc2New});
  83. std::vector<DocumentViewChange> documentChanges{
  84. DocumentViewChange(doc1New, DocumentViewChange::Type::Metadata),
  85. DocumentViewChange(doc2New, DocumentViewChange::Type::Modified),
  86. };
  87. std::shared_ptr<Firestore> firestore = FSTTestFirestore().wrapped;
  88. core::Query query = Query("foo");
  89. ViewSnapshot viewSnapshot(query, newDocuments, oldDocuments, std::move(documentChanges),
  90. /*mutated_keys=*/DocumentKeySet(),
  91. /*from_cache=*/false,
  92. /*sync_state_changed=*/true,
  93. /*excludes_metadata_changes=*/false,
  94. /*has_cached_results=*/false);
  95. SnapshotMetadata metadata(/*pending_writes=*/false, /*from_cache=*/false);
  96. FIRQuerySnapshot *snapshot = [[FIRQuerySnapshot alloc] initWithFirestore:firestore
  97. originalQuery:query
  98. snapshot:std::move(viewSnapshot)
  99. metadata:std::move(metadata)];
  100. auto doc1Snap = DocumentSnapshot::FromDocument(firestore, doc1New, SnapshotMetadata());
  101. auto doc2Snap = DocumentSnapshot::FromDocument(firestore, doc2New, SnapshotMetadata());
  102. NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
  103. [[FIRDocumentChange alloc]
  104. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  105. /*old_index=*/1, /*new_index=*/1)],
  106. ];
  107. XCTAssertEqualObjects(snapshot.documentChanges, changesWithoutMetadata);
  108. NSArray<FIRDocumentChange *> *changesWithMetadata = @[
  109. [[FIRDocumentChange alloc]
  110. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc1Snap,
  111. /*old_index=*/0, /*new_index=*/0)],
  112. [[FIRDocumentChange alloc]
  113. initWithDocumentChange:DocumentChange(DocumentChange::Type::Modified, doc2Snap,
  114. /*old_index=*/1, /*new_index=*/1)],
  115. ];
  116. XCTAssertEqualObjects([snapshot documentChangesWithIncludeMetadataChanges:YES],
  117. changesWithMetadata);
  118. }
  119. @end
  120. NS_ASSUME_NONNULL_END