FSTDocumentSetTests.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. // TODO(wilhuff) move to first include once this test filename matches
  21. #include "Firestore/core/src/firebase/firestore/model/document.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  23. #include "Firestore/core/src/firebase/firestore/util/delayed_constructor.h"
  24. #include "Firestore/core/test/firebase/firestore/testutil/xcgmock.h"
  25. namespace util = firebase::firestore::util;
  26. using firebase::firestore::model::DocumentComparator;
  27. using firebase::firestore::model::DocumentSet;
  28. using firebase::firestore::model::DocumentState;
  29. using testing::ElementsAre;
  30. NS_ASSUME_NONNULL_BEGIN
  31. @interface FSTDocumentSetTests : XCTestCase
  32. @end
  33. @implementation FSTDocumentSetTests {
  34. util::DelayedConstructor<DocumentComparator> _comp;
  35. FSTDocument *_doc1;
  36. FSTDocument *_doc2;
  37. FSTDocument *_doc3;
  38. }
  39. - (void)setUp {
  40. [super setUp];
  41. _comp.Init(FSTTestDocComparator("sort"));
  42. _doc1 = FSTTestDoc("docs/1", 0, @{@"sort" : @2}, DocumentState::kSynced);
  43. _doc2 = FSTTestDoc("docs/2", 0, @{@"sort" : @3}, DocumentState::kSynced);
  44. _doc3 = FSTTestDoc("docs/3", 0, @{@"sort" : @1}, DocumentState::kSynced);
  45. }
  46. - (void)testCount {
  47. XCTAssertEqual(FSTTestDocSet(*_comp, @[]).size(), 0);
  48. XCTAssertEqual(FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]).size(), 3);
  49. }
  50. - (void)testHasKey {
  51. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, _doc2 ]);
  52. XCTAssertTrue(set.ContainsKey(_doc1.key));
  53. XCTAssertTrue(set.ContainsKey(_doc2.key));
  54. XCTAssertFalse(set.ContainsKey(_doc3.key));
  55. }
  56. - (void)testDocumentForKey {
  57. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, _doc2 ]);
  58. XCTAssertEqualObjects(set.GetDocument(_doc1.key), _doc1);
  59. XCTAssertEqualObjects(set.GetDocument(_doc2.key), _doc2);
  60. XCTAssertNil(set.GetDocument(_doc3.key));
  61. }
  62. - (void)testFirstAndLastDocument {
  63. DocumentSet set = FSTTestDocSet(*_comp, @[]);
  64. XCTAssertNil(set.GetFirstDocument());
  65. XCTAssertNil(set.GetLastDocument());
  66. set = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  67. XCTAssertEqualObjects(set.GetFirstDocument(), _doc3);
  68. XCTAssertEqualObjects(set.GetLastDocument(), _doc2);
  69. }
  70. - (void)testKeepsDocumentsInTheRightOrder {
  71. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  72. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, _doc2));
  73. }
  74. - (void)testDeletes {
  75. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  76. DocumentSet setWithoutDoc1 = set.erase(_doc1.key);
  77. XC_ASSERT_THAT(setWithoutDoc1, ElementsAre(_doc3, _doc2));
  78. XCTAssertEqual(setWithoutDoc1.size(), 2);
  79. // Original remains unchanged
  80. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, _doc2));
  81. DocumentSet setWithoutDoc3 = setWithoutDoc1.erase(_doc3.key);
  82. XC_ASSERT_THAT(setWithoutDoc3, ElementsAre(_doc2));
  83. XCTAssertEqual(setWithoutDoc3.size(), 1);
  84. }
  85. - (void)testUpdates {
  86. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  87. FSTDocument *doc2Prime = FSTTestDoc("docs/2", 0, @{@"sort" : @9}, DocumentState::kSynced);
  88. set = set.insert(doc2Prime);
  89. XCTAssertEqual(set.size(), 3);
  90. XCTAssertEqualObjects(set.GetDocument(doc2Prime.key), doc2Prime);
  91. XC_ASSERT_THAT(set, ElementsAre(_doc3, _doc1, doc2Prime));
  92. }
  93. - (void)testAddsDocsWithEqualComparisonValues {
  94. FSTDocument *doc4 = FSTTestDoc("docs/4", 0, @{@"sort" : @2}, DocumentState::kSynced);
  95. DocumentSet set = FSTTestDocSet(*_comp, @[ _doc1, doc4 ]);
  96. XC_ASSERT_THAT(set, ElementsAre(_doc1, doc4));
  97. }
  98. - (void)testIsEqual {
  99. DocumentSet empty{DocumentComparator::ByKey()};
  100. DocumentSet set1 = FSTTestDocSet(DocumentComparator::ByKey(), @[ _doc1, _doc2, _doc3 ]);
  101. DocumentSet set2 = FSTTestDocSet(DocumentComparator::ByKey(), @[ _doc1, _doc2, _doc3 ]);
  102. XCTAssertEqual(set1, set1);
  103. XCTAssertEqual(set1, set2);
  104. XCTAssertNotEqual(set1, empty);
  105. DocumentSet sortedSet1 = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  106. DocumentSet sortedSet2 = FSTTestDocSet(*_comp, @[ _doc1, _doc2, _doc3 ]);
  107. XCTAssertEqual(sortedSet1, sortedSet1);
  108. XCTAssertEqual(sortedSet1, sortedSet2);
  109. XCTAssertNotEqual(sortedSet1, empty);
  110. DocumentSet shortSet = FSTTestDocSet(DocumentComparator::ByKey(), @[ _doc1, _doc2 ]);
  111. XCTAssertNotEqual(set1, shortSet);
  112. XCTAssertNotEqual(set1, sortedSet1);
  113. }
  114. @end
  115. NS_ASSUME_NONNULL_END