FSTDocumentSetTests.mm 4.8 KB

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