FIRQuerySnapshotTests.mm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/firebase/firestore/core/query.h"
  29. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  30. #include "Firestore/core/src/firebase/firestore/model/document.h"
  31. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  32. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  33. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  34. namespace util = firebase::firestore::util;
  35. namespace testutil = firebase::firestore::testutil;
  36. using firebase::firestore::api::DocumentChange;
  37. using firebase::firestore::api::DocumentSnapshot;
  38. using firebase::firestore::api::Firestore;
  39. using firebase::firestore::api::SnapshotMetadata;
  40. using firebase::firestore::core::DocumentViewChange;
  41. using firebase::firestore::core::ViewSnapshot;
  42. using firebase::firestore::model::Document;
  43. using firebase::firestore::model::DocumentComparator;
  44. using firebase::firestore::model::DocumentKeySet;
  45. using firebase::firestore::model::DocumentSet;
  46. using firebase::firestore::model::DocumentState;
  47. using testutil::Doc;
  48. using testutil::DocSet;
  49. using testutil::Map;
  50. using testutil::Query;
  51. NS_ASSUME_NONNULL_BEGIN
  52. @interface FIRQuerySnapshotTests : XCTestCase
  53. @end
  54. @implementation FIRQuerySnapshotTests
  55. - (void)testEquals {
  56. FIRQuerySnapshot *foo = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  57. FIRQuerySnapshot *fooDup = FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  58. FIRQuerySnapshot *differentPath =
  59. FSTTestQuerySnapshot("bar", @{}, @{@"a" : @{@"a" : @1}}, true, false);
  60. FIRQuerySnapshot *differentDoc =
  61. FSTTestQuerySnapshot("foo", @{@"a" : @{@"b" : @1}}, @{}, true, false);
  62. FIRQuerySnapshot *noPendingWrites =
  63. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, false, false);
  64. FIRQuerySnapshot *fromCache =
  65. FSTTestQuerySnapshot("foo", @{}, @{@"a" : @{@"a" : @1}}, true, true);
  66. XCTAssertEqualObjects(foo, fooDup);
  67. XCTAssertNotEqualObjects(foo, differentPath);
  68. XCTAssertNotEqualObjects(foo, differentDoc);
  69. XCTAssertNotEqualObjects(foo, noPendingWrites);
  70. XCTAssertNotEqualObjects(foo, fromCache);
  71. XCTAssertEqual([foo hash], [fooDup hash]);
  72. XCTAssertNotEqual([foo hash], [differentPath hash]);
  73. XCTAssertNotEqual([foo hash], [differentDoc hash]);
  74. XCTAssertNotEqual([foo hash], [noPendingWrites hash]);
  75. XCTAssertNotEqual([foo hash], [fromCache hash]);
  76. }
  77. - (void)testIncludeMetadataChanges {
  78. Document doc1Old = Doc("foo/bar", 1, Map("a", "b"), DocumentState::kLocalMutations);
  79. Document doc1New = Doc("foo/bar", 1, Map("a", "b"), DocumentState::kSynced);
  80. Document doc2Old = Doc("foo/baz", 1, Map("a", "b"));
  81. Document doc2New = Doc("foo/baz", 1, Map("a", "c"));
  82. DocumentSet oldDocuments = DocSet(DocumentComparator::ByKey(), {doc1Old, doc2Old});
  83. DocumentSet newDocuments = DocSet(DocumentComparator::ByKey(), {doc2New, doc2New});
  84. std::vector<DocumentViewChange> documentChanges{
  85. DocumentViewChange(doc1New, DocumentViewChange::Type::Metadata),
  86. DocumentViewChange(doc2New, DocumentViewChange::Type::Modified),
  87. };
  88. std::shared_ptr<Firestore> firestore = FSTTestFirestore().wrapped;
  89. core::Query query = Query("foo");
  90. ViewSnapshot viewSnapshot(query, newDocuments, oldDocuments, std::move(documentChanges),
  91. /*mutated_keys=*/DocumentKeySet(),
  92. /*from_cache=*/false,
  93. /*sync_state_changed=*/true,
  94. /*excludes_metadata_changes=*/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